diff --git a/plugins/communicators/winrm/communicator.rb b/plugins/communicators/winrm/communicator.rb index 850b0eba7..c806960e7 100644 --- a/plugins/communicators/winrm/communicator.rb +++ b/plugins/communicators/winrm/communicator.rb @@ -4,6 +4,7 @@ require "log4r" require_relative "helper" require_relative "shell" +require_relative "command_filter" module VagrantPlugins module CommunicatorWinRM @@ -16,9 +17,10 @@ module VagrantPlugins end def initialize(machine) - @machine = machine - @logger = Log4r::Logger.new("vagrant::communication::winrm") - @shell = nil + @machine = machine + @shell = nil + @logger = Log4r::Logger.new("vagrant::communication::winrm") + @cmd_filter = CommandFilter.new() @logger.info("Initializing WinRMCommunicator") end @@ -50,6 +52,10 @@ module VagrantPlugins end def execute(command, opts={}, &block) + # If this is a *nix command with no Windows equivilant, don't run it + command = @cmd_filter.filter(command) + return 0 if command.empty? + opts = { :error_check => true, :error_class => Errors::ExecutionError, @@ -58,12 +64,6 @@ module VagrantPlugins :shell => :powershell }.merge(opts || {}) - if opts[:shell] == :powershell - script = File.expand_path("../scripts/command_alias.ps1", __FILE__) - script = File.read(script) - command = script << "\r\n" << command << "\r\nexit $LASTEXITCODE" - end - output = shell.send(opts[:shell], command, &block) return output if opts[:shell] == :wql @@ -74,10 +74,9 @@ module VagrantPlugins alias_method :sudo, :execute def test(command, opts=nil) - @logger.info("Testing: #{command}") - - # HACK: to speed up Vagrant 1.2 OS detection, skip checking for *nix OS - return false unless (command =~ /^uname|^cat \/etc|^cat \/proc|grep 'Fedora/).nil? + # If this is a *nix command with no Windows equivilant, assume failure + command = @cmd_filter.filter(command) + return false if command.empty? opts = { :error_check => false }.merge(opts || {}) execute(command, opts) == 0 diff --git a/plugins/communicators/winrm/scripts/command_alias.ps1 b/plugins/communicators/winrm/scripts/command_alias.ps1 deleted file mode 100644 index 0955db6d8..000000000 --- a/plugins/communicators/winrm/scripts/command_alias.ps1 +++ /dev/null @@ -1,35 +0,0 @@ -function which { - $command = [Array](Get-Command $args[0] -errorAction SilentlyContinue) - if($null -eq $command) - { - exit 1 - } - write-host $command[0].Definition - exit 0 -} - -function test ([Switch] $d, [String] $path) { - if(Test-Path $path) - { - exit 0 - } - exit 1 -} - -function chmod { - exit 0 -} - -function chown { - exit 0 -} - -function mkdir ([Switch] $p, [String] $path) -{ - if(Test-Path $path) - { - exit 0 - } else { - New-Item $path -Type Directory -Force | Out-Null - } -} diff --git a/plugins/communicators/winrm/shell.rb b/plugins/communicators/winrm/shell.rb index 772c8944d..131c79141 100644 --- a/plugins/communicators/winrm/shell.rb +++ b/plugins/communicators/winrm/shell.rb @@ -52,6 +52,9 @@ module VagrantPlugins end def powershell(command, &block) + # ensure an exit code + command << "\r\n" + command << "if ($LASTEXITCODE) { exit $LASTEXITCODE } else { exit 0 }" execute_shell(command, :powershell, &block) end