Skip to content

Instantly share code, notes, and snippets.

@rgl
Created May 27, 2019 21:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rgl/095458a3f46aa867144fb2e125a00d6d to your computer and use it in GitHub Desktop.
Save rgl/095458a3f46aa867144fb2e125a00d6d to your computer and use it in GitHub Desktop.
an example on how to see which vagrant triggers exist
# NB before running this you need to export an environment variable: export VAGRANT_EXPERIMENTAL='typed_triggers'
# see https://www.vagrantup.com/docs/triggers/configuration.html#trigger-types
# NB to see all triggers you need to edit your vagrant source code:
# sudo vim /opt/vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/plugin/v2/trigger.rb
# and add a @ui.info to see all trigger action stages, et all:
# NB see https://github.com/hashicorp/vagrant/blob/v2.2.4/lib/vagrant/plugin/v2/trigger.rb
# # Fires all triggers, if any are defined for the action and guest. Returns early
# # and logs a warning if the community plugin `vagrant-triggers` is installed
# #
# # @param [Symbol] action Vagrant command to fire trigger on
# # @param [Symbol] stage :before or :after
# # @param [String] guest_name The guest that invoked firing the triggers
# def fire_triggers(action, stage, guest_name, type)
# @ui.info("XXX fire_triggers action=#{action} stage=#{stage} guest_name=#{guest_name} type=#{type}")
config.trigger.after :'VagrantPlugins::CommandUp::StoreBoxMetadata', type: :action do |trigger|
trigger.info = 'XXX VagrantPlugins::CommandUp::StoreBoxMetadata'
trigger.ruby do |env, machine|
# NB @env is https://github.com/hashicorp/vagrant/blob/v2.2.4/lib/vagrant/environment.rb
# NB @machine is https://github.com/hashicorp/vagrant/blob/v2.2.4/lib/vagrant/machine.rb
# see whether the machine is already provisioned.
# see https://github.com/hashicorp/vagrant/blob/v2.2.4/lib/vagrant/action/builtin/provision.rb#L41
# NB to start the first provisioner for this to be true.
already_provisioned = false
sentinel_path = machine.data_dir.join("action_provision")
if sentinel_path.file?
parts = sentinel_path.read.chomp.split(":", 2)
if parts[0] == "1.5" && parts[1] == machine.id.to_s
already_provisioned = true
end
end
puts "XXX machine.name=#{machine.name}"
puts "XXX machine.id=#{machine.id}"
puts "XXX machine.state=#{machine.state}"
puts "XXX already_provisioned? #{already_provisioned}"
puts "XXX trigger run machine=#{machine.inspect}"
puts "XXX trigger run machine.provider=#{machine.provider.inspect}"
puts "XXX machine.config.vm.communicator=#{machine.config.vm.communicator || :ssh}"
puts "XXX machine communicator=#{machine.communicate}" # for an example see https://github.com/rgl/vagrant-windows-update/blob/v0.0.7/lib/vagrant-windows-update.rb#L53
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment