Class: EcsDeploy::TaskDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/ecs_deploy/task_definition.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_definition_name:, region: nil, volumes: [], container_definitions: []) ⇒ TaskDefinition

Returns a new instance of TaskDefinition.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ecs_deploy/task_definition.rb', line 12

def initialize(
  task_definition_name:, region: nil,
  volumes: [], container_definitions: []
)
  @task_definition_name = task_definition_name
  @region = region || EcsDeploy.config.default_region || ENV["AWS_DEFAULT_REGION"]

  @container_definitions = container_definitions.map do |cd|
    if cd[:docker_labels]
      cd[:docker_labels] = cd[:docker_labels].map { |k, v| [k.to_s, v] }.to_h
    end
    if cd[:log_configuration] && cd[:log_configuration][:options]
      cd[:log_configuration][:options] = cd[:log_configuration][:options].map { |k, v| [k.to_s, v] }.to_h
    end
    cd
  end
  @volumes = volumes

  @client = Aws::ECS::Client.new(region: @region)
end

Class Method Details

.deregister(arn, region: nil) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/ecs_deploy/task_definition.rb', line 3

def self.deregister(arn, region: nil)
  region = region || EcsDeploy.config.default_region || ENV["AWS_DEFAULT_REGION"]
  client = Aws::ECS::Client.new(region: region)
  client.deregister_task_definition({
    task_definition: arn,
  })
  EcsDeploy.logger.info "deregister task definition [#{arn}] [#{region}] [#{Paint['OK', :green]}]"
end

Instance Method Details

#recent_task_definition_arns ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/ecs_deploy/task_definition.rb', line 33

def recent_task_definition_arns
  resp = @client.list_task_definitions(
    family_prefix: @task_definition_name,
    sort: "DESC"
  )
  resp.task_definition_arns
rescue
  []
end

#register ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/ecs_deploy/task_definition.rb', line 43

def register
  @client.register_task_definition({
    family: @task_definition_name,
    container_definitions: @container_definitions,
    volumes: @volumes,
  })
  EcsDeploy.logger.info "register task definition [#{@task_definition_name}] [#{@region}] [#{Paint['OK', :green]}]"
end

#run(info) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ecs_deploy/task_definition.rb', line 52

def run(info)
  resp = @client.run_task({
    cluster: info[:cluster],
    task_definition: @task_definition_name,
    overrides: {
      container_overrides: info[:container_overrides] || []
    },
    count: info[:count] || 1,
    started_by: "capistrano",
  })
  unless resp.failures.empty?
    resp.failures.each do |f|
      raise "#{f.arn}: #{f.reason}"
    end
  end

  wait_targets = Array(info[:wait_stop])
  unless wait_targets.empty?
    @client.wait_until(:tasks_running, cluster: info[:cluster], tasks: resp.tasks.map { |t| t.task_arn })
    @client.wait_until(:tasks_stopped, cluster: info[:cluster], tasks: resp.tasks.map { |t| t.task_arn })

    resp = @client.describe_tasks(cluster: info[:cluster], tasks: resp.tasks.map { |t| t.task_arn })
    resp.tasks.each do |t|
      t.containers.each do |c|
        next unless wait_targets.include?(c.name)

        unless c.exit_code.zero?
          raise "Task has errors: #{c.reason}"
        end
      end
    end
  end

  EcsDeploy.logger.info "run task [#{@task_definition_name} #{info.inspect}] [#{@region}] [#{Paint['OK', :green]}]"
end