Qemu binary list

This commit is contained in:
Julien Duponchelle committed 2015-02-20 14:39:13 +01:00
1 parent 90f71e7581
commit 8aa5514890
13 files changed
+505 -1

No files matched your search

+20
View File
@@ -24,6 +24,7 @@ from ..schemas.qemu import QEMU_CREATE_SCHEMA
from ..schemas.qemu import QEMU_UPDATE_SCHEMA
from ..schemas.qemu import QEMU_OBJECT_SCHEMA
from ..schemas.qemu import QEMU_NIO_SCHEMA
from ..schemas.qemu import QEMU_BINARY_LIST_SCHEMA
from ..modules.qemu import Qemu
@@ -258,3 +259,22 @@ class QEMUHandler:
vm = qemu_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
vm.adapter_remove_nio_binding(int(request.match_info["adapter_number"]), int(request.match_info["port_number"]))
response.set_status(204)
@classmethod
@Route.get(
r"/projects/{project_id}/qemu/binaries",
parameters={
"project_id": "UUID for the project"
},
status_codes={
200: "Success",
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Get a list of available Qemu binaries",
output=QEMU_BINARY_LIST_SCHEMA)
def list_binaries(request, response):
qemu_manager = Qemu.instance()
binaries = yield from Qemu.binary_list()
response.json(binaries)
+70 -1
View File
@@ -20,7 +20,12 @@ Qemu server module.
"""
import asyncio
import os
import sys
import re
import subprocess
from ...utils.asyncio import subprocess_check_output
from ..base_manager import BaseManager
from .qemu_error import QemuError
from .qemu_vm import QemuVM
@@ -38,4 +43,68 @@ class Qemu(BaseManager):
:returns: working directory name
"""
return "pc-{}".format(legacy_vm_id)
return "vm-{}".format(legacy_vm_id)
@staticmethod
def binary_list():
"""
Gets QEMU binaries list available on the matchine
:returns: Array of dictionnary {"path": Qemu binaries path, "version": Version of Qemu}
"""
qemus = []
paths = [os.getcwd()] + os.environ["PATH"].split(os.pathsep)
# look for Qemu binaries in the current working directory and $PATH
if sys.platform.startswith("win"):
# add specific Windows paths
if hasattr(sys, "frozen"):
# add any qemu dir in the same location as gns3server.exe to the list of paths
exec_dir = os.path.dirname(os.path.abspath(sys.executable))
for f in os.listdir(exec_dir):
if f.lower().startswith("qemu"):
paths.append(os.path.join(exec_dir, f))
if "PROGRAMFILES(X86)" in os.environ and os.path.exists(os.environ["PROGRAMFILES(X86)"]):
paths.append(os.path.join(os.environ["PROGRAMFILES(X86)"], "qemu"))
if "PROGRAMFILES" in os.environ and os.path.exists(os.environ["PROGRAMFILES"]):
paths.append(os.path.join(os.environ["PROGRAMFILES"], "qemu"))
elif sys.platform.startswith("darwin"):
# add specific locations on Mac OS X regardless of what's in $PATH
paths.extend(["/usr/local/bin", "/opt/local/bin"])
if hasattr(sys, "frozen"):
paths.append(os.path.abspath(os.path.join(os.getcwd(), "../../../qemu/bin/")))
for path in paths:
try:
for f in os.listdir(path):
if (f.startswith("qemu-system") or f == "qemu" or f == "qemu.exe") and \
os.access(os.path.join(path, f), os.X_OK) and \
os.path.isfile(os.path.join(path, f)):
qemu_path = os.path.join(path, f)
version = yield from Qemu._get_qemu_version(qemu_path)
qemus.append({"path": qemu_path, "version": version})
except OSError:
continue
return qemus
@staticmethod
@asyncio.coroutine
def _get_qemu_version(qemu_path):
"""
Gets the Qemu version.
:param qemu_path: path to Qemu
"""
if sys.platform.startswith("win"):
return ""
try:
output = yield from subprocess_check_output(qemu_path, "-version")
match = re.search("version\s+([0-9a-z\-\.]+)", output.decode("utf-8"))
if match:
version = match.group(1)
return version
else:
raise QemuError("Could not determine the Qemu version for {}".format(qemu_path))
except subprocess.SubprocessError as e:
raise QemuError("Error while looking for the Qemu version: {}".format(e))
+25
View File
@@ -361,3 +361,28 @@ QEMU_OBJECT_SCHEMA = {
"legacy_networking", "cpu_throttling", "process_priority", "options"
]
}
QEMU_BINARY_LIST_SCHEMA = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Request validation for a list of qemu binaries",
"type": "array",
"items": {
"$ref": "#/definitions/QemuPath"
},
"definitions": {
"QemuPath": {
"description": "Qemu path object",
"properties": {
"path": {
"description": "Qemu path",
"type": "string",
},
"version": {
"description": "Qemu version",
"type": "string",
},
},
}
},
"additionalProperties": False,
}