Class: Ufo::Task

Inherits:
Base
  • Object
show all
Extended by:
Memoist
Includes:
AwsService, Settings, Util
Defined in:
lib/ufo/task.rb

Instance Method Summary collapse

Methods included from Util

#default_cluster, #display_params, #execute, #pretty_time, #settings, #task_definition_arns, #user_params

Methods included from Settings

#cfn, #network, #settings

Methods included from AwsService

#cloudformation, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb

Methods inherited from Base

#full_service, #info, #no_service_message, #switch_current

Methods included from Stack::Helper

#adjust_stack_name, #find_stack, #status

Constructor Details

#initialize(task_definition, options) ⇒ Task

Returns a new instance of Task.



9
10
11
12
13
14
# File 'lib/ufo/task.rb', line 9

def initialize(task_definition, options)
  @task_definition = task_definition
  @options = options
  # Assume task_definition is the same name as the ecs service name
  @cluster = @options[:cluster] || default_cluster(task_definition)
end

Instance Method Details

#ensure_log_group_existObject



51
52
53
# File 'lib/ufo/task.rb', line 51

def ensure_log_group_exist
  LogGroup.new(@task_definition, @options).create
end

#exit_if_failures!(resp) ⇒ Object

Pretty hard to produce this edge case. Happens when:

launch_type: EC2
network_mode: awsvpc
assign_public_ip: DISABLED


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ufo/task.rb', line 59

def exit_if_failures!(resp)
  return if resp[:failures].nil? || resp[:failures].empty?

  puts "There was a failure running the ECS task.".color(:red)
  puts "This might be happen if you have a network_mode of awsvpc and have assigned_public_ip to DISABLED."
  puts "This cryptic error also shows up if the network settings have security groups and subnets that are not in the same vpc as the ECS cluster container instances.  Please double check that."
  puts "You can use this command to quickly reconfigure the network settings:"
  puts "  ufo network init --vpc-id XXX."
  puts "More details on the can be found under the 'Task Networking Considerations' section at: "
  puts "https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-networking.html"
  puts "Original response with failures:"
  pp resp
  exit 1
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ufo/task.rb', line 16

def run
  puts "Running task_definition: #{@task_definition}".color(:green) unless @options[:mute]
  return if @options[:noop]

  task_options = {
    cluster: @cluster,
    task_definition: @task_definition
  }
  task_options = adjust_fargate_options(task_options)
  task_options = task_options.merge(user_params[:run_task] || {})
  task_options = adjust_security_groups(task_options)

  if @options[:command]
    task_options.merge!(overrides: overrides)
    puts "Running task with container overrides."
    puts "Command: #{command_in_human_readable_form}"
  end

  unless @options[:mute]
    puts "Running task with params:"
    display_params(task_options)
  end

  ensure_log_group_exist

  resp = run_task(task_options)
  exit_if_failures!(resp)
  unless @options[:mute]
    task_arn = resp.tasks[0].task_arn
    puts "Task ARN: #{task_arn}"
    puts "  aws ecs describe-tasks --tasks #{task_arn} --cluster #{@cluster}"
    cloudwatch_info(task_arn)
  end
end

#run_task(options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ufo/task.rb', line 74

def run_task(options)
  puts "Equivalent aws cli command:"
  puts "  aws ecs run-task --cluster #{@cluster} --task-definition #{options[:task_definition]}".color(:green)
  run_task_result = ecs.run_task(options)
  if @options[:wait]
    task_arn = run_task_result.tasks[0].task_arn
    raise SystemExit, exit_status_of_task(task_arn)
  else
    run_task_result
  end
rescue Aws::ECS::Errors::ClientException => e
  if e.message =~ /ECS was unable to assume the role/
    puts "ERROR: #{e.class} #{e.message}".color(:red)
    puts "Please double check the executionRoleArn in your task definition."
    exit 1
  else
    raise
  end
rescue Aws::ECS::Errors::InvalidParameterException => e
  if e.message =~ /Network Configuration must be provided when networkMode 'awsvpc' is specified/
    puts "ERROR: #{e.class} #{e.message}".color(:red)
    puts "Please double check .ufo/params.yml and make sure that network_configuration is set."
    puts "Or run change the task definition template in .ufo/templates so it does not use vpcmode."
    exit 1
  else
    raise
  end
end