فهرست منبع

Quito creación desde plantilla

Celestino Rey 7 ماه پیش
والد
کامیت
c4463f3afc
3فایلهای تغییر یافته به همراه1 افزوده شده و 90 حذف شده
  1. 1 8
      README.md
  2. 0 22
      app/main.py
  3. 0 60
      templates/ubuntu.xml

+ 1 - 8
README.md

@@ -38,14 +38,7 @@ Access the interactive documentation at http://0.0.0.0:8000/docs
 
 - GET / : Root message
 - GET /vms : List all VMs
-- POST /vms?name={name}&template={template}&disk={disk} : Create a new VM from a template with custom disk name
 - GET /vms/{vm_id}/start : Start a VM by ID
 - GET /vms/{vm_id}/stop : Stop a VM by ID
 - DELETE /vms/{vm_name} : Delete a VM by name
-- POST /disks?name={name}&size={size} : Create a new disk image with specified size in GB
-
-## Templates
-
-VM templates are XML files stored in the `templates/` directory. Each template defines the base configuration for a VM. When creating a VM, the name in the XML is replaced with the provided name, and the disk file path is set using the `LIBVIRT_IMAGES_PATH` environment variable (default: /var/lib/libvirt/images) plus the provided disk name.
-
-Example template: `templates/ubuntu.xml`
+- POST /disks?name={name}&size={size} : Create a new disk image with specified size in GB

+ 0 - 22
app/main.py

@@ -1,6 +1,5 @@
 from fastapi import FastAPI
 import os
-import re
 import libvirt
 import subprocess
 
@@ -45,27 +44,6 @@ async def stop_vm(vm_id: int):
     #     return {"error": str(e)}
     return {"message": f"VM {vm_id} stopped (mock)"}
 
-@app.post("/vms")
-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} with disk {disk}.qcow2"}
-    except Exception as e:
-        return {"error": str(e)}
-
 @app.delete("/vms/{vm_name}")
 async def delete_vm(vm_name: str):
     try:

+ 0 - 60
templates/ubuntu.xml

@@ -1,60 +0,0 @@
-<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='/var/lib/libvirt/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>