From 48b7c682ad6a21314b7b5c7e5d7c157bafa4657e Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 29 Mar 2018 11:24:06 -0700 Subject: [PATCH] Use active machine object --- lib/vagrant/machine.rb | 3 ++- lib/vagrant/plugin/v2/trigger.rb | 35 +++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 12ea8b8dc..8633bd196 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -110,7 +110,6 @@ module Vagrant @ui = Vagrant::UI::Prefixed.new(@env.ui, @name) @ui_mutex = Mutex.new @state_mutex = Mutex.new - @triggers = Vagrant::Plugin::V2::Trigger.new(@env, @ui, @config.trigger) # Read the ID, which is usually in local storage @id = nil @@ -150,6 +149,8 @@ module Vagrant # Output a bunch of information about this machine in # machine-readable format in case someone is listening. @ui.machine("metadata", "provider", provider_name) + + @triggers = Vagrant::Plugin::V2::Trigger.new(@env, @config.trigger, self) end # This calls an action on the provider. The provider may or may not diff --git a/lib/vagrant/plugin/v2/trigger.rb b/lib/vagrant/plugin/v2/trigger.rb index 83dd075b7..4dcb1604e 100644 --- a/lib/vagrant/plugin/v2/trigger.rb +++ b/lib/vagrant/plugin/v2/trigger.rb @@ -1,4 +1,5 @@ require 'log4r' +require "vagrant/util/subprocess" require 'pry' @@ -13,12 +14,12 @@ module Vagrant # defined inside a Vagrantfile. # # @param [Object] env Vagrant environment - # @param [Object] ui Machines ui object # @param [Object] config Trigger configuration - def initialize(env, ui, config) - @env = env - @machine_ui = ui - @config = config + # @param [Object] machine Active Machine + def initialize(env, config, machine) + @env = env + @config = config + @machine = machine @logger = Log4r::Logger.new("vagrant::trigger::#{self.class.to_s.downcase}") end @@ -50,7 +51,7 @@ module Vagrant unless triggers.empty? @logger.info("Firing trigger for action #{action} on guest #{guest_name}") # TODO I18N me - @machine_ui.info("Running triggers #{stage} #{action}...") + @machine.ui.info("Running triggers #{stage} #{action}...") fire(triggers, guest_name) end end @@ -97,9 +98,9 @@ module Vagrant # TODO: I18n me if !trigger.name.nil? - @machine_ui.info("Running trigger: #{trigger.name}...") + @machine.ui.info("Running trigger: #{trigger.name}...") else - @machine_ui.info("Running trigger...") + @machine.ui.info("Running trigger...") end if !trigger.info.nil? @@ -128,20 +129,26 @@ module Vagrant # # @param [String] message The string to be printed def info(message) - @machine_ui.info(message) + @machine.ui.info(message) end # Prints the given message at warn level for a trigger # # @param [String] message The string to be printed def warn(message) - @machine_ui.warn(message) + @machine.ui.warn(message) end # Runs a script on a guest # # @param [ShellProvisioner/Config] config A Shell provisioner config def run(config, on_error) + begin + rescue Error + if on_error == :halt + raise Error + end + end end # Runs a script on the host @@ -149,6 +156,14 @@ module Vagrant # @param [ShellProvisioner/Config] config A Shell provisioner config def run_remote(config, on_error, guest_name) # make sure guest actually exists, if not, display a WARNING + # + # get machine, and run shell provisioner on it + begin + rescue Error + if on_error == :halt + raise Error + end + end end end end