Nicolas Peltier
Nicolas Peltier

Reputation: 128

Is it possible to add action hook before vagrant halt/suspend

i would like to trigger action on my VM before the suspend or halt action takes effect.

I see action hook i could use but they are all after the actions are done https://www.vagrantup.com/docs/plugins/action-hooks.html. Any clues?

Upvotes: 3

Views: 2286

Answers (4)

YouJiacheng
YouJiacheng

Reputation: 687

I don't know how to use config.trigger in a plugin instead of a Vagrantfile.

However, I found that the docs is completely wrong, that "after" is incorrect!

Actually, you just need to use prepend:

require_relative "action"
action_hook(:goodhosts, :machine_action_halt) do |hook|
  hook.prepend(Action)
end

And this is how integrated version of trigger works:

Note: the hooks/middlewares are nested, not sequential.

The community version of trigger always uses prepend with "sandwich middleware", i.e. there are operations both before and after the call to the next middleware.

def call(env)
  before
  @app.call(env)
  after

While the integrated version of trigger uses prepend for "before" and append for "after" with "linear middleware", i.e. there are operations only before the call to the next middleware, as if the hooks were sequential.

def call(env)
  operation
  @app.call(env)

Since provider's actions halt and suspend are middlewares as well (hyperv can be taken as an example), prepended hooks can definitely operate before the halt or suspend action takes effect.

Upvotes: 0

Frederic Henri
Frederic Henri

Reputation: 53753

I believe most of those hooks are for plugin development - what you're looking at is the vagrant trigger plugin so if you want to have action done before suspend or halt :

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

  # run some script before the guest is halted
  config.trigger.before :halt do
    info "Dumping the database before destroying the VM..."
    run_remote  "bash /vagrant/cleanup.sh"
  end

  # run some script before the guest is suspended
  config.trigger.before :suspend do
    info "Dumping the database before destroying the VM..."
    run_remote  "bash /vagrant/cleanup.sh"
  end

  # clean up files on the host after the guest is destroyed
  config.trigger.after :destroy do
    run "rm -Rf tmp/*"
  end

  # start apache on the guest after the guest starts
  config.trigger.after :up do
    run_remote "service apache2 start"
  end

end

Upvotes: 4

lqbweb
lqbweb

Reputation: 1708

You can have a look at:

https://github.com/lqbweb/vagrant_hook

It is a basic and simple vagrant plugin that proves how to attach to a certain vagrant chain action.

Upvotes: 0

Rafał Malinowski
Rafał Malinowski

Reputation: 1176

I think you could also try to use udev events on guest machine. This one can be done with provisioning when the vagrant up is done for the first time (or later with vagrant provision. For example I'm restarting php5-fpm after nfs mounts directory with code.

I think you could also use systemd on guest OS (if you're using OS that has systemd of course).

Upvotes: 0

Related Questions