From e37b20d30d5549e6c56cd9368e90df1fc0e8628b 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 04:07:06 +0900 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D1=82=D1=8C=20?= =?UTF-8?q?=D0=BF=D0=B0=D1=82=D1=87=20VM=20network=20guard=20=D1=83=D1=81?= =?UTF-8?q?=D1=82=D0=BE=D0=B9=D1=87=D0=B8=D0=B2=D1=8B=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/patch_vm_network_guard.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/scripts/patch_vm_network_guard.py b/scripts/patch_vm_network_guard.py index dbfea73..e35309d 100644 --- a/scripts/patch_vm_network_guard.py +++ b/scripts/patch_vm_network_guard.py @@ -18,11 +18,21 @@ def bridge_exists(name: str) -> bool: ''' if 'def bridge_exists(name: str) -> bool:' not in text: - marker = '\n\ndef default_network_mode() -> str:' - if marker not in text: - raise SystemExit('default_network_mode marker not found') - text = text.replace(marker, helper + marker, 1) - changed.append('bridge_exists helper added') + markers = [ + '\n\ndef default_network_mode() -> str:', + '\n\ndef valid_vm_name(name: str) -> bool:', + '\n\ndef parse_virsh_list() -> list[dict[str, str]]:', + ] + inserted = False + for marker in markers: + if marker in text: + text = text.replace(marker, helper + marker, 1) + changed.append('bridge_exists helper added') + inserted = True + break + if not inserted: + print('WARN: bridge helper marker not found, skip helper injection') + changed.append('bridge_exists helper skipped') else: changed.append('bridge_exists helper already present') @@ -33,7 +43,7 @@ old_validation = ''' elif network_mode == "bridge" and (not bridge or not re. ''' new_validation = ''' elif network_mode == "bridge" and (not bridge or not re.fullmatch(r"[a-zA-Z0-9_.:-]+", bridge)): error = "Некорректное имя bridge." - elif network_mode == "bridge" and not bridge_exists(bridge): + elif network_mode == "bridge" and 'bridge_exists' in globals() and not bridge_exists(bridge): error = f"Bridge {bridge} не найден на сервере. Для VPS выбери режим NAT Router — virtuality-nat, либо сначала создай bridge {bridge}." else: iso = Path(iso_path).resolve() @@ -42,10 +52,11 @@ new_validation = ''' elif network_mode == "bridge" and (not bridge or not re. if old_validation in text: text = text.replace(old_validation, new_validation, 1) changed.append('bridge existence validation added') -elif new_validation in text: +elif 'Bridge {bridge} не найден на сервере' in text: changed.append('bridge existence validation already present') else: - raise SystemExit('bridge validation marker not found') + print('WARN: bridge validation marker not found, skip validation injection') + changed.append('bridge validation skipped') app_path.write_text(text) print('vm network guard patch applied:')