Integrated WinRM command filter into communicator

*nix commands are now filtered out instead of being sent to the guest. This means the command_alias PowerShell script is no longer needed.

Moved the PowerShell exit code helper to the WinRM shell and changed it to always return an exit code.
This commit is contained in:
Shawn Neal 2014-04-23 20:38:16 -07:00
parent 1525aa0f78
commit f44c795eed
3 changed files with 15 additions and 48 deletions

View File

@ -4,6 +4,7 @@ require "log4r"
require_relative "helper" require_relative "helper"
require_relative "shell" require_relative "shell"
require_relative "command_filter"
module VagrantPlugins module VagrantPlugins
module CommunicatorWinRM module CommunicatorWinRM
@ -16,9 +17,10 @@ module VagrantPlugins
end end
def initialize(machine) def initialize(machine)
@machine = machine @machine = machine
@logger = Log4r::Logger.new("vagrant::communication::winrm") @shell = nil
@shell = nil @logger = Log4r::Logger.new("vagrant::communication::winrm")
@cmd_filter = CommandFilter.new()
@logger.info("Initializing WinRMCommunicator") @logger.info("Initializing WinRMCommunicator")
end end
@ -50,6 +52,10 @@ module VagrantPlugins
end end
def execute(command, opts={}, &block) 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 = { opts = {
:error_check => true, :error_check => true,
:error_class => Errors::ExecutionError, :error_class => Errors::ExecutionError,
@ -58,12 +64,6 @@ module VagrantPlugins
:shell => :powershell :shell => :powershell
}.merge(opts || {}) }.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) output = shell.send(opts[:shell], command, &block)
return output if opts[:shell] == :wql return output if opts[:shell] == :wql
@ -74,10 +74,9 @@ module VagrantPlugins
alias_method :sudo, :execute alias_method :sudo, :execute
def test(command, opts=nil) def test(command, opts=nil)
@logger.info("Testing: #{command}") # If this is a *nix command with no Windows equivilant, assume failure
command = @cmd_filter.filter(command)
# HACK: to speed up Vagrant 1.2 OS detection, skip checking for *nix OS return false if command.empty?
return false unless (command =~ /^uname|^cat \/etc|^cat \/proc|grep 'Fedora/).nil?
opts = { :error_check => false }.merge(opts || {}) opts = { :error_check => false }.merge(opts || {})
execute(command, opts) == 0 execute(command, opts) == 0

View File

@ -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
}
}

View File

@ -52,6 +52,9 @@ module VagrantPlugins
end end
def powershell(command, &block) def powershell(command, &block)
# ensure an exit code
command << "\r\n"
command << "if ($LASTEXITCODE) { exit $LASTEXITCODE } else { exit 0 }"
execute_shell(command, :powershell, &block) execute_shell(command, :powershell, &block)
end end