Class: EcsDeploy::TaskDefinition

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

Constant Summary collapse

DIGEST_SUFFIX =
/@sha256:[0-9a-f]{64}\z/
DIGEST_FORMAT =
/\Asha256:[0-9a-f]{64}\z/

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_definition_name:, region: nil, use_digest: false, docker_buildx_env: nil, **options) ⇒ TaskDefinition

Returns a new instance of TaskDefinition.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ecs_deploy/task_definition.rb', line 26

def initialize(task_definition_name:, region: nil, use_digest: false, docker_buildx_env: nil, **options)
  @task_definition_name = task_definition_name
  @use_digest = use_digest
  @docker_buildx_env = (EcsDeploy.config.docker_buildx_env || {})
    .merge(docker_buildx_env || {})
    .map { |k, v| [k.to_s, v&.to_s] }.to_h
  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 Attribute Details

.digest_cacheObject (readonly)

Returns the value of attribute digest_cache.



13
14
15
# File 'lib/ecs_deploy/task_definition.rb', line 13

def digest_cache
  @digest_cache
end

Class Method Details

.deregister(arn, region: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ecs_deploy/task_definition.rb', line 16

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



58
59
60
61
62
63
64
65
66
# File 'lib/ecs_deploy/task_definition.rb', line 58

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

#registerObject



68
69
70
71
72
73
74
# File 'lib/ecs_deploy/task_definition.rb', line 68

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