Class: Vagrant::Smartos::Zones::Util::GlobalZone::Connection

Inherits:
Struct
  • Object
show all
Includes:
Util::Retryable
Defined in:
lib/vagrant/smartos/zones/util/global_zone/connection.rb

Defined Under Namespace

Classes: ErrorHandler, SSHLogger

Constant Summary collapse

CONNECTION_TIMEOUT =
60
RETRYABLE_EXCEPTIONS =

These are the exceptions that we retry because they represent errors that are generally fixed from a retry and don’t necessarily represent immediate failure cases.

[
  Errno::EACCES,
  Errno::EADDRINUSE,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::ENETUNREACH,
  Errno::EHOSTUNREACH,
  Net::SSH::Disconnect,
  Timeout::Error
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger

Returns:

  • (Object)

    the current value of logger



12
13
14
# File 'lib/vagrant/smartos/zones/util/global_zone/connection.rb', line 12

def logger
  @logger
end

#machineObject

Returns the value of attribute machine

Returns:

  • (Object)

    the current value of machine



12
13
14
# File 'lib/vagrant/smartos/zones/util/global_zone/connection.rb', line 12

def machine
  @machine
end

Instance Method Details

#connect(**opts) ⇒ Object

rubocop:disable Metrics/MethodLength



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/vagrant/smartos/zones/util/global_zone/connection.rb', line 84

def connect(**opts)
  return @connection if connection_valid?

  validate_ssh_info!
  check_ssh_key_permissions

  # Default some options
  opts[:retries] = 5 unless opts.key?(:retries)

  # Connect to SSH, giving it a few tries
  connection = nil
  begin
    logger.info('Attempting SSH connection...')
    connection = retryable(tries: opts[:retries], on: RETRYABLE_EXCEPTIONS) do
      Timeout.timeout(CONNECTION_TIMEOUT) do
        begin
          ssh_logger = SSHLogger.new

          # Setup logging for connections
          connect_opts = common_connect_opts.dup
          connect_opts[:logger] = ssh_logger.logger

          if ssh_info[:proxy_command]
            connect_opts[:proxy] = Net::SSH::Proxy::Command.new(ssh_info[:proxy_command])
          end

          logger.info('Attempting to connect to SSH...')
          logger.info("  - Host: #{ssh_info[:host]}")
          logger.info("  - Port: #{ssh_info[:port]}")
          logger.info("  - Username: #{ssh_info[:username]}")
          logger.info("  - Password? #{uses_password?}")
          logger.info("  - Key Path: #{ssh_info[:private_key_path]}")

          Net::SSH.start(ssh_info[:host], ssh_info[:username], connect_opts)
        ensure
          # Make sure we output the connection log
          logger.debug('== Net-SSH connection debug-level log START ==')
          logger.debug(ssh_logger.to_s)
          logger.debug('== Net-SSH connection debug-level log END ==')
        end
      end
    end
  rescue StandardError => e
    ErrorHandler.new(e).handle!
  end

  @connection = connection
end

#ssh_infoObject



133
134
135
# File 'lib/vagrant/smartos/zones/util/global_zone/connection.rb', line 133

def ssh_info
  @ssh_info ||= Util::GlobalZone::SSHInfo.new(machine.provider, machine.config, machine.env).to_hash
end

#with_connection {|@connection| ... } ⇒ Object

Yields:

  • (@connection)


78
79
80
81
# File 'lib/vagrant/smartos/zones/util/global_zone/connection.rb', line 78

def with_connection
  connect
  yield @connection if block_given?
end