ecs_deploy_cli 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ecs_deploy_cli/cli.rb +4 -1
- data/lib/ecs_deploy_cli/cloudformation/default.yml +0 -1
- data/lib/ecs_deploy_cli/runner.rb +2 -2
- data/lib/ecs_deploy_cli/runners/ssh.rb +44 -10
- data/lib/ecs_deploy_cli/version.rb +1 -1
- data/spec/ecs_deploy_cli/runner_spec.rb +57 -18
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3c493de7ebb99888eb436db698b8215291b733983ac8b37a16e7842c8588d0d
|
4
|
+
data.tar.gz: 34f5613fef2b6746196ed91b3c67d358c02bdafcf89703e1704c2b5e93ca987d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa4641ecfd920aa198310ef81cd00f9f173d0fa3c98066172792e0c4d74cd09009c1c000f20ee128be9374e037c3ada0002f44e8a51a0ac7895b1a376699eb22
|
7
|
+
data.tar.gz: 3144d96e6f64b42faf4d47ffad4c2dfcbd4c818507978491b42586842730ae36a9c3d0cf367d00845dc347d87abc90e05ae7181264adec243d02078b19deee8d
|
data/lib/ecs_deploy_cli/cli.rb
CHANGED
@@ -75,9 +75,12 @@ module EcsDeployCli
|
|
75
75
|
|
76
76
|
desc 'ssh', 'Connects to ECS instance via SSH'
|
77
77
|
option :file, default: 'ECSFile'
|
78
|
+
option :service, default: nil
|
79
|
+
option :task, default: nil
|
78
80
|
def ssh
|
79
81
|
@parser = load(options[:file])
|
80
|
-
|
82
|
+
ssh_options = { family: options[:task], service_name: options[:service] }.delete_if { |_, v| v.nil? }
|
83
|
+
runner.ssh(**ssh_options)
|
81
84
|
end
|
82
85
|
|
83
86
|
private
|
@@ -31,8 +31,8 @@ module EcsDeployCli
|
|
31
31
|
EcsDeployCli::Runners::RunTask.new(@parser).run!(task_name, launch_type: launch_type, security_groups: security_groups, subnets: subnets)
|
32
32
|
end
|
33
33
|
|
34
|
-
def ssh
|
35
|
-
EcsDeployCli::Runners::SSH.new(@parser).run!
|
34
|
+
def ssh(**options)
|
35
|
+
EcsDeployCli::Runners::SSH.new(@parser).run!(options)
|
36
36
|
end
|
37
37
|
|
38
38
|
def diff
|
@@ -3,29 +3,63 @@
|
|
3
3
|
module EcsDeployCli
|
4
4
|
module Runners
|
5
5
|
class SSH < Base
|
6
|
-
def run!
|
7
|
-
instance_ids = load_container_instances
|
8
|
-
EcsDeployCli.logger.info "Found instances: #{instance_ids.join(', ')}"
|
6
|
+
def run!(params = {})
|
7
|
+
instance_ids = load_container_instances(params)
|
9
8
|
|
10
|
-
|
9
|
+
instance_id = choose_instance_id(instance_ids)
|
10
|
+
dns_name = load_dns_name_from_instance_id(instance_id)
|
11
11
|
run_ssh(dns_name)
|
12
12
|
end
|
13
13
|
|
14
14
|
private
|
15
15
|
|
16
|
-
def
|
16
|
+
def choose_instance_id(instance_ids)
|
17
|
+
raise 'No instance found' if instance_ids.empty?
|
18
|
+
return instance_ids[0] if instance_ids.length == 1
|
19
|
+
|
20
|
+
instances_selection_text = instance_ids.map.with_index do |instance, index|
|
21
|
+
"#{index + 1}) #{instance}"
|
22
|
+
end.join("\n")
|
23
|
+
|
24
|
+
EcsDeployCli.logger.info(
|
25
|
+
"Found #{instance_ids.count} instances:\n#{instances_selection_text}\nSelect which one you want to access:"
|
26
|
+
)
|
27
|
+
|
28
|
+
index = select_index_from_array(instance_ids, retry_message: 'Invalid option. Select which one you want to access:')
|
29
|
+
|
30
|
+
instance_ids[index]
|
31
|
+
end
|
32
|
+
|
33
|
+
def select_index_from_array(array, retry_message:)
|
34
|
+
while (index = STDIN.gets.chomp)
|
35
|
+
if index =~ /\A[1-9][0-9]*\Z/ && (index.to_i - 1) < array.count
|
36
|
+
index = index.to_i - 1
|
37
|
+
break
|
38
|
+
end
|
39
|
+
|
40
|
+
EcsDeployCli.logger.info(retry_message)
|
41
|
+
end
|
42
|
+
index
|
43
|
+
end
|
44
|
+
|
45
|
+
def load_dns_name_from_instance_id(instance_id)
|
17
46
|
response = ec2_client.describe_instances(
|
18
|
-
instance_ids:
|
47
|
+
instance_ids: [instance_id]
|
19
48
|
)
|
20
49
|
|
21
50
|
response.reservations[0].instances[0].public_dns_name
|
22
51
|
end
|
23
52
|
|
24
|
-
def load_container_instances
|
25
|
-
|
26
|
-
cluster: config[:cluster]
|
27
|
-
).to_h[:
|
53
|
+
def load_container_instances(params = {})
|
54
|
+
task_arns = ecs_client.list_tasks(
|
55
|
+
**params.merge(cluster: config[:cluster])
|
56
|
+
).to_h[:task_arns]
|
57
|
+
|
58
|
+
tasks = ecs_client.describe_tasks(
|
59
|
+
tasks: task_arns, cluster: config[:cluster]
|
60
|
+
).to_h[:tasks]
|
28
61
|
|
62
|
+
instances = tasks.map { |task| task[:container_instance_arn] }.uniq
|
29
63
|
response = ecs_client.describe_container_instances(
|
30
64
|
cluster: config[:cluster],
|
31
65
|
container_instances: instances
|
@@ -110,28 +110,67 @@ describe EcsDeployCli::Runner do
|
|
110
110
|
subject.setup!
|
111
111
|
end
|
112
112
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
.
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
113
|
+
context '#ssh' do
|
114
|
+
it 'runs ssh on a single container instance' do
|
115
|
+
expect(mock_ecs_client).to receive(:list_tasks).and_return({ task_arns: ['arn:123123'] })
|
116
|
+
expect(mock_ecs_client).to receive(:describe_tasks).and_return({ tasks: [{ container_instance_arn: 'arn:instance:123123' }] })
|
117
|
+
expect(mock_ecs_client).to receive(:describe_container_instances).and_return(double(container_instances: [double(ec2_instance_id: 'i-123123')]))
|
118
|
+
|
119
|
+
expect(mock_ec2_client).to receive(:describe_instances)
|
120
|
+
.with(instance_ids: ['i-123123'])
|
121
|
+
.and_return(
|
122
|
+
double(reservations: [
|
123
|
+
double(instances: [double(public_dns_name: 'test.com')])
|
124
|
+
])
|
125
|
+
)
|
126
|
+
|
127
|
+
expect(Process).to receive(:fork) do |&block|
|
128
|
+
block.call
|
129
|
+
end
|
130
|
+
expect(Process).to receive(:wait)
|
131
|
+
|
132
|
+
expect_any_instance_of(EcsDeployCli::Runners::SSH).to receive(:exec).with('ssh ec2-user@test.com')
|
133
|
+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
|
134
|
+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ec2_client).at_least(:once).and_return(mock_ec2_client)
|
124
135
|
|
125
|
-
|
126
|
-
block.call
|
136
|
+
subject.ssh
|
127
137
|
end
|
128
|
-
expect(Process).to receive(:wait)
|
129
138
|
|
130
|
-
|
131
|
-
|
132
|
-
|
139
|
+
it 'prompts which instance if there are multiple ones' do
|
140
|
+
expect(mock_ecs_client).to receive(:list_tasks).and_return({ task_arns: ['arn:123123', 'arn:321321'] })
|
141
|
+
expect(mock_ecs_client).to receive(:describe_tasks).and_return(
|
142
|
+
{
|
143
|
+
tasks: [
|
144
|
+
{ container_instance_arn: 'arn:instance:123123' },
|
145
|
+
{ container_instance_arn: 'arn:instance:321321' }
|
146
|
+
]
|
147
|
+
}
|
148
|
+
)
|
149
|
+
expect(mock_ecs_client).to receive(:describe_container_instances).and_return(
|
150
|
+
double(container_instances: [double(ec2_instance_id: 'i-123123'), double(ec2_instance_id: 'i-321321')])
|
151
|
+
)
|
152
|
+
|
153
|
+
expect(STDIN).to receive(:gets).and_return('2')
|
133
154
|
|
134
|
-
|
155
|
+
expect(mock_ec2_client).to receive(:describe_instances)
|
156
|
+
.with(instance_ids: ['i-321321'])
|
157
|
+
.and_return(
|
158
|
+
double(reservations: [
|
159
|
+
double(instances: [double(public_dns_name: 'test.com')])
|
160
|
+
])
|
161
|
+
)
|
162
|
+
|
163
|
+
expect(Process).to receive(:fork) do |&block|
|
164
|
+
block.call
|
165
|
+
end
|
166
|
+
expect(Process).to receive(:wait)
|
167
|
+
|
168
|
+
expect_any_instance_of(EcsDeployCli::Runners::SSH).to receive(:exec).with('ssh ec2-user@test.com')
|
169
|
+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
|
170
|
+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ec2_client).at_least(:once).and_return(mock_ec2_client)
|
171
|
+
|
172
|
+
subject.ssh
|
173
|
+
end
|
135
174
|
end
|
136
175
|
|
137
176
|
it '#diff' do
|