feat(lxc): support reinstall for LXC created from CT template file

Previously the reinstall endpoint required cached.template_id (clone-from-VM-
template flow), so LXC instances created via the wizard from an OS template
file (e.g. local:vztmpl/debian-13-...tar.zst) returned 'VM has no associated
template; reinstall is not available'.

Now reinstall works in two modes:
  1) clone-from-template (existing): VM/LXC with cached.template_id pointing
     to OSTemplate.vmid
  2) ostemplate-file (new, LXC only): cached.template_name like
     'storage:vztmpl/...tar.zst'. Reinstall stops, deletes, then re-creates
     the container with the same VMID using create_lxc_container, preserving
     network config, storage, rootfs size, swap, features, ssh_keys, etc.
     pulled from the previous container config + VMInstance cache.
This commit is contained in:
markmorado
2026-05-06 20:11:05 +02:00
parent daeddbc6c7
commit aba2eb3583
2 changed files with 189 additions and 50 deletions

View File

@@ -409,16 +409,24 @@ def reinstall_vm_endpoint(
VMInstance.vmid == vmid,
VMInstance.deleted_at.is_(None),
).first()
if not cached or not cached.template_id:
raise HTTPException(status_code=400, detail="VM has no associated template; reinstall is not available")
if not cached:
raise HTTPException(status_code=404, detail="VM not found")
tpl = db.query(OSTemplate).filter(OSTemplate.id == cached.template_id).first()
if not tpl or not tpl.vmid:
raise HTTPException(status_code=400, detail="Template not found or invalid")
is_lxc = (cached.vm_type == 'lxc')
tpl = None
if cached.template_id:
tpl = db.query(OSTemplate).filter(OSTemplate.id == cached.template_id).first()
if not tpl or not tpl.vmid:
raise HTTPException(status_code=400, detail="Template not found or invalid")
elif is_lxc and cached.template_name and ':' in cached.template_name:
# LXC created from CT template file (e.g. local:vztmpl/debian-13-...tar.zst)
tpl = None
else:
raise HTTPException(status_code=400, detail="VM has no associated template; reinstall is not available")
task = DeployTask(
kind='reinstall', name=cached.name, status='pending', step='В очереди...', progress=0,
template_id=tpl.id, server_id=server_id, user_id=current_user.id,
template_id=tpl.id if tpl else None, server_id=server_id, user_id=current_user.id,
vmid=vmid, node=node,
)
db.add(task); db.commit(); db.refresh(task)