lib/hako/schedulers/ecs.rb in hako-0.6.1 vs lib/hako/schedulers/ecs.rb in hako-0.7.0

- old
+ new

@@ -8,43 +8,52 @@ module Schedulers class Ecs < Scheduler DEFAULT_CLUSTER = 'default'.freeze DEFAULT_FRONT_PORT = 10000 - def initialize(app_id, options) + def initialize(app_id, options, force:, dry_run:) @app_id = app_id @cluster = options.fetch('cluster', DEFAULT_CLUSTER) @desired_count = options.fetch('desired_count') { validation_error!('desired_count must be set') } region = options.fetch('region') { validation_error!('region must be set') } @role = options.fetch('role', nil) @ecs = Aws::ECS::Client.new(region: region) @elb = EcsElb.new(app_id, Aws::ElasticLoadBalancing::Client.new(region: region), options.fetch('elb', nil)) @ec2 = Aws::EC2::Client.new(region: region) + @force = force + @dry_run = dry_run end - def deploy(containers, force: false) - @force_mode = force + def deploy(containers) app = containers.fetch('app') front = containers.fetch('front') front_port = determine_front_port - task_definition = register_task_definition(containers, front_port) - if task_definition == :noop - Hako.logger.info "Task definition isn't changed" - task_definition = @ecs.describe_task_definition(task_definition: @app_id).task_definition + definitions = create_definitions(containers, front_port) + + if @dry_run + definitions.each do |d| + Hako.logger.info "Add container #{d}" + end 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)}" + task_definition = register_task_definition(definitions) + if task_definition == :noop + 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)}" + end + service = create_or_update_service(task_definition.task_definition_arn, front_port) + if service == :noop + Hako.logger.info "Service isn't changed" + else + Hako.logger.info "Updated service: #{service.service_arn}" + wait_for_ready(service) + end + Hako.logger.info 'Deployment completed' end - service = create_or_update_service(task_definition.task_definition_arn, front_port) - if service == :noop - Hako.logger.info "Service isn't changed" - else - Hako.logger.info "Updated service: #{service.service_arn}" - wait_for_ready(service) - end - Hako.logger.info 'Deployment completed' end def oneshot(app, commands) task_definition = register_task_definition_for_oneshot(app) Hako.logger.info "Registered task definition: #{task_definition.task_definition_arn}" @@ -170,11 +179,11 @@ container_definitions['front'].port_mappings[0].host_port end end def task_definition_changed?(definitions) - if @force_mode + if @force return true end task_definition = @ecs.describe_task_definition(task_definition: @app_id).task_definition container_definitions = {} task_definition.container_definitions.each do |c| @@ -191,25 +200,28 @@ def different_definition?(expected_container, actual_container) EcsDefinitionComparator.new(expected_container).different?(actual_container) end - def register_task_definition(containers, front_port) - definitions = containers.map do |name, container| - case name - when 'front' - front_container(container, front_port) - else - app_container(name, container) - end - end + def register_task_definition(definitions) if task_definition_changed?(definitions) @ecs.register_task_definition( family: @app_id, container_definitions: definitions, ).task_definition else :noop + end + end + + def create_definitions(containers, front_port) + containers.map do |name, container| + case name + when 'front' + front_container(container, front_port) + else + app_container(name, container) + end end end def register_task_definition_for_oneshot(app) @ecs.register_task_definition(