-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_finished.py
81 lines (68 loc) · 2.63 KB
/
check_finished.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
import asyncio
import datetime
import time
import config
import db
import labelprinter.draw
import labelprinter.printer
from db import Switch, SwitchStatus
async def ip_responds_to_ping_async(host):
ping_process = await asyncio.create_subprocess_exec(
"ping",
"-c",
"1",
"-I",
config.ztp_interface,
host,
stdout=asyncio.subprocess.DEVNULL,
stderr=asyncio.subprocess.DEVNULL,
)
return_code = await ping_process.wait()
return return_code == 0
async def main():
while True:
with db.Session() as session:
# Check for successful config retrieval
switches = (
session.query(Switch)
.filter(Switch.status == SwitchStatus.DHCP_SUCCESS)
.all()
)
download_success = "Download file successfully."
rebooting = "After 0 seconds activation will be performed."
for sw in switches:
if any(download_success in le.msg for le in sw.syslog_entries) and any(
rebooting in le.msg for le in sw.syslog_entries
):
sw.status = SwitchStatus.REBOOTING
session.commit()
switches = (
session.query(Switch)
.filter(Switch.status != SwitchStatus.FINISHED)
.filter(Switch.name != None)
.filter(Switch.final_ip != None)
.all()
)
ping_tasks = [ip_responds_to_ping_async(sw.final_ip) for sw in switches]
ping_results = await asyncio.gather(*ping_tasks)
for ping_successful, switch in zip(ping_results, switches):
if ping_successful:
try:
if config.use_labelprinter:
print(f"Printing qr-label for {switch.name}...")
imgsurf = labelprinter.draw.render_small_label(switch.name)
labelprinter.printer.print_to_ip(
imgsurf, config.labelprinter_hostname
)
except Exception as e:
print(
f"Printing qr-label failed for {switch.name}: {e}. Please fix the printer."
)
continue
finally:
switch.status = SwitchStatus.FINISHED
switch.finished_date = datetime.datetime.now()
session.commit()
time.sleep(10)
if __name__ == "__main__":
asyncio.run(main())