|
1
|
1 |
|
from __future__ import print_function, absolute_import |
|
2
|
1 |
|
from time import sleep |
|
3
|
|
|
|
|
4
|
1 |
|
import click |
|
5
|
1 |
|
from datetime import datetime, timedelta |
|
6
|
|
|
|
|
7
|
1 |
|
from ecs_deploy.ecs import DeployAction, ConnectionError, ScaleAction, EcsClient |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
1 |
|
@click.group() |
|
11
|
|
|
def main(): |
|
12
|
|
|
return |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
1 |
|
@click.command() |
|
16
|
1 |
|
@click.argument('cluster') |
|
17
|
1 |
|
@click.argument('service') |
|
18
|
1 |
|
@click.option('-t', '--tag', help='Changes the tag for ALL container images') |
|
19
|
1 |
|
@click.option('-i', '--image', type=(str, str), multiple=True, help='Overwrites the image for a container') |
|
20
|
1 |
|
@click.option('-c', '--command', type=(str, str), multiple=True, help='Overwrites the command in a container') |
|
21
|
1 |
|
@click.option('--region', required=False, help='AWS region') |
|
22
|
1 |
|
@click.option('--access-key-id', required=False, help='AWS access key id') |
|
23
|
1 |
|
@click.option('--secret-access-key', required=False, help='AWS secret access yey') |
|
24
|
1 |
|
@click.option('--profile', required=False, help='AWS configuration profile') |
|
25
|
1 |
|
@click.option('--timeout', required=False, default=300, type=int, help='AWS configuration profile') |
|
26
|
|
|
def deploy(cluster, service, tag, image, command, access_key_id, secret_access_key, region, profile, timeout): |
|
27
|
|
|
""" |
|
28
|
|
|
Redeploy or modify a service. |
|
29
|
|
|
|
|
30
|
|
|
\b |
|
31
|
|
|
CLUSTER is the name of your cluster (e.g. 'my-custer') within ECS. |
|
32
|
|
|
SERVICE is the name of your service (e.g. 'my-app') within ECS. |
|
33
|
|
|
|
|
34
|
|
|
When not giving any other options, the task definition will not be changed. It will just be duplicated, so that |
|
35
|
|
|
all container images will be pulled and redeployed. |
|
36
|
|
|
""" |
|
37
|
|
|
|
|
38
|
|
|
try: |
|
39
|
|
|
client = EcsClient(access_key_id, secret_access_key, region, profile) |
|
40
|
|
|
deployment = DeployAction(client, cluster, service) |
|
41
|
|
|
task_definition = deployment.get_current_task_definition(deployment.service) |
|
42
|
|
|
|
|
43
|
|
|
images = {key: value for (key, value) in image} |
|
44
|
|
|
task_definition.set_images(tag, **images) |
|
45
|
|
|
|
|
46
|
|
|
commands = {key: value for (key, value) in command} |
|
47
|
|
|
task_definition.set_commands(**commands) |
|
48
|
|
|
|
|
49
|
|
|
print_diff(task_definition) |
|
50
|
|
|
|
|
51
|
|
|
click.secho('Creating new task definition revision') |
|
52
|
|
|
new_task_definition = deployment.update_task_definition(task_definition) |
|
53
|
|
|
click.secho('Successfully created revision: %d' % new_task_definition.revision, fg='green') |
|
54
|
|
|
click.secho('Successfully deregistered revision: %d' % task_definition.revision, fg='green') |
|
55
|
|
|
click.secho('') |
|
56
|
|
|
|
|
57
|
|
|
click.secho('Updating service') |
|
58
|
|
|
deployment.deploy(new_task_definition) |
|
59
|
|
|
click.secho('Successfully changed task definition to: %s:%s' % |
|
60
|
|
|
(new_task_definition.family, new_task_definition.revision), fg='green') |
|
61
|
|
|
click.secho('') |
|
62
|
|
|
|
|
63
|
|
|
wait_for_finish(deployment, timeout, 'Deploying new task definition', 'Deployment successful!') |
|
64
|
|
|
|
|
65
|
|
|
except Exception as e: |
|
66
|
|
|
click.secho(e.message, fg='red', err=True) |
|
67
|
|
|
click.secho('') |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
1 |
|
@click.command() |
|
71
|
1 |
|
@click.argument('cluster') |
|
72
|
1 |
|
@click.argument('service') |
|
73
|
1 |
|
@click.argument('desired_count', type=int) |
|
74
|
1 |
|
@click.option('--region', help='AWS region') |
|
75
|
1 |
|
@click.option('--access-key-id', help='AWS access key id') |
|
76
|
1 |
|
@click.option('--secret-access-key', help='AWS secret access yey') |
|
77
|
1 |
|
@click.option('--profile', help='AWS configuration profile') |
|
78
|
1 |
|
@click.option('--timeout', default=300, type=int, help='AWS configuration profile') |
|
79
|
|
|
def scale(cluster, service, desired_count, access_key_id, secret_access_key, region, profile, timeout): |
|
80
|
|
|
""" |
|
81
|
|
|
Scale a service up or down. |
|
82
|
|
|
|
|
83
|
|
|
\b |
|
84
|
|
|
CLUSTER is the name of your cluster (e.g. 'my-custer') within ECS. |
|
85
|
|
|
SERVICE is the name of your service (e.g. 'my-app') within ECS. |
|
86
|
|
|
DESIRED_COUNT is the number of tasks your service should run. |
|
87
|
|
|
|
|
88
|
|
|
""" |
|
89
|
|
|
try: |
|
90
|
|
|
client = EcsClient(access_key_id, secret_access_key, region, profile) |
|
91
|
|
|
scaling = ScaleAction(client, cluster, service) |
|
92
|
|
|
|
|
93
|
|
|
click.secho('Updating service') |
|
94
|
|
|
scaling.scale(desired_count) |
|
95
|
|
|
click.secho('Successfully changed desired count to: %s' % desired_count, fg='green') |
|
96
|
|
|
click.secho('') |
|
97
|
|
|
|
|
98
|
|
|
wait_for_finish(scaling, timeout, 'Scaling service', 'Scaling successful!') |
|
99
|
|
|
|
|
100
|
|
|
except Exception as e: |
|
101
|
|
|
click.secho(str(e), fg='red', err=True) |
|
102
|
|
|
click.secho('') |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
1 |
|
def wait_for_finish(action, timeout, title, success_message): |
|
106
|
|
|
click.secho(title, nl=False) |
|
107
|
|
|
|
|
108
|
|
|
waiting = True |
|
109
|
|
|
check_timeout = datetime.now() + timedelta(seconds=timeout) |
|
110
|
|
|
while waiting and datetime.now() < check_timeout: |
|
111
|
|
|
service = action.get_service() |
|
112
|
|
|
if action.is_deployed(service): |
|
113
|
|
|
click.secho('') |
|
114
|
|
|
click.secho(success_message, fg='green') |
|
115
|
|
|
click.secho('') |
|
116
|
|
|
waiting = False |
|
117
|
|
|
elif service.errors: |
|
118
|
|
|
for timestamp in service.errors: |
|
119
|
|
|
click.secho('') |
|
120
|
|
|
click.secho(service.errors[timestamp], fg='red', err=True) |
|
121
|
|
|
click.secho('') |
|
122
|
|
|
exit(1) |
|
123
|
|
|
else: |
|
124
|
|
|
click.secho('.', nl=False) |
|
125
|
|
|
sleep(2) |
|
126
|
|
|
|
|
127
|
|
|
if waiting: |
|
128
|
|
|
click.secho('') |
|
129
|
|
|
click.secho('Scaling failed (timeout)!', fg='red', err=True) |
|
130
|
|
|
click.secho('') |
|
131
|
|
|
|
|
132
|
|
|
click.secho('Older errors') |
|
133
|
|
|
for timestamp in service.older_errors: |
|
134
|
|
|
click.secho('%s\n%s' % (timestamp, service.older_errors[timestamp]), fg='yellow') |
|
135
|
|
|
click.secho('') |
|
136
|
|
|
exit(1) |
|
137
|
|
|
|
|
138
|
|
|
exit(0) |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
1 |
|
def print_diff(task_definition): |
|
142
|
|
|
if task_definition.diff: |
|
143
|
|
|
click.secho('Updating task definition') |
|
144
|
|
|
for diff in task_definition.diff: |
|
145
|
|
|
click.secho(str(diff), fg='blue') |
|
146
|
|
|
click.secho('') |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
1 |
|
main.add_command(deploy) |
|
150
|
1 |
|
main.add_command(scale) |
|
151
|
|
|
|
|
152
|
1 |
|
if __name__ == '__main__': |
|
153
|
|
|
main() |
|
154
|
|
|
|