[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:51:56 -0500

ok - to the why - I had an earlier post on this but here goes...

We're using maven and embedded jetty.  ALL of our embedded jetty artifacts produce a zip file that contains (at least):

war - the directory that holds the war file
conf - any ssl keys
templates - any templates

Inside templates, there are directories that need to be moved one directory higher and in them, templates - for example:

<install-dir>
|_templates
   |_bin
      |_control.sh (our start/stop init script)

Inside the control.sh file, maven replaces the ${foo} with <%=node[:foo]%>.

This way, we build ONE artifact for EVERY environment and let chef determine how that artifact is configured at deploy time.

SO - why the lwrp - well, because we have a ton of apps that are getting deployed this way and I'm one to enjoy copy/pasting recipes all over the place.   NOW - I can put a dependency on my lwrp via the depends inside the metadata file and now all of our apps go from having 100+ lines of code in their recipes (depending on the different resources getting moved around), to having about 10.



On Fri, Dec 21, 2012 at 8:41 PM, Maven User < " target="_blank"> > wrote:
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.

§