[chef] Re: best way to run something only once?


Chronological Thread 
  • From: Gregory Patmore < >
  • To: " " < >
  • Subject: [chef] Re: best way to run something only once?
  • Date: Wed, 18 Jun 2014 19:29:27 -0400

not if clauses work in some cases, but our team leverages chef tags as sentinels for controlling deployments as well. We just wrap the 'run once' code within an unless tagged('<yourtagnamehere>') block to prevent it from running if the node is tagged. 

This works for larger concerns such as wrapping an application resource block from deploying new versions of custom applications, but also works in the smaller cases like yours. 

Just remove the chef tag from the node and the next chef-client run will execute the controlled block. A few tips with this technique:

 1. just make sure your recipe adds the tag back to the node object if it's successfully run. If you don't add the tag back it will execute on every chef-client run and what use is all that pretty code then, right?

2. If you use the recipe in more then one environment, you may want to use a compound condition in your if/unless block that checks for the existence of the tag as well as a the correct chef_environment. This allows you to control say the prod environment from continuously redeploying/restarting an app (especially if the devs make you use the HEAD of a git branch instead of a git tag) while still giving you the rapid updating in your dev envs without the hassle of having to run knife tag delete... each time. 

Greg

Sent from my iPhone

On Jun 18, 2014, at 6:34 PM, Tomasz Chmielewski < "> > wrote:

Does chef have some built-in way to run some recipes (or parts of it)
once only?

Say, I have the following (here, bash snippet, but could be anything):

bash 'adjust-supervisor' do
 code <<-EOF
    ...some_code...
 EOF
end


How would you approach to make sure it only runs once?

I figured I could use something like this in the recipe:

  not_if { ::File.exists?("/etc/chef/stamps/some-description.stamp") }

followed by:

file "/etc/chef/stamps/some-description.stamp" do
 owner "root"
 group "root"
 mode "0644"
 action :create
end


It allows me to easily re-run by removing the stamp file, if needed.


But not sure it's the optimal way to make sure we run a given thing
once.

How do you approach this?




--
Tomasz Chmielewski
http://www.sslrack.com



Archive powered by MHonArc 2.6.16.

§