[chef] Re: Re: Re: Re: Re: Re: Good example of a lwrp


Chronological Thread 
  • From: Maven User < >
  • To: chef < >
  • Subject: [chef] Re: Re: Re: Re: Re: Re: Good example of a lwrp
  • Date: Fri, 21 Dec 2012 19:15:20 -0500

actually - I'm a BIT further now (maybe).

So removing the action and end.run_action items, I can see now it's executing the ruby code when it should.

BUT - now it doesn't understand what a "directory" or a "template" is - how do I import these things?


On Fri, Dec 21, 2012 at 7:02 PM, Maven User < " target="_blank"> > wrote:
Ahh - it needed to be end.run_action(:create) - but this all is resolved waaay too early it seems (which is what you were saying earlier).

How can I delay the execution of the ruby code?


On Fri, Dec 21, 2012 at 6:55 PM, Maven User < " target="_blank"> > wrote:
  So pardon the indenting:

ruby_block "apply-config" do
  block do
  ::Dir["/srv/#{new_resource.artifactid}-#{new_resource.version}/templates/**/*"].each do |file|

      puts "here is the file #{file}"
      if (::File.file?(file))
        puts "#{filename} is a file  -> #{file}"
        filename = ::File.basename(file)
        newfilename = ::File.absolute_path(file).split("templates/")
      puts "here is the new file name #{newfilename[1]}"

        if (newfilename[1].rindex('/') != nil && newfilename[1].rindex('/') > 1)
            puts "there are directories we need to create"
            dirstocreate = newfilename[1][0, newfilename[1].rindex('/')]
            puts "We need to make #{dirstocreate}"
           directory "/srv/#{new_resource.artifactid}-#{new_resource.version}/#{dirstocreate}"  do
               recursive true
               action :create
               not_if "test -d /srv/#{new_resource.artifactid}-#{new_resource.version}/#{dirstocreate}"
            end
        end
          template "/srv/#{new_resource.artifactid}-#{new_resource.version}/#{newfilename[1]}" do
             local true
             source "#{file}"
          end
       end
     end
  end
  action :nothing
end.run_action(:run)


??


On Fri, Dec 21, 2012 at 6:40 PM, Maven User < " target="_blank"> > wrote:
WOW - dood - thank you!

I'm a bit overwhelmed by the reply and trying to grock it - so bear with me....

I'm trying to solve a scenario where we have a dozen recipes that they ALL do the same 70 lines of code.  So I'm trying to refactor that away in to a lwrp.

I'm also not clear about the "action :nothing" and the run_action bits you're mentioning above - any usecase links?

The tickets also confused me more >.<

The full block of code I have has ruby wrapping resources:


:Dir["/srv/#{new_resource.artifactid}-#{new_resource.version}/templates/**/*"].each do |file|

    puts "here is the file #{file}"
    if (::File.file?(file))
      puts "#{filename} is a file  -> #{file}"
      filename = ::File.basename(file)
      newfilename = ::File.absolute_path(file).split("templates/")
      puts "here is the new file name #{newfilename[1]}"

      if (newfilename[1].rindex('/') != nil && newfilename[1].rindex('/') > 1)
          puts "there are directories we need to create"
          dirstocreate = newfilename[1][0, newfilename[1].rindex('/')]
          puts "We need to make #{dirstocreate}"
         directory "/srv/#{new_resource.artifactid}-#{new_resource.version}/#{dirstocreate}"  do
             recursive true
             action :create
             not_if "test -d /srv/#{new_resource.artifactid}-#{new_resource.version}/#{dirstocreate}"
          end
      end
      template "/srv/#{new_resource.artifactid}-#{new_resource.version}/#{newfilename[1]}" do
         local true
         source "#{file}"
      end
   end
end

Could I have put that inside a ruby block or would I lose the resources inside there?









On Fri, Dec 21, 2012 at 6:19 PM, Daniel DeLeo < " target="_blank"> > wrote:

On Friday, December 21, 2012 at 2:54 PM, Maven User wrote:

Hmmm - some new weirdness...

I have a block of ruby code like this in the provider:

Dir["/srv/#{new_resource.artifactid}-#{new_resource.version}/templates/**/*"].each do |file|
    puts "here is the file #{file}"
....

The first time a recipe is run that is using this provider, that evaluates to nothing, so the subsequent lines are skipped.

It also seems to run some of the steps out of order (right above this block in the provider is an unzip step which now is run AFTER the Dir bit...).

Am I missing something?
Short: This is basically the LWRP version of the compile/converge phase issue. Use action :nothing and run_action to make resources execute immediately.

Long:
LWRPs currently handle nested resources by inserting them in the top-level resource collection *after* the LWRP's position in the resource collection. For a recipe like this:

resource_one
lwrp_resource -> sub_resource_one, sub_resource_two
resource_three

…after lwrp_resource is converged, your resource collection looks like:

resource_one #already executed
lwrp_resource #already executed
sub_resource_one
sub_resource_two
resource_three

This implementation was chosen so that the "sub resources" within an LWRP could notify resources outside of the LWRP. The downside is that the LWRP resource itself cannot properly determine if it was updated or not. See:


In either of the "insert after" or "inline recipe" implementations, you'll still have the general compile/converge pattern, which means that raw ruby code that's not wrapped in a ruby block will execute *before* resources, unless you force the issue with #run_action.

Another option for implementing LWRPs would be to scrap the compile/converge distinction altogether, but then notifications are pretty much impossible, which makes anything that wants to trigger an execute resource based on something else being updated (like a config file) much more difficult to write.

-- 
Daniel DeLeo








Archive powered by MHonArc 2.6.16.

§