From 53af03b474b1ff2dda5ed56c6f89f3cfc1ca35e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80?= <78488229+viktor138irk@users.noreply.github.com> Date: Thu, 7 May 2026 00:22:28 +0900 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D0=B0=D1=82=D1=87=20=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=87=D0=B8=D0=BA=D0=B0=20=D1=81=D0=B5=D1=82?= =?UTF-8?q?=D0=B5=D0=B2=D1=8B=D1=85=20=D1=88=D0=B0=D0=B1=D0=BB=D0=BE=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/patch_network_templates.py | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 scripts/patch_network_templates.py diff --git a/scripts/patch_network_templates.py b/scripts/patch_network_templates.py new file mode 100644 index 0000000..5a5c29a --- /dev/null +++ b/scripts/patch_network_templates.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +from pathlib import Path +import sys + +app_path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path('/opt/virtuality/web/app.py') +if not app_path.exists(): + raise SystemExit(f'app.py not found: {app_path}') + +text = app_path.read_text() + +if 'import network_templates' not in text: + text = text.replace('import network_core\n', 'import network_core\nimport network_templates\n', 1) + +text = text.replace('"ctx": network_core.network_context(), "error": error}', '"ctx": network_core.network_context(), "templates": network_templates.list_templates(), "error": error}', 1) +text = text.replace('"ctx": network_core.network_context(), "error": str(exc)}', '"ctx": network_core.network_context(), "templates": network_templates.list_templates(), "error": str(exc)}') +text = text.replace('"ctx": network_core.network_context(), "error": str(exc)}, status_code=400)', '"ctx": network_core.network_context(), "templates": network_templates.list_templates(), "error": str(exc)}, status_code=400)') +text = text.replace('"ctx": network_core.network_context(), "error": str(exc)}, status_code=500)', '"ctx": network_core.network_context(), "templates": network_templates.list_templates(), "error": str(exc)}, status_code=500)') + +if 'def network_template_apply(' not in text: + handler = ''' + +@app.post("/network/template/apply") +def network_template_apply(request: Request, vm_name: str = Form(...), template_key: str = Form(...), external_base_port: int = Form(0)): + auth_redirect = require_auth(request) + if auth_redirect: + return auth_redirect + try: + network_templates.apply_template(vm_name, template_key, external_base_port if external_base_port > 0 else None) + except NetworkError as exc: + return templates.TemplateResponse("network.html", { + "request": request, + "app_name": APP_NAME, + "user": AUTH_USER, + "vms": parse_virsh_list(), + "ctx": network_core.network_context(), + "templates": network_templates.list_templates(), + "error": str(exc), + }, status_code=400) + return RedirectResponse(url="/network", status_code=303) +''' + marker = '\n\n@app.post("/network/forward/add")' + if marker not in text: + raise SystemExit('forward/add marker not found') + text = text.replace(marker, handler + marker, 1) + +app_path.write_text(text) +print(f'network templates patch applied: {app_path}')