ecs_deploy_cli 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36c1f26447ccb1748f1c064fbee8c5417502162d564e43b697148f0c9b399a12
4
- data.tar.gz: d2571aff0f5ae5dcdf556a3b3cdeb9177310bcbb230a0817d92c7d276d8fcd30
3
+ metadata.gz: f3c493de7ebb99888eb436db698b8215291b733983ac8b37a16e7842c8588d0d
4
+ data.tar.gz: 34f5613fef2b6746196ed91b3c67d358c02bdafcf89703e1704c2b5e93ca987d
5
5
  SHA512:
6
- metadata.gz: 71e0f4b2ac6191c6bc8194f07a352ddb51056caa1b30071e7a92a108ca22b0a2d6fe0fa4b5e92e7793a0df13c0260b96c1d0748939816014254bf4027afd524a
7
- data.tar.gz: c40587c040e0077860b3cf573b8d03b31a189ec931143612de2f1184b72379562ff8cc03fe14da6415713cd230edf854ced7844c7b611435c1caa1f0b71021ab
6
+ metadata.gz: aa4641ecfd920aa198310ef81cd00f9f173d0fa3c98066172792e0c4d74cd09009c1c000f20ee128be9374e037c3ada0002f44e8a51a0ac7895b1a376699eb22
7
+ data.tar.gz: 3144d96e6f64b42faf4d47ffad4c2dfcbd4c818507978491b42586842730ae36a9c3d0cf367d00845dc347d87abc90e05ae7181264adec243d02078b19deee8d
@@ -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
- runner.ssh
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
@@ -274,7 +274,6 @@ Resources:
274
274
  ToPort: !Ref SecurityIngressToPort
275
275
  CidrIp: !Ref SecurityIngressCidrIp
276
276
 
277
-
278
277
  ## TODO: FINISH
279
278
  # ALBSecurityGroup:
280
279
  # Condition: CreateNewLoadBalancer
@@ -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
- dns_name = load_dns_name_from_instance_ids(instance_ids)
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 load_dns_name_from_instance_ids(instance_ids)
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: 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
- instances = ecs_client.list_container_instances(
26
- cluster: config[:cluster]
27
- ).to_h[:container_instance_arns]
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EcsDeployCli
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -110,28 +110,67 @@ describe EcsDeployCli::Runner do
110
110
  subject.setup!
111
111
  end
112
112
 
113
- it '#ssh' do
114
- expect(mock_ecs_client).to receive(:list_container_instances).and_return({ container_instance_arns: ['arn:123123'] })
115
- expect(mock_ecs_client).to receive(:describe_container_instances).and_return(double(container_instances: [double(ec2_instance_id: 'i-123123')]))
116
-
117
- expect(mock_ec2_client).to receive(:describe_instances)
118
- .with(instance_ids: ['i-123123'])
119
- .and_return(
120
- double(reservations: [
121
- double(instances: [double(public_dns_name: 'test.com')])
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
- expect(Process).to receive(:fork) do |&block|
126
- block.call
136
+ subject.ssh
127
137
  end
128
- expect(Process).to receive(:wait)
129
138
 
130
- expect_any_instance_of(EcsDeployCli::Runners::SSH).to receive(:exec).with('ssh ec2-user@test.com')
131
- expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
132
- expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ec2_client).at_least(:once).and_return(mock_ec2_client)
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
- subject.ssh
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecs_deploy_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mònade