Allow #provision_winrm to modify upload_path

We need to ensure that Windows files have an extension when provisioning
via WinRM.
This commit is contained in:
Jeff Bonhag 2020-02-03 17:30:38 -05:00 committed by Chris Roberts
parent d1ad67e333
commit 85f0fce57a
4 changed files with 60 additions and 12 deletions

View File

@ -48,28 +48,27 @@ module VagrantPlugins
remote_name = "#{machine_config_ssh.upload_directory}/#{File.basename(tfile.path)}.#{remote_ext}"
if shell == "powershell"
base_cmd = "powershell -File #{remote_name}"
tfile.puts <<-SCRIPT.force_encoding('ASCII-8BIT')
Remove-Item #{remote_name}
Write-Host #{CMD_GARBAGE_MARKER}
[Console]::Error.WriteLine("#{CMD_GARBAGE_MARKER}")
#{command}
SCRIPT
base_cmd = "powershell -file #{remote_name}"
else
base_cmd = remote_name
tfile.puts <<-SCRIPT.force_encoding('ASCII-8BIT')
DEL #{remote_name}
ECHO OFF
ECHO #{CMD_GARBAGE_MARKER}
ECHO #{CMD_GARBAGE_MARKER} 1>&2
#{command}
SCRIPT
base_cmd = "cmd /q /c #{remote_name}"
end
tfile.close
upload(tfile.path, remote_name)
tfile.delete
base_cmd = shell_cmd(opts.merge(shell: base_cmd))
end
@logger.debug("Base SSH exec command: #{base_cmd}")

View File

@ -194,19 +194,20 @@ module VagrantPlugins
@machine.communicate.tap do |comm|
# Make sure that the upload path has an extension, since
# having an extension is critical for Windows execution
if File.extname(upload_path) == ""
upload_path += File.extname(path.to_s)
winrm_upload_path = upload_path
if File.extname(winrm_upload_path) == ""
winrm_upload_path += File.extname(path.to_s)
end
# Upload it
comm.upload(path.to_s, upload_path)
comm.upload(path.to_s, winrm_upload_path)
# Build the environment
env = config.env.map { |k,v| "$env:#{k} = #{quote_and_escape(v.to_s)}" }
env = env.join("; ")
# Calculate the path that we'll be executing
exec_path = upload_path
exec_path = winrm_upload_path
exec_path.gsub!('/', '\\')
exec_path = "c:#{exec_path}" if exec_path.start_with?("\\")

View File

@ -416,4 +416,52 @@ describe "Vagrant::Shell::Provisioner" do
end
end
end
describe "#provision_winrm" do
let(:config) {
double(
:config,
:args => "doesn't matter",
:env => {},
:upload_path => "arbitrary",
:remote? => false,
:path => "script/info.ps1",
:binary => false,
:md5 => nil,
:sha1 => 'EXPECTED_VALUE',
:sha256 => nil,
:sha384 => nil,
:sha512 => nil,
:reset => false,
:reboot => false,
:powershell_args => "",
:name => nil,
:privileged => false,
:powershell_elevated_interactive => false
)
}
let(:vsp) {
VagrantPlugins::Shell::Provisioner.new(machine, config)
}
let(:communicator) { double("communicator") }
let(:guest) { double("guest") }
let(:ui) { double("ui") }
before {
allow(guest).to receive(:capability?).with(:wait_for_reboot).and_return(false)
allow(ui).to receive(:detail)
allow(communicator).to receive(:sudo)
allow(machine).to receive(:communicate).and_return(communicator)
allow(machine).to receive(:guest).and_return(guest)
allow(machine).to receive(:ui).and_return(ui)
allow(vsp).to receive(:with_script_file).and_yield(config.path)
}
it "ensures that files are uploaded with an extension" do
expect(communicator).to receive(:upload).with(config.path, /arbitrary.ps1$/)
vsp.send(:provision_winrm, "")
end
end
end

View File

@ -533,11 +533,11 @@ EOF
expect(subject.wsl_drvfs_path?("/home/vagrant/some/path")).to be_falsey
end
end
end
describe ".unix_windows_path" do
it "takes a windows path and returns a unixy path" do
expect(subject.unix_windows_path("C:\\Temp\\Windows")).to eq("C:/Temp/Windows")
end
describe ".unix_windows_path" do
it "takes a windows path and returns a POSIX-like path" do
expect(subject.unix_windows_path("C:\\Temp\\Windows")).to eq("C:/Temp/Windows")
end
end
end