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.



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

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.



49
50
51
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 49

def logger
  @logger
end

Instance Method Details

#command(cmd) ⇒ Object



101
102
103
104
105
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 101

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)


97
98
99
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 97

def connected?
  !@socket.nil?
end

#disconnect!Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 86

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/smart_proxy_remote_execution_ssh/multiplexed_ssh_connection.rb', line 69

def establish!
  @available_auth_methods ||= available_authentication_methods
  method = @available_auth_methods.find do |method|
    pm = try_auth_method(method)
    method.errors = pm.stderr
    if pm.status.zero?
      @available_auth_methods.unshift(method).uniq!
      true
    end
  end
  return method if method

  msg = "Could not establish connection to remote host using any available authentication method, tried #{@available_auth_methods.map(&:name).join(', ')}"
  method_errors = @available_auth_methods.map { |method| "Authentication method '#{method.name}' failed with:\n#{method.errors}" }.join("\n")
  raise "#{msg}\n\n#{method_errors}"
end