From e078767b2b5feae2b8b3537a739112fe2aaeea2f Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 29 Mar 2018 15:46:45 -0700 Subject: [PATCH] run now supports script files as well as inline scripts --- lib/vagrant/plugin/v2/trigger.rb | 34 +++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/vagrant/plugin/v2/trigger.rb b/lib/vagrant/plugin/v2/trigger.rb index faca79405..17c0469c9 100644 --- a/lib/vagrant/plugin/v2/trigger.rb +++ b/lib/vagrant/plugin/v2/trigger.rb @@ -1,5 +1,6 @@ require 'log4r' require 'shellwords' +require 'fileutils' require "vagrant/util/subprocess" @@ -126,7 +127,7 @@ module Vagrant if !trigger.run_remote.nil? @logger.debug("Executing trigger run_remote script on #{guest_name}...") - self.run_remote(trigger.run, trigger.on_error, guest_name) + self.run_remote(trigger.run, trigger.on_error) end end end @@ -149,32 +150,36 @@ module Vagrant # # @param [ShellProvisioner/Config] config A Shell provisioner config def run(config, on_error) + # TODO: I18n me if !config.inline.nil? cmd = Shellwords.split(config.inline) - @machine.ui.info("Running local: Inline script") + @machine.ui.info("Executing local: Inline script") else - @machine.ui.info("Running local: File script #{config.path}") + cmd = File.expand_path(config.path, @env.root_path) + FileUtils.chmod("+x", cmd) # TODO: what about windows + @machine.ui.info("Executing local: File script #{config.path}") end begin + # TODO: should we check config or command for sudo? And if so, WARN the user? result = Vagrant::Util::Subprocess.execute(*cmd, :notify => [:stdout, :stderr]) do |type,data| case type when :stdout - @machine.ui.info(data) + @machine.ui.detail(data) when :stderr - @machine.ui.warn(data) + @machine.ui.error(data) end end rescue Exception => e - #binding.pry if on_error == :halt @logger.debug("Trigger run encountered an error. Halting on error...") raise e else @logger.debug("Trigger run encountered an error. Continuing on anyway...") - @machine.ui.warn("Trigger run failed:") - @machine.ui.warn(e.message) + # TODO: I18n me and write better message + @machine.ui.error("Trigger run failed:") + @machine.ui.error(e.message) end end end @@ -182,16 +187,21 @@ module Vagrant # Runs a script on the host # # @param [ShellProvisioner/Config] config A Shell provisioner config - def run_remote(config, on_error, guest_name) + def run_remote(config, on_error) # make sure guest actually exists, if not, display a WARNING # # get machine, and run shell provisioner on it begin - rescue Error + rescue Exception => e if on_error == :halt - raise Error + @logger.debug("Trigger run encountered an error. Halting on error...") + raise e + else + @logger.debug("Trigger run encountered an error. Continuing on anyway...") + # TODO: I18n me and write better message + @machine.ui.error("Trigger run failed:") + @machine.ui.error(e.message) end - @logger.debug("Trigger run_remote encountered an error. Continuing on anyway...") end end end