Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 7bfca3c

Browse files
committed
🆕 Raise error if ecsInstanceRole does not exist
1 parent d6cb255 commit 7bfca3c

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

ecs-deploy-cli.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
2525
s.add_dependency 'aws-sdk-ec2', '~> 1'
2626
s.add_dependency 'aws-sdk-ecs', '~> 1'
2727
s.add_dependency 'aws-sdk-ssm', '~> 1'
28+
s.add_dependency 'aws-sdk-iam', '~> 1'
2829
s.add_dependency 'colorize', '~> 0.8.1'
2930
s.add_dependency 'hashdiff', '~> 1.0'
3031
s.add_dependency 'thor', '~> 1.1'

lib/ecs_deploy_cli/runners/setup.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ def run!
1313
private
1414

1515
def setup_cluster!(cluster_options)
16-
clusters = ecs_client.describe_clusters(clusters: [config[:cluster]]).to_h[:clusters]
17-
if clusters.length == 1
16+
if cluster_exists?
1817
EcsDeployCli.logger.info 'Cluster already created, skipping.'
1918
return
2019
end
2120

21+
unless ecs_instance_role_exists?
22+
EcsDeployCli.logger.info 'IAM Role ecsInstanceRole does not exist. Please create it: https://docs.aws.amazon.com/batch/latest/userguide/instance_IAM_role.html.'
23+
return
24+
end
25+
2226
EcsDeployCli.logger.info "Creating cluster #{config[:cluster]}..."
2327

2428
params = create_params(cluster_options)
@@ -110,6 +114,19 @@ def create_params(cluster_options)
110114
}
111115
end
112116

117+
def cluster_exists?
118+
clusters = ecs_client.describe_clusters(clusters: [config[:cluster]]).to_h[:clusters]
119+
120+
clusters.length == 1
121+
end
122+
123+
def ecs_instance_role_exists?
124+
role = iam_client.get_role(role_name: 'ecsInstanceRole').to_h
125+
true
126+
rescue Aws::IAM::Errors::NoSuchEntity
127+
false
128+
end
129+
113130
def format_cloudformation_params(params)
114131
params.map { |k, v| { parameter_key: k, parameter_value: v.to_s } }
115132
end

spec/ecs_deploy_cli/runner_spec.rb

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
require 'aws-sdk-ec2'
77
require 'aws-sdk-ssm'
88
require 'aws-sdk-cloudformation'
9+
require 'aws-sdk-iam'
910

1011
describe EcsDeployCli::Runner do
1112
context 'defines task data' do
1213
let(:parser) { EcsDeployCli::DSL::Parser.load('spec/support/ECSFile') }
1314
subject { described_class.new(parser) }
15+
let(:mock_iam_client) { Aws::IAM::Client.new(stub_responses: true) }
1416
let(:mock_cf_client) { Aws::CloudFormation::Client.new(stub_responses: true) }
1517
let(:mock_ssm_client) { Aws::SSM::Client.new(stub_responses: true) }
1618
let(:mock_ecs_client) { Aws::ECS::Client.new(stub_responses: true) }
@@ -90,24 +92,56 @@
9092
ENV['AWS_REGION'] = nil
9193
end
9294

93-
it '#setup!' do
94-
mock_ssm_client.stub_responses(:get_parameter, {
95-
parameter: {
96-
name: '/aws/service/ecs/optimized-ami/amazon-linux-2/recommended',
97-
type: 'String',
98-
value: '{"schema_version":1,"image_name":"amzn2-ami-ecs-hvm-2.0.20210331-x86_64-ebs","image_id":"ami-03bbf53329af34379","os":"Amazon Linux 2","ecs_runtime_version":"Docker version 19.03.13-ce","ecs_agent_version":"1.51.0"}'
99-
}
100-
})
95+
context '#setup!' do
96+
it 'setups the cluster correctly' do
97+
mock_ssm_client.stub_responses(:get_parameter, {
98+
parameter: {
99+
name: '/aws/service/ecs/optimized-ami/amazon-linux-2/recommended',
100+
type: 'String',
101+
value: '{"schema_version":1,"image_name":"amzn2-ami-ecs-hvm-2.0.20210331-x86_64-ebs","image_id":"ami-03bbf53329af34379","os":"Amazon Linux 2","ecs_runtime_version":"Docker version 19.03.13-ce","ecs_agent_version":"1.51.0"}'
102+
}
103+
})
104+
105+
expect(mock_iam_client).to receive(:get_role).with({ role_name: 'ecsInstanceRole' }).and_return({ role: { arn: 'some' } })
106+
expect(mock_cf_client).to receive(:wait_until)
107+
expect(mock_ecs_client).to receive(:create_service)
108+
109+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:iam_client).at_least(:once).and_return(mock_iam_client)
110+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwl_client).at_least(:once).and_return(mock_cwl_client)
111+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
112+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ssm_client).at_least(:once).and_return(mock_ssm_client)
113+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cf_client).at_least(:once).and_return(mock_cf_client)
101114

102-
expect(mock_cf_client).to receive(:wait_until)
103-
expect(mock_ecs_client).to receive(:create_service)
115+
subject.setup!
116+
end
104117

105-
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwl_client).at_least(:once).and_return(mock_cwl_client)
106-
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
107-
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ssm_client).at_least(:once).and_return(mock_ssm_client)
108-
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cf_client).at_least(:once).and_return(mock_cf_client)
118+
it 'fails if the IAM role is not setup' do
119+
expect(EcsDeployCli.logger).to receive(:info).at_least(:once) do |message|
120+
puts message
121+
end
122+
123+
expect(mock_iam_client).to receive(:get_role).with({ role_name: 'ecsInstanceRole' }) do
124+
raise Aws::IAM::Errors::NoSuchEntity.new(nil, 'some')
125+
end
126+
127+
128+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:iam_client).at_least(:once).and_return(mock_iam_client)
129+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
130+
131+
expect { subject.setup! }.to output(/IAM Role ecsInstanceRole does not exist./).to_stdout
132+
end
109133

110-
subject.setup!
134+
it 'fails if the cluster is already there' do
135+
expect(mock_ecs_client).to receive(:describe_clusters).and_return(clusters: [{ }])
136+
137+
expect(EcsDeployCli.logger).to receive(:info).at_least(:once) do |message|
138+
puts message
139+
end
140+
141+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
142+
143+
expect { subject.setup! }.to output(/Cluster already created, skipping./).to_stdout
144+
end
111145
end
112146

113147
context '#ssh' do

0 commit comments

Comments
 (0)