Error Handling with Shell Provisioner

711 views
Skip to first unread message

Harish Patel

unread,
Aug 13, 2014, 3:22:29 PM8/13/14
to vagra...@googlegroups.com
Is there any way that I could run 'vagrant destroy' if one of my shell provisioners fails?  Out of the box, it seems like --destroy-on-error is supposed to do this but it doesn't.  I have 3 shell files that my Vagrantfile calls, and if any one of them fail, the downstream ones are not run but the VM is still up.

Harish Patel

unread,
Aug 15, 2014, 10:58:57 AM8/15/14
to vagra...@googlegroups.com
I calling my shell file like below:

  config.vm.provision "shell" do |s|
    s.path = "#{SCRIPTS}/#{CONFIGURE_SETTINGS}"
    s.args = "#{USER} #{PW}"
  end

If this script returns a non zero exit status, can I destroy the VM?

Harish Patel

unread,
Aug 15, 2014, 4:26:53 PM8/15/14
to vagra...@googlegroups.com
Tried something like before but the 'puts' statement is only run at the very beggining of 'vagrant up', not when the actual provisioner is run.  In this case, I want to print a message so the user knows that the shell file failed.

config.vm.provision "shell" do |s|
    begin
      s.path = "#{SCRIPTS}/#{CONFIGURE_SETTINGS}"
      s.args = "#{USER} #{PW}"
    rescue
      puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!AN ERROR REACHED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    end
  end

Alvaro Miranda Aguilera

unread,
Aug 15, 2014, 7:21:20 PM8/15/14
to vagra...@googlegroups.com
can those messages in case of errors be on the script?



--
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Harish Patel

unread,
Aug 18, 2014, 10:35:35 AM8/18/14
to vagra...@googlegroups.com
I would rather have the code in the VagrantFile so I do not have to duplicate across shell files.

Alvaro Miranda Aguilera

unread,
Aug 18, 2014, 5:08:20 PM8/18/14
to vagra...@googlegroups.com
I understand, but i think will be easier in the other way around.

The scripts you create should be idempotent, that is, sense they are already executed, or skip running twice for any check you do.

when you provision, if one script fail, my impression is the provision will fail, and the err will be displayed on the screen on a nice red color.

There is nothing wrong with this approach, as you can just 'vagrant provision' a 2nd time, and the script(s) should be smart enough to sense witch ones/parts did run and skip them.

I deploy multi node oracle rac clusters, and I need all the scripts to be run at least once, but in case of error, I would like to avoid a partition be blown away, or the 2nd disk to be formatted again, etc..

Then later on my test case, I add a 3rd or 4th node, and I can safely run vagrant up / vagrant provision and will just do the new nodes.

this require a little bit more effor on the script, but later it pays pretty soon.

Reply all
Reply to author
Forward
0 new messages