Issue
I'm trying to get Chef to perform the following:
- Check if the desired debian (can be any package really) is available
- If it is, apt-get install the package
- If it's not, build the package using the source
I know you can do:
remote_file "some remote file" do
...
not_if "apt-cache search 'mypackage'"
end
However, I tried:
ruby_block "Attempting to install #{node[:bact][:application_name]}" do
block do
cmd = Chef::ShellOut.new("apt-get install -y --force-yes #{node[:bact][:application_name]}")
exec_result = cmd.run_command
if exec_result.exitstatus != 0
Chef::Log.info 'Go grab some coffee, this might be a while....'
resources("execute[install-#{node[:bact][:application_name]}-via-pip]").run_action(:run)
end
end
action :create
end
Is there an easier and less uglier way to do this?
Basically, what I'd ideally like to do is:
begin
package 'some-package-name' do
action :install
done
rescue Chef::Exception
# Do something here
end
Solution
You could install the Debian package using ignore_failure true. Then you can install the pip package only if the Debian package is not installed at this point. This could look something like this:
package node[:bact][:application_name] do
ignore_failure true
end
# Resource available from the opscode python cookbook
python_pip node[:bact][:application_name] do
# Install the pip package only if the debian package is not installed
not_if "dpkg-query -W '#{node[:bact][:application_name]}'"
end
Answered By - Holger Just Answer Checked By - Senaida (WPSolving Volunteer)