2014-03-11 05:38:31 +00:00
|
|
|
require "timeout"
|
|
|
|
|
|
|
|
require "log4r"
|
|
|
|
|
|
|
|
require "vagrant/util/retryable"
|
2014-04-05 18:26:31 +00:00
|
|
|
require "vagrant/util/silence_warnings"
|
|
|
|
|
|
|
|
Vagrant::Util::SilenceWarnings.silence! do
|
|
|
|
require "winrm"
|
|
|
|
end
|
2014-03-11 05:38:31 +00:00
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
require "winrm-elevated"
|
2015-06-05 07:11:06 +00:00
|
|
|
require "winrm-fs"
|
2014-04-22 21:42:13 +00:00
|
|
|
|
2014-03-11 05:38:31 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module CommunicatorWinRM
|
|
|
|
class WinRMShell
|
|
|
|
include Vagrant::Util::Retryable
|
|
|
|
|
2018-12-07 20:11:17 +00:00
|
|
|
# Exit code generated when user is invalid. Can occur
|
|
|
|
# after a hostname update
|
|
|
|
INVALID_USERID_EXITCODE = -196608
|
|
|
|
|
2014-03-11 05:38:31 +00:00
|
|
|
# These are the exceptions that we retry because they represent
|
|
|
|
# errors that are generally fixed from a retry and don't
|
|
|
|
# necessarily represent immediate failure cases.
|
|
|
|
@@exceptions_to_retry_on = [
|
|
|
|
HTTPClient::KeepAliveDisconnected,
|
|
|
|
WinRM::WinRMHTTPTransportError,
|
2015-01-28 15:47:03 +00:00
|
|
|
WinRM::WinRMAuthorizationError,
|
2015-08-21 20:37:10 +00:00
|
|
|
WinRM::WinRMWSManFault,
|
2014-03-11 05:38:31 +00:00
|
|
|
Errno::EACCES,
|
|
|
|
Errno::EADDRINUSE,
|
|
|
|
Errno::ECONNREFUSED,
|
|
|
|
Errno::ECONNRESET,
|
|
|
|
Errno::ENETUNREACH,
|
|
|
|
Errno::EHOSTUNREACH,
|
|
|
|
Timeout::Error
|
|
|
|
]
|
|
|
|
|
|
|
|
attr_reader :logger
|
|
|
|
attr_reader :host
|
|
|
|
attr_reader :port
|
2014-07-25 21:00:10 +00:00
|
|
|
attr_reader :username
|
|
|
|
attr_reader :password
|
2015-08-30 17:09:25 +00:00
|
|
|
attr_reader :execution_time_limit
|
2014-07-22 23:30:29 +00:00
|
|
|
attr_reader :config
|
2014-03-11 05:38:31 +00:00
|
|
|
|
2014-07-22 23:30:29 +00:00
|
|
|
def initialize(host, port, config)
|
2014-03-11 05:38:31 +00:00
|
|
|
@logger = Log4r::Logger.new("vagrant::communication::winrmshell")
|
|
|
|
@logger.debug("initializing WinRMShell")
|
|
|
|
|
2014-07-22 22:28:50 +00:00
|
|
|
@host = host
|
2014-07-22 23:30:29 +00:00
|
|
|
@port = port
|
2014-07-25 21:00:10 +00:00
|
|
|
@username = config.username
|
|
|
|
@password = config.password
|
2015-08-30 17:09:25 +00:00
|
|
|
@execution_time_limit = config.execution_time_limit
|
2014-07-22 23:30:29 +00:00
|
|
|
@config = config
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
def powershell(command, opts = {}, &block)
|
|
|
|
connection.shell(:powershell) do |shell|
|
|
|
|
execute_with_rescue(shell, command, &block)
|
2016-08-13 14:49:08 +00:00
|
|
|
end
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
def cmd(command, opts = {}, &block)
|
|
|
|
shell_opts = {}
|
|
|
|
shell_opts[:codepage] = @config.codepage if @config.codepage
|
|
|
|
connection.shell(:cmd, shell_opts) do |shell|
|
|
|
|
execute_with_rescue(shell, command, &block)
|
2016-08-13 14:49:08 +00:00
|
|
|
end
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
def elevated(command, opts = {}, &block)
|
|
|
|
connection.shell(:elevated) do |shell|
|
|
|
|
shell.interactive_logon = opts[:interactive] || false
|
2018-12-07 20:11:17 +00:00
|
|
|
result = execute_with_rescue(shell, command, &block)
|
|
|
|
if result.exitcode == INVALID_USERID_EXITCODE && result.stderr.include?(":UserId:")
|
|
|
|
uname = shell.username
|
|
|
|
ename = elevated_username
|
|
|
|
if uname != ename
|
|
|
|
@logger.warn("elevated command failed due to username error")
|
|
|
|
@logger.warn("retrying command using machine prefixed username - #{ename}")
|
|
|
|
begin
|
|
|
|
shell.username = ename
|
|
|
|
result = execute_with_rescue(shell, command, &block)
|
|
|
|
ensure
|
|
|
|
shell.username = uname
|
|
|
|
end
|
|
|
|
end
|
2018-11-08 22:21:20 +00:00
|
|
|
end
|
2018-12-07 20:11:17 +00:00
|
|
|
result
|
2016-12-11 08:52:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def wql(query, opts = {}, &block)
|
2016-01-25 07:33:16 +00:00
|
|
|
retryable(tries: @config.max_tries, on: @@exceptions_to_retry_on, sleep: @config.retry_delay) do
|
2016-12-11 08:52:00 +00:00
|
|
|
connection.run_wql(query)
|
2016-01-25 07:33:16 +00:00
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
raise_winrm_exception(e, "run_wql", query)
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
2017-08-30 22:48:46 +00:00
|
|
|
# @param from [Array<String>, String] a single path or folder, or an
|
|
|
|
# array of paths and folders to upload to the guest
|
|
|
|
# @param to [String] a path or folder on the guest to upload to
|
|
|
|
# @return [FixNum] Total size transfered from host to guest
|
2014-03-11 05:38:31 +00:00
|
|
|
def upload(from, to)
|
2016-12-11 08:52:00 +00:00
|
|
|
file_manager = WinRM::FS::FileManager.new(connection)
|
2019-02-25 22:11:15 +00:00
|
|
|
if from.is_a?(String) && File.directory?(from)
|
|
|
|
if from.end_with?(".")
|
|
|
|
from = from[0, from.length - 1]
|
|
|
|
else
|
|
|
|
to = File.join(to, File.basename(File.expand_path(from)))
|
|
|
|
end
|
|
|
|
end
|
2017-08-30 22:48:46 +00:00
|
|
|
if from.is_a?(Array)
|
|
|
|
# Preserve return FixNum of bytes transfered
|
|
|
|
return_bytes = 0
|
|
|
|
from.each do |file|
|
|
|
|
return_bytes += file_manager.upload(file, to)
|
|
|
|
end
|
|
|
|
return return_bytes
|
|
|
|
else
|
|
|
|
file_manager.upload(from, to)
|
|
|
|
end
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def download(from, to)
|
2016-12-11 08:52:00 +00:00
|
|
|
file_manager = WinRM::FS::FileManager.new(connection)
|
2015-01-20 19:45:57 +00:00
|
|
|
file_manager.download(from, to)
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
def execute_with_rescue(shell, command, &block)
|
|
|
|
handle_output(shell, command, &block)
|
2016-01-25 07:33:16 +00:00
|
|
|
rescue => e
|
2016-12-11 08:52:00 +00:00
|
|
|
raise_winrm_exception(e, shell.class.name.split("::").last, command)
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
def handle_output(shell, command, &block)
|
|
|
|
output = shell.run(command) do |out, err|
|
2016-01-25 07:33:16 +00:00
|
|
|
block.call(:stdout, out) if block_given? && out
|
|
|
|
block.call(:stderr, err) if block_given? && err
|
|
|
|
end
|
2015-01-06 00:52:57 +00:00
|
|
|
|
2016-01-25 07:33:16 +00:00
|
|
|
@logger.debug("Output: #{output.inspect}")
|
|
|
|
|
|
|
|
# Verify that we didn't get a parser error, and if so we should
|
|
|
|
# set the exit code to 1. Parse errors return exit code 0 so we
|
|
|
|
# need to do this.
|
2016-12-11 08:52:00 +00:00
|
|
|
if output.exitcode == 0
|
|
|
|
if output.stderr.include?("ParserError")
|
|
|
|
@logger.warn("Detected ParserError, setting exit code to 1")
|
|
|
|
output.exitcode = 1
|
2015-01-06 00:52:57 +00:00
|
|
|
end
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
2016-01-25 07:33:16 +00:00
|
|
|
|
|
|
|
return output
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
2014-12-09 00:09:11 +00:00
|
|
|
def raise_winrm_exception(exception, shell = nil, command = nil)
|
|
|
|
case exception
|
|
|
|
when WinRM::WinRMAuthorizationError
|
|
|
|
raise Errors::AuthenticationFailed,
|
|
|
|
user: @config.username,
|
|
|
|
password: @config.password,
|
|
|
|
endpoint: endpoint,
|
|
|
|
message: exception.message
|
|
|
|
when WinRM::WinRMHTTPTransportError
|
|
|
|
raise Errors::ExecutionError,
|
|
|
|
shell: shell,
|
|
|
|
command: command,
|
|
|
|
message: exception.message
|
|
|
|
when OpenSSL::SSL::SSLError
|
|
|
|
raise Errors::SSLError, message: exception.message
|
|
|
|
when HTTPClient::TimeoutError
|
|
|
|
raise Errors::ConnectionTimeout, message: exception.message
|
2015-07-15 17:41:10 +00:00
|
|
|
when Errno::ETIMEDOUT
|
|
|
|
raise Errors::ConnectionTimeout
|
|
|
|
# This is raised if the connection timed out
|
2014-12-09 00:09:11 +00:00
|
|
|
when Errno::ECONNREFUSED
|
|
|
|
# This is raised if we failed to connect the max amount of times
|
|
|
|
raise Errors::ConnectionRefused
|
|
|
|
when Errno::ECONNRESET
|
|
|
|
# This is raised if we failed to connect the max number of times
|
|
|
|
# due to an ECONNRESET.
|
|
|
|
raise Errors::ConnectionReset
|
|
|
|
when Errno::EHOSTDOWN
|
|
|
|
# This is raised if we get an ICMP DestinationUnknown error.
|
|
|
|
raise Errors::HostDown
|
|
|
|
when Errno::EHOSTUNREACH
|
|
|
|
# This is raised if we can't work out how to route traffic.
|
|
|
|
raise Errors::NoRoute
|
|
|
|
else
|
2014-12-10 20:15:07 +00:00
|
|
|
raise Errors::ExecutionError,
|
|
|
|
shell: shell,
|
|
|
|
command: command,
|
|
|
|
message: exception.message
|
2014-12-09 00:09:11 +00:00
|
|
|
end
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
def new_connection
|
2014-03-11 05:38:31 +00:00
|
|
|
@logger.info("Attempting to connect to WinRM...")
|
|
|
|
@logger.info(" - Host: #{@host}")
|
|
|
|
@logger.info(" - Port: #{@port}")
|
2014-07-22 23:30:29 +00:00
|
|
|
@logger.info(" - Username: #{@config.username}")
|
|
|
|
@logger.info(" - Transport: #{@config.transport}")
|
2014-03-11 05:38:31 +00:00
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
client = ::WinRM::Connection.new(endpoint_options)
|
2016-01-24 21:14:15 +00:00
|
|
|
client.logger = @logger
|
2014-03-11 05:38:31 +00:00
|
|
|
client
|
|
|
|
end
|
|
|
|
|
2016-12-11 08:52:00 +00:00
|
|
|
def connection
|
|
|
|
@connection ||= new_connection
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint
|
2014-07-23 18:56:49 +00:00
|
|
|
case @config.transport.to_sym
|
2014-07-22 23:58:27 +00:00
|
|
|
when :ssl
|
|
|
|
"https://#{@host}:#{@port}/wsman"
|
2016-01-24 18:38:22 +00:00
|
|
|
when :plaintext, :negotiate
|
2014-07-22 23:58:27 +00:00
|
|
|
"http://#{@host}:#{@port}/wsman"
|
|
|
|
else
|
|
|
|
raise Errors::WinRMInvalidTransport, transport: @config.transport
|
|
|
|
end
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint_options
|
2016-12-11 08:52:00 +00:00
|
|
|
{ endpoint: endpoint,
|
|
|
|
transport: @config.transport,
|
|
|
|
operation_timeout: @config.timeout,
|
|
|
|
user: @username,
|
|
|
|
password: @password,
|
2014-05-22 16:35:12 +00:00
|
|
|
host: @host,
|
|
|
|
port: @port,
|
2016-01-24 18:38:22 +00:00
|
|
|
basic_auth_only: @config.basic_auth_only,
|
2016-01-25 07:33:16 +00:00
|
|
|
no_ssl_peer_verification: !@config.ssl_peer_verification,
|
|
|
|
retry_delay: @config.retry_delay,
|
|
|
|
retry_limit: @config.max_tries }
|
2014-03-11 05:38:31 +00:00
|
|
|
end
|
2018-11-08 22:21:20 +00:00
|
|
|
|
|
|
|
def elevated_username
|
|
|
|
if username.include?("\\")
|
2018-12-07 20:11:17 +00:00
|
|
|
return username
|
2018-11-08 22:21:20 +00:00
|
|
|
end
|
|
|
|
computername = ""
|
|
|
|
powershell("Write-Output $env:computername") do |type, data|
|
|
|
|
computername << data if type == :stdout
|
|
|
|
end
|
|
|
|
computername.strip!
|
|
|
|
if computername.empty?
|
2018-12-07 20:11:17 +00:00
|
|
|
return username
|
2018-11-08 22:21:20 +00:00
|
|
|
end
|
2018-12-07 20:11:17 +00:00
|
|
|
"#{computername}\\#{username}"
|
2018-11-08 22:21:20 +00:00
|
|
|
end
|
2014-03-11 05:38:31 +00:00
|
|
|
end #WinShell class
|
|
|
|
end
|
|
|
|
end
|