Fixup paths to work with server-side Bash

This commit normalizes our Windows paths to use `/` instead of `\`.
These paths are compatible with both cmd and PowerShell, and are
required if the server-side shell is set to Bash.

The OpenSSH server executes all commands inside a default login shell
which cannot be controlled by the Vagrant configuration. So what ends up
getting executed on the server side looks something like this:

    "c:\\program files\\git\\bin\\bash.exe" -c "C:\\Windows\\Temp\\vagrant-ssh20200130-41670-1w5nsjy.bat"

By flipping the direction of the directory slashes, we end up with:

    "c:\\program files\\git\\bin\\bash.exe" -c "C:/Windows/Temp/vagrant-ssh20200130-43415-f1d5n2.bat"

This works whether the server-side shell is set to cmd, powershell, or
bash.
This commit is contained in:
Jeff Bonhag 2020-01-30 13:29:21 -05:00 committed by Chris Roberts
parent fcd07e3a37
commit 4b7d55afaa
4 changed files with 26 additions and 7 deletions

View File

@ -45,7 +45,7 @@ module VagrantPlugins
else
tfile = Tempfile.new('vagrant-ssh')
remote_ext = shell == "powershell" ? "ps1" : "bat"
remote_name = "#{machine_config_ssh.upload_directory}\\#{File.basename(tfile.path)}.#{remote_ext}"
remote_name = "#{machine_config_ssh.upload_directory}/#{File.basename(tfile.path)}.#{remote_ext}"
if shell == "powershell"
base_cmd = "powershell -File #{remote_name}"

View File

@ -14,7 +14,7 @@ module VagrantPlugins
def finalize!
@shell = "cmd" if @shell == UNSET_VALUE
@sudo_command = "%c" if @sudo_command == UNSET_VALUE
@upload_directory = "C:\\Windows\\Temp" if @upload_directory == UNSET_VALUE
@upload_directory = "C:/Windows/Temp" if @upload_directory == UNSET_VALUE
if @export_command_template == UNSET_VALUE
if @shell == "cmd"
@export_command_template = 'set %ENV_KEY%="%ENV_VALUE%"'

View File

@ -40,12 +40,17 @@ module VagrantPlugins
def upload_path
if !defined?(@_upload_path)
@_upload_path = config.upload_path.to_s
case @machine.config.vm.guest
when :windows
@_upload_path = Vagrant::Util::Platform.unix_windows_path(config.upload_path.to_s)
else
@_upload_path = config.upload_path.to_s
end
if @_upload_path.empty?
case @machine.config.vm.guest
when :windows
@_upload_path = "C:\\tmp\\vagrant-shell"
@_upload_path = "C:/tmp/vagrant-shell"
else
@_upload_path = "/tmp/vagrant-shell"
end

View File

@ -355,19 +355,21 @@ describe "Vagrant::Shell::Provisioner" do
allow(machine).to receive_message_chain(:config, :vm, :guest).and_return(:windows)
end
it "should default to C:\\tmp\\vagrant-shell" do
expect(vsp.upload_path).to eq("C:\\tmp\\vagrant-shell")
it "should default to C:/tmp/vagrant-shell" do
expect(vsp.upload_path).to eq("C:/tmp/vagrant-shell")
end
end
end
context "when upload_path is set" do
let(:upload_path) { "arbitrary" }
let(:config) {
double(
:config,
:args => "doesn't matter",
:env => {},
:upload_path => "arbitrary",
:upload_path => upload_path,
:remote? => false,
:path => "doesn't matter",
:inline => "doesn't matter",
@ -384,6 +386,18 @@ describe "Vagrant::Shell::Provisioner" do
it "should use the value from from config" do
expect(vsp.upload_path).to eq("arbitrary")
end
context "windows" do
let(:upload_path) { "C:\\Windows\\Temp" }
before do
allow(machine).to receive_message_chain(:config, :vm, :guest).and_return(:windows)
end
it "should normalize the slashes" do
expect(vsp.upload_path).to eq("C:/Windows/Temp")
end
end
end
context "with cached value" do