From 8affdd24b67bce754221a554f26a2bb35b799613 Mon Sep 17 00:00:00 2001 From: Jeff Bonhag Date: Mon, 13 Jan 2020 16:33:41 -0500 Subject: [PATCH] Wire up inline command for popular Windows shells * Remove connection between shell provisioner directory config and WinSSH directory config because these should remain separate. --- plugins/communicators/winssh/communicator.rb | 10 +++++++--- plugins/provisioners/shell/provisioner.rb | 6 +++--- .../plugins/communicators/winssh/communicator_test.rb | 10 ++++++++-- .../plugins/provisioners/shell/provisioner_test.rb | 9 +++++---- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/plugins/communicators/winssh/communicator.rb b/plugins/communicators/winssh/communicator.rb index d2d0de60a..5d78255c5 100644 --- a/plugins/communicators/winssh/communicator.rb +++ b/plugins/communicators/winssh/communicator.rb @@ -34,7 +34,11 @@ module VagrantPlugins stderr_data_buffer = '' if force_raw - base_cmd = "cmd /Q /C #{command}" + if shell == "powershell" + base_cmd = "powershell \"Write-Host #{CMD_GARBAGE_MARKER}; [Console]::Error.WriteLine('#{CMD_GARBAGE_MARKER}'); #{command}\"" + else + base_cmd = "cmd /q /c \"ECHO #{CMD_GARBAGE_MARKER} && ECHO #{CMD_GARBAGE_MARKER} 1>&2 && #{command}\"" + end else tfile = Tempfile.new('vagrant-ssh') remote_ext = shell == "powershell" ? "ps1" : "bat" @@ -197,7 +201,7 @@ SCRIPT end end @logger.debug("Ensuring remote directory exists for destination upload") - create_remote_directory(File.dirname(dest)) + create_remote_directory(File.dirname(dest), force_raw: true) @logger.debug("Uploading file #{path} to remote #{dest}") upload_file = File.open(path, "rb") begin @@ -223,7 +227,7 @@ SCRIPT end def create_remote_directory(dir, force_raw=false) - execute("dir \"#{dir}\"\n if errorlevel 1 (mkdir \"#{dir}\")", shell: "cmd", force_raw: force_raw) + execute("if not exist \"#{dir}\" mkdir \"#{dir}\"", shell: "cmd", force_raw: force_raw) end end end diff --git a/plugins/provisioners/shell/provisioner.rb b/plugins/provisioners/shell/provisioner.rb index 1758a995d..d63f7bfb7 100644 --- a/plugins/provisioners/shell/provisioner.rb +++ b/plugins/provisioners/shell/provisioner.rb @@ -43,9 +43,9 @@ module VagrantPlugins @_upload_path = config.upload_path.to_s if @_upload_path.empty? - case @machine.config.vm.communicator - when :winssh - @_upload_path = "C:\\Windows\\Temp\\vagrant-shell" + case @machine.config.vm.guest + when :windows + @_upload_path = "C:\\tmp\\vagrant-shell" else @_upload_path = "/tmp/vagrant-shell" end diff --git a/test/unit/plugins/communicators/winssh/communicator_test.rb b/test/unit/plugins/communicators/winssh/communicator_test.rb index 8370e93db..d04a3652a 100644 --- a/test/unit/plugins/communicators/winssh/communicator_test.rb +++ b/test/unit/plugins/communicators/winssh/communicator_test.rb @@ -207,18 +207,24 @@ describe VagrantPlugins::CommunicatorWinSSH::Communicator do context "with force_raw set to true" do it "does not write to a temp file" do expect(ssh_cmd_file).to_not receive(:puts) - communicator.execute("dir", force_raw: true) + expect(communicator.execute("dir", force_raw: true)).to eq(0) end it "does not upload a wrapper script" do expect(communicator).to_not receive(:upload) - communicator.execute("dir", force_raw: true) + expect(communicator.execute("dir", force_raw: true)).to eq(0) end it "executes the base command" do expect(channel).to receive(:exec).with(/dir/) expect(communicator.execute("dir", force_raw: true)).to eq(0) end + + it "prepends UUID output to command for garbage removal" do + expect(channel).to receive(:exec). + with(/ECHO #{command_garbage_marker} && ECHO #{command_garbage_marker}.*/) + expect(communicator.execute("dir", force_raw: true)).to eq(0) + end end end diff --git a/test/unit/plugins/provisioners/shell/provisioner_test.rb b/test/unit/plugins/provisioners/shell/provisioner_test.rb index 45625a60e..6f49fadad 100644 --- a/test/unit/plugins/provisioners/shell/provisioner_test.rb +++ b/test/unit/plugins/provisioners/shell/provisioner_test.rb @@ -8,6 +8,7 @@ describe "Vagrant::Shell::Provisioner" do let(:machine) { double(:machine, env: env, id: "ID").tap { |machine| allow(machine).to receive_message_chain(:config, :vm, :communicator).and_return(:not_winrm) + allow(machine).to receive_message_chain(:config, :vm, :guest).and_return(:linux) allow(machine).to receive_message_chain(:communicate, :tap) {} } } @@ -349,13 +350,13 @@ describe "Vagrant::Shell::Provisioner" do expect(vsp.upload_path).to eq("/tmp/vagrant-shell") end - context "with winssh provisioner" do + context "windows" do before do - allow(machine).to receive_message_chain(:config, :vm, :communicator).and_return(:winssh) + allow(machine).to receive_message_chain(:config, :vm, :guest).and_return(:windows) end - it "should default to C:\\Windows\\Temp\\vagrant-shell" do - expect(vsp.upload_path).to eq("C:\\Windows\\Temp\\vagrant-shell") + it "should default to C:\\tmp\\vagrant-shell" do + expect(vsp.upload_path).to eq("C:\\tmp\\vagrant-shell") end end end