Class: ECSHelper::Command::RunCommand
- Defined in:
- lib/ecs_helper/command/run_command.rb
Constant Summary collapse
- DEFAULT_TIMEOUT =
300
- STEP =
5
Instance Attribute Summary collapse
-
#new_task_definition ⇒ Object
Returns the value of attribute new_task_definition.
-
#repositories ⇒ Object
Returns the value of attribute repositories.
-
#service ⇒ Object
writeonly
Sets the attribute service.
-
#task_definition ⇒ Object
Returns the value of attribute task_definition.
Attributes inherited from Base
#client, #helper, #option_parser, #options, #type
Instance Method Summary collapse
- #cmd_option_parser ⇒ Object
- #custom_task_definition(hash) ⇒ Object
- #required ⇒ Object
- #run ⇒ Object
- #run_task(task_definition_arn) ⇒ Object
- #task(arn) ⇒ Object
- #wait_for_task(task_arn, time = 0) ⇒ Object
Methods inherited from Base
#application, #check_bin, #initialize, #printable?, #project, #validate
Methods included from Logging
Constructor Details
This class inherits a constructor from ECSHelper::Command::Base
Instance Attribute Details
#new_task_definition ⇒ Object
Returns the value of attribute new_task_definition.
4 5 6 |
# File 'lib/ecs_helper/command/run_command.rb', line 4 def new_task_definition @new_task_definition end |
#repositories ⇒ Object
Returns the value of attribute repositories.
4 5 6 |
# File 'lib/ecs_helper/command/run_command.rb', line 4 def repositories @repositories end |
#service=(value) ⇒ Object
Sets the attribute service
4 5 6 |
# File 'lib/ecs_helper/command/run_command.rb', line 4 def service=(value) @service = value end |
#task_definition ⇒ Object
Returns the value of attribute task_definition.
4 5 6 |
# File 'lib/ecs_helper/command/run_command.rb', line 4 def task_definition @task_definition end |
Instance Method Details
#cmd_option_parser ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ecs_helper/command/run_command.rb', line 8 def cmd_option_parser = {} parser = ::OptionParser.new do |opts| opts. = "Usage: ecs_helper run_command [options]" opts.on("-p VALUE", "--project VALUE", "Set project name, if not specified will look at ENV['PROJECT'], will be used to detect cluster") { |p| [:project] = processEqual(p) } opts.on("-a VALUE", "--application VALUE", "Set application name, if not specified will look at ENV['APPLICATION'], will be used to detect service and task definition") { |a| [:application] = processEqual(a) } opts.on("-e VALUE", "--environment VALUE", "Set environment, if not specified will look at ENV['ENVIRONMENT'], it there is empty will try to detect based on the branch") { |e| [:environment] = processEqual(e) } opts.on("-v VALUE", "--version VALUE", "Set version which will be applied to all containers in the task if tag is present in the repo") { |t| [:version] = processEqual(t) } opts.on("--cluster VALUE", "Set cluster name, could be autodetected if project and environment are specified") { |c| [:cluster] = processEqual(c) } opts.on("-s VALUE", "--service VALUE", "Set service, could be autodetected if application and environment are specified") { |s| [:service] = processEqual(s) } opts.on("-t VALUE", "--timeout VALUE", "Set timeout how long to wait until deployment finished") { |t| [:timeout] = processEqual(t) } opts.on("--command VALUE", "Set command, should not demonize container") { |c| [:command] = processEqual(c) } opts.on("-n VALUE", "--name VALUE", "Set name (will be used for task definition name and log prefix") { |l| [:name] = processEqual(l) } opts.on("--container-name VALUE", "Set container name (default is the first container") { |cn| [:container_name] = processEqual(cn) } end [parser, ] end |
#custom_task_definition(hash) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/ecs_helper/command/run_command.rb', line 77 def custom_task_definition(hash) hash.merge({ container_definitions: new_container_definition(hash), family: "#{hash[:family]}-#{name}", }) end |
#required ⇒ Object
26 27 28 |
# File 'lib/ecs_helper/command/run_command.rb', line 26 def required [:name, :command] end |
#run ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ecs_helper/command/run_command.rb', line 30 def run task_definition_helper = ECSHelper::TaskDefinitionHelper.new(helper, service) service_task_definition = task_definition_helper.service_task_definition new_task_definition_hash = task_definition_helper.new_task_definition_hash custom_task_definition_hash = custom_task_definition(new_task_definition_hash) custom_task_definition = task_definition_helper.register_task_definition(custom_task_definition_hash) log("Command", type) log("Options", ) log("Environment", environment) log("Cluster", cluster_arn) log("Service", service_arn) log("Version", version) log("New task definition", custom_task_definition.task_definition_arn) task = run_task(custom_task_definition.task_definition_arn) log("Start task", "Task #{task.task_arn} was started") log("Waiting for task job...") wait_for_task(task.task_arn) && log("Success", "Task finished successfully", :cyan) end |
#run_task(task_definition_arn) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/ecs_helper/command/run_command.rb', line 50 def run_task(task_definition_arn) helper.client.run_task({ cluster: cluster_arn, task_definition: task_definition_arn, network_configuration: service.network_configuration.to_hash, launch_type: service.launch_type }) end |
#task(arn) ⇒ Object
59 60 61 |
# File 'lib/ecs_helper/command/run_command.rb', line 59 def task(arn) helper.client.describe_tasks({ cluster: cluster_arn, tasks: [arn] })[0] end |
#wait_for_task(task_arn, time = 0) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ecs_helper/command/run_command.rb', line 63 def wait_for_task(task_arn, time = 0) task = task(task_arn) container = task.containers[0]; log("container: #{container.name}, time: #{time}, timeout: #{timeout}, status: #{container.last_status}, exit_code: #{container.exit_code || 'NONE'}") if container.last_status == "STOPPED" return true if container.exit_code == 0 error("Task #{task_arn} finished with exit code #{container.exit_code}") end error("Task run timeout (#{timeout})") if time > timeout sleep STEP wait_for_task(task_arn, time + STEP) end |