Class: Proxy::RemoteExecution::Ssh::Runners::MultiplexedSSHConnection

Inherits:
Object
  • Object
show all
Includes:
CommandLogging
Defined in:
lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandLogging

#log_command, #set_pm_debug_logging

Constructor Details

#initialize(options, logger:) ⇒ MultiplexedSSHConnection

Returns a new instance of MultiplexedSSHConnection.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 48

def initialize(options, logger:)
  @logger = logger

  @id = options.fetch(:id)
  @host = options.fetch(:hostname)
  @script = options.fetch(:script)
  @ssh_user = options.fetch(:ssh_user, 'root')
  @ssh_port = options.fetch(:ssh_port, 22)
  @ssh_password = options.fetch(:secrets, {}).fetch(:ssh_password, nil)
  @key_passphrase = options.fetch(:secrets, {}).fetch(:key_passphrase, nil)
  @host_public_key = options.fetch(:host_public_key, nil)
  @verify_host = options.fetch(:verify_host, nil)
  @client_private_key_file = settings.ssh_identity_key_file

  @local_working_dir = options.fetch(:local_working_dir, settings.local_working_dir)
  @socket_working_dir = options.fetch(:socket_working_dir, settings.socket_working_dir)
  @socket = nil
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



47
48
49
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 47

def logger
  @logger
end

Instance Method Details

#command(cmd) ⇒ Object



93
94
95
96
97
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 93

def command(cmd)
  raise "Cannot build command to run over multiplexed connection without having an established connection" unless connected?

  ['ssh', reuse_ssh_options, cmd].flatten
end

#connected?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 89

def connected?
  !@socket.nil?
end

#disconnect!Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 78

def disconnect!
  return unless connected?

  cmd = command(%w[-O exit])
  log_command(cmd, label: "Closing shared connection")
  pm = Proxy::Dynflow::ProcessManager.new(cmd)
  set_pm_debug_logging(pm)
  pm.run!
  @socket = nil
end

#establish!Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 67

def establish!
  @available_auth_methods ||= available_authentication_methods
  method = @available_auth_methods.find do |method|
    if try_auth_method(method)
      @available_auth_methods.unshift(method).uniq!
      true
    end
  end
  method || raise("Could not establish connection to remote host using any available authentication method, tried #{@available_auth_methods.map(&:name).join(', ')}")
end