run now supports script files as well as inline scripts

This commit is contained in:
Brian Cain 2018-03-29 15:46:45 -07:00
parent 8b70abd4d0
commit e078767b2b
No known key found for this signature in database
GPG Key ID: 43D51080D357A001
1 changed files with 22 additions and 12 deletions

View File

@ -1,5 +1,6 @@
require 'log4r' require 'log4r'
require 'shellwords' require 'shellwords'
require 'fileutils'
require "vagrant/util/subprocess" require "vagrant/util/subprocess"
@ -126,7 +127,7 @@ module Vagrant
if !trigger.run_remote.nil? if !trigger.run_remote.nil?
@logger.debug("Executing trigger run_remote script on #{guest_name}...") @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 end
end end
@ -149,32 +150,36 @@ module Vagrant
# #
# @param [ShellProvisioner/Config] config A Shell provisioner config # @param [ShellProvisioner/Config] config A Shell provisioner config
def run(config, on_error) def run(config, on_error)
# TODO: I18n me
if !config.inline.nil? if !config.inline.nil?
cmd = Shellwords.split(config.inline) cmd = Shellwords.split(config.inline)
@machine.ui.info("Running local: Inline script") @machine.ui.info("Executing local: Inline script")
else 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 end
begin 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| result = Vagrant::Util::Subprocess.execute(*cmd, :notify => [:stdout, :stderr]) do |type,data|
case type case type
when :stdout when :stdout
@machine.ui.info(data) @machine.ui.detail(data)
when :stderr when :stderr
@machine.ui.warn(data) @machine.ui.error(data)
end end
end end
rescue Exception => e rescue Exception => e
#binding.pry
if on_error == :halt if on_error == :halt
@logger.debug("Trigger run encountered an error. Halting on error...") @logger.debug("Trigger run encountered an error. Halting on error...")
raise e raise e
else else
@logger.debug("Trigger run encountered an error. Continuing on anyway...") @logger.debug("Trigger run encountered an error. Continuing on anyway...")
@machine.ui.warn("Trigger run failed:") # TODO: I18n me and write better message
@machine.ui.warn(e.message) @machine.ui.error("Trigger run failed:")
@machine.ui.error(e.message)
end end
end end
end end
@ -182,16 +187,21 @@ module Vagrant
# Runs a script on the host # Runs a script on the host
# #
# @param [ShellProvisioner/Config] config A Shell provisioner config # @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 # make sure guest actually exists, if not, display a WARNING
# #
# get machine, and run shell provisioner on it # get machine, and run shell provisioner on it
begin begin
rescue Error rescue Exception => e
if on_error == :halt 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 end
@logger.debug("Trigger run_remote encountered an error. Continuing on anyway...")
end end
end end
end end