Module: Bcome::Ssh::DriverConnection

Included in:
Driver
Defined in:
lib/objects/ssh/driver_concerns/connection.rb

Constant Summary collapse

DEFAULT_TIMEOUT_IN_SECONDS =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/objects/ssh/driver_concerns/connection.rb', line 7

def connection
  @connection
end

Instance Method Details

#close_ssh_connectionObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/objects/ssh/driver_concerns/connection.rb', line 22

def close_ssh_connection
  return unless @connection

  begin
    @connection.close unless @connection.closed?
    @connection = nil
  rescue Net::SCP::Error
    @connection = nil
  end
end

#has_open_ssh_con?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/objects/ssh/driver_concerns/connection.rb', line 42

def has_open_ssh_con?
  !@connection.nil? && !@connection.closed?
end

#net_ssh_paramsObject



51
52
53
54
55
56
57
58
# File 'lib/objects/ssh/driver_concerns/connection.rb', line 51

def net_ssh_params
  params = { paranoid: false }
  params[:proxy] = proxy if has_proxy?
  params[:timeout] = timeout_in_seconds
  params[:verbose] = :fatal # All but silent

  params
end

#node_host_or_ipObject



46
47
48
49
# File 'lib/objects/ssh/driver_concerns/connection.rb', line 46

def node_host_or_ip
  return @context_node.internal_ip_address if @context_node.local_network?
  has_proxy? ? @context_node.internal_ip_address : @context_node.public_ip_address
end

#proxyObject

PROXYING –



66
67
68
# File 'lib/objects/ssh/driver_concerns/connection.rb', line 66

def proxy
  connection_wrangler.proxy
end

#ssh_connect!Object

CONNECTION –



11
12
13
14
15
16
17
18
19
20
# File 'lib/objects/ssh/driver_concerns/connection.rb', line 11

def ssh_connect!
  @connection = nil
  begin
    raise ::Bcome::Exception::InvalidProxyConfig, "missing target ip address for #{@context_node.identifier}. Perhaps you meant to configure a proxy?" unless node_host_or_ip
    @connection = ::Net::SSH.start(node_host_or_ip, user, net_ssh_params)
  rescue Net::SSH::AuthenticationFailed, Net::SSH::Proxy::ConnectError, Net::SSH::ConnectionTimeout => e
    raise Bcome::Exception::CouldNotInitiateSshConnection, @context_node.namespace + "\s-\s#{e.message}"
  end
  @connection
end

#ssh_connection(ping = false) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/objects/ssh/driver_concerns/connection.rb', line 33

def ssh_connection(ping = false)
  if ping
    # We do not cache ping results
    ssh_connect!
  else
    has_open_ssh_con? ? @connection : ssh_connect!
  end
end

#timeout_in_secondsObject



60
61
62
# File 'lib/objects/ssh/driver_concerns/connection.rb', line 60

def timeout_in_seconds
  @config[:timeout_in_seconds] ||= DEFAULT_TIMEOUT_IN_SECONDS
end