Skip to content

Commit 3647851

Browse files
author
Istemi Ekin Akkus
committed
ansible: fix available hosts script
1 parent 6913fbb commit 3647851

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

deploy/ansible/scripts/available_hosts.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def add_host(hostname,hostip=None):
6767
hasGPU = False
6868
# get environment of current hostname
6969
if os.environ['KNIX_node_hasGPU'] == "True":
70-
print("found GPU Environent: " +str(os.environ['KNIX_node_hasGPU']) )
70+
print("found GPU environment: " +str(os.environ['KNIX_node_hasGPU']) )
7171
hasGPU = True
7272

7373
v = dl_get("available_hosts")
@@ -76,17 +76,27 @@ def add_host(hostname,hostip=None):
7676
print("existing hosts: " + str(hosts))
7777
if isinstance(hosts,list):
7878
hosts = {host: socket.gethostbyname(host) for host in hosts}
79-
80-
hosts['has_gpu'] = hasGPU
81-
8279
else:
8380
hosts = {}
84-
if hostname != None and hostname not in hosts:
85-
hosts[hostname] = hostip
86-
if hasGPU == True:
87-
hosts["has_gpu"] = hasGPU
88-
v.encoded_data = json.dumps(hosts).encode()
89-
v.store()
81+
82+
cur_entry2 = {}
83+
84+
if hostname is not None and hostname in hosts:
85+
cur_entry = hosts[hostname]
86+
if isinstance(cur_entry, str):
87+
hostip = cur_entry
88+
del hosts[hostname]
89+
elif isinstance(cur_entry, dict):
90+
cur_entry2 = cur_entry
91+
92+
cur_entry2["ip"] = hostip
93+
cur_entry2["has_gpu"] = hasGPU
94+
95+
hosts[hostname] = cur_entry2
96+
97+
v.encoded_data = json.dumps(hosts).encode()
98+
v.store()
99+
90100
print("found hosts: " + str(hosts))
91101
return hosts
92102

@@ -100,10 +110,13 @@ def remove_host(hostname):
100110
hosts = {host: socket.gethostbyname(host) for host in hosts}
101111
else:
102112
hosts = {}
113+
103114
if hostname != None and hostname in hosts:
104115
del hosts[hostname]
105116
v.encoded_data = json.dumps(hosts).encode()
106117
v.store()
118+
119+
print("found hosts: " + str(hosts))
107120
return hosts
108121

109122

@@ -114,22 +127,24 @@ def remove_host(hostname):
114127
workflowid = "Management"
115128
hosts = []
116129
set_bucket_name(sandboxid,workflowid)
117-
try:
118-
host=defaulthost
119-
if len(sys.argv) > 2:
120-
host = sys.argv[2]
121-
if sys.argv[1] == "add":
122-
hosts = add_host(host)
123-
elif sys.argv[1] == "remove":
124-
hosts = remove_host(host)
125-
else:
126-
raise Exception()
127-
except Exception as e:
128-
print(e)
130+
131+
host=defaulthost
132+
if len(sys.argv) > 2:
133+
host = sys.argv[2]
134+
135+
if len(sys.argv) <= 1:
129136
print("usage: python "+sys.argv[0]+" [add|remove] (<hostname>)")
130137
print(" optional <hostname> defaults to %s" % defaulthost)
138+
sys.exit(1)
139+
140+
if sys.argv[1] == "add":
141+
hosts = add_host(host)
142+
elif sys.argv[1] == "remove":
143+
hosts = remove_host(host)
144+
else:
131145
v = dl_get("available_hosts")
132146
if v.encoded_data is not None and len(v.encoded_data) > 0:
133147
hosts = json.loads((v.encoded_data).decode())
134-
print("Current available_hosts=" + str(hosts))
148+
149+
print("Current available_hosts=" + str(hosts))
135150

0 commit comments

Comments
 (0)