|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'ecs_deploy_cli/runners/base' |
| 4 | +require 'ecs_deploy_cli/runners/ssh' |
| 5 | +require 'ecs_deploy_cli/runners/validate' |
| 6 | +require 'ecs_deploy_cli/runners/diff' |
| 7 | +require 'ecs_deploy_cli/runners/update_crons' |
| 8 | +require 'ecs_deploy_cli/runners/update_services' |
| 9 | +require 'ecs_deploy_cli/runners/run_task' |
| 10 | + |
1 | 11 | module EcsDeployCli |
2 | 12 | class Runner |
3 | 13 | def initialize(parser) |
4 | 14 | @parser = parser |
5 | 15 | end |
6 | 16 |
|
7 | 17 | def validate! |
8 | | - @parser.resolve |
| 18 | + EcsDeployCli::Runners::Validate.new(@parser).run! |
9 | 19 | end |
10 | 20 |
|
11 | 21 | def update_crons! |
12 | | - _, tasks, crons = @parser.resolve |
13 | | - |
14 | | - crons.each do |cron_name, cron_definition| |
15 | | - task_definition = tasks[cron_definition[:task_name]] |
16 | | - raise "Undefined task #{cron_definition[:task_name].inspect} in (#{tasks.keys.inspect})" unless task_definition |
17 | | - |
18 | | - updated_task = _update_task(task_definition) |
19 | | - |
20 | | - current_target = cwe_client.list_targets_by_rule( |
21 | | - { |
22 | | - rule: cron_name, |
23 | | - limit: 1 |
24 | | - } |
25 | | - ).to_h[:targets].first |
26 | | - |
27 | | - cwe_client.put_rule( |
28 | | - cron_definition[:rule] |
29 | | - ) |
| 22 | + EcsDeployCli::Runners::UpdateCrons.new(@parser).run! |
| 23 | + end |
30 | 24 |
|
31 | | - cwe_client.put_targets( |
32 | | - rule: cron_name, |
33 | | - targets: [ |
34 | | - id: current_target[:id], |
35 | | - arn: current_target[:arn], |
36 | | - role_arn: current_target[:role_arn], |
37 | | - input: cron_definition[:input].to_json, |
38 | | - ecs_parameters: cron_definition[:ecs_parameters].merge(task_definition_arn: updated_task[:task_definition_arn]) |
39 | | - ] |
40 | | - ) |
41 | | - EcsDeployCli.logger.info "Deployed scheduled task \"#{cron_name}\"!" |
42 | | - end |
| 25 | + def run_task!(task_name, launch_type:, security_groups:, subnets:) |
| 26 | + EcsDeployCli::Runners::RunTask.new(@parser).run!(task_name, launch_type: launch_type, security_groups: security_groups, subnets: subnets) |
43 | 27 | end |
44 | 28 |
|
45 | 29 | def ssh |
46 | | - instances = ecs_client.list_container_instances( |
47 | | - cluster: config[:cluster] |
48 | | - ).to_h[:container_instance_arns] |
49 | | - |
50 | | - response = ecs_client.describe_container_instances( |
51 | | - cluster: config[:cluster], |
52 | | - container_instances: instances |
53 | | - ) |
54 | | - |
55 | | - EcsDeployCli.logger.info "Found instances: #{response.container_instances.map(&:ec2_instance_id).join(', ')}" |
56 | | - |
57 | | - response = ec2_client.describe_instances( |
58 | | - instance_ids: response.container_instances.map(&:ec2_instance_id) |
59 | | - ) |
60 | | - |
61 | | - dns_name = response.reservations[0].instances[0].public_dns_name |
62 | | - EcsDeployCli.logger.info "Connecting to ec2-user@#{dns_name}..." |
| 30 | + EcsDeployCli::Runners::SSH.new(@parser).run! |
| 31 | + end |
63 | 32 |
|
64 | | - Process.fork { exec("ssh ec2-user@#{dns_name}") } |
65 | | - Process.wait |
| 33 | + def diff |
| 34 | + EcsDeployCli::Runners::Diff.new(@parser).run! |
66 | 35 | end |
67 | 36 |
|
68 | 37 | def update_services!(service: nil, timeout: 500) |
69 | | - services, resolved_tasks = @parser.resolve |
70 | | - |
71 | | - services.each do |service_name, service_definition| |
72 | | - next if !service.nil? && service != service_name |
73 | | - |
74 | | - task_definition = _update_task resolved_tasks[service_definition.options[:task]] |
75 | | - task_name = "#{task_definition[:family]}:#{task_definition[:revision]}" |
76 | | - |
77 | | - ecs_client.update_service( |
78 | | - cluster: config[:cluster], |
79 | | - service: service_name, |
80 | | - task_definition: "#{task_definition[:family]}:#{task_name}" |
81 | | - ) |
82 | | - wait_for_deploy(service_name, task_name, timeout: timeout) |
83 | | - EcsDeployCli.logger.info "Deployed service \"#{service_name}\"!" |
84 | | - end |
| 38 | + EcsDeployCli::Runners::UpdateServices.new(@parser).run!(service: service, timeout: timeout) |
85 | 39 | end |
86 | 40 |
|
87 | 41 | private |
88 | 42 |
|
89 | | - def wait_for_deploy(service_name, task_name, timeout:) |
90 | | - wait_data = { cluster: config[:cluster], services: [service_name] } |
91 | | - |
92 | | - started_at = Time.now |
93 | | - ecs_client.wait_until( |
94 | | - :services_stable, wait_data, |
95 | | - max_attempts: nil, |
96 | | - before_wait: lambda { |_, response| |
97 | | - deployments = response.services.first.deployments |
98 | | - log_deployments task_name, deployments |
99 | | - |
100 | | - throw :success if deployments.count == 1 && deployments[0].task_definition.end_with?(task_name) |
101 | | - throw :failure if Time.now - started_at > timeout |
102 | | - } |
103 | | - ) |
104 | | - end |
105 | | - |
106 | 43 | def _update_task(definition) |
107 | 44 | ecs_client.register_task_definition( |
108 | 45 | definition |
109 | 46 | ).to_h[:task_definition] |
110 | 47 | end |
111 | | - |
112 | | - def log_deployments(task_name, deployments) |
113 | | - EcsDeployCli.logger.info "Waiting for task: #{task_name} to become ok." |
114 | | - EcsDeployCli.logger.info 'Deployment status:' |
115 | | - deployments.each do |deploy| |
116 | | - EcsDeployCli.logger.info "[#{deploy.status}] task=#{deploy.task_definition.split('/').last}, "\ |
117 | | - "desired_count=#{deploy.desired_count}, pending_count=#{deploy.pending_count}, running_count=#{deploy.running_count}, failed_tasks=#{deploy.failed_tasks}" |
118 | | - end |
119 | | - EcsDeployCli.logger.info '' |
120 | | - end |
121 | | - |
122 | | - def ec2_client |
123 | | - @ec2_client ||= begin |
124 | | - require 'aws-sdk-ec2' |
125 | | - Aws::EC2::Client.new( |
126 | | - profile: ENV.fetch('AWS_PROFILE', 'default'), |
127 | | - region: config[:aws_region] |
128 | | - ) |
129 | | - end |
130 | | - end |
131 | | - |
132 | | - def ecs_client |
133 | | - @ecs_client ||= Aws::ECS::Client.new( |
134 | | - profile: ENV.fetch('AWS_PROFILE', 'default'), |
135 | | - region: config[:aws_region] |
136 | | - ) |
137 | | - end |
138 | | - |
139 | | - def cwe_client |
140 | | - @cwe_client ||= begin |
141 | | - require 'aws-sdk-cloudwatchevents' |
142 | | - Aws::CloudWatchEvents::Client.new( |
143 | | - profile: ENV.fetch('AWS_PROFILE', 'default'), |
144 | | - region: config[:aws_region] |
145 | | - ) |
146 | | - end |
147 | | - end |
148 | | - |
149 | | - def config |
150 | | - @parser.config |
151 | | - end |
152 | 48 | end |
153 | 49 | end |
0 commit comments