lib/hako/schedulers/ecs.rb in hako-2.11.1 vs lib/hako/schedulers/ecs.rb in hako-2.12.0

- old
+ new

@@ -196,34 +196,35 @@ # @param [Hash<String, Container>] containers # @param [Array<String>] commands # @param [Hash<String, String>] env # @param [Boolean] no_wait + # @param [Hako::CLI::Oneshot::Overrides, nil] overrides # @return [Integer] Returns exit code - def oneshot(containers, commands, env, no_wait: false) + def oneshot(containers, commands, env, no_wait: false, overrides: nil) definitions = create_definitions(containers) if @dry_run volumes_definition.each do |d| print_volume_definition_in_cli_format(d) end definitions.each do |d| if d[:name] == 'app' d[:command] = commands end - print_definition_in_cli_format(d, additional_env: env) + print_definition_in_cli_format(d, additional_env: env, overrides: overrides) check_secrets(d) end 0 else updated, task_definition = register_task_definition_for_oneshot(definitions) if updated Hako.logger.info "Registered task definition: #{task_definition.task_definition_arn}" else Hako.logger.info "Task definition isn't changed: #{task_definition.task_definition_arn}" end - @task = run_task(task_definition, commands, env) + @task = run_task(task_definition, commands, env, overrides) Hako.logger.info "Started task: #{@task.task_arn}" @scripts.each { |script| script.oneshot_started(self) } if no_wait info = { cluster: @cluster, task_arn: @task.task_arn } puts JSON.dump(info) @@ -656,25 +657,17 @@ end # @param [Aws::ECS::Types::TaskDefinition] task_definition # @param [Array<String>] commands # @param [Hash<String, String>] env + # @param [Hako::CLI::Oneshot::Overrides] overrides # @return [Aws::ECS::Types::Task] - def run_task(task_definition, commands, env) - environment = env.map { |k, v| { name: k, value: v } } + def run_task(task_definition, commands, env, overrides) result = ecs_client.run_task( cluster: @cluster, task_definition: task_definition.task_definition_arn, - overrides: { - container_overrides: [ - { - name: 'app', - command: commands, - environment: environment, - }, - ], - }, + overrides: overrides_option(commands, env, overrides), count: 1, placement_constraints: @placement_constraints, started_by: 'hako oneshot', launch_type: @launch_type, platform_version: @platform_version, @@ -700,10 +693,29 @@ else raise e end end + # @param [Array<String>] commands + # @param [Hash<String, String>] env + # @param [Hako::CLI::Oneshot::Overrides, nil] overrides + # @doc https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_ContainerOverride.html + def overrides_option(commands, env, overrides) + { + container_overrides: [ + { + name: 'app', + cpu: overrides&.app_cpu, + memory: overrides&.app_memory, + memory_reservation: overrides&.app_memory_reservation, + command: commands, + environment: env.map { |k, v| { name: k, value: v } }, + }, + ], + } + end + # @return [Fixnum] def wait_for_oneshot_finish containers = wait_for_task(@task) @task = nil Hako.logger.info 'Oneshot task finished' @@ -1152,21 +1164,37 @@ nil end # @param [Hash] definition # @param [Hash<String, String>] additional_env + # @param [Hako::CLI::Oneshot::Overrides, nil] overrides # @return [nil] - def print_definition_in_cli_format(definition, additional_env: {}) + def print_definition_in_cli_format(definition, additional_env: {}, overrides: nil) cmd = %w[docker run] cmd << '--name' << definition.fetch(:name) - cmd << '--cpu-shares' << definition.fetch(:cpu) - if definition[:memory] - cmd << '--memory' << "#{definition[:memory]}M" + + if overrides && definition.fetch(:name) == 'app' + cpu = overrides.app_cpu || definition.fetch(:cpu) + cmd << '--cpu-shares' << cpu + memory = overrides.app_memory || definition[:memory] + if memory + cmd << '--memory' << "#{memory}M" + end + memory_reservation = overrides.app_memory_reservation || definition[:memory_reservation] + if memory_reservation + cmd << '--memory-reservation' << "#{memory_reservation}M" + end + else + cmd << '--cpu-shares' << definition.fetch(:cpu) + if definition[:memory] + cmd << '--memory' << "#{definition[:memory]}M" + end + if definition[:memory_reservation] + cmd << '--memory-reservation' << "#{definition[:memory_reservation]}M" + end end - if definition[:memory_reservation] - cmd << '--memory-reservation' << "#{definition[:memory_reservation]}M" - end + definition.fetch(:links).each do |link| cmd << '--link' << link end definition.fetch(:port_mappings).each do |port_mapping| cmd << '--publish' << "#{port_mapping.fetch(:host_port)}:#{port_mapping.fetch(:container_port)}" @@ -1225,13 +1253,10 @@ definition[:linux_parameters].fetch(:tmpfs, []).each do |tmpfs| options = ["size=#{tmpfs[:size]}m"].concat(tmpfs[:mount_options]) cmd << '--tmpfs' << "#{tmpfs[:container_path]}:#{options.join(',')}" end end - definition.fetch(:volumes_from).each do |volumes_from| - p volumes_from - end if definition[:user] cmd << '--user' << definition[:user] end if definition[:readonly_root_filesystem] cmd << '--read-only' @@ -1243,9 +1268,31 @@ cmd << '--entrypoint' << definition[:entry_point] end if definition[:system_controls] definition.fetch(:system_controls).each do |system_control| cmd << '--sysctl' << "#{system_control.fetch(:namespace)}=#{system_control.fetch(:value)}" + end + end + if definition[:health_check] + if definition[:health_check][:command] + health_check_command_type = definition[:health_check][:command][0] + case health_check_command_type + when 'NONE' + cmd << '--no-healthcheck' + when 'CMD', 'CMD-SHELL' + health_check_command = definition[:health_check][:command][1..-1].join(' ') + cmd << '--health-cmd' << health_check_command.inspect + else + raise "Health check command type #{health_check_command_type} is not supported. CMD, CMD-SHELL and NONE are supported." + end + end + if definition[:health_check][:retries] + cmd << '--health-retries' << definition[:health_check][:retries] + end + %i[interval timeout start_period].each do |property| + if definition[:health_check][property] + cmd << "--health-#{property}" << "#{definition[:health_check][property]}s" + end end end cmd << "\\\n " definition.fetch(:environment).each do |env|