Class: ECSHelper::Command::RunCommand

Inherits:
Base
  • Object
show all
Defined in:
lib/ecs_helper/command/run_command.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
300
STEP =
5

Instance Attribute Summary collapse

Attributes inherited from Base

#client, #helper, #option_parser, #options, #type

Instance Method Summary collapse

Methods inherited from Base

#application, #check_bin, #initialize, #printable?, #project, #validate

Methods included from Logging

#console, #error, #log

Constructor Details

This class inherits a constructor from ECSHelper::Command::Base

Instance Attribute Details

#new_task_definitionObject

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

#repositoriesObject

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

Parameters:

  • value

    the value to set the attribute service to.



4
5
6
# File 'lib/ecs_helper/command/run_command.rb', line 4

def service=(value)
  @service = value
end

#task_definitionObject

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_parserObject



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
  options = {}
  parser = ::OptionParser.new do |opts|
    opts.banner = "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| options[: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| options[: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| options[: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| options[:version] = processEqual(t) }
    opts.on("--cluster VALUE", "Set cluster name, could be autodetected if project and environment are specified") { |c| options[:cluster] = processEqual(c) }
    opts.on("-s VALUE", "--service VALUE", "Set service, could be autodetected if application and environment are specified") { |s| options[:service] = processEqual(s) }
    opts.on("-t VALUE", "--timeout VALUE", "Set timeout how long to wait until deployment finished") { |t| options[:timeout] = processEqual(t) }
    opts.on("--command VALUE", "Set command, should not demonize container") { |c| options[:command] = processEqual(c) }
    opts.on("-n VALUE", "--name VALUE", "Set name (will be used for task definition name and log prefix") { |l| options[:name] = processEqual(l) }
    opts.on("--container-name VALUE", "Set container name (default is the first container") { |cn| options[:container_name] = processEqual(cn) }
  end
  [parser, options]
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

#requiredObject



26
27
28
# File 'lib/ecs_helper/command/run_command.rb', line 26

def required
  [:name, :command]
end

#runObject



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", 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