emyl / vagrant-triggers

Allow the definition of arbitrary scripts that will run on the host before and/or after Vagrant commands.
MIT License
546 stars 35 forks source link

Trigger script only on first vagrant up #63

Closed WillGibson closed 8 years ago

WillGibson commented 8 years ago

Maybe this is already possible, but I can't find any documentation to point me in the right direction, but...

Is it possible to hook into the "after up" only when the Vagrant box is created from scratch?

damko commented 8 years ago

Hello Will,

this does not answer your question precisely but I've been working on this today.

So far I've got this http://pastebin.com/9kjgmiDm

Check the FROM_SCRATCH variable in that Vagrantfile, maybe you can get some ideas.

I'm also looking for the "after up" scenario. If I will have some news I'll keep you posted

WillGibson commented 8 years ago

Thanks @damko, that would work apart from the fact that we are using VMware.

I'm trying something in our Puppet manifest...

damko commented 8 years ago

@WillGibson did you have any luck?

WillGibson commented 8 years ago

We had to go another route due to deadlines.

In our Vagrantfile...

config.vm.provision :shell, :inline => "cd /vagrant/_migrations && ./migrate_on_first_vagrant_up"

In /vagrant/_migrations/migrate_on_first_vagrant_up...

#!/usr/bin/env bash

migration_done_file="/home/vagrant/.first_build_migration_done"

function check_last_command {
    status=$?
    if [ $status -ne 0 ]; then
        exit $status
    fi
    return 0
}

if [ ! -e $migration_done_file ]; then
    /vagrant/vendor/bin/phinx migrate -c /vagrant/_migrations/phinx.yml
    check_last_command
    touch $migration_done_file
    check_last_command
fi

exit 0

It works well enough.

emyl commented 8 years ago

The built-in provisioner should work for your use case:

Vagrant.configure("2") do |config|
  # Your existing Vagrant configuration
  ...

  config.vm.provision "trigger", :option => "value" do |trigger|
    trigger.fire do
      run "script"
    end
  end
end

Cheers :smiley:

WillGibson commented 8 years ago

@emyl Wouldn't that run any time you do vagrant provision?