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

Execute a trigger before/after/instead_of *any* command #23

Closed gidantribal closed 9 years ago

gidantribal commented 10 years ago

Hello,

I have a specific use case, where I need to copy a private key to a temporary location and assign it proper access rights. I need this copy because it's impossible to correctly control the file access rights from a directory on windows host mapped to the Ubuntu guest.

....
override.ssh.username = "ubuntu"
override.ssh.private_key_path = "/local_ubuntu_directory/default.pem"
....

To do this I used your plugin:

  config.trigger.before [:up, :provision ....]  do
     run "sudo /bin/cp /mapped_directory/default.pem /local_ubuntu_directory/"
     run "sudo /bin/chmod 0600 /local_ubuntu_directory/default.pem"
     run "sudo /bin/chown vagrant:vagrant /local_ubuntu_directory/default.pem"
   end

  config.trigger.after [:up, :provision ....] do
    run "sudo /bin/rm -f /local_ubuntu_directory/default.pem"
  end

But I have two problem with this:

  1. Security issue, if a vagrant command fails I don't get the temporary key deleted
  2. I have to list every possible vagrant command in the trigger, because if I don't include a command, it will fail with "file not found" error (vagrant enforces _override.ssh.private_keypath file existence): is there an easy way to specify 'any' command?
emyl commented 9 years ago

This works since version 0.5.0 using the special :ALL action:

config.trigger.before :ALL do
  run "ls"
end

If instead you need to run a trigger on all actions except one or more, you could use the new blacklist syntax:

config.trigger.blacklist :destroy
config.trigger.before :ALL do
  run "ls"
end

BTW, I leave the ticket open since some documentation is still needed.

gidantribal commented 9 years ago

thanks, that works like a charm!