-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod_setup.py
88 lines (77 loc) · 3.11 KB
/
pod_setup.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
from kubernetes import client, config
from kubernetes.client.rest import ApiException
import click
import time
import yaml
import random
import string
import os
@click.command()
@click.option("--pod_yaml", "-p", default="/home/eidf095/eidf095/crae-ml/smartback/my_pod.yaml", type=str)
def main(pod_yaml):
os.system("cd /home/eidf095/eidf095/crae-ml/smartback/; sudo docker build -t=bigballoon8/custom-backprop:latest .; sudo docker push bigballoon8/custom-backprop")
try:
namespace = "eidf095ns"
config.load_kube_config()
v1 = client.CoreV1Api()
with open(pod_yaml, "r") as stream:
pod_def = yaml.safe_load(stream)
base_identifier =''.join(random.choices(string.ascii_lowercase, k=8))
pod_def["metadata"]["generateName"] = f"{pod_def['metadata']['generateName']}{base_identifier}-"
base_pod_name = pod_def["metadata"]["generateName"]
v1.create_namespaced_pod(body=pod_def, namespace=namespace)
pods = v1.list_namespaced_pod(namespace)
pod_names = [pod.metadata.name for pod in pods.items]
# Pod name should have rand string
count = 1
while True:
for pod_name in pod_names:
if base_pod_name in pod_name:
pod = pod_name
break
else:
# Allow 2 minutes
if count == 12:
raise ValueError(f"Pod:{base_pod_name} not found in available pods {pod_names}")
click.echo(f"Pod:{base_pod_name} not found in available pods {pod_names}")
time.sleep(10)
count += 1
continue
break
status = ""
start_idx = 0
while status not in ("Failed", "Succeeded"):
status = v1.read_namespaced_pod(name=pod, namespace=namespace).status.phase
try:
pod_log = v1.read_namespaced_pod_log(name=pod_name, namespace=namespace)
if pod_log[start_idx:]:
click.echo(pod_log[start_idx:])
start_idx = len(pod_log)
except ApiException:
pass
time.sleep(0.5)
time.sleep(4)
try:
pod_log = v1.read_namespaced_pod_log(name=pod_name, namespace=namespace)
if pod_log[start_idx:]:
click.echo(pod_log[start_idx:])
start_idx = len(pod_log)
except ApiException:
pass
pod_log = v1.read_namespaced_pod_log(name=pod_name, namespace=namespace)
click.echo(pod_log[start_idx:])
v1.delete_namespaced_pod(name=pod_name, namespace=namespace)
except KeyboardInterrupt:
while True:
for pod_name in pod_names:
if base_pod_name in pod_name:
pod = pod_name
print(f"Found Pod: {pod}")
break
else:
raise KeyboardInterrupt()
break
v1.delete_namespaced_pod(name=pod_name, namespace=namespace)
raise KeyboardInterrupt()
if __name__ == "__main__":
main()