diff --git a/scripts/patch_dhcp_leases_empty.py b/scripts/patch_dhcp_leases_empty.py index 61ddff9..5a7b0ec 100644 --- a/scripts/patch_dhcp_leases_empty.py +++ b/scripts/patch_dhcp_leases_empty.py @@ -4,18 +4,22 @@ import re import sys app_path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path('/opt/virtuality/web/app.py') +changed: list[str] = [] +warnings: list[str] = [] + if not app_path.exists(): - raise SystemExit(f'app.py not found: {app_path}') + print(f'WARN: app.py not found: {app_path}') + raise SystemExit(0) dashboard_template_path = app_path.parent / 'templates' / 'dashboard.html' if not dashboard_template_path.exists(): - raise SystemExit(f'dashboard.html not found: {dashboard_template_path}') + print(f'WARN: dashboard.html not found: {dashboard_template_path}') + raise SystemExit(0) -changed: list[str] = [] text = app_path.read_text(encoding='utf-8') -# Remove helper blocks injected by older versions of this patch. They were too invasive -# and could leave stale references on servers that had several hotfixes applied. +# Older versions of this patch injected helper blocks. Remove them first so repeated +# installs stay deterministic and do not accumulate stale NETWORK_NAME references. text, removed_helpers = re.subn( r"\n\ndef parse_dhcp_leases_output\(output: str\).*?\n\ndef parse_virsh_list\(\) -> list\[dict\[str, str\]\]:", "\n\ndef parse_virsh_list() -> list[dict[str, str]]:", @@ -68,35 +72,42 @@ text, replaced = re.subn( count=1, flags=re.S, ) -if not replaced: - raise SystemExit('parse_virsh_list block not found') -changed.append('parse_virsh_list was replaced with a safe inline IP resolver') +if replaced: + changed.append('parse_virsh_list was replaced with a safe inline IP resolver') +else: + warnings.append('parse_virsh_list block not found, app.py IP injection skipped') app_path.write_text(text, encoding='utf-8') dashboard_html = dashboard_template_path.read_text(encoding='utf-8') + +header_old = 'IDNameStateActions' +header_new = 'IDNameIPStateActions' +name_cell = '''{{ vm.name }} + {{ vm.name }} + {{ vm.ip|default("—") }} + IP' not in dashboard_html: - dashboard_html = dashboard_html.replace( - 'IDNameStateActions', - 'IDNameIPStateActions' - ) - dashboard_html = dashboard_html.replace( - '{{ vm.name }}\n {{ vm.name }}\n {{ vm.ip|default(\'—\') }}\n Виртуальных машин пока нет', 'colspan="5" class="muted">Виртуальных машин пока нет') changed.append('Dashboard VM IP column was added') -elif "vm.ip" not in dashboard_html: - dashboard_html = dashboard_html.replace( - '{{ vm.name }}\n {{ vm.name }}\n {{ vm.ip|default(\'—\') }}\n