[chef] Re: Re: Where does the new line symbol is ?


Chronological Thread 
  • From: Vladimir Skubriev < >
  • To:
  • Subject: [chef] Re: Re: Where does the new line symbol is ?
  • Date: Mon, 07 Oct 2013 10:31:49 +0400

On 10/04/2013 09:21 PM, Kendall Gifford wrote:
" type="cite">
On Fri, Oct 4, 2013 at 5:57 AM, Vladimir Skubriev < " target="_blank"> > wrote:
I have this code:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/, '\1'.chomp

The "chomp" in the line of code above has no effect. The problem is that it is being applied to the literal string: '\1' which has no newline character in it.

If you want it to apply to the string that is the result of the call to the #sub method, you need some parenthesis:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub(/.*\((.*)\).*/, '\1').chomp

I don't know why. But () don't help for me. As in the recipe code and erb template code including.
" type="cite">
Another way to get rid of the newline character that comes from the "gem list | grep ..." command is to alter your regular _expression_ so that the "." (dot) character will also match newline characters, my adding the "m" modifier:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/m, '\1'
I cannot find via google /m, May be /m has a literal name for googling ?
" type="cite">

Anyhow, this explains why the pasver variable holds a string that includes a newline.
 

if ! ::File.exists?("/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver.chomp!}/buildout/apache2/mod_passenger.so") then

    bash "build_and_install_passenger_module_for_apache" do
    user "root"
    code <<-EOH
                source /etc/profile.d/rvm.sh
/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/bin/passenger-install-apache2-module -a
    EOH
    end
end

And a problem is that "/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver.chomp!}/buildout/apache2/mod_passenger.so" if a two lines string variable

Why this happens ?

The code "pasver.chomp!" should be removing that newline character successfully. I don't know why there would still be a newline character. However, if, somehow, the pasver string was terminated with more than one newline character (2, for example) that would explain why #chomp! is failing to remove it. A ruby example in ruby's irb shell:

$ irb
> "test\n\n".chomp!
=> "test\n"

I've tested out this example. All it's okey in irb.

I don't get strange behaviour in IRB never. But chef runtime is something else.
" type="cite">
Note: you can just use "chomp" instead of "chomp!" unless you use "pasver" later and expect it to already have newlines removed.
 

I have second use case in other (erb) file:

<% pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/, '\1'.chomp %>
PassengerRoot <%= "/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver.chomp!}" %>
PassengerDefaultRuby /usr/local/rvm/wrappers/<%= node['redmine']['rubyversion']%>/ruby

This file generates:

LoadModule passenger_module /usr/local/rvm/gems/ruby-2.0.0-p247/gems/passenger-4.0.19
/buildout/apache2/mod_passenger.so

How does a template with "Passenger Root ..." and "PassengerDefaultRuby ..." create a file with "LoadModule ..." ?
 
I am mistake when copy-past a template example:

LoadModule passenger_module <%= /buildout/apache2/mod_passenger.so">"/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/ /buildout/apache2/mod_passenger.so" %>

I remake a template with a local var, that sending to a template from recipe code:

    configfile = "#{node['redmine']['datafs']}/config/mods-available/passenger.load"
    template configfile do
      source "passenger.load.erb"
      mode 0400
      variables ({
                :pasver => pasver
                })
      notifies :restart, resources(:service=>"apache2") 
    end

I add "variables ({ ... })". This is because you suggestions isn't worked out for me at all.

I don't understand why. But if I use /m modifier then in the my old template code there is no value for pasver variable, and template generates all without pasver value:

LoadModule passenger_module /usr/local/rvm/gems/ruby-2.0.0-p247/gems/passenger-/buildout/apache2/mod_passenger.so

May be this is an ruby object scope area ?

Now I use pasver variable generation in the recipe code:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/m, '\1'

And after I transfer it to the erb's.

So I using this generated var in recipe - and all it's okey:

pasver = `/usr/local/rvm/bin/gem list | grep passenger`.sub /.*\((.*)\).*/m, '\1'

if ! ::File.exists?("/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver}/buildout/apache2/mod_passenger.so") then

    bash "build_and_install_passenger_module_for_apache" do
    user "root"
    code <<-EOH
                source /etc/profile.d/rvm.sh
                /usr/local/rvm/gems/#{node['redmine']['rubyversion']}/bin/passenger-install-apache2-module -a
    EOH
    not_if ::File.exists?("/usr/local/rvm/gems/#{node['redmine']['rubyversion']}/gems/passenger-#{pasver}/buildout/apache2/mod_passenger.so")
    end
 
Strange behaviour happens.

" type="cite">

Why ?

Where does the new line symbol is ?


Again, I'm not sure why the call to #chomp! isn't removing the trailing newline (again, unless there are somehow multiple newlines).

However, in both cases, fixing your call to #sub by either including the "m" modifier, and/or changing the regular _expression_ itself will result in "pasver" _not_ having any trailing newlines in the first place, making it unnecessary to call #chomp! or #chomp at all.



-- 
Best regards,

CVision Lab System Administrator
Vladmir Skubriev



Archive powered by MHonArc 2.6.16.

§