Hello Tomasz,
I think it sort of depends on why you only want to do it once. Checking to
see if you need to run the code is a common way. Wrapping that up in a
resource and provider then letting it figure out whether or not the
supervisor needs to be updated or not might be idiomatic.
For stuff that's slow/expensive to check for I store the fact that it has
been run in the node mash.
The way you have done it could be an option as well, but I would suggest
having the bash resource notify the file resource to :create. This will
then only create the file if the bash resource's action is run successfully:
file "/etc/chef/stamps/some-description.stamp" do
owner "root"
group "root"
mode "0644"
action :nothing
end
bash 'adjust-supervisor' do
not_if { ::File.exists?("/etc/chef/stamps/some-description.stamp") }
code <<-EOF
...some_code...
EOF
notifies :create, "file[/etc/chef/stamps/some-description.stamp]"
end
In any case it can be tricky to run something ONLY once without checking
the side effects of it. What if the bash resource runs but chef dies for
some reason before creating the file? For this reason I think it's
important to make sure stuff CAN run twice safely, as much as possible.
Cheers,
-Greg
On Thu, Jun 19, 2014 at 10:34 AM, Tomasz Chmielewski tch@wpkg.org 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