- From: Juan Jesús Ojeda Croissier <
>
- To:
- Subject: [chef] Re: Re: where is the better place for this code?
- Date: Wed, 16 Nov 2011 08:30:59 +0100
Hi Hani,
On Wed, Nov 16, 2011 at 3:58 AM, hani elabed
<
>
wrote:
>
Hi Juan,
>
>
I am also new at this, so bear with me... And hope others will correct me if
>
I am giving the wrong advice.
>
I think you should use the "Template" resource... here is an example of its
>
usage to change the config file of the NTP server
>
>
template "/etc/ntp.conf" do
>
source "ntp.conf.erb"
>
variables( :ntp_server => "time.nist.gov" )
>
end
>
>
Note the template file is the ntp.conf.erb ( erb for embedded ruby as in
>
Rails *.erb ), and the file that would be produced as a result of ERB
>
substitution is /etc/ntp.conf...
>
I know about the .erb templates but (AFAIK) it will create a new file
to replace the old one, but that's exactly what try to avoid.
I want to keep the old file because is a file that will contain
settings changed by the user (or app) that I want to keep. I just want
to change a single string from the current file, not create a new one.
Thans for the answer a nd your time anyway :-)
[...]
>
2011/11/15 Juan Jesús Ojeda Croissier
>
<
>
>
>
>
> Hi :-)
>
>
>
> I'm quite new here, but I really love the project and the community.
>
> You all are great :-)
>
>
>
> Well, I was writting a couple of new cookbooks for a project[1] where
>
> I'm working on and I found that I need very often to check if some
>
> string is in a file and replace just one string keepking the rest of
>
> the file as it was.
>
>
>
> For example a file that a user (or app) can write and usually does.
>
> Maybe we like to be sure that specific value for a key is always the
>
> right one, but not to change the part of the file where the user (or
>
> app) has already changed.
>
>
>
> 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...
>
>
>
> I hope you guy could give me some light about it. And also tell me if
>
> you find this interesting enough to have it at Chef itself. I'm pretty
>
> sure that I gonna use them a lot for my recipes...
>
>
>
> Thanks a lot for your time.
>
> Cheers
>
>
>
>
>
> [1] http://www.guadalinex.org
>
> http://goo.gl/WSoU2 (google treanslated version of the actual project)
>
> --
>
> Juanje
>
>
--
Juanje
Archive powered by MHonArc 2.6.16.