Просмотр исходного кода

intentando arreglar la consola

Celestino Rey 6 месяцев назад
Родитель
Сommit
1fba558479
1 измененных файлов с 13 добавлено и 3 удалено
  1. 13 3
      app/main.py

+ 13 - 3
app/main.py

@@ -275,10 +275,13 @@ async def create_vm(name: str, memory: int, disk_size: int, cpus: int = 1, iso:
 @app.get("/console/{vm_name}")
 @app.get("/console/{vm_name}")
 async def get_console(vm_name: str):
 async def get_console(vm_name: str):
     try:
     try:
-        domain = conn.lookupByName(vm_name)
+        console_conn = libvirt.open('qemu:///system')
+        domain = console_conn.lookupByName(vm_name)
         xml_desc = domain.XMLDesc()
         xml_desc = domain.XMLDesc()
         root = ET.fromstring(xml_desc)
         root = ET.fromstring(xml_desc)
         graphics = root.find(".//graphics[@type='vnc']")
         graphics = root.find(".//graphics[@type='vnc']")
+        console_conn.close()
+        
         if graphics is None:
         if graphics is None:
             return {"error": "No VNC graphics found"}
             return {"error": "No VNC graphics found"}
         vnc_port = graphics.get('port')
         vnc_port = graphics.get('port')
@@ -288,12 +291,19 @@ async def get_console(vm_name: str):
         
         
         if vm_name not in ws_processes:
         if vm_name not in ws_processes:
             ws_port = get_free_port()
             ws_port = get_free_port()
-            proc = subprocess.Popen(['websockify', str(ws_port), f'localhost:{vnc_port}'])
-            ws_processes[vm_name] = (proc, ws_port)
+            try:
+                proc = subprocess.Popen(['websockify', str(ws_port), f'localhost:{vnc_port}'], 
+                                      stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                ws_processes[vm_name] = (proc, ws_port)
+            except FileNotFoundError:
+                return {"error": "websockify not found. Please install it: apt-get install websockify"}
         
         
         _, ws_port = ws_processes[vm_name]
         _, ws_port = ws_processes[vm_name]
         return {"url": f"/static/novnc/vnc.html?host={socket.gethostname()}&port={ws_port}"}
         return {"url": f"/static/novnc/vnc.html?host={socket.gethostname()}&port={ws_port}"}
     except Exception as e:
     except Exception as e:
+        import traceback
+        error_msg = f"Error: {str(e)}\n{traceback.format_exc()}"
+        print(error_msg)
         return {"error": str(e)}
         return {"error": str(e)}
 
 
 @app.delete("/vms/{vm_name}")
 @app.delete("/vms/{vm_name}")