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

Commit 9e49a15

Browse files
committed
🆕 Create scheduled tasks if not already present
1 parent 67565e0 commit 9e49a15

File tree

6 files changed

+64
-34
lines changed

6 files changed

+64
-34
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Changelog
22
All notable changes to this project made by Monade Team are documented in this file. For info refer to [email protected]
33

4-
## [UNRELEASED]
4+
## [0.5.0] - 2021-07-16
55
### Added
66
- Command `setup` now create the keypair if it's missing
7+
- `deploy-scheduled-tasks` now creates scheduled tasks if not already there
78

89
### Fixed
910
- Command `setup` now raises error when the IAM role `ecsInstanceRole` doesn't exist in your account

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ runner.update_services!
237237

238238
## TODOs
239239
- Creating the ecsInstanceRole automatically
240-
- Create scheduled tasks if not present?
240+
- Create scheduled tasks on setup
241241
- Navigate through logs (or maybe not: https://github.com/jorgebastida/awslogs)
242242
- Recap cluster status
243243
- More configuration options

lib/ecs_deploy_cli/runners/setup.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def setup_cluster!(cluster_options)
3636
ecs_client.create_cluster(
3737
cluster_name: config[:cluster]
3838
)
39-
EcsDeployCli.logger.info "Cluster created, now running cloudformation..."
39+
EcsDeployCli.logger.info 'Cluster created, now running cloudformation...'
4040

4141
stack_name = "EC2ContainerService-#{config[:cluster]}"
4242

@@ -129,14 +129,14 @@ def cluster_exists?
129129

130130
def ensure_ecs_roles_exists!
131131
REQUIRED_ECS_ROLES.each do |role_name, link|
132-
role = iam_client.get_role(role_name: role_name).to_h
132+
iam_client.get_role(role_name: role_name).to_h
133133
rescue Aws::IAM::Errors::NoSuchEntity
134134
raise SetupError, "IAM Role #{role_name} does not exist. Please create it: #{link}."
135135
end
136136
end
137137

138138
def create_keypair_if_required!(cluster_options)
139-
keypairs = ec2_client.describe_key_pairs(key_names: [cluster_options[:keypair_name]]).to_h[:key_pairs]
139+
ec2_client.describe_key_pairs(key_names: [cluster_options[:keypair_name]]).to_h[:key_pairs]
140140
rescue Aws::EC2::Errors::InvalidKeyPairNotFound
141141
EcsDeployCli.logger.info "Keypair \"#{cluster_options[:keypair_name]}\" not found, creating it..."
142142
key_material = ec2_client.create_key_pair(key_name: cluster_options[:keypair_name]).to_h[:key_material]

lib/ecs_deploy_cli/runners/update_crons.rb

+16-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ def run!
88

99
crons.each do |cron_name, cron_definition|
1010
task_definition = tasks[cron_definition[:task_name]]
11-
raise "Undefined task #{cron_definition[:task_name].inspect} in (#{tasks.keys.inspect})" unless task_definition
11+
unless task_definition
12+
raise "Undefined task #{cron_definition[:task_name].inspect} in (#{tasks.keys.inspect})"
13+
end
1214

1315
updated_task = _update_task(task_definition)
1416

15-
current_target = cwe_client.list_targets_by_rule(
16-
{
17-
rule: cron_name,
18-
limit: 1
19-
}
20-
).to_h[:targets].first
17+
current_target = load_or_init_target(cron_name)
2118

2219
cwe_client.put_rule(
2320
cron_definition[:rule]
@@ -36,6 +33,18 @@ def run!
3633
EcsDeployCli.logger.info "Deployed scheduled task \"#{cron_name}\"!"
3734
end
3835
end
36+
37+
private
38+
39+
def load_or_init_target(cron_name)
40+
cwe_client.list_targets_by_rule({ rule: cron_name, limit: 1 }).to_h[:targets].first
41+
rescue Aws::CloudWatchEvents::Errors::ResourceNotFoundException
42+
{
43+
id: cron_name,
44+
arn: "arn:aws:ecs:#{config[:aws_region]}:#{config[:aws_profile_id]}:cluster/#{config[:cluster]}",
45+
role_arn: "arn:aws:iam::#{config[:aws_profile_id]}:role/ecsEventsRole"
46+
}
47+
end
3948
end
4049
end
4150
end

lib/ecs_deploy_cli/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module EcsDeployCli
4-
VERSION = '0.4.0'
4+
VERSION = '0.5.0'
55
end

spec/ecs_deploy_cli/runner_spec.rb

+41-21
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@
9494

9595
context '#setup!' do
9696
before 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-
})
97+
mock_ssm_client.stub_responses(
98+
:get_parameter, {
99+
parameter: {
100+
name: '/aws/service/ecs/optimized-ami/amazon-linux-2/recommended',
101+
type: 'String',
102+
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"}'
103+
}
104+
}
105+
)
104106
end
105107

106108
it 'setups the cluster correctly' do
@@ -130,19 +132,19 @@
130132
end
131133

132134
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:iam_client).at_least(:once).and_return(mock_iam_client)
133-
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
134135

135136
expect { subject.setup! }.to output(/IAM Role ecsInstanceRole does not exist./).to_stdout
136137
end
137138

138139
it 'fails if the cluster is already there' do
139-
expect(mock_ecs_client).to receive(:describe_clusters).and_return(clusters: [{ }])
140+
expect(mock_ecs_client).to receive(:describe_clusters).and_return(clusters: [{}])
140141

141142
expect(EcsDeployCli.logger).to receive(:info).at_least(:once) do |message|
142143
puts message
143144
end
144145

145146
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwl_client).at_least(:once).and_return(mock_cwl_client)
147+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:iam_client).at_least(:once).and_return(mock_iam_client)
146148
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
147149

148150
expect { subject.setup! }.to output(/Cluster already created, skipping./).to_stdout
@@ -172,9 +174,11 @@
172174
expect(mock_ec2_client).to receive(:describe_instances)
173175
.with(instance_ids: ['i-123123'])
174176
.and_return(
175-
double(reservations: [
176-
double(instances: [double(public_dns_name: 'test.com')])
177-
])
177+
double(
178+
reservations: [
179+
double(instances: [double(public_dns_name: 'test.com')])
180+
]
181+
)
178182
)
179183

180184
expect(Process).to receive(:fork) do |&block|
@@ -209,8 +213,8 @@
209213
.with(instance_ids: ['i-321321'])
210214
.and_return(
211215
double(reservations: [
212-
double(instances: [double(public_dns_name: 'test.com')])
213-
])
216+
double(instances: [double(public_dns_name: 'test.com')])
217+
])
214218
)
215219

216220
expect(Process).to receive(:fork) do |&block|
@@ -248,16 +252,32 @@
248252
subject.run_task!('yourproject-cron', launch_type: 'FARGATE', security_groups: [], subnets: [])
249253
end
250254

251-
it '#update_crons!' do
252-
mock_ecs_client.stub_responses(:register_task_definition, { task_definition: { family: 'some', revision: 1, task_definition_arn: 'arn:task:eu-central-1:xxxx' } })
255+
context '#update_crons!' do
256+
it 'creates missing crons' do
257+
mock_ecs_client.stub_responses(:register_task_definition, { task_definition: { family: 'some', revision: 1, task_definition_arn: 'arn:task:eu-central-1:xxxx' } })
253258

254-
mock_cwe_client.stub_responses(:list_targets_by_rule, { targets: [{ id: '123', arn: 'arn:123' }] })
259+
expect(mock_cwe_client).to receive(:list_targets_by_rule) do
260+
raise Aws::CloudWatchEvents::Errors::ResourceNotFoundException.new(nil, 'some')
261+
end
255262

256-
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwl_client).at_least(:once).and_return(mock_cwl_client)
257-
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
258-
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwe_client).at_least(:once).and_return(mock_cwe_client)
263+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwl_client).at_least(:once).and_return(mock_cwl_client)
264+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
265+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwe_client).at_least(:once).and_return(mock_cwe_client)
266+
267+
subject.update_crons!
268+
end
269+
270+
it 'updates existing crons' do
271+
mock_ecs_client.stub_responses(:register_task_definition, { task_definition: { family: 'some', revision: 1, task_definition_arn: 'arn:task:eu-central-1:xxxx' } })
272+
273+
mock_cwe_client.stub_responses(:list_targets_by_rule, { targets: [{ id: '123', arn: 'arn:123' }] })
259274

260-
subject.update_crons!
275+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwl_client).at_least(:once).and_return(mock_cwl_client)
276+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:ecs_client).at_least(:once).and_return(mock_ecs_client)
277+
expect_any_instance_of(EcsDeployCli::Runners::Base).to receive(:cwe_client).at_least(:once).and_return(mock_cwe_client)
278+
279+
subject.update_crons!
280+
end
261281
end
262282

263283
it '#update_services!' do

0 commit comments

Comments
 (0)