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, **options) ⇒ TaskDefinition

Returns a new instance of TaskDefinition.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ecs_deploy/task_definition.rb', line 13

def initialize(task_definition_name:, region: nil, **options)
  @task_definition_name = task_definition_name
  region ||= EcsDeploy.config.default_region
  params ||= EcsDeploy.config.ecs_client_params

  @options = options.dup
  @options[:network_mode] ||= "bridge"
  @options[:volumes] ||= []
  @options[:container_definitions] ||= []
  @options[:placement_constraints] ||= []
  @options[:runtime_platform] ||= {}

  @options[:container_definitions] = @options[: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.dig(:log_configuration, :options)
      cd[:log_configuration][:options] = cd.dig(:log_configuration, :options).map { |k, v| [k.to_s, v] }.to_h
    end
    cd
  end
  @options[:cpu] = @options[:cpu]&.to_s
  @options[:memory] = @options[:memory]&.to_s

  @client = region ? Aws::ECS::Client.new(params.merge(region: region)) : Aws::ECS::Client.new(params)
  @region = @client.config.region
end

Class Method Details

.deregister(arn, region: nil) ⇒ Object



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

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

Instance Method Details

#recent_task_definition_arnsObject



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

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

#registerObject



51
52
53
54
55
56
57
# File 'lib/ecs_deploy/task_definition.rb', line 51

def register
  res = @client.register_task_definition(
    @options.merge(family: @task_definition_name)
  )
  EcsDeploy.logger.info "registered task definition [#{@task_definition_name}] [#{@region}] [#{Paint['OK', :green]}]"
  res.task_definition
end