-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscale_deployment.py
More file actions
55 lines (48 loc) · 1.6 KB
/
Copy pathscale_deployment.py
File metadata and controls
55 lines (48 loc) · 1.6 KB
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
from kubernetes import client, config
import sys
config.load_kube_config()
v1 = client.AppsV1Api()
try:
function = sys.argv[1]
ns = sys.argv[2]
except IndexError:
print(f"Usage: {sys.argv[0]} <stop|start|restart> <namespace> ")
sys.exit(1)
deployment = v1.list_namespaced_deployment(ns)
def open_file(file_method):
if file_method == 'r':
file = open(f"{ns}-replicas.txt", 'r')
elif file_method == 'w':
file = open(f"{ns}-replicas.txt", 'w')
else:
print(f"{file_method} is not supported.")
return file
def stop_deployment(ns):
file_w = open_file("w")
for i in deployment.items:
deployment_info = i.metadata.name + ":" + str(i.status.replicas)
file_w.write(deployment_info+'\n')
file_w.close()
file_r = open_file("r")
for line in file_r:
deployment_name = line.split(":")[0]
scale_response = v1.patch_namespaced_deployment_scale(deployment_name, ns, {'spec': {'replicas': 0}})
print(scale_response.spec)
file_r.close()
def start_deployment(ns):
file_r = open_file("r")
for line in file_r:
deployment_name = line.split(":")[0]
deployment_replicas = int(line.split(":")[1])
scale_response = v1.patch_namespaced_deployment_scale(deployment_name, ns, {'spec': {'replicas': (deployment_replicas)}})
print(scale_response.spec)
file_r.close()
if function == 'stop':
stop_deployment(ns)
elif function == 'start':
start_deployment(ns)
elif function == 'restart':
stop_deployment(ns)
start_deployment(ns)
else:
print(f"Function {function} is not supported.")