This repository has been archived by the owner on Oct 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
services.py
executable file
·546 lines (435 loc) · 18.3 KB
/
services.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#!/usr/bin/env python
import baker
import json
import requests
import sys
import time
import websocket
import base64
from BaseHTTPServer import HTTPServer
HOST = "http://rancher.local:8080/v1"
URL_SERVICE = "/services/"
URL_ENVIRONMENT = "/projects/"
USERNAME = "userid"
PASSWORD = "password"
kwargs = {}
# HTTP
def get(url):
r = requests.get(url, auth=(USERNAME, PASSWORD), **kwargs)
r.raise_for_status()
return r
def post(url, data=""):
if data:
r = requests.post(url, data=json.dumps(data), auth=(USERNAME, PASSWORD), **kwargs)
else:
r = requests.post(url, data="", auth=(USERNAME, PASSWORD), **kwargs)
r.raise_for_status()
return r.json()
def delete(url, data=""):
if data:
r = requests.delete(url, data=json.dumps(data), auth=(USERNAME, PASSWORD), **kwargs)
else:
r = requests.delete(url, data="", auth=(USERNAME, PASSWORD), **kwargs)
r.raise_for_status()
return r.json()
# Websocket
def ws(url):
webS = websocket.create_connection(url)
resp = base64.b64decode( webS.recv() )
webS.close()
return resp
# Helper
def print_json(data):
print json.dumps(data, sort_keys=True, indent=3, separators=(',', ': '))
#
# Query the service configuration.
#
@baker.command(default=True, params={"service_id": "The ID of the service to read (optional)"})
def query(service_id=""):
"""Retrieves the service information.
If you don't specify an ID, data for all services
will be retrieved.
"""
r = get(HOST + URL_SERVICE + service_id)
print_json(r.json())
#
# Converts a service name into an ID
#
@baker.command(params={
"name": "The name of the service to lookup.",
"newest": "From list of IDs, return newest (optional)"})
def id_of (name="", newest=False):
"""Retrieves the ID of a service, given its name.
"""
if newest:
index = -1
else:
index = 0
service = get(HOST + "/services?name=" + name).json()
return service['data'][index]['id']
#
# Converts a environment name into an ID
#
@baker.command(params={"name": "The name of the environment to lookup."})
def id_of_env (name=""):
"""Retrieves the ID of a project, given its name.
"""
environment = get(HOST + "/project?name=" + name).json()
return environment['data'][0]['id']
#
# Start containers within a service (e.g. for Start Once containers).
#
@baker.command(params={"service_id": "The ID of the service to start the containers of."})
def start_containers (service_id):
"""Starts the containers of a given service, typically a Start Once service.
"""
start_service (service_id)
#
# Start containers within a service (e.g. for Start Once containers).
#
@baker.command(params={"service_id": "The ID of the service to start the containers of."})
def start_service (service_id):
"""Starts the containers of a given service, typically a Start Once service.
"""
# Get the array of containers
containers = get(HOST + URL_SERVICE + service_id + "/instances").json()['data']
for container in containers:
start_url = container['actions']['start']
print "Starting container %s with url %s" % (container['name'], start_url)
post(start_url, "")
#
# Stop containers within a service.
#
@baker.command(params={"service_id": "The ID of the service to stop the containers of."})
def stop_service (service_id):
"""Stop the containers of a given service.
"""
# Get the array of containers
containers = get(HOST + URL_SERVICE + service_id + "/instances").json()['data']
for container in containers:
stop_url = container['actions']['stop']
print "Stopping container %s with url %s" % (container['name'], stop_url)
post(stop_url, "")
#
# Restart containers within a service
#
@baker.command(params={"service_id": "The ID of the service to restart the containers of."})
def restart_service(service_id):
"""Restart the containers of a given service.
"""
# Get the array of containers
containers = get(HOST + URL_SERVICE + service_id + "/instances").json()['data']
for container in containers:
restart_url = container['actions']['restart']
print "Restarting container: " + container['name']
post(restart_url)
#
# Upgrades the service.
#
@baker.command(params={
"service_id": "The ID of the service to upgrade.",
"start_first": "Whether or not to start the new instance first before stopping the old one.",
"complete_previous": "If set and the service was previously upgraded but the upgrade wasn't completed, it will be first marked as Finished and then the upgrade will occur.",
"imageUuid": "If set the config will be overwritten to use new image. Don't forget Rancher Formatting 'docker:<Imagename>:tag'",
"auto_complete": "Set this to automatically 'finish upgrade' once upgrade is complete",
"replace_env_name": "The name of an environment variable to be changed in the launch config (requires replace_env_value).",
"replace_env_value": "The value of the environment variable to be replaced (requires replace_env_name).",
"timeout": "How many seconds to wait until an upgrade fails"
})
def upgrade(service_id, start_first=True, complete_previous=False, imageUuid=None, auto_complete=False,
batch_size=1, interval_millis=10000, replace_env_name=None, replace_env_value=None, timeout=60):
"""Upgrades a service
Performs a service upgrade, keeping the same configuration, but otherwise
pulling new image as needed and starting new containers, dropping the old
ones.
"""
upgrade_strategy = json.loads('{"inServiceStrategy": {"batchSize": 1,"intervalMillis": 10000,"startFirst": true,"launchConfig": {},"secondaryLaunchConfigs": []}}')
upgrade_strategy['inServiceStrategy']['batchSize'] = batch_size
upgrade_strategy['inServiceStrategy']['intervalMillis'] = interval_millis
if start_first:
upgrade_strategy['inServiceStrategy']['startFirst'] = "true"
else:
upgrade_strategy['inServiceStrategy']['startFirst'] = "false"
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
# complete previous upgrade flag on
if complete_previous and current_service_config['state'] == "upgraded":
print "Previous service upgrade wasn't completed, completing it now..."
post(HOST + URL_SERVICE + service_id + "?action=finishupgrade", "")
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
sleep_count = 0
while current_service_config['state'] != "active" and sleep_count < timeout // 2:
print "Waiting for upgrade to finish..."
time.sleep (2)
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
sleep_count += 1
# can't upgrade a service if it's not in active state
if current_service_config['state'] != "active":
print "Service cannot be updated due to its current state: %s" % current_service_config['state']
sys.exit(1)
# Stuff the current service launch config into the request for upgrade
upgrade_strategy['inServiceStrategy']['launchConfig'] = current_service_config['launchConfig']
# replace the environment variable specified (if one was)
if replace_env_name != None and replace_env_value != None:
print "Replacing environment variable %s from %s to %s" % (replace_env_name, upgrade_strategy['inServiceStrategy']['launchConfig']['environment'][replace_env_name], replace_env_value)
upgrade_strategy['inServiceStrategy']['launchConfig']['environment'][replace_env_name] = replace_env_value
if imageUuid != None:
# place new image into config
upgrade_strategy['inServiceStrategy']['launchConfig']['imageUuid'] = imageUuid
print "New Image: %s" % upgrade_strategy['inServiceStrategy']['launchConfig']['imageUuid']
# post the upgrade request
post(current_service_config['actions']['upgrade'], upgrade_strategy)
print "Upgrade of %s service started!" % current_service_config['name']
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
print "Service State '%s.'" % current_service_config['state']
print "Waiting for upgrade to finish..."
sleep_count = 0
while current_service_config['state'] != "upgraded" and sleep_count < timeout // 2:
print "."
time.sleep (2)
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
sleep_count += 1
if sleep_count >= timeout // 2:
print "Upgrading take to much time! Check Rancher UI for more details."
sys.exit(1)
else:
print "Upgraded"
if auto_complete and current_service_config['state'] == "upgraded":
post(HOST + URL_SERVICE + service_id + "?action=finishupgrade", "")
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
print "Auto Finishing Upgrade..."
upgraded_sleep_count = 0
while current_service_config['state'] != "active" and upgraded_sleep_count < timeout // 2:
print "."
time.sleep (2)
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
upgraded_sleep_count += 1
if current_service_config['state'] == "active":
print "DONE"
else:
print "Something has gone wrong! Check Rancher UI for more details."
sys.exit(1)
#
# Execute remote command on container.
#
@baker.command(params={
"service_id": "The ID of the service to execute on",
"command": "The command to execute"
})
def execute(service_id,command):
"""Execute remote command
Executes a command on one container of the service you specified.
"""
# Get the array of containers
containers = get(HOST + URL_SERVICE + service_id + "/instances").json()['data']
# guard we have at least one container available
if len(containers) <= 0:
print "No container available"
sys.exit(1)
# take the first (random) container to execute the command on
execution_url = containers[0]['actions']['execute']
print "Executing '%s' on container '%s'" % (command, containers[0]['name'])
# prepare post payload
payload = json.loads('{"attachStdin": true,"attachStdout": true,"command": ["/bin/sh","-c"],"tty": true}')
payload['command'].append(command)
# call execution action -> returns token and url for websocket access
intermediate = post(execution_url,payload)
ws_token = intermediate['token']
ws_url = intermediate['url'] + "?token=" + ws_token
# call websocket and print answer
print "> \n%s" % ws(ws_url)
print "DONE"
#
# Rollback the service.
#
@baker.command(params={
"service_id": "The ID of the service to rollback.",
"timeout": "How many seconds to wait until an rollback fails"
})
def rollback(service_id, timeout=60):
"""Performs a service rollback
"""
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
# can't rollback a service if it's not in upgraded state
if current_service_config['state'] != "upgraded":
print "Service cannot be updated due to its current state: %s" % current_service_config['state']
sys.exit(1)
# post the rollback request
post(current_service_config['actions']['rollback'], "");
print "Rollback of %s service started!" % current_service_config['name']
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
print "Service State '%s.'" % current_service_config['state']
print "Waiting for rollback to finish..."
sleep_count = 0
while current_service_config['state'] != "active" and sleep_count < timeout // 2:
print "."
time.sleep (2)
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
sleep_count += 1
if sleep_count >= timeout // 2:
print "Rolling back take to much time! Check Rancher UI for more details."
sys.exit(1)
else:
print "Rolled back"
#
# Activate a service.
#
@baker.command(params={"service_id": "The ID of the service to activate.",
"timeout": "How many seconds to wait until an upgrade fails"})
def activate (service_id, timeout=60):
"""Activate the containers of a given service.
"""
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
# can't activate a service if it's not in inactive state
if current_service_config['state'] != "inactive":
print "Service cannot be deactivated due to its current state: %s" % current_service_config['state']
sys.exit(1)
post(current_service_config['actions']['activate'], "");
# Wait Activation to finish
sleep_count = 0
while current_service_config['state'] != "active" and sleep_count < timeout // 2:
print "Waiting for activation to finish..."
time.sleep (2)
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
sleep_count += 1
#
# Deactivate a service.
#
@baker.command(params={"service_id": "The ID of the service to deactivate.",
"timeout": "How many seconds to wait until an upgrade fails"})
def deactivate (service_id, timeout=60):
"""Stops the containers of a given service. (e.g. for maintenance purposes)
"""
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
# can't deactivate a service if it's not in active state
if current_service_config['state'] != "active" and current_service_config['state'] != "updating-active":
print "Service cannot be deactivated due to its current state: %s" % current_service_config['state']
sys.exit(1)
post(current_service_config['actions']['deactivate'], "");
# Wait deactivation to finish
sleep_count = 0
while current_service_config['state'] != "inactive" and sleep_count < timeout // 2:
print "Waiting for deactivation to finish..."
time.sleep (2)
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
sleep_count += 1
#
# Deactivate a env.
#
@baker.command(params={"environment_id": "The ID of the environment to deactivate.",
"timeout": "How many seconds to wait until an upgrade fails"})
def deactivate_env (environment_id, timeout=60):
"""Stops the environment
"""
r = get(HOST + URL_ENVIRONMENT + environment_id )
current_environment_config = r.json()
# can't deactivate a service if it's not in active state
if current_environment_config['state'] != "active":
print "Environment cannot be deactivated due to its current state: %s" % current_environment_config['state']
sys.exit(1)
post(current_environment_config['actions']['deactivate'], "");
# Wait deactivation to finish
sleep_count = 0
while current_environment_config['state'] != "inactive" and sleep_count < timeout // 2:
print "Waiting for deactivation to finish..."
time.sleep (2)
r = get(HOST + URL_ENVIRONMENT + environment_id)
current_environment_config = r.json()
sleep_count += 1
#
# Delete a env.
#
@baker.command(params={"environment_id": "The ID of the environment to delete.",
"timeout": "How many seconds to wait until an upgrade fails"})
def delete_env (environment_id, timeout=60):
"""Stops the environment
"""
r = get(HOST + URL_ENVIRONMENT + environment_id )
current_environment_config = r.json()
# can't deactivate a service if it's not in active state
if current_environment_config['state'] != "inactive":
print "Environment cannot be deactivated due to its current state: %s" % current_environment_config['state']
sys.exit(1)
delete(current_environment_config['actions']['delete'], "");
# Wait deactivation to finish
sleep_count = 0
while current_environment_config['state'] != "removed" and sleep_count < timeout // 2:
print "Waiting for delete to finish..."
time.sleep (2)
r = get(HOST + URL_ENVIRONMENT + environment_id)
current_environment_config = r.json()
sleep_count += 1
#
# Remove a service.
#
@baker.command(params={"service_id": "The ID of the service to remove.",
"timeout": "How many seconds to wait until an upgrade fails"})
def remove (service_id, timeout=60):
"""Remove the service
"""
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
# can't remove a service if it's not in inactive state
if current_service_config['state'] != "inactive":
print "Service cannot be removed due to its current state: %s" % current_service_config['state']
sys.exit(1)
post(current_service_config['actions']['remove'], "");
# Wait remove to finish
sleep_count = 0
while current_service_config['state'] != "removed" and sleep_count < timeout // 2:
print "Waiting for remove to finish..."
time.sleep (2)
r = get(HOST + URL_SERVICE + service_id)
current_service_config = r.json()
sleep_count += 1
#
# Get a service state
#
@baker.command(default=True, params={"service_id": "The ID of the service to read"})
def state(service_id=""):
"""Retrieves the service state information.
"""
r = get(HOST + URL_SERVICE + service_id)
print(r.json()["state"])
#
# Script's entry point, starts Baker to execute the commands.
# Attempts to read environment variables to configure the program.
#
if __name__ == '__main__':
import os
# support for new Rancher agent services
# http://docs.rancher.com/rancher/latest/en/rancher-services/service-accounts/
if 'CATTLE_ACCESS_KEY' in os.environ:
USERNAME = os.environ['CATTLE_ACCESS_KEY']
if 'CATTLE_SECRET_KEY' in os.environ:
PASSWORD = os.environ['CATTLE_SECRET_KEY']
if 'CATTLE_URL' in os.environ:
HOST = os.environ['CATTLE_URL']
if 'RANCHER_ACCESS_KEY' in os.environ:
USERNAME = os.environ['RANCHER_ACCESS_KEY']
if 'RANCHER_SECRET_KEY' in os.environ:
PASSWORD = os.environ['RANCHER_SECRET_KEY']
if 'RANCHER_URL' in os.environ:
HOST = os.environ['RANCHER_URL']
if 'SSL_VERIFY' in os.environ:
if os.environ['SSL_VERIFY'].lower() == "false":
kwargs['verify'] = False
else:
kwargs['verify'] = os.environ['SSL_VERIFY']
# make sure host ends with v1 if it is not contained in host
if '/v1' not in HOST:
HOST = HOST + '/v1'
baker.run()