Class: ElasticBeans::Command::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_beans/command/ssh.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: BastionAuthenticationError, NoInstanceError, TerminatedInstanceError

Constant Summary collapse

USAGE =
"ssh ENVIRONMENT_TYPE"
DESC =
"Connect to an instance for debugging, tunneling through a bastion server"
LONG_DESC =
<<-LONG_DESC
Connect to an instance for debugging, tunneling through a bastion server.
Opens a login shell.

Requires AWS credentials to be set in the environment, i.e. AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
LONG_DESC

Instance Method Summary collapse

Constructor Details

#initialize(application:, bastion_host:, bastion_identity_file:, bastion_username:, identity_file:, index:, queue:, username:, ec2:, elastic_beanstalk:, ui:) ⇒ SSH

Returns a new instance of SSH.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/elastic_beans/command/ssh.rb', line 17

def initialize(
  application:,
  bastion_host:,
  bastion_identity_file:,
  bastion_username:,
  identity_file:,
  index:,
  queue:,
  username:,
  ec2:,
  elastic_beanstalk:,
  ui:
)
  @application = application
  @bastion_host = bastion_host
  @bastion_identity_file = bastion_identity_file
  @bastion_username = bastion_username
  @identity_file = identity_file
  @index = index.to_i
  @queue = queue
  @username = username || "ec2-user"
  @ec2 = ec2
  @elastic_beanstalk = elastic_beanstalk
  @ui = ui
end

Instance Method Details

#run(environment_type) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/elastic_beans/command/ssh.rb', line 43

def run(environment_type)
  environment = ElasticBeans::Environment.new_by_type(
    environment_type,
    queue: queue,
    application: application,
    elastic_beanstalk: elastic_beanstalk,
  )
  instance_id = environment.instance_ids[index]
  ui.debug { "In environment '#{environment.name}' found instance '#{instance_id}' at index '#{index}' of #{environment.instance_ids.inspect}" }
  if instance_id.nil?
    raise NoInstanceError.new(environment: environment, index: index)
  end

  instance_ip = ec2.describe_instances(instance_ids: [instance_id]).reservations[0].instances[0].private_ip_address
  # It's possible a retry would just work, but I'd like to see this happen in reality before I assume that.
  if instance_ip.nil?
    raise TerminatedInstanceError.new(instance_id: instance_id, environment: environment)
  end

  ui.info("Connecting to #{username}@#{instance_ip}...")
  ElasticBeans::SSH.new(
    hostname: instance_ip,
    username: username,
    identity_file: identity_file,
    bastion_host: bastion_host,
    bastion_username: bastion_username,
    bastion_identity_file: bastion_identity_file,
    logger: ui,
  ).connect
rescue ::Aws::EC2::Errors::InvalidInstanceIDMalformed
  raise NoInstanceError.new(environment: environment, index: index)
rescue ElasticBeans::SSH::BastionAuthenticationError => e
  raise BastionAuthenticationError.new(cause: e)
end