Class: Train::Transports::WinRM::Connection

Inherits:
BaseConnection
  • Object
show all
Defined in:
lib/train/transports/winrm_connection.rb

Overview

A Connection instance can be generated and re-generated, given new connection details such as connection port, hostname, credentials, etc. This object is responsible for carrying out the actions on the remote host such as executing commands, transferring files, etc.

Author:

Defined Under Namespace

Classes: OS

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



31
32
33
34
35
36
37
38
39
# File 'lib/train/transports/winrm_connection.rb', line 31

def initialize(options)
  super(options)
  @endpoint               = @options.delete(:endpoint)
  @rdp_port               = @options.delete(:rdp_port)
  @winrm_transport        = @options.delete(:winrm_transport)
  @connection_retries     = @options.delete(:connection_retries)
  @connection_retry_sleep = @options.delete(:connection_retry_sleep)
  @max_wait_until_ready   = @options.delete(:max_wait_until_ready)
end

Instance Method Details

#closeObject



42
43
44
45
46
47
# File 'lib/train/transports/winrm_connection.rb', line 42

def close
  return if @session.nil?
  session.close
ensure
  @session = nil
end

#file(path) ⇒ Object



53
54
55
# File 'lib/train/transports/winrm_connection.rb', line 53

def file(path)
  @files[path] ||= WindowsFile.new(self, path)
end

#login_commandObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/train/transports/winrm_connection.rb', line 70

def 
  case RbConfig::CONFIG['host_os']
  when /darwin/
    
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    
  when /linux/
    
  else
    fail ActionFailed,
         "Remote login not supported in #{self.class} " \
         "from host OS '#{RbConfig::CONFIG['host_os']}'."
  end
end

#osObject



49
50
51
# File 'lib/train/transports/winrm_connection.rb', line 49

def os
  @os ||= OS.new(self)
end

#run_command(command) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/train/transports/winrm_connection.rb', line 57

def run_command(command)
  return if command.nil?
  logger.debug("[WinRM] #{self} (#{command})")
  out = ''

  response = session.run_powershell_script(command) do |stdout, _|
    out << stdout if stdout
  end

  CommandResult.new(out, response.stderr, response[:exitcode])
end

#upload(locals, remote) ⇒ Object



86
87
88
# File 'lib/train/transports/winrm_connection.rb', line 86

def upload(locals, remote)
  file_transporter.upload(locals, remote)
end

#uriObject



100
101
102
# File 'lib/train/transports/winrm_connection.rb', line 100

def uri
  "winrm://#{options[:user]}@#{@endpoint}:#{@rdp_port}"
end

#wait_until_readyObject



91
92
93
94
95
96
97
98
# File 'lib/train/transports/winrm_connection.rb', line 91

def wait_until_ready
  delay = 3
  session(
    retry_limit: @max_wait_until_ready / delay,
    retry_delay: delay,
  )
  execute(PING_COMMAND.dup)
end