[chef] Re: where is the better place for this code?


Chronological Thread 
  • From: hani elabed < >
  • To:
  • Subject: [chef] Re: where is the better place for this code?
  • Date: Tue, 15 Nov 2011 20:58:06 -0600

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...
The article below helped me a lot...
http://blog.afistfulofservers.net/post/3902042503/a-brief-chef-tutorial-from-concentrate
Look at Step 3... And please note that I have used it successfully. 
Good luck
Hani Elabed


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




Archive powered by MHonArc 2.6.16.

§