Class: Remotus::WinrmConnection
- Inherits:
-
Object
- Object
- Remotus::WinrmConnection
- Extended by:
- Forwardable
- Defined in:
- lib/remotus/winrm_connection.rb
Overview
Class representing a WinRM connection to a host
Constant Summary collapse
- REMOTE_PORT =
Standard WinRM remote port
5985
Instance Attribute Summary collapse
-
#host ⇒ String
readonly
Host hostname.
-
#host_pool ⇒ Remotus::HostPool
readonly
Host_pool associated host pool.
-
#port ⇒ Integer
readonly
Remote port.
-
#shell ⇒ String
readonly
Shell type.
Instance Method Summary collapse
-
#base_connection(reload: false) ⇒ WinRM::Connection
Retrieves/creates the base WinRM connection for the host If the base connection already exists, the existing connection will be retrieved.
-
#close ⇒ Object
Closes the current WinRM connection if it is active.
-
#connection(shell = :powershell) ⇒ WinRM::Shells::Powershell, WinRM::Shells::Elevated
Retrieves/creates the WinRM shell connection for the host.
-
#download(remote_path, local_path, _options = {}) ⇒ String
Downloads a file from the remote host to the local host.
-
#file_exist?(remote_path, **_options) ⇒ Boolean
Checks if a remote file or directory exists.
-
#initialize(host, port = REMOTE_PORT, host_pool: nil) ⇒ WinrmConnection
constructor
Creates a WinrmConnection.
-
#port_open? ⇒ Boolean
Whether the remote host's WinRM port is available.
-
#run(command, *args, **options) ⇒ Remotus::Result
Runs a command on the host.
-
#run_script(local_path, remote_path, *args, **options) ⇒ Remotus::Result
Uploads a script and runs it on the host.
-
#type ⇒ Symbol
Connection type.
-
#upload(local_path, remote_path, _options = {}) ⇒ String
Uploads a file from the local host to the remote host.
Constructor Details
#initialize(host, port = REMOTE_PORT, host_pool: nil) ⇒ WinrmConnection
Creates a WinrmConnection
42 43 44 45 46 47 |
# File 'lib/remotus/winrm_connection.rb', line 42 def initialize(host, port = REMOTE_PORT, host_pool: nil) @host = host @port = port @host_pool = host_pool @shell = :powershell end |
Instance Attribute Details
#host ⇒ String (readonly)
Returns host hostname.
24 25 26 |
# File 'lib/remotus/winrm_connection.rb', line 24 def host @host end |
#host_pool ⇒ Remotus::HostPool (readonly)
Returns host_pool associated host pool.
30 31 32 |
# File 'lib/remotus/winrm_connection.rb', line 30 def host_pool @host_pool end |
#port ⇒ Integer (readonly)
Returns Remote port.
21 22 23 |
# File 'lib/remotus/winrm_connection.rb', line 21 def port @port end |
#shell ⇒ String (readonly)
Returns shell type.
27 28 29 |
# File 'lib/remotus/winrm_connection.rb', line 27 def shell @shell end |
Instance Method Details
#base_connection(reload: false) ⇒ WinRM::Connection
Retrieves/creates the base WinRM connection for the host If the base connection already exists, the existing connection will be retrieved
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/remotus/winrm_connection.rb', line 64 def base_connection(reload: false) return @base_connection if !reload && !restart_base_connection? # Close any active connections close Remotus.logger.debug { "Initializing WinRM connection to #{Remotus::Auth.credential(self).user}@#{@host}:#{@port}" } @base_connection = WinRM::Connection.new( endpoint: "http://#{remote_host}:#{@port}/wsman", transport: :negotiate, user: Remotus::Auth.credential(self).user, password: Remotus::Auth.credential(self).password ) end |
#close ⇒ Object
Closes the current WinRM connection if it is active
97 98 99 100 101 |
# File 'lib/remotus/winrm_connection.rb', line 97 def close @connection&.close @connection = nil @base_connection = nil end |
#connection(shell = :powershell) ⇒ WinRM::Shells::Powershell, WinRM::Shells::Elevated
Retrieves/creates the WinRM shell connection for the host
If the connection already exists, the existing connection will be retrieved
87 88 89 90 91 92 |
# File 'lib/remotus/winrm_connection.rb', line 87 def connection(shell = :powershell) return @connection unless restart_connection?(shell: shell) @shell = shell @connection = base_connection(reload: true).shell(@shell) end |
#download(remote_path, local_path, _options = {}) ⇒ String
Downloads a file from the remote host to the local host
169 170 171 172 173 |
# File 'lib/remotus/winrm_connection.rb', line 169 def download(remote_path, local_path, = {}) Remotus.logger.debug { "Downloading file #{local_path} from #{@host}:#{remote_path}" } WinRM::FS::FileManager.new(base_connection).download(remote_path, local_path) local_path end |
#file_exist?(remote_path, **_options) ⇒ Boolean
Checks if a remote file or directory exists
183 184 185 186 |
# File 'lib/remotus/winrm_connection.rb', line 183 def file_exist?(remote_path, **) Remotus.logger.debug { "Checking if file #{remote_path} exists on #{@host}" } WinRM::FS::FileManager.new(base_connection).exists?(remote_path) end |
#port_open? ⇒ Boolean
Whether the remote host's WinRM port is available
108 109 110 |
# File 'lib/remotus/winrm_connection.rb', line 108 def port_open? Remotus.port_open?(remote_host, @port) end |
#run(command, *args, **options) ⇒ Remotus::Result
Runs a command on the host
122 123 124 125 126 127 128 |
# File 'lib/remotus/winrm_connection.rb', line 122 def run(command, *args, **) command = "#{command}#{args.empty? ? "" : " "}#{args.join(" ")}" run_result = [:shell].nil? ? connection.run(command) : connection([:shell]).run(command) Remotus::Result.new(command, run_result.stdout, run_result.stderr, run_result.output, run_result.exitcode) rescue WinRM::WinRMAuthorizationError => e raise Remotus::AuthenticationError, e.to_s end |
#run_script(local_path, remote_path, *args, **options) ⇒ Remotus::Result
Uploads a script and runs it on the host
140 141 142 143 |
# File 'lib/remotus/winrm_connection.rb', line 140 def run_script(local_path, remote_path, *args, **) upload(local_path, remote_path) run(remote_path, *args, **) end |
#type ⇒ Symbol
Connection type
54 55 56 |
# File 'lib/remotus/winrm_connection.rb', line 54 def type :winrm end |
#upload(local_path, remote_path, _options = {}) ⇒ String
Uploads a file from the local host to the remote host
154 155 156 157 158 |
# File 'lib/remotus/winrm_connection.rb', line 154 def upload(local_path, remote_path, = {}) Remotus.logger.debug { "Uploading file #{local_path} to #{@host}:#{remote_path}" } WinRM::FS::FileManager.new(base_connection).upload(local_path, remote_path) remote_path end |