|
|
@@ -45,19 +45,23 @@ async def stop_vm(vm_id: int):
|
|
|
return {"message": f"VM {vm_id} stopped (mock)"}
|
|
|
|
|
|
@app.post("/vms")
|
|
|
-async def create_vm(name: str, template: str):
|
|
|
+async def create_vm(name: str, template: str, disk: str):
|
|
|
xml_path = f"templates/{template}.xml"
|
|
|
if not os.path.exists(xml_path):
|
|
|
return {"error": f"Template {template} not found"}
|
|
|
with open(xml_path, 'r') as f:
|
|
|
xml = f.read()
|
|
|
+ # Get images path from env
|
|
|
+ images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images')
|
|
|
# Replace the name in XML
|
|
|
xml = re.sub(r'<name>.*?</name>', f'<name>{name}</name>', xml)
|
|
|
+ # Replace the disk file name
|
|
|
+ xml = re.sub(r'file=\'/var/lib/libvirt/images/.*?\.qcow2\'', f"file='{images_path}/{disk}.qcow2'", xml)
|
|
|
try:
|
|
|
# Create VM from template
|
|
|
domain = conn.defineXML(xml)
|
|
|
domain.create()
|
|
|
- return {"message": f"VM {name} created from template {template}"}
|
|
|
+ return {"message": f"VM {name} created from template {template} with disk {disk}.qcow2"}
|
|
|
except Exception as e:
|
|
|
return {"error": str(e)}
|
|
|
|