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.



6
7
8
9
# File 'lib/moonshot/ssh_target_selector.rb', line 6

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

Instance Method Details

#choose!Object



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

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

  asg = if groups.count == 1
          groups.first
        elsif groups.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 == @asg_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 StandardError => e
  raise "Failed to select an instance from the Auto Scaling Group: #{e.message}"
end