Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any hook like "pre vagrant up"?

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.

like image 277
Nepomuk Frädrich Avatar asked Jan 31 '14 09:01

Nepomuk Frädrich


4 Answers

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 => "..."
like image 87
Emyl Avatar answered Nov 20 '22 04:11

Emyl


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
like image 11
tmatilai Avatar answered Nov 20 '22 04:11

tmatilai


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
like image 5
Rudger Avatar answered Nov 20 '22 04:11

Rudger


.. 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.

like image 2
moritz Avatar answered Nov 20 '22 04:11

moritz