Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm installing something from source using chef and the script resource. Should I be doing a check for the resultant executable etc. as part of it?

e.g. What I currently have is:

remote_file "/tmp/foo.tar.gz" do
  source "foo.tar.gz"
  mode "0644"
end

script "install_foo" do
  interpreter "bash"
  user "root"
  cwd "/tmp"
  code <<-EOH
  tar -zxf foo.tar.gz
  cd foo
  make
  cp foo /usr/local/bin
  EOH
end
share|improve this question

2 Answers 2

This script will exit with 1 if foo doesn't exist and therefore cannot be copied. If chef will properly handle scripts not exiting successful (exit code 0), you wouldn't strictly need another check, but if it doesn't, I would strongly recommend creating one.

I can't check chef's behaviour right now, but testing this is easy: Put an exit 1 as the last line of your script and see what happens.

Anyway, since checking for an existing file isn't difficult, why not simply do it, just to be on the safe side?

share|improve this answer

You should definitely use a not_if or only_if metaparameter to the script resource, otherwise Chef will execute the script every time it runs on the node. Depending on the software you're compiling, that can be as simple as existence of the file, or that it is a non-zero size, or as complex as verifying that the command copied in /usr/local/bin outputs some particular string.

For example, if you want to see if the file merely exists and assume the best:

script "install_foo" do
  # .. other stuff you wrote!
  not_if "test -x /usr/local/bin/foo"
end

You should probably make sure that the file is executable, since you're putting it in /usr/local/bin.

file "/usr/local/bin/foo" do
  mode 0755
end

If you want to check that the command has some particular output, you could do something like:

script "install_foo" do
  # .. other stuff you wrote!
  not_if "test -x /usr/local/bin/foo && /usr/local/bin/foo | grep -x 'foo is awesome'"
end

(assuming that running foo would have that output, of course.)

The not_if (and only_if) metaparameters can take a string or a Ruby block. If it is a string, then that will be shell commands executed on the system. If it is a Ruby block (do .. end or { }), then it will be executed as Ruby.

Meta-parameters are documented on the Chef wiki under the "resources" page.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.