-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait-for-ifs.py
77 lines (61 loc) · 1.99 KB
/
wait-for-ifs.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
#!/usr/bin/python3
import json
import os
import sys
import subprocess
from time import sleep
from signal import signal,SIGINT
polling_wait = 2 # wait between checks, in seconds
name = os.path.basename(sys.argv[0])
ifaces = sys.argv[1:]
def usage():
print(f"usage: {name} <iface1> [<iface2> ...]")
def sigint(signum, frame):
print("caught SIGINT, exiting")
exit(0)
def check_if_state():
ready = True
try:
proc = subprocess.run([ '/sbin/ip', '-j', 'addr' ], check=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
except subprocess.CalledProcessError as e:
print(f'got non-zero return code executing ip command {proc.returncode}, {str(e)}')
exit(1)
except FileNotFoundError as e:
print(f'could not find the ip command, {str(e)}')
ip = json.loads(proc.stdout)
id = dict()
for i in ip:
id[i['ifname']] = i
violations = []
for i in ifaces:
if i not in id.keys():
ready = False
violations.append(f"{i} is not available")
continue
if id[i]['operstate'] != "UP":
ready = False
violations.append(f"{i} is present but link is not up")
continue
found = False
for a in id[i]['addr_info']:
if a["family"] == "inet":
found = True
break
if not found:
ready = False
violations.append(f"{i} is does not have an ipv4 address")
return ready, violations
def main():
if len(ifaces) == 0:
usage()
exit(1)
signal(SIGINT, sigint)
print(f"waiting for ifaces [{', '.join(ifaces)}]")
ready, violations = check_if_state()
while not ready:
print(f"ifaces [{', '.join(ifaces)}] are not ready, sleeping {polling_wait}s: [{'; '.join(violations)}]")
sleep(polling_wait)
ready, violations = check_if_state()
print(f"ifaces [{', '.join(ifaces)}] are ready, exiting")
if __name__ == "__main__":
main()