2

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

3 Answers 3

0

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?

0

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.

0

I would rather use Chef::Mixin::ShellOut utilities like in this post https://stackoverflow.com/questions/16309808/how-can-i-put-the-output-of-a-chef-execute-resource-into-a-variable

I would run the command with the right parameter to check its version number (-v in most cases[?]), this way you are checking two things:

  1. compiling not only worked, but created a binary that runs
  2. you have the version you expect to have

because having a file is not enough to assume that the binary runs and that it is the version you expect

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .