michail_w
michail_w

Reputation: 4481

Chef notifies when command fails

execute "pear7 install mail" do
    command "pear7 install mail"
    action :nothing
end

bash "Check if pear/mail was not installed" do
    command "! (pear7 info pear/mail > /dev/null 2>&1)"
    notifies :run,"execute[pear7 install mail]", :immediately
end

pear7 info pear/mail returns 0 exit code when package is installed and 1 when it doesn't exist. I would like to notify first action only in the second case, that's why I added negative on the beginning of command.

The thing is that first block is executed always. How can I achieve result when notification is send only if the package is not installed?

I can't use only_if or not_if guards, because I have to run other commands which will make notifications to the same job.

Upvotes: 0

Views: 1264

Answers (2)

michail_w
michail_w

Reputation: 4481

Here is how I dealt with it. Unfortunately I couldn't avoid guards.

execute "pear7 install mail" do
    command "pear7 install mail"
    action :nothing
end

Chef::Resource::Bash.send(:include, Chef::Mixin::ShellOut)

bash "Check if pear/mail was not installed" do
    command ":"
    only_if { shell_out("pear7 info pear/mail > /dev/null 2>&1").exitstatus != 0 }
    notifies :run,"execute[pear7 install mail]", :immediately
end

Command which is only a colon means "do nothing". It's required to add a guard.

Upvotes: 0

coderanger
coderanger

Reputation: 54267

This is not how notifications work. They fire if the resource is considered "updated". For execute, it is considered updated if the command runs. The return code is not looked at for notification purposes at all. Despite saying you can't, the correct answer is probably not_if/only_if guards, otherwise you'll need some custom resource stuffs and to use shell_out() and manually sequence things.

Upvotes: 2

Related Questions