Class: Ufo::CLI::Exec
- Inherits:
-
Base
- Object
- Base
- Ufo::CLI::Exec
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
#sure?
#pretty_home, #pretty_path, #pretty_time
#logger
#build, #deploy, #info, #ps
#names
#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
|
#command ⇒ Object
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 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
|
#run ⇒ Object
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
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
task_name = task.task_arn.split('/').last
execute_command(
cluster: "#{@cluster}",
task: task_name,
container: container(task), interactive: true,
command: command
)
end
|
#service_tasks ⇒ Object
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
|