|
@@ -1,16 +1,21 @@
|
|
|
from fastapi import FastAPI
|
|
from fastapi import FastAPI
|
|
|
|
|
+from fastapi.staticfiles import StaticFiles
|
|
|
|
|
+from fastapi.responses import FileResponse
|
|
|
import os
|
|
import os
|
|
|
import libvirt
|
|
import libvirt
|
|
|
import subprocess
|
|
import subprocess
|
|
|
|
|
+import random
|
|
|
|
|
|
|
|
app = FastAPI(title="VirtManager API", description="API for managing virtual machines")
|
|
app = FastAPI(title="VirtManager API", description="API for managing virtual machines")
|
|
|
|
|
|
|
|
|
|
+app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
|
+
|
|
|
# Connect to libvirt
|
|
# Connect to libvirt
|
|
|
conn = libvirt.open('qemu:///system')
|
|
conn = libvirt.open('qemu:///system')
|
|
|
|
|
|
|
|
@app.get("/")
|
|
@app.get("/")
|
|
|
async def root():
|
|
async def root():
|
|
|
- return {"message": "VirtManager API"}
|
|
|
|
|
|
|
+ return FileResponse("static/index.html")
|
|
|
|
|
|
|
|
@app.get("/vms")
|
|
@app.get("/vms")
|
|
|
async def list_vms():
|
|
async def list_vms():
|
|
@@ -48,6 +53,9 @@ async def stop_vm(vm_id: int):
|
|
|
async def create_vm(name: str, memory: int, disk_size: int, iso: str):
|
|
async def create_vm(name: str, memory: int, disk_size: int, iso: str):
|
|
|
images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images')
|
|
images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images')
|
|
|
disk_path = f"{images_path}/{name}.qcow2"
|
|
disk_path = f"{images_path}/{name}.qcow2"
|
|
|
|
|
+ iso_path = f"{images_path}/{iso}"
|
|
|
|
|
+
|
|
|
|
|
+ print(f"Using ISO path: {iso_path}")
|
|
|
|
|
|
|
|
# Create disk
|
|
# Create disk
|
|
|
try:
|
|
try:
|
|
@@ -57,6 +65,9 @@ async def create_vm(name: str, memory: int, disk_size: int, iso: str):
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
return {"error": f"Error creating disk: {str(e)}"}
|
|
return {"error": f"Error creating disk: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
+ # Generate unique MAC
|
|
|
|
|
+ mac = f"52:54:00:{random.randint(0, 255):02x}:{random.randint(0, 255):02x}:{random.randint(0, 255):02x}"
|
|
|
|
|
+
|
|
|
# Basic XML template
|
|
# Basic XML template
|
|
|
xml_template = f"""<domain type='kvm'>
|
|
xml_template = f"""<domain type='kvm'>
|
|
|
<name>{name}</name>
|
|
<name>{name}</name>
|
|
@@ -93,12 +104,12 @@ async def create_vm(name: str, memory: int, disk_size: int, iso: str):
|
|
|
</disk>
|
|
</disk>
|
|
|
<disk type='file' device='cdrom'>
|
|
<disk type='file' device='cdrom'>
|
|
|
<driver name='qemu' type='raw'/>
|
|
<driver name='qemu' type='raw'/>
|
|
|
- <source file='{images_path}/{iso}'/>
|
|
|
|
|
|
|
+ <source file='{iso_path}'/>
|
|
|
<target dev='hda' bus='ide'/>
|
|
<target dev='hda' bus='ide'/>
|
|
|
<readonly/>
|
|
<readonly/>
|
|
|
</disk>
|
|
</disk>
|
|
|
<interface type='network'>
|
|
<interface type='network'>
|
|
|
- <mac address='52:54:00:00:00:01'/>
|
|
|
|
|
|
|
+ <mac address='{mac}'/>
|
|
|
<source network='default'/>
|
|
<source network='default'/>
|
|
|
<model type='virtio'/>
|
|
<model type='virtio'/>
|
|
|
</interface>
|
|
</interface>
|