forked from omerxx/drone-ecs-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecs-deploy.py
188 lines (163 loc) · 6.29 KB
/
ecs-deploy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/python
import os
import boto3
def pp(name):
# pp as in plugin parameter
param = os.environ.get('{}_{}'.format('PLUGIN', name.upper()))
if param:
print param
return param
else:
print 'No such variable {}'.format(name)
return None
def port_handler(paramString):
omap = []
portSets = paramString.split(',')
for set in portSets:
omap.append(
{
'containerPort': int(set.split(' ')[1]),
'hostPort': int(set.split(' ')[0])
}
)
return omap
def env_handler(paramString):
omap = []
envSets = paramString.split(',')
for set in envSets:
value = os.environ.get(set.split('=')[0]) if not set.split('=')[1] and os.environ.get(set.split('=')[0]) else set.split('=')[1]
omap.append(
{
'name': set.split('=')[0],
'value': value
}
)
return omap
def options_handler(paramString):
opts = {}
optSets = paramString.split(',')
for set in optSets:
opts[set.split('=')[0]] = set.split('=')[1]
return opts
def register_task_definition():
try:
taskResponse = client.register_task_definition(
family = pp('family'),
containerDefinitions = [
{
'name': '{}-container'.format((pp('family'))),
'image': '{}:{}'.format(pp('image_name'), pp('image_tag')),
'memory': int(pp('memory')),
'portMappings': port_handler(pp('port_mappings')),
'environment': env_handler(pp('environment_variables')),
'logConfiguration': {
'logDriver': pp('log_driver'),
'options': options_handler(pp('log_options'))
},
"links": [
'{}-container-linked'.format((pp('family')))+":"+pp('link-name')
],
},
{
'name': '{}-container-linked'.format((pp('family'))),
'image': '{}:{}'.format(pp('image_name_linked'), pp('image_tag_linked')),
'memory': int(pp('memory_linked')),
'portMappings': port_handler(pp('port_mappings_linked')),
'environment': env_handler(pp('environment_variables_linked')),
'logConfiguration': {
'logDriver': pp('log_driver_linked'),
'options': options_handler(pp('log_options_linked'))
},
}
]
)
print 'Completing new task registration...'
print taskResponse
print 'Revision: {}'.format(taskResponse['taskDefinition']['revision'])
return taskResponse['taskDefinition']['taskDefinitionArn']
except Exception as e:
print 'Error registring TaskDefinition: {}'.format(e)
exit(1)
def register_task_definition_dockersock():
try:
taskResponse = client.register_task_definition(
family = pp('family'),
containerDefinitions = [
{
'name': '{}-container'.format((pp('family'))),
'image': '{}:{}'.format(pp('image_name'), pp('image_tag')),
'memory': int(pp('memory')),
'portMappings': port_handler(pp('port_mappings')),
'environment': env_handler(pp('environment_variables')),
'logConfiguration': {
'logDriver': pp('log_driver'),
'options': options_handler(pp('log_options'))
},
'mountPoints': [
{
'sourceVolume': 'dockersock',
'containerPath': '/var/run/docker.sock'
},
],
"links": [
'{}-container-linked'.format((pp('family')))+":"+pp('link-name')
],
},
{
'name': '{}-container'.format((pp('family'))),
'image': '{}:{}'.format(pp('image_name_linked'), pp('image_tag_linked')),
'memory': int(pp('memory_linked')),
'portMappings': port_handler(pp('port_mappings_linked')),
'environment': env_handler(pp('environment_variables_linked')),
'logConfiguration': {
'logDriver': pp('log_driver_linked'),
'options': options_handler(pp('log_options_linked'))
},
'mountPoints': [
{
'sourceVolume': 'dockersock',
'containerPath': '/var/run/docker.sock'
}
],
}
],
volumes = [
{
'name': 'dockersock',
'host': {
'sourcePath': '/var/run/docker.sock'
}
},
],
)
print 'Completing new task registration...'
print taskResponse
print 'Revision: {}'.format(taskResponse['taskDefinition']['revision'])
return taskResponse['taskDefinition']['taskDefinitionArn']
except Exception as e:
print 'Error registring TaskDefinition: {}'.format(e)
exit(1)
def update_service(taskArn):
try:
updateResponse = client.update_service(
cluster = pp('cluster'),
service = pp('service'),
taskDefinition = taskArn
)
print 'Completing service update to cluster'
print updateResponse
except Exception as e:
print 'Error updating service: {}'.format(e)
exit(1)
ACCESS_KEY = pp('access_key')
SECRET_KEY = pp('secret_key')
AWS_REGION = pp('region')
if __name__ == "__main__":
global client
client = boto3.client(
'ecs',
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=SECRET_KEY,
region_name=AWS_REGION
)
update_service(register_task_definition()) if not pp('mount_dockersock') else update_service(register_task_definition_dockersock())