class Sonic::Ssh::IdentifierDetector

Public Class Methods

new(cluster, service, identifier, options) click to toggle source
# File lib/sonic/ssh/identifier_detector.rb, line 10
def initialize(cluster, service, identifier, options)
  @cluster = cluster
  @service = service
  @identifier = identifier
  @options = options
end

Public Instance Methods

container_instance_or_task_arn?() click to toggle source

Examples:

Container instance ids: b748e59a-b679-42a7-b713-afb12294935b 9f1dadc7-4f67-41da-abec-ec08810bfbc9

Task ids: 222c9e66-780b-4755-8c16-8670988e8011 6358f9c2-b231-4f5b-a59b-15bf19d52a15

Container instance and task ids have the same format

# File lib/sonic/ssh/identifier_detector.rb, line 94
def container_instance_or_task_arn?
  @identifier =~ /.{8}-.{4}-.{4}-.{4}-.{12}/
end
detect!() click to toggle source

Returns exactly 1 instance_id or exits the program. The bang check_* methods can exit early.

# File lib/sonic/ssh/identifier_detector.rb, line 19
def detect!
  @instance_id ||= case detected_type
    when :ecs_container_instance_or_task_arn
      check_cluster_exists! unless @options[:noop]

      find_container_instance(@identifier) ||
      find_task(@identifier)
    when :ecs_service
      check_cluster_exists! unless @options[:noop]
      check_service_exists!
      check_tasks_running!

      find_container_instance(first_task.container_instance_arn)
    when :ec2_tag
      find_ec2_instance
    when :ec2_instance
      @identifier
    end
end
detected_type() click to toggle source

Any of these methods either returns exactly 1 instance_id or exit the program.

Returns detected_type

# File lib/sonic/ssh/identifier_detector.rb, line 46
def detected_type
  # Note order here matters because some checks are slower in performance
  @detected_type ||= if instance_id? # fast
    :ec2_instance
  elsif container_instance_or_task_arn? # fast
    :ecs_container_instance_or_task_arn
  elsif ec2_tag_exists? # slow
    :ec2_tag
  else
    :ecs_service # will be slower with the logic from the calling method
  end
end
find_container_instance(container_instance_arn) click to toggle source

takes argument

# File lib/sonic/ssh/identifier_detector.rb, line 60
def find_container_instance(container_instance_arn)
  response = ecs.describe_container_instances(
                cluster: @cluster,
                container_instances: [container_instance_arn])

  container_instance = response.container_instances.first
  return false unless container_instance

  ec2_instance_id = container_instance.ec2_instance_id
end
find_task(task_arn) click to toggle source
# File lib/sonic/ssh/identifier_detector.rb, line 71
def find_task(task_arn)
  response = ecs.describe_tasks(cluster: @cluster, tasks: [task_arn])

  task = response.tasks.first
  unless task
    puts "Unable to find a #{task_arn.color(:green)} container instance or task in the #{@cluster.color(:green)} cluster."
    exit 1
  end

  find_container_instance(first_task.container_instance_arn)
end
instance_id() click to toggle source
# File lib/sonic/ssh/identifier_detector.rb, line 39
def instance_id
  detect!
end
instance_id?() click to toggle source

Examples: i-006a097bb10643e20

# File lib/sonic/ssh/identifier_detector.rb, line 100
def instance_id?
  @identifier =~ /i-.{17}/
end