Sha256: 474b6f28a8440b02c298848cc2f1426423bda501d8a6070e9f3a1c94ee5cc8bd

Contents?: true

Size: 1.9 KB

Versions: 24

Compression:

Stored size: 1.9 KB

Contents

class Ufo::Apps
  class CfnMap
    extend Memoist
    include Ufo::Stack::Helper

    def initialize(options = {})
      @options = options
      @cluster = @options[:cluster] || default_cluster(options[:service])
      @map = {}
    end

    # Example:
    #   {"development-demo-web-Ecs-1L3WUTJFFM5JV"=>"demo-web"}
    def map
      return @map if @populated

      populate_map!
      @populated = true
      @map
    end

    def summaries
      filter = %w[
        UPDATE_COMPLETE
        CREATE_COMPLETE
        UPDATE_ROLLBACK_COMPLETE
        UPDATE_IN_PROGRESS
        UPDATE_COMPLETE_CLEANUP_IN_PROGRESS
        UPDATE_ROLLBACK_IN_PROGRESS
        UPDATE_ROLLBACK_FAILED
        UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS
        REVIEW_IN_PROGRESS
      ]

      summaries = []
      next_token = true
      while next_token
        resp = cloudformation.list_stacks(stack_status_filter: filter)
        summaries += resp.stack_summaries
        next_token = resp.next_token
      end

      # look for stacks that beling that ufo create
      summaries.select do |s|
        s.template_description =~ /Ufo ECS stack/
      end
    end
    memoize :summaries

    def populate_map!
      threads = []
      summaries.each do |summary|
        threads << Thread.new do
          resp = cloudformation.describe_stack_resources(stack_name: summary.stack_name)
          ecs_resource = resp.stack_resources.find do |resource|
            resource.logical_resource_id == "Ecs"
          end
          # Example: "PhysicalResourceId": "arn:aws:ecs:us-east-1:111111111111:service/dev-demo-web-Ecs-1HRL8Y9F4D1CR"
          ecs_service_name = ecs_resource.physical_resource_id.split('/').last
          @map[ecs_service_name] = stack_name_to_service_name(summary.stack_name)
        end
      end
      threads.map(&:join)
    end

    def stack_name_to_service_name(stack_name)
      stack_name.sub("#{@cluster}-",'')
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ufo-5.0.7 lib/ufo/apps/cfn_map.rb
ufo-5.0.6 lib/ufo/apps/cfn_map.rb
ufo-5.0.5 lib/ufo/apps/cfn_map.rb
ufo-5.0.4 lib/ufo/apps/cfn_map.rb
ufo-5.0.3 lib/ufo/apps/cfn_map.rb
ufo-5.0.2 lib/ufo/apps/cfn_map.rb
ufo-5.0.1 lib/ufo/apps/cfn_map.rb
ufo-5.0.0 lib/ufo/apps/cfn_map.rb
ufo-4.6.3 lib/ufo/apps/cfn_map.rb
ufo-4.6.2 lib/ufo/apps/cfn_map.rb
ufo-4.6.1 lib/ufo/apps/cfn_map.rb
ufo-4.6.0 lib/ufo/apps/cfn_map.rb
ufo-4.5.11 lib/ufo/apps/cfn_map.rb
ufo-4.5.10 lib/ufo/apps/cfn_map.rb
ufo-4.5.9 lib/ufo/apps/cfn_map.rb
ufo-4.5.8 lib/ufo/apps/cfn_map.rb
ufo-4.5.7 lib/ufo/apps/cfn_map.rb
ufo-4.5.6 lib/ufo/apps/cfn_map.rb
ufo-4.5.5 lib/ufo/apps/cfn_map.rb
ufo-4.5.4 lib/ufo/apps/cfn_map.rb