Files
homelab/automations/updates/main.py
ducoterra f2015e2c71
All checks were successful
Podman DDNS Image / build-and-push-ddns (push) Successful in 1m3s
checkpoint commit
2026-05-05 06:26:40 -04:00

65 lines
2.0 KiB
Python

import subprocess
import time
import yaml
from tqdm import tqdm
def main():
print("Retrieving hosts")
with open("ansible/inventory.yaml", "r") as f:
all_hosts = yaml.load(f, yaml.SafeLoader)
fedora_hosts = all_hosts["fedora"]["hosts"].keys()
with open("update.log", "w") as log_file:
for _, host in enumerate(tqdm(fedora_hosts, desc="Running system updates")):
log_file.write(f"Updating {host}\n")
log_file.flush()
try:
subprocess.run(
["ssh", host, "dnf", "upgrade", "-y"],
stdout=log_file,
stderr=log_file,
check=True,
)
except Exception as e:
log_file.write(f"Couldn't connect to {host}. Skipping...\n")
continue
log_file.flush()
log_file.write(f"Rebooting {host}\n")
log_file.flush()
subprocess.run(
["ssh", host, "reboot"],
stdout=log_file,
stderr=log_file,
check=True,
)
time.sleep(5) # wait for reboot to take effect
booted = False
max_attempts = 10
cur_attempts = 0
while max_attempts > cur_attempts and not booted:
try:
subprocess.run(
["ssh", host, "echo"],
stdout=log_file,
stderr=log_file,
check=True,
timeout=2,
)
log_file.write(f"{host} booted!\n")
log_file.flush()
booted = True
except Exception as e:
cur_attempts += 1
log_file.write(f"Waiting for {host} to reboot\n")
log_file.flush()
time.sleep(5)
if cur_attempts >= max_attempts:
exit(1)
if __name__ == "__main__":
main()