Parcourir la source

Crea vm desde plantilla

Celestino Rey il y a 7 mois
Parent
commit
1d82968e1b
2 fichiers modifiés avec 81 ajouts et 1 suppressions
  1. 21 1
      app/main.py
  2. 60 0
      templates/ubuntu.xml

+ 21 - 1
app/main.py

@@ -1,4 +1,6 @@
 from fastapi import FastAPI
+import os
+import re
 import libvirt
 
 app = FastAPI(title="VirtManager API", description="API for managing virtual machines")
@@ -40,4 +42,22 @@ async def stop_vm(vm_id: int):
     #     return {"message": f"VM {vm_id} stopped"}
     # except Exception as e:
     #     return {"error": str(e)}
-    return {"message": f"VM {vm_id} stopped (mock)"}
+    return {"message": f"VM {vm_id} stopped (mock)"}
+
+@app.post("/vms")
+async def create_vm(name: str, template: 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()
+    # Replace the name in XML
+    xml = re.sub(r'<name>.*?</name>', f'<name>{name}</name>', xml)
+    try:
+        # Create VM from template
+        domain = conn.defineXML(xml)
+        domain.create()
+        return {"message": f"VM {name} created from template {template}"}
+    except Exception as e:
+        return {"error": str(e)}
+    #return {"message": f"VM created from template {template} (mock)"}

+ 60 - 0
templates/ubuntu.xml

@@ -0,0 +1,60 @@
+<domain type='kvm'>
+  <name>ubuntu-template</name>
+  <memory unit='KiB'>1048576</memory>
+  <currentMemory unit='KiB'>1048576</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc-i440fx-6.2'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <features>
+    <acpi/>
+    <apic/>
+  </features>
+  <cpu mode='host-model' check='partial'/>
+  <clock offset='utc'>
+    <timer name='rtc' tickpolicy='catchup'/>
+    <timer name='pit' tickpolicy='delay'/>
+    <timer name='hpet' present='no'/>
+  </clock>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <pm>
+    <suspend-to-mem enabled='no'/>
+    <suspend-to-disk enabled='no'/>
+  </pm>
+  <devices>
+    <emulator>/usr/bin/kvm</emulator>
+    <disk type='file' device='disk'>
+      <driver name='qemu' type='qcow2'/>
+      <source file='/mnt/vms/images/ubuntu.qcow2'/>
+      <target dev='vda' bus='virtio'/>
+    </disk>
+    <disk type='file' device='cdrom'>
+      <driver name='qemu' type='raw'/>
+      <target dev='hda' bus='ide'/>
+      <readonly/>
+    </disk>
+    <interface type='network'>
+      <mac address='52:54:00:00:00:01'/>
+      <source network='default'/>
+      <model type='virtio'/>
+    </interface>
+    <console type='pty'>
+      <target type='serial' port='0'/>
+    </console>
+    <input type='tablet' bus='usb'>
+      <address type='usb' bus='0' port='1'/>
+    </input>
+    <input type='mouse' bus='ps2'/>
+    <input type='keyboard' bus='ps2'/>
+    <graphics type='vnc' port='-1' autoport='yes'/>
+    <video>
+      <model type='cirrus' vram='16384' heads='1' primary='yes'/>
+    </video>
+    <memballoon model='virtio'>
+      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
+    </memballoon>
+  </devices>
+</domain>