Сделать патч VM network guard устойчивым

Этот коммит содержится в:
Виктор
2026-05-07 04:07:06 +09:00
родитель b329eb2ee5
Коммит e37b20d30d
+19 -8
Просмотреть файл
@@ -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:')