Enhance update_system.py with timestamp logging for better tracking of system updates
This commit is contained in:
@@ -1,15 +1,45 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
def generate_timestamp(fmt: Optional[str] = None, utc: bool = False) -> str:
|
||||||
|
"""Return a timestamp string.
|
||||||
|
|
||||||
|
- If `fmt` is provided, return `datetime.strftime(fmt)`.
|
||||||
|
- If `fmt` is None, return an ISO 8601 timestamp. When `utc` is True
|
||||||
|
the timestamp ends with 'Z' (UTC). When `utc` is False it returns
|
||||||
|
the local time ISO format.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
- `generate_timestamp()` -> '2025-11-20T12:34:56Z'
|
||||||
|
- `generate_timestamp("%Y-%m-%d %H:%M:%S")` -> '2025-11-20 12:34:56'
|
||||||
|
"""
|
||||||
|
if fmt:
|
||||||
|
dt = datetime.now(timezone.utc) if utc else datetime.now()
|
||||||
|
return dt.strftime(fmt)
|
||||||
|
|
||||||
|
dt = datetime.now(timezone.utc) if utc else datetime.now()
|
||||||
|
if utc:
|
||||||
|
# force trailing Z for UTC instead of +00:00
|
||||||
|
return dt.replace(tzinfo=timezone.utc).isoformat().replace('+00:00', 'Z')
|
||||||
|
return dt.isoformat()
|
||||||
|
|
||||||
|
print(f"Starting system update at {generate_timestamp()}")
|
||||||
# Update package lists
|
# Update package lists
|
||||||
|
print(f"{generate_timestamp()}: Updating package lists...")
|
||||||
subprocess.run(['sudo', 'apt', 'update'], check=True)
|
subprocess.run(['sudo', 'apt', 'update'], check=True)
|
||||||
# Upgrade packages
|
# Upgrade packages
|
||||||
|
print(f"{generate_timestamp()}: Upgrading packages...")
|
||||||
subprocess.run(['sudo', 'apt', 'upgrade', '-y'], check=True)
|
subprocess.run(['sudo', 'apt', 'upgrade', '-y'], check=True)
|
||||||
|
|
||||||
# Check if reboot is required
|
# Check if reboot is required
|
||||||
if os.path.exists('/var/run/reboot-required'):
|
if os.path.exists('/var/run/reboot-required'):
|
||||||
print("Reboot required. Rebooting now...")
|
print(f"{generate_timestamp()}: Reboot required. Rebooting now...")
|
||||||
subprocess.run(['sudo', 'reboot'])
|
subprocess.run(['sudo', 'reboot'])
|
||||||
else:
|
else:
|
||||||
print("No reboot required.")
|
print(f"{generate_timestamp()}: No reboot required.")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user