Преглед на файлове

Actualizo instrucciones y añado número de cpus a la hora de crear VM

Celestino Rey преди 7 месеца
родител
ревизия
27f054fb5d
променени са 4 файла, в които са добавени 14 реда и са изтрити 9 реда
  1. 1 1
      README.md
  2. 7 7
      app/main.py
  3. 2 1
      static/app.js
  4. 4 0
      static/index.html

+ 1 - 1
README.md

@@ -40,7 +40,7 @@ sudo mkdir -p /var/lib/libvirt/images/isos
 
 3. Instala las dependencias de Python:
    ```bash
-   pip install -e .
+   pip install -r requirements.txt
    ```
 
 4. Configura permisos para libvirt (opcional, si no usas sudo):

+ 7 - 7
app/main.py

@@ -83,10 +83,10 @@ async def stop_vm(vm_name: str):
         return {"error": str(e)}
 
 @app.post("/vms")
-async def create_vm(name: str, memory: int, disk_size: int, iso: str):
+async def create_vm(name: str, memory: int, disk_size: int, cpus: int = 1, iso: str = ""):
     if not iso:
         return {"error": "ISO path is required"}
-    images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/mnt/vms/images')
+    images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images')
     disk_path = f"{images_path}/{name}.qcow2"
     iso_path = iso  # iso ya es la ruta completa desde el select
     
@@ -108,7 +108,7 @@ async def create_vm(name: str, memory: int, disk_size: int, iso: str):
   <name>{name}</name>
   <memory unit='MiB'>{memory}</memory>
   <currentMemory unit='MiB'>{memory}</currentMemory>
-  <vcpu placement='static'>1</vcpu>
+  <vcpu placement='static'>{cpus}</vcpu>
   <os>
     <type arch='x86_64' machine='pc-i440fx-6.2'>hvm</type>
     <boot dev='cdrom'/>
@@ -169,7 +169,7 @@ async def create_vm(name: str, memory: int, disk_size: int, iso: str):
     try:
         domain = conn.defineXML(xml_template)
         domain.create()
-        return {"message": f"VM {name} created with {memory} MiB memory, {disk_size}G disk, booting from {iso}"}
+        return {"message": f"VM {name} created with {cpus} CPUs, {memory} MiB memory, {disk_size}G disk, booting from {iso}"}
     except Exception as e:
         return {"error": str(e)}
 
@@ -239,7 +239,7 @@ async def set_boot_order(vm_name: str, boot_order: BootOrder):
 
 @app.get("/isos")
 async def list_isos():
-    images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/mnt/vms/images')
+    images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images')
     iso_dir = f"{images_path}/isos"
     if os.path.exists(iso_dir):
         isos = [{"name": f, "path": os.path.join(iso_dir, f)} for f in os.listdir(iso_dir) if f.endswith('.iso')]
@@ -248,7 +248,7 @@ async def list_isos():
 
 @app.post("/upload-iso")
 async def upload_iso(file: UploadFile = File(...)):
-    images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/mnt/vms/images')
+    images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images')
     iso_dir = f"{images_path}/isos"
     os.makedirs(iso_dir, exist_ok=True)
     file_path = os.path.join(iso_dir, file.filename)
@@ -259,7 +259,7 @@ async def upload_iso(file: UploadFile = File(...)):
 
 @app.delete("/isos/{iso_name}")
 async def delete_iso(iso_name: str):
-    images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/mnt/vms/images')
+    images_path = os.environ.get('LIBVIRT_IMAGES_PATH', '/var/lib/libvirt/images')
     iso_dir = f"{images_path}/isos"
     file_path = os.path.join(iso_dir, iso_name)
     if os.path.exists(file_path) and iso_name.endswith('.iso'):

+ 2 - 1
static/app.js

@@ -154,10 +154,11 @@ document.getElementById('createVMForm').addEventListener('submit', async (e) =>
     const name = document.getElementById('name').value;
     const memory = document.getElementById('memory').value;
     const disk_size = document.getElementById('disk_size').value;
+    const cpus = document.getElementById('cpus').value;
     const iso = document.getElementById('iso').value;
     
     try {
-        const response = await fetch(`${API_BASE}/vms?name=${name}&memory=${memory}&disk_size=${disk_size}&iso=${iso}`, {
+        const response = await fetch(`${API_BASE}/vms?name=${name}&memory=${memory}&disk_size=${disk_size}&cpus=${cpus}&iso=${iso}`, {
             method: 'POST'
         });
         let result;

+ 4 - 0
static/index.html

@@ -43,6 +43,10 @@
                         <label for="disk_size" class="form-label">Disk Size (GB)</label>
                         <input type="number" class="form-control" id="disk_size" required>
                     </div>
+                    <div class="mb-3">
+                        <label for="cpus" class="form-label">CPUs</label>
+                        <input type="number" class="form-control" id="cpus" value="1" min="1" required>
+                    </div>
                     <div class="mb-3">
                         <label for="iso" class="form-label">ISO File</label>
                         <select class="form-control" id="iso" required>