How to execute a command block only if previous block succeed?


#1

I have written a custom provider, foo

action :helloworld do

end

And in my recipe, I have lines…

foo “bar” do
action : helloworld
end



execute “bye” do
command "xxx"
action :run
end

How to make my last execute block run only if my custom provider “foo” is
executed correctly?

i.e. How to make my helloworld action return success/error status and such
that the later block can make use of this value?

Thanks.


#2

You’re asking two different questions here, and the solution will depend on which is actually the case.

If you really just need for one resource to be dependent on the other, use the action :nothing / notifies pattern:

execute “bye” do
command "xxx"
action :nothing
end

foo “bar” do
action :helloworld
notifies :run, "execute[bye]"
end

If you actually need to send the exit status from one resource to the other, then you’ll probably need to hack something on your provider to populate a node attribute, I think… :slight_smile:

  • cassiano

On Monday, May 20, 2013 at 14:22, howard chen wrote:

I have written a custom provider, foo

action :helloworld do

end

And in my recipe, I have lines…

foo “bar” do
action : helloworld
end



execute “bye” do
command "xxx"
action :run
end

How to make my last execute block run only if my custom provider “foo” is executed correctly?

i.e. How to make my helloworld action return success/error status and such that the later block can make use of this value?

Thanks.


#3

in chef resources are executed in the same order as they are written in the
recipe. also if one of them is borked the entore chef run will bomb. This
is the default way and you can change the ordering using notify/subscribe
and also ignore any resource’s failure using the ignore_failure meta
attribute.
So, what you have asked should be doable by vanilla chef run, without any
foo, assuming your lwrp provider throws exception when its not able to do
the job.
Also if you think the sole purpose of the execute block is tied with the
lwrp, you might pull it down in your provider itself, (may be in a separate
action),

On Mon, May 20, 2013 at 10:22 AM, howard chen howachen@gmail.com wrote:

I have written a custom provider, foo

action :helloworld do

end

And in my recipe, I have lines…

foo “bar” do
action : helloworld
end



execute “bye” do
command "xxx"
action :run
end

How to make my last execute block run only if my custom provider “foo” is
executed correctly?

i.e. How to make my helloworld action return success/error status and such
that the later block can make use of this value?

Thanks.


#4

Unfortunately, there are more steps in between the two blocks (foo LWRP and
execute) , so I can’t notify immediately, e.g.

foo “bar” do
action : helloworld
end


…(these steps need to be run first)

execute “bye” do
command "xxx"
action :run
end

On Tue, May 21, 2013 at 1:26 AM, Cassiano Leal cassianoleal@gmail.comwrote:

You’re asking two different questions here, and the solution will depend
on which is actually the case.

If you really just need for one resource to be dependent on the other, use
the action :nothing / notifies pattern:

execute “bye” do
command "xxx"
action :nothing
end

foo “bar” do
action :helloworld
notifies :run, "execute[bye]"
end

If you actually need to send the exit status from one resource to the
other, then you’ll probably need to hack something on your provider to
populate a node attribute, I think… :slight_smile:

  • cassiano

On Monday, May 20, 2013 at 14:22, howard chen wrote:

I have written a custom provider, foo

action :helloworld do

end

And in my recipe, I have lines…

foo “bar” do
action : helloworld
end



execute “bye” do
command "xxx"
action :run
end

How to make my last execute block run only if my custom provider “foo” is
executed correctly?

i.e. How to make my helloworld action return success/error status and such
that the later block can make use of this value?

Thanks.