This repository was archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadjust
executable file
·74 lines (62 loc) · 2.76 KB
/
adjust
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
#!/usr/bin/env python3
import sys
import os
import errno
import subprocess
import traceback
import time
import json
import argparse
from client import RancherClient
from client import RancherConfig
class RancherAdjust:
VERSION="0.1"
def __init__(self, *args, **kwargs):
self.config = RancherConfig()
self.client = RancherClient(self.config)
self.parser = argparse.ArgumentParser(description='Adjust Rancher Stack Settings')
self.parser.add_argument('app_id', help='Default stack name to use. Pass a JSON object with settings to stdin in order to update that stack.', default=None)
self.parser.add_argument('--version', help='Print the current driver version', action='store_true')
self.parser.add_argument('--info', help='Print driver version and capabilities.', action='store_true')
self.parser.add_argument('--describe', help='Describe stack configuration.', action='store_true')
self.parser.add_argument('--query', dest='describe', help='Alias for --describe', action='store_true')
self.args = self.parser.parse_args()
# set app_id as stack name if stack not configured
if not self.config.stack: # empty or not defined
self.config.stack = self.args.app_id
def describe(self):
try:
r = self.client.describe(self.config.stack)
except (Exception) as e:
traceback.print_exc(file=sys.stderr)
print(json.dumps({"error":e.__class__.__name__, "class":"failure", "message":str(e)}))
sys.exit(3)
self.client.print(r)
def adjust(self):
data = json.load(sys.stdin)
data = data.get('application', {}).get('components', {})
try:
for servicename in data.keys():
try:
self.client.services(stack_name=self.config.stack, name=servicename, action='upgrade', body=data[servicename])
except PermissionError as e:
print(json.dumps({"error":e.__class__.__name__, "class":"failure", "message":str(e)}))
except Exception as e:
traceback.print_exc(file=sys.stderr)
print(json.dumps({"error":e.__class__.__name__, "class":"failure", "message":str(e)}))
sys.exit(3)
self.client.print(dict(status="ok"))
def run(self):
if self.args.version:
print(self.VERSION)
elif self.args.info:
print(json.dumps({"version":self.VERSION, "has_cancel":True}))
elif self.args.describe:
self.describe()
elif self.args.app_id: # app_id is specified with no options - means adjust
self.adjust()
else:
self.parser.print_help()
if __name__ == "__main__":
adjuster = RancherAdjust()
adjuster.run()