Sha256: 5cdb3ce0ea1960e53b603413491abd6766d689feff79cc245b3d30515e595268

Contents?: true

Size: 1.66 KB

Versions: 18

Compression:

Stored size: 1.66 KB

Contents

require 'tty-prompt'

module Sonic
  # ec2 tag related methods
  module Ssh::Ec2Tag
    def ec2_instances
      return @ec2_instances if @ec2_instances

      filters = [{ name: 'tag-value', values: tags_filter }]
      @ec2_instances = ec2_resource.instances(filters: filters)
    end

    # matches any tag value
    def ec2_tag_exists?
      ec2_instances.count > 0
    end

    # If no instances found
    #   Exit immediately with error message
    # If all instances found have the same tag name
    #   Immediately return the first instance id
    # If multiple tag values
    #   Prompt user to select instance tag value of interest
    def find_ec2_instance
      tag_values = ec2_instances.map{ |i| matched_tag_value(i) }.uniq
      case tag_values.size
      when 0
        UI.error("Unable to find an instance with a one of the tag values: #{@identifier}")
        exit 1
      when 1
        ec2_instances.first.instance_id
      else
        # prompt
        select_instance_type(tag_values).instance_id
      end
    end

    def select_instance_type(tag_values)
      UI.say("Found multiple instance types matching the tag filter: #{@identifier}")
      prompt = TTY::Prompt.new
      tag_value = prompt.select("Select an instance type tag:", tag_values)

      # find the first instance with the tag_value
      instance = ec2_instances.find do |i|
        i.tags.find { |t| t.value == tag_value }
      end
    end

    def matched_tag_value(instance)
      tags = instance.tags
      tag = tags.find {|t| tags_filter.include?(t.value) }
      tag.value
    end

    def tags_filter
      @identifier.split(',') # identifier from CLI could be a comma separated list
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
sonic-screwdriver-2.2.7 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.2.6 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.2.5 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.2.4 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.2.3 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.2.2 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.2.1 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.2.0 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.1.1 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.1.0 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-2.0.0 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-1.4.0 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-1.3.2 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-1.3.0 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-1.2.0 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-1.1.1 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-1.1.0 lib/sonic/ssh/ec2_tag.rb
sonic-screwdriver-1.0.0 lib/sonic/ssh/ec2_tag.rb