From 8f73f92a967f3fc7cb19925d99c9ebf8e34b512a Mon Sep 17 00:00:00 2001 From: Khue Doan Date: Thu, 12 Nov 2020 23:37:48 +0700 Subject: [PATCH] Check if node alive or not --- infra/scripts/reset.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/infra/scripts/reset.py b/infra/scripts/reset.py index bbbdc498..37c0158e 100755 --- a/infra/scripts/reset.py +++ b/infra/scripts/reset.py @@ -28,20 +28,28 @@ nodes = [ } ] -def poweroff(nodes): - for node in nodes: +def is_alive(node): + return os.system(f"ping -c 1 {node['ip']}") == 0 + +def poweroff(node): + if is_alive(node): print(f"Poweroff {node['hostname']}") os.system(f"ssh {user}@{node['ip']} poweroff") + else: + print(f"Node {node['hostname']} is already dead!") def wake(nodes): - for node in nodes: - print(f"Waking up {node['hostname']}") - os.system(f"wol {node['mac']}") + print(f"Waking up {node['hostname']}") + os.system(f"wol {node['mac']}") if __name__ == "__main__": os.chdir(f"./infra/pxe-server") os.system(f"docker-compose up -d --build") - poweroff(nodes) + for node in nodes: + poweroff(node) + time.sleep(10) - wake(nodes) + + for node in nodes: + wake(node)