lib/hako/schedulers/ecs.rb in hako-0.7.4 vs lib/hako/schedulers/ecs.rb in hako-0.8.0

- old
+ new

@@ -40,11 +40,11 @@ Hako.logger.info "Task definition isn't changed" task_definition = @ecs.describe_task_definition(task_definition: @app_id).task_definition else Hako.logger.info "Registered task definition: #{task_definition.task_definition_arn}" upload_front_config(@app_id, front, app.port) - Hako.logger.info "Uploaded front configuration to s3://#{front.s3.bucket}/#{front.s3.key(@app_id)}" + Hako.logger.debug "Uploaded front configuration to s3://#{front.s3.bucket}/#{front.s3.key(@app_id)}" end service = create_or_update_service(task_definition.task_definition_arn, front_port) if service == :noop Hako.logger.info "Service isn't changed" else @@ -53,14 +53,23 @@ end Hako.logger.info 'Deployment completed' end end - def oneshot(app, commands) - task_definition = register_task_definition_for_oneshot(app) - Hako.logger.info "Registered task definition: #{task_definition.task_definition_arn}" - task = run_task(task_definition, app.env, commands) + def oneshot(containers, commands) + definitions = create_definitions(containers, -1) + definitions.each do |definition| + definition.delete(:essential) + end + task_definition = register_task_definition_for_oneshot(definitions) + if task_definition == :noop + Hako.logger.info "Task definition isn't changed" + task_definition = @ecs.describe_task_definition(task_definition: "#{@app_id}-oneshot").task_definition + else + Hako.logger.info "Registered task definition: #{task_definition.task_definition_arn}" + end + task = run_task(task_definition, commands) Hako.logger.info "Started task: #{task.task_arn}" exit_code = wait_for_task(task) Hako.logger.info 'Oneshot task finished' exit_code end @@ -150,29 +159,33 @@ end service = describe_service if service find_front_port(service) else - max_port = -1 - @ecs.list_services(cluster: @cluster).each do |page| - unless page.service_arns.empty? - @ecs.describe_services(cluster: @cluster, services: page.service_arns).services.each do |s| - if s.status != 'INACTIVE' - port = find_front_port(s) - if port - max_port = [max_port, port].max - end + new_front_port + end + end + + def new_front_port + max_port = -1 + @ecs.list_services(cluster: @cluster).each do |page| + unless page.service_arns.empty? + @ecs.describe_services(cluster: @cluster, services: page.service_arns).services.each do |s| + if s.status != 'INACTIVE' + port = find_front_port(s) + if port + max_port = [max_port, port].max end end end end - if max_port == -1 - DEFAULT_FRONT_PORT - else - max_port + 1 - end end + if max_port == -1 + DEFAULT_FRONT_PORT + else + max_port + 1 + end end def find_front_port(service) task_definition = @ecs.describe_task_definition(task_definition: service.task_definition).task_definition container_definitions = {} @@ -182,15 +195,15 @@ if container_definitions['front'] container_definitions['front'].port_mappings[0].host_port end end - def task_definition_changed?(definitions) + def task_definition_changed?(family, definitions) if @force return true end - task_definition = @ecs.describe_task_definition(task_definition: @app_id).task_definition + task_definition = @ecs.describe_task_definition(task_definition: family).task_definition container_definitions = {} task_definition.container_definitions.each do |c| container_definitions[c.name] = c end if definitions.any? { |definition| different_definition?(definition, container_definitions.delete(definition[:name])) } @@ -205,11 +218,11 @@ def different_definition?(expected_container, actual_container) EcsDefinitionComparator.new(expected_container).different?(actual_container) end def register_task_definition(definitions) - if task_definition_changed?(definitions) + if task_definition_changed?(@app_id, definitions) @ecs.register_task_definition( family: @app_id, container_definitions: definitions, ).task_definition else @@ -226,25 +239,20 @@ app_container(name, container) end end end - def register_task_definition_for_oneshot(app) - @ecs.register_task_definition( - family: "#{@app_id}-oneshot", - container_definitions: [ - { - name: 'oneshot', - image: app.image_tag, - cpu: app.cpu, - memory: app.memory, - links: [], - port_mappings: [], - environment: [], - }, - ], - ).task_definition + def register_task_definition_for_oneshot(definitions) + family = "#{@app_id}-oneshot" + if task_definition_changed?(family, definitions) + @ecs.register_task_definition( + family: "#{@app_id}-oneshot", + container_definitions: definitions, + ).task_definition + else + :noop + end end def front_container(front, front_port) environment = front.env.map { |k, v| { name: k, value: v } } { @@ -273,21 +281,19 @@ environment: environment, docker_labels: app.docker_labels, } end - def run_task(task_definition, env, commands) - environment = env.map { |k, v| { name: k, value: v } } + def run_task(task_definition, commands) @ecs.run_task( cluster: @cluster, task_definition: task_definition.task_definition_arn, overrides: { container_overrides: [ { - name: 'oneshot', + name: 'app', command: commands, - environment: environment, }, ], }, count: 1, started_by: "hako oneshot #{@app_id}", @@ -309,16 +315,20 @@ if started_at Hako.logger.info "Started at #{started_at}" end end - Hako.logger.info " status #{task.last_status}" + Hako.logger.debug " status #{task.last_status}" if task.last_status == 'STOPPED' Hako.logger.info "Stopped at #{task.stopped_at}" - container = task.containers[0] - Hako.logger.info "Exit code is #{container.exit_code}" - return container.exit_code + containers = {} + task.containers.each do |c| + containers[c.name] = c + end + app = containers.fetch('app') + Hako.logger.info "Exit code is #{app.exit_code}" + return app.exit_code end sleep 1 end end