fix winssh to use the command filters from winrm, and make sure we use powershell for certain commands

This commit is contained in:
Z. Cliffe Schreuders 2019-09-20 15:28:48 +01:00
parent 29e741c30c
commit ed8542e782
1 changed files with 12 additions and 1 deletions

View File

@ -1,4 +1,5 @@
require File.expand_path("../../ssh/communicator", __FILE__) require File.expand_path("../../ssh/communicator", __FILE__)
require_relative "../winrm/command_filter"
module VagrantPlugins module VagrantPlugins
module CommunicatorWinSSH module CommunicatorWinSSH
@ -9,6 +10,7 @@ module VagrantPlugins
def initialize(machine) def initialize(machine)
super super
@logger = Log4r::Logger.new("vagrant::communication::winssh") @logger = Log4r::Logger.new("vagrant::communication::winssh")
@cmd_filter = VagrantPlugins::CommunicatorWinRM::CommandFilter.new()
end end
# Executes the command on an SSH connection within a login shell. # Executes the command on an SSH connection within a login shell.
@ -21,6 +23,15 @@ module VagrantPlugins
sudo = opts[:sudo] sudo = opts[:sudo]
shell = (opts[:shell] || machine_config_ssh.shell).to_s shell = (opts[:shell] || machine_config_ssh.shell).to_s
# If this is a *nix command with no Windows equivalent, don't run it
command_filtered = @cmd_filter.filter(command)
# transform command to windows equivalent, and force powershell for certain commands
if command_filtered != command || command_filtered =~ /^(cp|\$)/
return 0 if command_filtered.empty?
shell = "powershell"
command = command_filtered
end
@logger.info("Execute: #{command} (sudo=#{sudo.inspect})") @logger.info("Execute: #{command} (sudo=#{sudo.inspect})")
exit_status = nil exit_status = nil
@ -36,7 +47,7 @@ module VagrantPlugins
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" if shell == "powershell"
base_cmd = "powershell -File #{remote_name} -InputFormat None" base_cmd = "powershell -File #{remote_name} < nul"
tfile.puts <<-SCRIPT.force_encoding('ASCII-8BIT') tfile.puts <<-SCRIPT.force_encoding('ASCII-8BIT')
Remove-Item #{remote_name} Remove-Item #{remote_name}
Write-Host #{CMD_GARBAGE_MARKER} Write-Host #{CMD_GARBAGE_MARKER}