Патчер порядка загрузки существующих VM был защищён

Этот коммит содержится в:
Виктор
2026-05-07 10:01:17 +09:00
родитель 4c80a64d66
Коммит a3fa88d99d
+26 -10
Просмотреть файл
@@ -9,6 +9,7 @@ if not app_path.exists():
app_dir = app_path.resolve().parent app_dir = app_path.resolve().parent
template_path = app_dir / 'templates' / 'vm_detail.html' template_path = app_dir / 'templates' / 'vm_detail.html'
changed = [] changed = []
warnings = []
text = app_path.read_text() text = app_path.read_text()
@@ -94,7 +95,6 @@ def apply_vm_boot_order(name: str, boot_order: str) -> tuple[bool, str]:
if os_node is None: if os_node is None:
os_node = ET.SubElement(root, "os") os_node = ET.SubElement(root, "os")
# Remove only boot-order entries. Keep loader, nvram, type and firmware-related nodes intact.
for boot in list(os_node.findall("boot")): for boot in list(os_node.findall("boot")):
os_node.remove(boot) os_node.remove(boot)
@@ -122,20 +122,22 @@ def apply_vm_boot_order(name: str, boot_order: str) -> tuple[bool, str]:
if 'def apply_vm_boot_order(name: str, boot_order: str)' not in text: if 'def apply_vm_boot_order(name: str, boot_order: str)' not in text:
marker = '\n\ndef vm_details(name: str) -> dict[str, Any]:' marker = '\n\ndef vm_details(name: str) -> dict[str, Any]:'
if marker not in text: if marker in text:
raise SystemExit('vm_details marker not found')
text = text.replace(marker, helpers + marker, 1) text = text.replace(marker, helpers + marker, 1)
changed.append('existing VM boot order helpers added') changed.append('existing VM boot order helpers added')
else:
warnings.append('vm_details marker not found, helpers skipped')
else: else:
changed.append('existing VM boot order helpers already present') changed.append('existing VM boot order helpers already present')
old = '"host_ip": system_summary()["ip"]})' old = '"host_ip": system_summary()["ip"]})'
new = '"host_ip": system_summary()["ip"], "boot_options": vm_boot_order_options(), "current_boot_order": current_vm_boot_order(name), "boot_message": request.query_params.get("boot_message", ""), "boot_error": request.query_params.get("boot_error", "")})' new = '"host_ip": system_summary()["ip"], "boot_options": vm_boot_order_options(), "current_boot_order": current_vm_boot_order(name), "boot_message": request.query_params.get("boot_message", ""), "boot_error": request.query_params.get("boot_error", "")})'
if '"current_boot_order": current_vm_boot_order(name)' not in text: if '"current_boot_order": current_vm_boot_order(name)' not in text:
if old not in text: if old in text:
raise SystemExit('vm_detail context marker not found')
text = text.replace(old, new, 1) text = text.replace(old, new, 1)
changed.append('vm detail context gets boot order') changed.append('vm detail context gets boot order')
else:
warnings.append('vm_detail context marker not found, skipped')
else: else:
changed.append('vm detail context already has boot order') changed.append('vm detail context already has boot order')
@@ -154,10 +156,11 @@ def vm_boot_order_apply(request: Request, name: str, boot_order: str = Form("aut
if '@app.post("/vm/{name}/boot-order")' not in text: if '@app.post("/vm/{name}/boot-order")' not in text:
marker = '\n\n@app.post("/vm/{name}/{action}")' marker = '\n\n@app.post("/vm/{name}/{action}")'
if marker not in text: if marker in text:
raise SystemExit('generic vm action marker not found')
text = text.replace(marker, route + marker, 1) text = text.replace(marker, route + marker, 1)
changed.append('existing VM boot order route added') changed.append('existing VM boot order route added')
else:
warnings.append('generic vm action marker not found, route skipped')
else: else:
changed.append('existing VM boot order route already present') changed.append('existing VM boot order route already present')
@@ -195,14 +198,27 @@ if template_path.exists():
<p class="muted small-note">Настройка меняет XML-конфигурацию VM через virsh define. Если машина сейчас запущена, новый порядок загрузки сработает после перезапуска.</p> <p class="muted small-note">Настройка меняет XML-конфигурацию VM через virsh define. Если машина сейчас запущена, новый порядок загрузки сработает после перезапуска.</p>
</section> </section>
''' '''
marker = '\n\n <section class="grid two">' markers = ['\n\n <section class="grid two">', '\n\n <section class="grid two">', '\n\n <section class="card">']
if marker not in tpl: inserted = False
raise SystemExit('vm detail grid marker not found') for marker in markers:
if marker in tpl:
tpl = tpl.replace(marker, block + marker, 1) tpl = tpl.replace(marker, block + marker, 1)
inserted = True
changed.append('boot order card added to vm_detail.html') changed.append('boot order card added to vm_detail.html')
break
if not inserted:
warnings.append('vm detail insertion marker not found, boot order card skipped')
else:
changed.append('boot order card already present in vm_detail.html')
if tpl != original: if tpl != original:
template_path.write_text(tpl) template_path.write_text(tpl)
else:
warnings.append(f'vm_detail.html not found: {template_path}')
print('existing VM boot order patch applied:') print('existing VM boot order patch applied:')
for item in changed: for item in changed:
print(f'- {item}') print(f'- {item}')
if warnings:
print('Warnings:')
for item in warnings:
print(f'- {item}')