Class: Ufo::CLI::Exec

Inherits:
Base
  • Object
show all
Defined in:
lib/ufo/cli/exec.rb

Instance Attribute Summary

Attributes inherited from Base

#task_definition

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Utils::Sure

#sure?

Methods included from Utils::Pretty

#pretty_home, #pretty_path, #pretty_time

Methods included from Utils::Logging

#logger

Methods included from Ufo::Concerns

#build, #deploy, #info, #ps

Methods included from Ufo::Concerns::Names

#names

Methods included from AwsServices

#acm, #applicationautoscaling, #aws_options, #cfn, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb, #s3, #ssm_client, #waf_client

Methods included from AwsServices::Concerns

#find_stack, #find_stack_resources, #stack_resources, #status, #task_definition_arns

Constructor Details

This class inherits a constructor from Ufo::CLI::Base

Instance Method Details

#check_aws_cli!Object



85
86
87
88
89
90
# File 'lib/ufo/cli/exec.rb', line 85

def check_aws_cli!
  installed = system "type aws > /dev/null 2>&1"
  return if installed
  logger.error "ERROR: aws cli is required to use ufo exec".color(:red)
  exit 1
end

#check_install!Object



73
74
75
76
# File 'lib/ufo/cli/exec.rb', line 73

def check_install!
  check_session_manager_plugin!
  check_aws_cli!
end

#check_session_manager_plugin!Object



78
79
80
81
82
83
# File 'lib/ufo/cli/exec.rb', line 78

def check_session_manager_plugin!
  installed = system "type session-manager-plugin > /dev/null 2>&1"
  return if installed
  logger.error "ERROR: The Session Manager plugin required to use ufo exec".color(:red)
  exit 1
end

#commandObject



69
70
71
# File 'lib/ufo/cli/exec.rb', line 69

def command
  @options[:command] || Ufo.config.exec.command
end

#container(task) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/ufo/cli/exec.rb', line 39

def container(task)
  return @options[:container] if @options[:container]
  containers = task.containers
  container = containers.find do |c|
    c.name == @options[:role]
  end
  container ||= containers.first  # assume first task if not roles match
  container.name if container
end

#execute_command(options = {}) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/ufo/cli/exec.rb', line 49

def execute_command(options={})
  args = options.inject('') do |args, (k,v)|
    arg = k == :interactive ? "--#{k}" : "--#{k} #{v}"
    args += " #{arg}"
  end
  sh "aws ecs execute-command#{args}"
end

#runObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ufo/cli/exec.rb', line 3

def run
  check_install!
  stack = info.stack
  unless stack
    logger.error "Stack not found: #{@stack_name}".color(:red)
    exit 1
  end

  service = info.service
  unless service # brand new deploy
    logger.error "ECS Service not yet available".color(:red)
    logger.info "Try again in a little bit"
    exit 1
  end

  running = service_tasks.select do |task|
    task.last_status == "RUNNING"
  end
  if running.empty?
    logger.info "No running tasks found to exec into"
    return
  end

  tasks = running.sort_by { |t| t.started_at }
  task = tasks.last # most recent

  task_name = task.task_arn.split('/').last
  execute_command(
    cluster: @cluster,
    task: task_name,
    container: container(task), # only required if multiple containers in a task
    interactive: true,
    command: command
  )
end

#service_tasksObject



57
58
59
60
61
62
# File 'lib/ufo/cli/exec.rb', line 57

def service_tasks
  service_name = info.service.service_name
  all_task_arns = ecs.list_tasks(cluster: @cluster, service_name: service_name).task_arns
  return [] if all_task_arns.empty?
  ecs.describe_tasks(cluster: @cluster, tasks: all_task_arns).tasks
end

#sh(command) ⇒ Object



64
65
66
67
# File 'lib/ufo/cli/exec.rb', line 64

def sh(command)
  puts "=> #{command}"
  Kernel.exec command
end