I'm trying to automate my development boxes with vagrant. I need to share the vagrant setup with other developers, so we need to be sure that some boundary conditions are fullfilled before the normal vagrant up process is started.
Is there any hook (like in git, pre-commit or other pre-* scripts) in vagrant? The provision scripts are much too late.
My current setup looks like this:
Vagrantfile
vagrant-templates/
vagrant-templates/apache.conf
vagrant-templates/...
sub-project1/
sub-project2/
I need to be sure, that sub-project{1..n} exists and if not, there should be a error message.
I would prefer a bash-like solution, but I'm open minded for other solutions.
You could give a try to this Vagrant plugin I've written:
https://github.com/emyl/vagrant-triggers
Once installed, you could put in your Vagrantfile something like:
config.trigger.before :up, :execute => "..."
Another plugin to check out is vagrant-host-shell which is only run when provisioning the box. Just add it before other provisoners in Vagrantfile:
config.vm.provision :host_shell do |shell|
  shell.inline = './clone-projects.sh'
  shell.abort_on_nonzero = true
end
It's possible to write your own plugin for vagrant and use action_hook on the machine_action_up, something like:
require 'vagrant-YOURPLUGINNAME/YOURACTIONCLASS'
module VagrantPlugins
  module YOURPLUGINNAME
    class Plugin < Vagrant.plugin('2')
      name 'YOURPLUGINNAME'
      description <<-DESC
          Some description of your plugin
      DESC
      config(:YOURPLUGINNAME) do
        require_relative 'config'
        Config
      end
      action_hook(:YOURPLUGINNAME, :machine_action_up) do |hook|
        hook.prepend(YOURACTIONCLASS.METHOD)
      end
    end
  end
end
.. adding to tmatilai's answer, you can add something like this:
case ARGV[0]
when "provision", "up"
  system "./prepare.sh"
else
  # do nothing
end
into your Vagrantfile so that it will only be run on specific commands.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With