Module: Stax::Ecs

Defined in:
lib/stax/mixin/ecs.rb,
lib/stax/mixin/ecs/deploy.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



6
7
8
9
# File 'lib/stax/mixin/ecs.rb', line 6

def self.included(thor)
  thor.desc('ecs COMMAND', 'ECS subcommands')
  thor.subcommand(:ecs, Cmd::Ecs)
end

Instance Method Details

#ecs_cluster_nameObject



15
16
17
# File 'lib/stax/mixin/ecs.rb', line 15

def ecs_cluster_name
  @_ecs_cluster_name ||= (ecs_clusters&.first&.physical_resource_id || 'default')
end

#ecs_clustersObject



11
12
13
# File 'lib/stax/mixin/ecs.rb', line 11

def ecs_clusters
  @_ecs_clusters ||= Aws::Cfn.resources_by_type(stack_name, 'AWS::ECS::Cluster')
end

#ecs_deploy(id) {|hash| ... } ⇒ Object

update taskdef for a service, triggering a deploy modify current taskdef in block

Yields:

  • (hash)


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stax/mixin/ecs/deploy.rb', line 36

def ecs_deploy(id, &block)
  service = Aws::Ecs.services(ecs_cluster_name, [resource(id)]).first
  taskdef = get_taskdef(service)

  ## convert to a hash and modify in block
  hash = taskdef_to_hash(taskdef)
  yield(hash) if block_given?

  taskdef = register_taskdef(hash)
  update_service(service, taskdef)
end

#ecs_service_namesObject



45
46
47
# File 'lib/stax/mixin/ecs.rb', line 45

def ecs_service_names
  @_ecs_service_names ||= ecs_services.map(&:physical_resource_id)
end

#ecs_service_objectsObject



49
50
51
# File 'lib/stax/mixin/ecs.rb', line 49

def ecs_service_objects
  Aws::Ecs.services(ecs_cluster_name, ecs_service_names)
end

#ecs_servicesObject



19
20
21
# File 'lib/stax/mixin/ecs.rb', line 19

def ecs_services
  @_ecs_services ||= Aws::Cfn.resources_by_type(stack_name, 'AWS::ECS::Service')
end

#ecs_services_with_ids(*ids) ⇒ Object

get services with a list of logical ids



28
29
30
31
32
33
34
35
36
# File 'lib/stax/mixin/ecs.rb', line 28

def ecs_services_with_ids(*ids)
  if ids.empty?
    ecs_services
  else
    ecs_services.select do |s|
      ids.include?(s.logical_resource_id)
    end
  end
end

#ecs_task_definitionsObject



23
24
25
# File 'lib/stax/mixin/ecs.rb', line 23

def ecs_task_definitions
  @_ecs_task_definitions ||= Aws::Cfn.resources_by_type(stack_name, 'AWS::ECS::TaskDefinition')
end

#ecs_task_familiesObject

mangle taskdef arn into family name



39
40
41
42
43
# File 'lib/stax/mixin/ecs.rb', line 39

def ecs_task_families
  ecs_task_definitions.map do |r|
    r.physical_resource_id.split(':')[-2].split('/').last
  end
end

#ecs_update_service(id, taskdef) ⇒ Object

deprecated: update service to use a new task definition



64
65
66
67
68
69
70
71
# File 'lib/stax/mixin/ecs.rb', line 64

def ecs_update_service(id, taskdef)
  service_name = resource(id).split('/').last
  taskdef_name = taskdef.task_definition_arn.split('/').last
  debug("Updating #{service_name} to #{taskdef_name}")
  Aws::Ecs.update_service(service: service_name, task_definition: taskdef_name).tap do |s|
    puts s.task_definition
  end
end

#ecs_update_taskdef(id) ⇒ Object

deprecated: register a new revision of existing task definition



54
55
56
57
58
59
60
61
# File 'lib/stax/mixin/ecs.rb', line 54

def ecs_update_taskdef(id)
  taskdef = Aws::Ecs.task_definition(resource(id))
  debug("Registering new revision of #{taskdef.family}")
  args = %i[family cpu memory requires_compatibilities task_role_arn execution_role_arn network_mode container_definitions volumes placement_constraints]
  Aws::Ecs.client.register_task_definition(taskdef.to_hash.slice(*args)).task_definition.tap do |t|
    puts t.task_definition_arn
  end
end

#get_taskdef(service) ⇒ Object



10
11
12
13
14
15
# File 'lib/stax/mixin/ecs/deploy.rb', line 10

def get_taskdef(service)
  debug("Current task definition for #{service.service_name}")
  Aws::Ecs.task_definition(service.task_definition).tap do |t|
    puts t.task_definition_arn
  end
end

#register_taskdef(hash) ⇒ Object



17
18
19
20
21
22
# File 'lib/stax/mixin/ecs/deploy.rb', line 17

def register_taskdef(hash)
  debug("Registering new revision")
  Aws::Ecs.client.register_task_definition(hash).task_definition.tap do |t|
    puts t.task_definition_arn
  end
end

#taskdef_to_hash(taskdef) ⇒ Object

convert to hash for registering new taskdef



5
6
7
8
# File 'lib/stax/mixin/ecs/deploy.rb', line 5

def taskdef_to_hash(taskdef)
  args = %i[family cpu memory requires_compatibilities task_role_arn execution_role_arn network_mode container_definitions volumes placement_constraints]
  taskdef.to_hash.slice(*args)
end

#update_service(service, taskdef) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/stax/mixin/ecs/deploy.rb', line 24

def update_service(service, taskdef)
  debug("Updating #{service.service_name} to new revision")
  Aws::Ecs.update_service(
    service:         service.service_name,
    task_definition: taskdef.task_definition_arn
  ).tap do |s|
    puts s.deployments.first.id
  end
end