Added WQL support to Vagrant communicator execute

- Removed duplication for WQL specific commands
This commit is contained in:
Shawn Neal 2014-04-22 11:29:22 -07:00
parent 929e41aa5c
commit 9177bd8a48
2 changed files with 16 additions and 30 deletions

View File

@ -57,11 +57,19 @@ module VagrantPlugins
:command => command, :command => command,
:shell => :powershell :shell => :powershell
}.merge(opts || {}) }.merge(opts || {})
exit_status = do_execute(command, opts[:shell], &block)
if opts[:error_check] && exit_status != 0 if opts[:shell] == :powershell
raise_execution_error(opts, exit_status) script = File.expand_path("../scripts/command_alias.ps1", __FILE__)
script = File.read(script)
command = script << "\r\n" << command << "\r\nexit $LASTEXITCODE"
end end
exit_status
output = shell.send(opts[:shell], command, &block)
return output if opts[:shell] == :wql
exitcode = output[:exitcode]
raise_execution_error(opts, exitcode) if opts[:error_check] && exitcode != 0
exitcode
end end
alias_method :sudo, :execute alias_method :sudo, :execute
@ -103,17 +111,6 @@ module VagrantPlugins
) )
end end
def do_execute(command, shell_type, &block)
if shell_type == :cmd
return shell.cmd(command, &block)[:exitcode]
end
script = File.expand_path("../scripts/command_alias.ps1", __FILE__)
script = File.read(script)
command = script << "\r\n" << command << "\r\nexit $LASTEXITCODE"
shell.powershell(command, &block)[:exitcode]
end
def raise_execution_error(opts, exit_code) def raise_execution_error(opts, exit_code)
# The error classes expect the translation key to be _key, but that makes for an ugly # The error classes expect the translation key to be _key, but that makes for an ugly
# configuration parameter, so we set it here from `error_key` # configuration parameter, so we set it here from `error_key`

View File

@ -57,8 +57,8 @@ module VagrantPlugins
execute_shell(command, :cmd, &block) execute_shell(command, :cmd, &block)
end end
def wql(query) def wql(query, &block)
execute_wql(query) execute_shell(query, :wql, &block)
end end
def upload(from, to) def upload(from, to)
@ -92,7 +92,7 @@ module VagrantPlugins
protected protected
def execute_shell(command, shell=:powershell, &block) def execute_shell(command, shell=:powershell, &block)
raise Errors::InvalidShell, shell: shell unless shell == :cmd || shell == :powershell raise Errors::WinRMInvalidShell, :shell => shell unless [:powershell, :cmd, :wql].include?(shell)
begin begin
execute_shell_with_retry(command, shell, &block) execute_shell_with_retry(command, shell, &block)
@ -108,22 +108,11 @@ module VagrantPlugins
block.call(:stdout, out) if block_given? && out block.call(:stdout, out) if block_given? && out
block.call(:stderr, err) if block_given? && err block.call(:stderr, err) if block_given? && err
end end
@logger.debug("Exit status: #{output[:exitcode].inspect}") @logger.debug("Output: #{output.inspect}")
return output return output
end end
end end
def execute_wql(query)
retryable(:tries => @max_tries, :on => @@exceptions_to_retry_on, :sleep => 10) do
@logger.debug("#executing wql: #{query}")
output = session.wql(query)
@logger.debug("wql result: #{output.inspect}")
return output
end
rescue => e
raise_winrm_exception(e, :wql, query)
end
def raise_winrm_exception(winrm_exception, shell, command) def raise_winrm_exception(winrm_exception, shell, command)
# If the error is a 401, we can return a more specific error message # If the error is a 401, we can return a more specific error message
if winrm_exception.message.include?("401") if winrm_exception.message.include?("401")