2014-11-17 18:16:39 +00:00
|
|
|
require "fileutils"
|
|
|
|
require "tempfile"
|
2014-12-12 09:36:39 +00:00
|
|
|
require "vagrant/util/safe_exec"
|
2014-11-14 20:51:35 +00:00
|
|
|
|
|
|
|
require_relative "errors"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module LocalExecPush
|
|
|
|
class Push < Vagrant.plugin("2", :push)
|
|
|
|
def push
|
2014-11-17 18:16:39 +00:00
|
|
|
if config.inline
|
|
|
|
execute_inline!(config.inline)
|
|
|
|
else
|
|
|
|
execute_script!(config.script)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Execute the inline script by writing it to a tempfile and executing.
|
|
|
|
def execute_inline!(inline)
|
|
|
|
script = Tempfile.new(["vagrant-local-exec-script", ".sh"])
|
|
|
|
script.write(inline)
|
|
|
|
script.rewind
|
2015-07-07 05:42:07 +00:00
|
|
|
script.close
|
2014-11-17 18:16:39 +00:00
|
|
|
|
|
|
|
execute_script!(script.path)
|
|
|
|
ensure
|
|
|
|
if script
|
|
|
|
script.close
|
|
|
|
script.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Execute the script, expanding the path relative to the current env root.
|
|
|
|
def execute_script!(path)
|
|
|
|
path = File.expand_path(path, env.root_path)
|
|
|
|
FileUtils.chmod("+x", path)
|
|
|
|
execute!(path)
|
2014-11-14 20:51:35 +00:00
|
|
|
end
|
|
|
|
|
2014-11-17 18:16:39 +00:00
|
|
|
# Execute the script, raising an exception if it fails.
|
2014-11-14 20:51:35 +00:00
|
|
|
def execute!(*cmd)
|
2015-11-20 00:55:48 +00:00
|
|
|
if Vagrant::Util::Platform.windows?
|
|
|
|
execute_subprocess!(*cmd)
|
|
|
|
else
|
|
|
|
execute_exec!(*cmd)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Run the command as exec (unix).
|
|
|
|
def execute_exec!(*cmd)
|
2014-12-12 09:36:39 +00:00
|
|
|
Vagrant::Util::SafeExec.exec(cmd[0], *cmd[1..-1])
|
2014-11-14 20:51:35 +00:00
|
|
|
end
|
2015-11-20 00:55:48 +00:00
|
|
|
|
|
|
|
# Run the command as a subprocess (windows).
|
|
|
|
def execute_subprocess!(*cmd)
|
|
|
|
cmd = cmd.dup << { notify: [:stdout, :stderr] }
|
|
|
|
result = Vagrant::Util::Subprocess.execute(*cmd) do |type, data|
|
|
|
|
if type == :stdout
|
|
|
|
@env.ui.info(data, new_line: false)
|
|
|
|
elsif type == :stderr
|
|
|
|
@env.ui.warn(data, new_line: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Kernel.exit(result.exit_code)
|
|
|
|
end
|
2014-11-14 20:51:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|