Chef recipe does not fail on bash resource


#1

Hi All,

I have written this chef recipe below,

This is what i expect/require

  • The bash resource shall fail is the shell script i am running below
    fails,
  • the cookbook file should not say up to date if the bash script below fail

Please advise

cookbook_file “#{node[:mule][:deploy_dir]}/x-billing-1.1.0-SNAPSHOT.zip” do
source "x-billing-1.1.0-SNAPSHOT.zip"
mode 777
notifies :run, “bash[install_bill]”, :immediately
end

Run a bash shell - download and extract tomcat

bash “install_bill” do
cwd "#{node[:mule][:bin_dir]}"
code <<-EOH
./deploy.sh -v x-billing-1.1.0-SNAPSHOT -e $env ebpi x-billing
sleep 30
echo " i am done deploying the app"
EOH
returns 0
action :nothing
end


#2

On Mon, Sep 30, 2013 at 8:17 PM, Manoj Thakkar manoj.thakkar@gmail.com wrote:

Hi All,

I have written this chef recipe below,

This is what i expect/require

  • The bash resource shall fail is the shell script i am running below fails,
  • the cookbook file should not say up to date if the bash script below fail

The shell script always completes successfully because the last
command, echo, works. You probably want to put…

“set -e”

… at the beginning which will case bash to exit when it firsts
encounters an error.


Andy Gale
http://andy-gale.com
http://twitter.com/andygale


#3

Thanks Andy,

It helped, The tricky part is if the bash script fails the cookbook file
attribute will not run in next time since the file is the same so the bash
resource will not run also.

How do i make bash script run if it failed last time ? is there a feature ?
or may be i shall throw a file output check the status is that file and
rerun bash script no matter what.

Thanks
Manoj

On Mon, Sep 30, 2013 at 1:09 PM, Andy Gale andy@salgo.net wrote:

On Mon, Sep 30, 2013 at 8:17 PM, Manoj Thakkar manoj.thakkar@gmail.com
wrote:

Hi All,

I have written this chef recipe below,

This is what i expect/require

  • The bash resource shall fail is the shell script i am running below
    fails,
  • the cookbook file should not say up to date if the bash script below
    fail

The shell script always completes successfully because the last
command, echo, works. You probably want to put…

“set -e”

… at the beginning which will case bash to exit when it firsts
encounters an error.


Andy Gale
http://andy-gale.com
http://twitter.com/andygale


#4

On Monday, September 30, 2013 at 2:35 PM, Manoj Thakkar wrote:

Thanks Andy,

It helped, The tricky part is if the bash script fails the cookbook file attribute will not run in next time since the file is the same so the bash resource will not run also.

How do i make bash script run if it failed last time ? is there a feature ? or may be i shall throw a file output check the status is that file and rerun bash script no matter what.
Your best bet is to use some knowledge about what the script actually does to guard against it running when not needed, and code this into a not_if, only_if or creates parameter on the bash resource itself. See: http://docs.opscode.com/resource_common.html

Thanks
Manoj


Daniel DeLeo


#5

Thanks Daniel,

will the not_if work with action:nothing ? if not then it will be a little
trickier , i think i might have to break the dependency on cookbook file
for bash script and depend on some other file or resource.

Thanks
Manoj

On Mon, Sep 30, 2013 at 2:41 PM, Daniel DeLeo dan@kallistec.com wrote:

On Monday, September 30, 2013 at 2:35 PM, Manoj Thakkar wrote:

Thanks Andy,

It helped, The tricky part is if the bash script fails the cookbook file
attribute will not run in next time since the file is the same so the bash
resource will not run also.

How do i make bash script run if it failed last time ? is there a feature
? or may be i shall throw a file output check the status is that file and
rerun bash script no matter what.

Your best bet is to use some knowledge about what the script actually does
to guard against it running when not needed, and code this into a not_if,
only_if or creates parameter on the bash resource itself. See:
http://docs.opscode.com/resource_common.html

Thanks
Manoj


Daniel DeLeo


#6

On Monday, September 30, 2013 at 2:46 PM, Manoj Thakkar wrote:

Thanks Daniel,

will the not_if work with action:nothing ? if not then it will be a little trickier , i think i might have to break the dependency on cookbook file for bash script and depend on some other file or resource.
It does, but I think what you want is for the bash script to run anytime some condition is not met (looks like this condition is, “my application is correctly deployed to the latest version”). If it fails for some reason, and you want Chef to retry it next time, then you probably want to use the default action on the script resource, and let the not_if/only_if check take care of deciding whether it should run.

Thanks
Manoj


Daniel DeLeo


#7

Thanks Daniel,

wil this serve the purpose ? i understand what you are trying to say , but
i am not sure the mix/match like this will work or not.

bash “install_bill” do
cwd "#{node[:mule][:bin_dir]}"
code <<-EOH
./deploy.sh -v x-billing-1.1.0-SNAPSHOT -e $env ebpi x-billing
sleep 30
echo " i am done deploying the app"
EOH
returns 0
not_if { ::File.exists?(extract_path) }
action :nothing
end

On Mon, Sep 30, 2013 at 2:50 PM, Daniel DeLeo dan@kallistec.com wrote:

On Monday, September 30, 2013 at 2:46 PM, Manoj Thakkar wrote:

Thanks Daniel,

will the not_if work with action:nothing ? if not then it will be a
little trickier , i think i might have to break the dependency on cookbook
file for bash script and depend on some other file or resource.

It does, but I think what you want is for the bash script to run anytime
some condition is not met (looks like this condition is, “my application is
correctly deployed to the latest version”). If it fails for some reason,
and you want Chef to retry it next time, then you probably want to use the
default action on the script resource, and let the not_if/only_if check
take care of deciding whether it should run.

Thanks
Manoj


Daniel DeLeo


#8

On Monday, September 30, 2013 at 2:57 PM, Manoj Thakkar wrote:

Thanks Daniel,

wil this serve the purpose ? i understand what you are trying to say , but i am not sure the mix/match like this will work or not.

bash “install_bill” do
cwd "#{node[:mule][:bin_dir]}"
code <<-EOH
./deploy.sh (http://deploy.sh) -v x-billing-1.1.0-SNAPSHOT -e $env ebpi x-billing
sleep 30
echo " i am done deploying the app"
EOH
returns 0
not_if { ::File.exists?(extract_path) }
action :nothing
end

This is the basic idea. If you intend to upgrade the x-billing-1.1.0-SNAPSHOT package and you need to re-run deploy.sh whenever that happens, you’ll need to find a condition for not_if that will properly account for that. Also, you no longer should need action :nothing, as this bash resource will skip itself whenever “extract_path” exists.


Daniel DeLeo


#9

Thanks Daniel,

I think i got the point, i wil basically end up removing the notify section
and add an if condition for that also,

Anyways id there a way to do a a force rerun of a recipe , i mean like
cookbook_file should be rerun if i pass a flag or something, i think that’s
easier than writing an if condition for this use case.

On Mon, Sep 30, 2013 at 3:19 PM, Daniel DeLeo dan@kallistec.com wrote:

On Monday, September 30, 2013 at 2:57 PM, Manoj Thakkar wrote:

Thanks Daniel,

wil this serve the purpose ? i understand what you are trying to say , but
i am not sure the mix/match like this will work or not.

bash “install_bill” do
cwd "#{node[:mule][:bin_dir]}"
code <<-EOH
./deploy.sh -v x-billing-1.1.0-SNAPSHOT -e $env ebpi x-billing
sleep 30
echo " i am done deploying the app"
EOH
returns 0
not_if { ::File.exists?(extract_path) }
action :nothing
end

This is the basic idea. If you intend to upgrade the x-billing-1.1.0-SNAPSHOT
package and you need to re-run deploy.sh whenever that happens, you’ll need
to find a condition for not_if that will properly account for that. Also,
you no longer should need action :nothing, as this bash resource will
skip itself whenever “extract_path” exists.


Daniel DeLeo


#10

On Monday, September 30, 2013 at 4:07 PM, Manoj Thakkar wrote:

Thanks Daniel,

I think i got the point, i wil basically end up removing the notify section and add an if condition for that also,

Anyways id there a way to do a a force rerun of a recipe , i mean like cookbook_file should be rerun if i pass a flag or something, i think that’s easier than writing an if condition for this use case.
No. Core Chef resources check the current state before hand and only make changes if necessary. If you update the cookbook file, then Chef will notice and modify the file on disk when you run chef next.


Daniel DeLeo

On Mon, Sep 30, 2013 at 3:19 PM, Daniel DeLeo <dan@kallistec.com (mailto:dan@kallistec.com)> wrote:

On Monday, September 30, 2013 at 2:57 PM, Manoj Thakkar wrote:

Thanks Daniel,

wil this serve the purpose ? i understand what you are trying to say , but i am not sure the mix/match like this will work or not.

bash “install_bill” do
cwd "#{node[:mule][:bin_dir]}"
code <<-EOH
./deploy.sh (http://deploy.sh) -v x-billing-1.1.0-SNAPSHOT -e $env ebpi x-billing
sleep 30
echo " i am done deploying the app"
EOH
returns 0
not_if { ::File.exists?(extract_path) }
action :nothing
end

This is the basic idea. If you intend to upgrade the x-billing-1.1.0-SNAPSHOT package and you need to re-run deploy.sh (http://deploy.sh) whenever that happens, you’ll need to find a condition for not_if that will properly account for that. Also, you no longer should need action :nothing, as this bash resource will skip itself whenever “extract_path” exists.


Daniel DeLeo