-
Notifications
You must be signed in to change notification settings - Fork 0
/
helios.py
417 lines (354 loc) · 11.1 KB
/
helios.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python
'''
Module for wrapping Helios CLI
:maintainer: Christoffer Gunning <[email protected]>
:maturity: new
:depends: helios
:platform: linux
'''
import json
import salt
import logging
from argument_handler import ArgumentHandler
__virtualname__ = 'helios'
log = logging.getLogger(__name__)
valid_kwargs = {
'global': [
'master',
'sites',
'domains',
'srv_name',
'username',
'verbose',
'version',
'json'
],
'create': [
'file',
'template',
'token',
'env_vars',
'ports',
'registration',
'registration_domain',
'grace_period',
'volumes',
'expires',
'exec_check',
'http_check',
'tcp_check',
'security_opt',
'network_mode'
],
'remove': [
'token',
'yes',
'force'
],
'inspect': [],
'deploy': [
'token',
'no_start',
'watch',
'interval'
],
'undeploy': [
'token',
'all',
'yes',
'force'
],
'start': [
'token'
],
'stop': [
'token'
],
'history': [],
'jobs': [
'f',
'q',
'y'
],
'status': [
'job',
'host',
'f'
],
'watch': [
'interval',
'exact'
],
'hosts': [
'q',
'f',
'labels'
],
'register': [],
'deregister': [
'yes',
'force'
],
'masters': [
'f'
],
'create_deployment_group': [
'q'
],
'remove_deployment_group': [],
'list_deployment_groups': [],
'inspect_deployment_group': [],
'deployment_group_status': [
'f'
],
'watch_deployment_group': [
'f',
'interval'
],
'rolling_update': [
'timeout',
'par',
'async',
'rollout_timeout',
'migrate'
],
'stop_deployment_group': [],
'version': [],
}
# TODO - Populate with statuses that means success
success_status = [
'OK',
'CREATED',
'REMOVED',
'NOT_MODIFIED'
]
def __virtual__():
required_cmds = ['helios']
# Iterate over all of the commands this module uses and make sure
# each of them are available in the standard PATH to prevent breakage
for cmd in required_cmds:
if not salt.utils.which(cmd):
log.trace('Could not find helios on system')
return False
return __virtualname__
def kwargs_to_string(method, kwargs):
master = __salt__['config.get']('helios:master') or 'http://localhost:5801'
domains = __salt__['config.get']('helios:domains')
srv_name = __salt__['config.get']('helios:srv_name')
username = __salt__['config.get']('helios:username')
arg_handler = ArgumentHandler(master=master, domains=domains, srv_name=srv_name, username=username)
arg_str = ''
for k,v in kwargs.iteritems():
if "__pub" in k:
continue
if not k in valid_kwargs[method] and not k in valid_kwargs['global']:
return False
arg_str += getattr(arg_handler, k)(v)
return arg_handler.get_global_args() + arg_str
def call(method, args, kwargs):
ret = { 'success': True }
kwargs_string = kwargs_to_string(method, kwargs)
if not kwargs_string:
ret['success'] = False
ret['result'] = {'status': 'INVALID_KWARG'}
ret['errors'] = ['Invalid key/value argument, see docs.']
return ret
arg_string = 'helios ' + method.replace('_', '-') + ' ' + args + kwargs_to_string(method, kwargs)
res = __salt__['cmd.run'](arg_string)
try:
ret['result'] = json.loads(res)
except ValueError:
ret['success'] = False
ret['result'] = {'status': 'REQUEST_FAILED'}
ret['errors'] = [res]
return ret
if 'status' in ret['result'] and ret['result']['status'] not in success_status:
ret['success'] = False
return ret
def create(job_id, image, args, **kwargs):
'''
Create a new helios job
:param job_id: name:version[:hash] of the job
:param image: image, the container image to use
:param args: args, list of command line arguments passed to the container
'''
arg_string = job_id + ' ' + image
for arg in args:
arg_string += ' "' + arg.replace('"', '\\"') + '"'
return call('create', arg_string, kwargs)
def remove(job_id, **kwargs):
'''
Remove a helios job
:param job_id: name:version[:hash] of the job
'''
arg_string = job_id
return call('remove', arg_string, kwargs)
def inspect(job_id, **kwargs):
'''
Inspect a job
:param job_id: name:version[:hash] of the job
'''
arg_string = job_id
return call('inspect', arg_string, kwargs)
# Hosts must be a list
# TODO - How does this work in orchestrate?
def deploy(job_id, hosts, **kwargs):
'''
Deploy a job
:param job_id: name:version[:hash] of the job
'''
arg_string = job_id + ' ' + ' '.join(hosts)
return call('deploy', arg_string, kwargs)
def undeploy(job_id, hosts, **kwargs):
'''
Undeploy a job
:param job_id: name:version[:hash] of the job
:param hosts: a list containing host onto which the job should be deployed
'''
arg_string = job_id + ' ' + ' '.join(hosts)
return call('undeploy', arg_string, kwargs)
def start(job_id, hosts, **kwargs):
'''
Start a stopped job
:param job_id: name:version[:hash] of the job
:param hosts: a list of hosts on which the job should be started
'''
arg_string = job_id + ' ' + ' '.join(hosts)
return call('start', arg_string, kwargs)
def stop(job_id, hosts, **kwargs):
'''
Stop a job
:param job_id: name:version of the job
:param hosts: a list of hosts on which the job should be stopped
'''
arg_string = job_id + ' ' + ' '.join(hosts)
return call('stop', arg_string, kwargs)
def history(job_id, **kwargs):
'''
Show history of a job
:param job_id: name:version[:hash] of the job
'''
arg_string = job_id
return call('history', arg_string, kwargs)
def jobs(pattern='', **kwargs):
'''
List jobs
:param pattern: [optional] Show only jobs matching this pattern
'''
arg_string = pattern
return call('jobs', arg_string, kwargs)
def status(**kwargs):
'''
Show status of a job or a host
:param kwargs: Key/Value arguments. Ether host or job need to be present in order to determine which to show status for
'''
arg_string = ''
return call('status', arg_string, kwargs)
def watch(job_id, hosts, **kwargs):
'''
Watch a job
:param job_id: name:version[:hash] of the job to be watched
:param hosts: A list of hostname prefixes to watch the job on
'''
arg_string = job_id + ' ' + ' '.join(hosts)
return call('watch', arg_string, kwargs)
def hosts(pattern = '', **kwargs):
'''
List hosts
:param pattern: [optional] Only list hosts matchin this pattern
'''
arg_string = pattern
return call('hosts', arg_string, kwargs)
def register(host, id, **kwargs):
'''
Register a host
:param host: The hostname of the agent you want to register with the Helios master
:param id: A unique ID for this host
'''
arg_string = host + ' ' + id
return call('register', arg_string, kwargs)
def deregister(host, **kwargs):
'''
Deregister a host
:param host: The hostname of the agent you want to deregister from the Helios master
'''
arg_string = host
return call('deregister', arg_string, kwargs)
def masters(**kwargs):
'''
List all Helios masters
'''
arg_string = ''
return call('masters', arg_string, kwargs)
# TODO - Check how to handle host_selectors
def create_deployment_group(name, host_selectors = [], **kwargs):
'''
Create a deployment group
:param name: The name of the deployment group
:param host_selectors: [optional] A list of dicts used to select which hosts that should be a part of this deployment-group. This matches against labels and all conditions must be fulfilled.
[{environment: production}, {location: !sweden}] will match hosts having the label environment=production but not hosts having the label location=sweden
'''
arg_string = name
for host_selector in host_selectors:
k = host_selector.keys()[0]
arg_string += ' ' + k
if host_selector[k][1:] == '!':
arg_string += '!'
host_selector[k] = host_selector[k][1:]
arg_string += '=' + host_selector[k]
return call('create_deployment_group', arg_string, kwargs)
def remove_deployment_group(name, **kwargs):
'''
Remove a deployment group
:param name: The name of the deployment group to be removed
'''
arg_string = name
return call('remove_deployment_group', arg_string, kwargs)
def list_deployment_groups(**kwargs):
'''
List deployment groups
'''
arg_string = ''
return call('list_deployment_groups', arg_string, kwargs)
def inspect_deployment_group(name, **kwargs):
'''
Inspect a deployment group
:param name: The name of the deployment group to be inpected
'''
arg_string = name
return call('inspect_deployment_group', arg_string, kwargs)
def deployment_group_status(name, **kwargs):
'''
Show status of a deployment group
:param name: The name of the deployment group
'''
arg_string = name
return call('deployment_group_status', arg_string, kwargs)
def watch_deployment_group(name, **kwargs):
'''
Watch a deployment group
:param name: The name of the deployment group
'''
arg_string = name
return call('watch_deployment_group', arg_string, kwargs)
def rolling_update(job_id, deployment_group_name, **kwargs):
'''
Initiate a rolling update
:param job_id: name:version[:hash] of the job
:param deployment_group_name: The name of the deployment group
'''
arg_string = job_id + ' ' + deployment_group_name
return call('rolling_update', arg_string, kwargs)
def stop_deployment_group(name, **kwargs):
'''
Stop a deployment-group or abort a rolling-update
:param name: The name of the deployment group
'''
arg_string = name
return call('stop_deployment_group', arg_string, kwargs)
def version(**kwargs):
'''
Print version of master and client
'''
arg_string = ''
return call('version', arg_string, kwargs)