- From: Andrea Campi <
>
- To:
- Subject: [chef] Re: Re: Re: where is the better place for this code?
- Date: Wed, 16 Nov 2011 13:25:44 +0100
2011/11/16 Juan Jesús Ojeda Croissier
<
>:
>
On Wed, Nov 16, 2011 at 9:19 AM, Andrea Campi
>
> On Nov 16, 2011, at 12:41 AM, Juan Jesús Ojeda Croissier
>
> <
>
>
> wrote:
>
[...]
>
>> I wrote some code for this (which is currently working), but I don't
>
>> know if I did in the right place and in the right way. I love to have
>
>> it at Chef, but I guess it need to be proven useful and the community
>
>> agree. By now I put it in my cookbooks at:
>
>> cookbooks/the_cookbook/libraries/file.rb
>
>>
>
>> Here is the actual code and example or recipe using it:
>
>> https://gist.github.com/1368640
>
>>
>
>> But I'll write down here a simplier version:
>
>>
>
>> def include?(str)
>
>> file_content = ::File.open(@path).read
>
>> return true if file_content =~ /#{str}/
>
>> return false
>
>> end
>
>>
>
>> def replace(str, str2)
>
>> old_content = ::File.open(@path).read
>
>> content old_content.gsub(str, str2)
>
>> end
>
>>
>
>> These methods are Chef::Resource::File's methods. I wrote like that
>
>> because was the only way I got them working, but it seems to me that
>
>> it should be a Providers' instead... I don't know...
>
>
>
> Looking only at your gist it's a wash, but in general yes, I would use an
>
> LWRP. Keep the library, but put the "file" resource in an LWRP.
>
>
>
>
Sorry, but I don't get what you are saying :-/ I thing my English is
>
not as good as I thought. What do you mean by 'wash'?
Ah, sorry, it's kind of idiomatic—I meant "you can do it either way,
each has pros and cons".
>
Could you explain to me what do you mean by keep the library but
>
putting the 'file' resource in an LWRP? I'm a bit confuse, sorry.
Sure.
In your gist, I wouldn't change anything in the library; I would
remove the recipe and replace it with an LWRP:
resources/gtk_bookmarks.rb
action :replace # and possibly more
attribute :name, :kind_of => String, :name_attribute => true
attribute :owner, :kind_of => String
attribute :group, :kind_of => String
attribute :before, :kind_of => String
attribute :after, :kind_of => String
providers/gtk_bookmarks.rb
action :replace do
file new_resource.name do
replace(new_resource.before, new_resource.after) if
include?(new_resource.before)
owner new_resource.owner
group new_resource.group
mode "0644"
action :create_if_missing
end
end
This way you can call it with:
<cookbook_name>_gtk_bookmarks "/home/#{node.user}/.gtk-bookmarks" do
before "smb"
after "smb"
owner node.user
group node.user
action :replace
end
Makes more sense?
Just for completeness: in this case it would probably be easier/faster
to use a definition instead. But the LWRP is useful in case you want
to add other actions (:add, :replace, :gsub come to mind).
definitions/gtk_bookmarks.rb
define :gtk_bookmarks_replace do
file params[:name] do
replace(params[:before], params[:after]) if include?(params[:before])
owner params[:owner]
group params[:group]
mode "0644"
action :create_if_missing
end
end
You would call this in the exact same way.
One more thing to keep in mind in all cases, even your first:
resources are unique per run, so if you try to modify this same file
twice in a run, that's going to be a problem. That's true in general,
but when you introduce LWRPs and definitions it may become hard to
diagnose.
The solution here would be something like:
filename = "/path/to/your/file"
file "#{filename} (replace)" do
path filename
# and then everything else
end
Basically this will just make give the resource a unique name (which
will show up in logs), and then set the "real" path.
Hope it helps.
Andrea
- [chef] where is the better place for this code?, Juan Jesús Ojeda Croissier, 11/15/2011
- [chef] Re: where is the better place for this code?, hani elabed, 11/15/2011
- [chef] Re: where is the better place for this code?, Andrea Campi, 11/16/2011
- [chef] Re: Re: where is the better place for this code?, Juan Jesús Ojeda Croissier, 11/16/2011
- [chef] Re: Re: Re: where is the better place for this code?, Andrea Campi, 11/16/2011
- [chef] Re: Re: Re: Re: where is the better place for this code?, Juan Jesús Ojeda Croissier, 11/16/2011
- [chef] Re: Re: Re: Re: Re: where is the better place for this code?, Andrea Campi, 11/16/2011
- [chef] Re: Re: Re: Re: Re: Re: where is the better place for this code?, Daniel DeLeo, 11/17/2011
- [chef] Re: Re: Re: Re: Re: Re: Re: where is the better place for this code?, Juan Jesús Ojeda Croissier, 11/17/2011
- [chef] Re: Re: Re: Re: Re: Re: Re: Re: where is the better place for this code?, John E. Vincent (lusis), 11/17/2011
- [chef] Re: Re: Re: Re: Re: Re: Re: Re: Re: where is the better place for this code?, Juan Jesús Ojeda Croissier, 11/17/2011
- [chef] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: where is the better place for this code?, Matthew Moretti, 11/17/2011
- [chef] Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: where is the better place for this code?, Juan Jesús Ojeda Croissier, 11/17/2011
- [chef] Re: Re: Re: Re: Re: Re: Re: where is the better place for this code?, Andrea Campi, 11/17/2011
Archive powered by MHonArc 2.6.16.