[chef] Re: Re: 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: Re: Re: Good example of a lwrp
  • Date: Fri, 21 Dec 2012 20:41:57 -0500

Gotcha - your rewrite of my example is a bit wrong - I'm using any files found in a given directory as templates and recreating the entire folder structure up one directory expanding the templates as I go.

The trick was to get the directory provider working within the ruby block.

I'm _not_ sure this is the right thing to do or not (but it cleaned up my code slightly), but I created two defs - one for directories and one for the templates that are needed.

I'm also using the ruby_block syntax too:

def mkdir (dirname)
    directory "#{dirname}" do
        recursive true
        action :create
    end
end

def mktemplate (templatename, sourcefile)
      template "#{templatename}" do
          local true
          source "#{sourcefile}"
      end
end

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}"
                  mkdir("/srv/#{new_resource.artifactid}-#{new_resource.version}/#{dirstocreate}")
              end
              mktemplate("/srv/#{new_resource.artifactid}-#{new_resource.version}/#{newfilename[1]}", "#{file}")
           end
       end
    end
end

This is working (for now).








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

On Friday, December 21, 2012 at 4:15 PM, Maven User wrote:

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?

You went a bit in the wrong direction. I'd recommend either:
1) use resources like normal, and put raw ruby code into ruby blocks:

action :something do
  directory "path" do
    #stuff
  end
  ruby_block "arbitrary_code" do
    block do
      # some code
    end
  end
  file "path/file" do
    #stuff
  end
end

OR

2) Use action :nothing plus run_action, but *no* ruby blocks

action :something do
  directory "path" do
    #stuff
    action :nothing
    run_action(:create) # this works if it's very last
  end # or you can put `.run_action()` here, it'll be the same result

  # random ruby code, NO ruby_block resource

  r = file "path/file" do
    #stuff
    action :nothing
  end
  r.run_action(:create) # this is the other way to use run_action(): assign resource to a variable
end

This example is a little complicated, but you should be able to see the general pattern:


-- 
Daniel DeLeo





Archive powered by MHonArc 2.6.16.

§