Think interface (resource) vs. implementation (provider)
First, you need to define what the LWRP should actually be able to do, which actions it should support, etc.
In your example, you could create a resource called warfile. In your cookbook, create a file resources/warfile.rb that contains something like this:
def initialize(*args)
super
# initialize any attributes your LWRP has
end
action :deploy
attribute :name, :kind_of => String, :name_attribute => true
# You may need additional attributes, for instance to control permissions, URLs, or anything else that needs to be configurable during your deployment.
Next, you'd create the corresponding provider, in a file providers/warfile.rb . That's where you implement each of your actions, in pretty much plain ruby (but you can embed resources):
action :deploy do
# download and unzip the files, create your link, etc., either in Ruby code or using cookbook_file, remote_file etc. resources.
end
Kevin Keane
The NetTech
760-721-8339
http://www.4nettech.com
Our values: Privacy, Liberty, Justice
See https://www.4nettech.com/corp/the-nettech-values.html
-----Original message-----
From: Manoj Thakkar < >
Sent: Friday 4th October 2013 14:05
To:
Subject: [chef] Re: Re: Re: Re: good example of chef definitions ? need help with issue below
Thanks a lot everyone,to me writing LWRP is a tedious job, or the documentation is not enough to make it easier for me,Can anyone please help me refactor the recipe below in resource and provider, I am sorry if its too much to ask for, it will at least help me write the next one myself.cookbook_file "#{node[:tcat][:base_dir]}/webapps/#{node[:tcat][:war_name]}.war" dosource "#{node[:tcat][:war_name]}-#{node[:file][:version]}.war"mode 0777owner "deploy"group "deploy"endremote_file "#{node[:tcat][:base_dir]}/#{node[:tcat][:env]}-config.zip" dosource "http://abc-data-dev.com.zip&v=LATEST"mode '777'owner "deploy"notifies :run, "bash[unzip_file]", :immediatelyendbash "unzip_file" docwd "#{node[:tcat][:base_dir]}"code <<-EOSpwdunzip "#{node[:tcat][:env]}-config.zip"EOSaction :nothingendlink "#{node[:tcat][:base_dir]}/zak-config" doto "#{node[:tcat][:env]}-config"endOn Fri, Oct 4, 2013 at 12:38 PM, Ranjib Dey < " target="_blank" title="This external link will open in a new window"> > wrote:i dont think anyone should use definition, iirc a debate where we were discussing deprecating them (or at least not advocating them), as steve pointed out, they are more like macro expansion, but since chef run is dual phases, macro expansion has subtle consequences. they pretty much blocks building higher abstraction on top of them.
Consider this, you want is to write an abstraction using aggregated ( 1 template, 1 service , couple of other resources). You can add a standard ruby method in a library to do this. you can make a definition . or you can make a resource. Now only the last option will give you full strengths (like notifications, why run, use_inline_mode etc), you might not need them, but why impose such restrictions anyway, given basic lwrp development is as simple as writing a definition (i think definitions are more code that their equivalent lwrp, but thats me)
bestranjibOn Fri, Oct 4, 2013 at 10:06 AM, steve . < " target="_blank" title="This external link will open in a new window"> > wrote:
My two cents:When asking yourself if you should use a definition, remember that it's a macro. You may invoke it like a resource, but it is not a resource. Are you only looking to save yourself some typing or are you looking to do something more?If all you want to do is blast 26 Java apps out in the same way onto a node, you could do that with a macro. So a define's probably a good fit for that.If you are looking to ultimately _manage_ those apps (and, say, have their config file changes trigger redeploys/restarts), but right now you just need to deploy them, you might want to consider throwing all that into a LWRP (or HWRP, if you fear not the Ruby). You'll probably end up doing it eventually anyway, so you might as well start as you mean to go on.On Fri, Oct 4, 2013 at 1:42 AM, Andy Gale < " target="_blank" title="This external link will open in a new window"> > wrote:Good example of a definition here :)LWRPs are where it's at nowadays though definitions are a bit easier to understand I think.If people feel strongly about it though, convert that to a LWRP and I'll be happy to update the book!--On Fri, Oct 4, 2013 at 8:24 AM, Manoj Thakkar < " target="_blank" title="This external link will open in a new window"> > wrote:
Hi,I am looking for some good example for chef definition, or may be someone can help me change my recipe in to a definition so that i can use it in any recipe.
* this is the use case , i have this tomcat web deployment for 10+ wars since the deployment mechanism remains the same for every war i think a definition is the right thing to do, correct me if i am wronghere is my recipe .Please help change it to a definition and a use case on how to sue itcookbook_file "#{node[:tcat][:base_dir]}/webapps/#{node[:tcat][:war_name]}.war" dosource "#{node[:tcat][:war_name]}-#{node[:file][:version]}.war"mode 0777owner "deploy"group "deploy"endremote_file "#{node[:tcat][:base_dir]}/#{node[:tcat][:env]}-config.zip" domode '777'owner "deploy"notifies :run, "bash[unzip_file]", :immediatelyendbash "unzip_file" docwd "#{node[:tcat][:base_dir]}"code <<-EOSpwdunzip "#{node[:tcat][:env]}-config.zip"EOSaction :nothingendlink "#{node[:tcat][:base_dir]}/cas-config" doto "#{node[:tcat][:env]}-config"end
Andy Gale
http://andy-gale.com
http://twitter.com/andygale
Archive powered by MHonArc 2.6.16.