Class: Moonshot::SSHTargetSelector

Inherits:
Object
  • Object
show all
Defined in:
lib/moonshot/ssh_target_selector.rb

Overview

Choose a publically accessible instance to run commands on, given a Moonshot::Stack.

Instance Method Summary collapse

Constructor Details

#initialize(stack, asg_name: nil) ⇒ SSHTargetSelector

Returns a new instance of SSHTargetSelector.



4
5
6
7
# File 'lib/moonshot/ssh_target_selector.rb', line 4

def initialize(stack, asg_name: nil)
  @asg_name = asg_name
  @stack = stack
end

Instance Method Details

#choose!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/moonshot/ssh_target_selector.rb', line 9

def choose!
  groups = @stack.resources_of_type('AWS::AutoScaling::AutoScalingGroup')

  asg = if groups.count == 1
          groups.first
        elsif asgs.count > 1
          unless @asg_name
            raise 'Multiple Auto Scaling Groups found in the stack. Please specify which '\
                  'one to SSH into using the --auto-scaling-group (-g) option.'
          end
          groups.detect { |x| x.logical_resource_id == @config.ssh_auto_scaling_group_name }
        end
  raise 'Failed to find the Auto Scaling Group.' unless asg

  Aws::AutoScaling::Client.new.describe_auto_scaling_groups(
    auto_scaling_group_names: [asg.physical_resource_id]
  ).auto_scaling_groups.first.instances.map(&:instance_id).first
rescue
  raise 'Failed to find instances in the Auto Scaling Group!'
end