Class: EcsDeploy::TaskDefinition
- Inherits:
-
Object
- Object
- EcsDeploy::TaskDefinition
- Defined in:
- lib/ecs_deploy/task_definition.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(task_definition_name:, region: nil, network_mode: "bridge", volumes: [], container_definitions: [], placement_constraints: [], task_role_arn: nil) ⇒ TaskDefinition
constructor
A new instance of TaskDefinition.
- #recent_task_definition_arns ⇒ Object
- #register ⇒ Object
- #run(info) ⇒ Object
Constructor Details
#initialize(task_definition_name:, region: nil, network_mode: "bridge", volumes: [], container_definitions: [], placement_constraints: [], task_role_arn: nil) ⇒ 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 32 33 34 35 |
# File 'lib/ecs_deploy/task_definition.rb', line 12 def initialize( task_definition_name:, region: nil, network_mode: "bridge", volumes: [], container_definitions: [], placement_constraints: [], task_role_arn: nil ) @task_definition_name = task_definition_name @task_role_arn = task_role_arn @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 @network_mode = network_mode @placement_constraints = placement_constraints @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
37 38 39 40 41 42 43 44 45 |
# File 'lib/ecs_deploy/task_definition.rb', line 37 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
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ecs_deploy/task_definition.rb', line 47 def register res = @client.register_task_definition({ family: @task_definition_name, network_mode: @network_mode, container_definitions: @container_definitions, volumes: @volumes, placement_constraints: @placement_constraints, task_role_arn: @task_role_arn, }) EcsDeploy.logger.info "register task definition [#{@task_definition_name}] [#{@region}] [#{Paint['OK', :green]}]" res.task_definition end |
#run(info) ⇒ Object
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 87 88 89 90 91 92 93 94 |
# File 'lib/ecs_deploy/task_definition.rb', line 60 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 |