Update triggers based on code review

This commit is contained in:
Brian Cain 2018-04-05 14:59:02 -07:00
parent 5e9387dca8
commit 12b1a3dfe4
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
3 changed files with 18 additions and 22 deletions

View File

@ -11,7 +11,7 @@ module Vagrant
module Plugin module Plugin
module V2 module V2
class Trigger class Trigger
# @return [Kernel_V2/Config/Trigger] # @return [Kernel_V2::Config::Trigger]
attr_reader :config attr_reader :config
# This class is responsible for setting up basic triggers that were # This class is responsible for setting up basic triggers that were
@ -53,7 +53,7 @@ module Vagrant
triggers = filter_triggers(triggers, guest_name) triggers = filter_triggers(triggers, guest_name)
unless triggers.empty? if !triggers.empty?
@logger.info("Firing trigger for action #{action} on guest #{guest_name}") @logger.info("Firing trigger for action #{action} on guest #{guest_name}")
@machine.ui.info(I18n.t("vagrant.trigger.start", stage: stage, action: action)) @machine.ui.info(I18n.t("vagrant.trigger.start", stage: stage, action: action))
fire(triggers, guest_name) fire(triggers, guest_name)
@ -79,7 +79,7 @@ module Vagrant
filter.each do |trigger| filter.each do |trigger|
index = nil index = nil
match = false match = false
if !trigger.only_on.nil? if trigger.only_on
trigger.only_on.each do |o| trigger.only_on.each do |o|
if o.match?(guest_name) if o.match?(guest_name)
# trigger matches on current guest, so we're fine to use it # trigger matches on current guest, so we're fine to use it
@ -116,24 +116,20 @@ module Vagrant
@machine.ui.info(I18n.t("vagrant.trigger.fire")) @machine.ui.info(I18n.t("vagrant.trigger.fire"))
end end
if !trigger.info.nil? if trigger.info
@logger.debug("Executing trigger info message...") info(trigger.info)
self.info(trigger.info)
end end
if !trigger.warn.nil? if trigger.warn
@logger.debug("Executing trigger warn message...") warn(trigger.warn)
self.warn(trigger.warn)
end end
if !trigger.run.nil? if trigger.run
@logger.debug("Executing trigger run script...") run(trigger.run, trigger.on_error)
self.run(trigger.run, trigger.on_error)
end end
if !trigger.run_remote.nil? if trigger.run_remote
@logger.debug("Executing trigger run_remote script on #{guest_name}...") run_remote(trigger.run_remote, trigger.on_error)
self.run_remote(trigger.run_remote, trigger.on_error)
end end
end end
end end
@ -154,7 +150,7 @@ module Vagrant
# Runs a script on a guest # Runs a script on a guest
# #
# @param [ShellProvisioner/Config] config A Shell provisioner config # @param [Provisioners::Shell::Config] config A Shell provisioner config
def run(config, on_error) def run(config, on_error)
if !config.inline.nil? if !config.inline.nil?
cmd = Shellwords.split(config.inline) cmd = Shellwords.split(config.inline)
@ -164,10 +160,9 @@ module Vagrant
cmd = Shellwords.split("#{powershell_exe} #{config.powershell_args} '#{cmd.join(' ')}'") cmd = Shellwords.split("#{powershell_exe} #{config.powershell_args} '#{cmd.join(' ')}'")
end end
@machine.ui.detail(I18n.t("vagrant.trigger.run.inline")) @machine.ui.detail(I18n.t("vagrant.trigger.run.inline", command: config.inline))
else else
cmd = File.expand_path(config.path, @env.root_path) cmd = File.expand_path(config.path, @env.root_path)
FileUtils.chmod("+x", cmd)
cmd << " #{config.args.join(' ' )}" if config.args cmd << " #{config.args.join(' ' )}" if config.args
@ -194,7 +189,7 @@ module Vagrant
@machine.ui.detail(data, options) @machine.ui.detail(data, options)
end end
rescue Exception => e rescue => e
@machine.ui.error(I18n.t("vagrant.errors.triggers_run_fail")) @machine.ui.error(I18n.t("vagrant.errors.triggers_run_fail"))
@machine.ui.error(e.message) @machine.ui.error(e.message)
@ -230,7 +225,7 @@ module Vagrant
begin begin
prov.provision prov.provision
rescue Exception => e rescue => e
@machine.ui.error(I18n.t("vagrant.errors.triggers_run_fail")) @machine.ui.error(I18n.t("vagrant.errors.triggers_run_fail"))
if on_error == :halt if on_error == :halt

View File

@ -113,7 +113,7 @@ module VagrantPlugins
# Convert @run and @run_remote to be a "Shell provisioner" config # Convert @run and @run_remote to be a "Shell provisioner" config
if @run && @run.is_a?(Hash) if @run && @run.is_a?(Hash)
new_run = VagrantPlugins::Shell::Config.new() new_run = VagrantPlugins::Shell::Config.new
new_run.set_options(@run) new_run.set_options(@run)
# don't run local commands as sudo by default # don't run local commands as sudo by default
new_run.privileged = false new_run.privileged = false
@ -122,7 +122,7 @@ module VagrantPlugins
end end
if @run_remote && @run_remote.is_a?(Hash) if @run_remote && @run_remote.is_a?(Hash)
new_run = VagrantPlugins::Shell::Config.new() new_run = VagrantPlugins::Shell::Config.new
new_run.set_options(@run_remote) new_run.set_options(@run_remote)
new_run.finalize! new_run.finalize!
@run_remote = new_run @run_remote = new_run

View File

@ -292,6 +292,7 @@ en:
run: run:
inline: |- inline: |-
Running local: Inline script Running local: Inline script
%{command}
script: |- script: |-
Running local script: %{path} Running local script: %{path}