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

Clean Up when "Vagrant Destroying" Specific Machine in Multi-Machine Vagrantfile #28

Closed jamiejackson closed 9 years ago

jamiejackson commented 9 years ago

Say I've got a multi-machine Vagrantfile:

# -*- mode: ruby -*-
# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker'

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  unless Vagrant.has_plugin?("vagrant-triggers")
    raise 'vagrant-triggers plugin needs to be installed: vagrant plugin install vagrant-triggers'
  end

  host_script_dir = "./scripts"
  vm_script_dir = "/vagrant/scripts"

  host_provisioned_dir = "#{host_temp_dir}/provisioned"
  vm_provisioned_dir = "#{vm_temp_dir}/provisioned"

  config.vm.define "db" do |v|

    # do stuff

  end

  config.vm.define "web" do |v|

    # do stuff

  end

  config.trigger.after :destroy, :stdout => true, :force => true do
    # this is too coarse of a provisioning step. for instance, 
    # maybe i want to remove a specific subdirectory when i 
    # destroy the "web" machine
    info "Removing provisioned directory contents: #{host_provisioned_dir}/*"
    FileUtils.rm_rf Dir.glob("#{host_provisioned_dir}/*")
  end

end

Is there a way to tie the cleanup to the machine being destroyed?

emyl commented 9 years ago

Hello,

The :vm option should be a perfect fit for your use case:

config.trigger.after :destroy, :stdout => true, :force => true, :vm => "web" do
  info "Removing provisioned directory contents: #{host_provisioned_dir}/*"
  FileUtils.rm_rf Dir.glob("#{host_provisioned_dir}/*")
end
jamiejackson commented 9 years ago

I'll try it out, thanks, Emilio.

On Tue, Nov 25, 2014 at 11:39 AM, Emiliano Ticci notifications@github.com wrote:

Hello,

The :vm option should be a perfect fit for your use case:

config.trigger.after :destroy, :stdout => true, :force => true, :vm => "web" do info "Removing provisioned directory contents: #{host_provisioneddir}/" FileUtils.rm_rf Dir.glob("#{host_provisioneddir}/")end

— Reply to this email directly or view it on GitHub https://github.com/emyl/vagrant-triggers/issues/28#issuecomment-64429629 .