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, #cloudformation, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb, #find_stack, #ssm_client, #stack_resources, #status, #task_definition_arns

Constructor Details

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

Instance Method Details

#check_aws_cli!Object



75
76
77
78
79
80
# File 'lib/ufo/cli/exec.rb', line 75

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



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

def check_install!
  check_session_manager_plugin!
  check_aws_cli!
end

#check_session_manager_plugin!Object



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

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



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

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

#container(task) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/ufo/cli/exec.rb', line 29

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



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

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
# File 'lib/ufo/cli/exec.rb', line 3

def run
  check_install!
  service = info.service
  return unless service # brand new deploy

  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



47
48
49
50
51
52
# File 'lib/ufo/cli/exec.rb', line 47

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



54
55
56
57
# File 'lib/ufo/cli/exec.rb', line 54

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