Added a method for getting qemu-img binaries, and moved the qemu folder into a separate method.

This commit is contained in:
Vasil Rangelov 2015-05-10 20:46:51 +03:00
parent 117630c887
commit 9ef4628cbc
3 changed files with 95 additions and 6 deletions

View File

@ -292,6 +292,21 @@ class QEMUHandler:
binaries = yield from Qemu.binary_list()
response.json(binaries)
@classmethod
@Route.get(
r"/qemu/img-binaries",
status_codes={
200: "Success",
400: "Invalid request",
404: "Instance doesn't exist"
},
description="Get a list of available Qemu-img binaries",
output=QEMU_BINARY_LIST_SCHEMA)
def list_img_binaries(request, response):
binaries = yield from Qemu.img_binary_list()
response.json(binaries)
@Route.get(
r"/qemu/vms",
status_codes={

View File

@ -39,14 +39,13 @@ class Qemu(BaseManager):
_VM_CLASS = QemuVM
@staticmethod
def binary_list():
def paths_list():
"""
Gets QEMU binaries list available on the host.
Gets a folder list of possibly available QEMU binaries on the host.
:returns: Array of dictionary {"path": Qemu binary path, "version": version of Qemu}
:returns: List of folders where Qemu binaries MAY reside.
"""
qemus = []
paths = []
try:
paths.append(os.getcwd())
@ -72,14 +71,25 @@ class Qemu(BaseManager):
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"])
paths.extend(["/usr/bin", "/usr/local/bin", "/opt/local/bin"])
if hasattr(sys, "frozen"):
try:
paths.append(os.path.abspath(os.path.join(os.getcwd(), "../../../qemu/bin/")))
# If the user run the server by hand from outside
except FileNotFoundError:
paths.append(["/Applications/GNS3.app/Contents/Resources/qemu/bin"])
for path in paths:
return paths
@staticmethod
def binary_list():
"""
Gets QEMU binaries list available on the host.
:returns: Array of dictionary {"path": Qemu binary path, "version": version of Qemu}
"""
qemus = []
for path in Qemu.paths_list():
try:
for f in os.listdir(path):
if (f.startswith("qemu-system") or f.startswith("qemu-kvm") or f == "qemu" or f == "qemu.exe") and \
@ -93,6 +103,28 @@ class Qemu(BaseManager):
return qemus
@staticmethod
def img_binary_list():
"""
Gets QEMU-img binaries list available on the host.
:returns: Array of dictionary {"path": Qemu-img binary path, "version": version of Qemu-img}
"""
qemu_imgs = []
for path in Qemu.paths_list():
try:
for f in os.listdir(path):
if (f == "qemu-img" or f == "qemu-img.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_img_version(qemu_path)
qemu_imgs.append({"path": qemu_path, "version": version})
except OSError:
continue
return qemu_imgs
@staticmethod
@asyncio.coroutine
def _get_qemu_version(qemu_path):
@ -115,6 +147,26 @@ class Qemu(BaseManager):
except subprocess.SubprocessError as e:
raise QemuError("Error while looking for the Qemu version: {}".format(e))
@staticmethod
@asyncio.coroutine
def _get_qemu_img_version(qemu_img_path):
"""
Gets the Qemu-img version.
:param qemu_img_path: path to Qemu-img executable.
"""
try:
output = yield from subprocess_check_output(qemu_img_path, "--version")
match = re.search("version\s+([0-9a-z\-\.]+)", output)
if match:
version = match.group(1)
return version
else:
raise QemuError("Could not determine the Qemu-img version for {}".format(qemu_img_path))
except subprocess.SubprocessError as e:
raise QemuError("Error while looking for the Qemu-img version: {}".format(e))
@staticmethod
def get_legacy_vm_workdir(legacy_vm_id, name):
"""

View File

@ -57,6 +57,28 @@ def test_binary_list(loop):
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x42"), "version": version} in qemus
assert {"path": os.path.join(os.environ["PATH"], "hello"), "version": version} not in qemus
def test_img_binary_list(loop):
files_to_create = ["qemu-img", "qemu-io", "qemu-system-x86", "qemu-system-x42", "qemu-kvm", "hello"]
for file_to_create in files_to_create:
path = os.path.join(os.environ["PATH"], file_to_create)
with open(path, "w+") as f:
f.write("1")
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
with asyncio_patch("gns3server.modules.qemu.subprocess_check_output", return_value="qemu-img version 2.2.0, Copyright (c) 2004-2008 Fabrice Bellard") as mock:
qemus = loop.run_until_complete(asyncio.async(Qemu.img_binary_list()))
version = "2.2.0"
assert {"path": os.path.join(os.environ["PATH"], "qemu-img"), "version": version} in qemus
assert {"path": os.path.join(os.environ["PATH"], "qemu-io"), "version": version} not in qemus
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x86"), "version": version} not in qemus
assert {"path": os.path.join(os.environ["PATH"], "qemu-kvm"), "version": version} not in qemus
assert {"path": os.path.join(os.environ["PATH"], "qemu-system-x42"), "version": version} not in qemus
assert {"path": os.path.join(os.environ["PATH"], "hello"), "version": version} not in qemus
def test_get_legacy_vm_workdir():