Update powerup to handle spaces properly in arguments

This commit is contained in:
Chris Roberts 2018-11-09 15:26:55 -08:00
parent e8cee899a8
commit 4ff5291b89
1 changed files with 23 additions and 25 deletions

View File

@ -1,3 +1,4 @@
require "base64"
require "tmpdir" require "tmpdir"
require_relative "subprocess" require_relative "subprocess"
require_relative "which" require_relative "which"
@ -208,18 +209,26 @@ module Vagrant
# @return [Array<String>] # @return [Array<String>]
def self.powerup_command(path, args, opts) def self.powerup_command(path, args, opts)
Dir.mktmpdir("vagrant") do |dpath| Dir.mktmpdir("vagrant") do |dpath|
all_args = ["-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", path] + args all_args = [path] + args.flatten.map{ |a|
arg_list = "@('" + all_args.join("', '") + "')" a.gsub(/^['"](.+)['"]$/, "\\1")
}
arg_list = "\"" + all_args.join("\" \"") + "\""
stdout = File.join(dpath, "stdout.txt") stdout = File.join(dpath, "stdout.txt")
stderr = File.join(dpath, "stderr.txt") stderr = File.join(dpath, "stderr.txt")
exitcode = File.join(dpath, "exitcode.txt")
script = "$sp = Start-Process -FilePath powershell -ArgumentList #{arg_list} " \ script = "& #{arg_list} ; exit $LASTEXITCODE;"
"-PassThru -Wait -RedirectStandardOutput '#{stdout}' -RedirectStandardError '#{stderr}' -WindowStyle Hidden; " \ script_content = Base64.urlsafe_encode64(script.encode("UTF-16LE", "UTF-8"))
"if($sp){ Set-Content -Path '#{exitcode}' -Value $sp.ExitCode;exit $sp.ExitCode; }else{ exit 1 }"
# escape quotes so we can nest our script within a start-process # Wrap so we can redirect output to read later
script.gsub!("'", "''") wrapper = "$p = Start-Process -FilePath powershell -ArgumentList @('-NoLogo', '-NoProfile', " \
"'-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', '#{script_content}') " \
"-PassThru -WindowStyle Hidden -Wait -RedirectStandardOutput '#{stdout}' -RedirectStandardError '#{stderr}'; " \
"if($p){ exit $p.ExitCode; }else{ exit 1 }"
wrapper_content = Base64.urlsafe_encode64(wrapper.encode("UTF-16LE", "UTF-8"))
powerup = "$p = Start-Process -FilePath powershell -ArgumentList @('-NoLogo', '-NoProfile', " \
"'-NonInteractive', '-ExecutionPolicy', 'Bypass', '-EncodedCommand', '#{wrapper_content}') " \
"-PassThru -WindowStyle Hidden -Wait -Verb RunAs; if($p){ exit $p.ExitCode; }else{ exit 1 }"
cmd = [ cmd = [
"powershell", "powershell",
@ -227,31 +236,20 @@ module Vagrant
"-NoProfile", "-NoProfile",
"-NonInteractive", "-NonInteractive",
"-ExecutionPolicy", "Bypass", "-ExecutionPolicy", "Bypass",
"-Command", "$p = Start-Process -FilePath powershell -ArgumentList " \ "-Command", powerup
"@('-NoLogo', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-Command', '#{script}') " \
"-PassThru -Wait -WindowStyle Hidden -Verb RunAs; if($p){ exit $p.ExitCode; }else{ exit 1 }"
] ]
result = Subprocess.execute(*cmd.push(opts)) result = Subprocess.execute(*cmd.push(opts))
r_stdout = result.stdout
if File.exist?(stdout) if File.exist?(stdout)
r_stdout = File.read(stdout) r_stdout += File.read(stdout)
else
r_stdout = result.stdout
end end
r_stderr = result.stderr
if File.exist?(stderr) if File.exist?(stderr)
r_stderr = File.read(stderr) r_stderr += File.read(stderr)
else
r_stderr = result.stderr
end end
code = 1 Subprocess::Result.new(result.exit_code, r_stdout, r_stderr)
if File.exist?(exitcode)
code_txt = File.read(exitcode).strip
if code_txt.match(/^\d+$/)
code = code_txt.to_i
end
end
Subprocess::Result.new(code, r_stdout, r_stderr)
end end
end end