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 to execute on guest and create a database dump before vagrant destroy #8

Closed micheldenegri closed 10 years ago

micheldenegri commented 10 years ago

Hello, Im curious if i can use your plugin to execute a script that connects to guest machine and create a database dump before vagrant destroy. or is it possible to use those triggers to execute the script directly on the guest machine?

emyl commented 10 years ago

Hey,

Despite the plugin is intended for running script on the host, it could perhaps be used in a quite hacky way to get what you need:

config.trigger.before :destroy do
  info "Dumping the database before destroying the VM..."
  run  "vagrant ssh -c 'your dump command'"
end

Note that:

  1. I've used the new syntax introduced today with version 0.3 of the plugin (here some details).
  2. Altough I don't see why it shouldn't work, I've never tested a similar scenario.
  3. Obviously your dump destination must be a shared folder, it would otherwise be destroyed.
micheldenegri commented 10 years ago

Thanks a lot @emyl it works

komlenic commented 9 years ago

For anyone finding this, you can also now use 'run_remote':

config.trigger.before :destroy do
  info "Dumping the database before destroying the VM..."
  run_remote  "command"
end

If you prefer to use a shell script to contain all your teardown/cleanup tasks:

config.trigger.before :destroy do
  info "Dumping the database before destroying the VM..."
  run_remote  "bash /vagrant/cleanup.sh"
end

... where /vagrant/cleanup.sh is a script that will exist on both the host and the guest, because vagrant shares the directory containing the vagrantfile on the host, with /vagrant on the guest by default.

fhenri commented 9 years ago

I see its a bit old but really useful, the run_remote command should be highlighted from the main documentation.