Class: Ufo::Apps::CfnMap

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Stack::Helper
Defined in:
lib/ufo/apps/cfn_map.rb

Instance Method Summary collapse

Methods included from Stack::Helper

#adjust_stack_name, #find_stack, #status

Methods included from Settings

#cfn, #network, #settings

Methods included from Util

#default_cluster, #display_params, #execute, #pretty_time, #settings, #task_definition_arns, #user_params

Methods included from Ufo::AwsService

#cloudformation, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb

Constructor Details

#initialize(options = {}) ⇒ CfnMap

Returns a new instance of CfnMap.



6
7
8
9
10
# File 'lib/ufo/apps/cfn_map.rb', line 6

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

Instance Method Details

#mapObject

Example:

{"development-demo-web-Ecs-1L3WUTJFFM5JV"=>"demo-web"}


14
15
16
17
18
19
20
# File 'lib/ufo/apps/cfn_map.rb', line 14

def map
  return @map if @populated

  populate_map!
  @populated = true
  @map
end

#populate_map!Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ufo/apps/cfn_map.rb', line 50

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

#stack_name_to_service_name(stack_name) ⇒ Object



66
67
68
# File 'lib/ufo/apps/cfn_map.rb', line 66

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

#summariesObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ufo/apps/cfn_map.rb', line 22

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