From aebcd9f08bcb145aa669a363da34cfc0207e99b3 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Wed, 11 Mar 2015 15:04:11 -0600 Subject: [PATCH] Relative path support of IOU, IOS and Qemu images. --- gns3server/modules/dynamips/__init__.py | 3 +- gns3server/modules/dynamips/nodes/router.py | 9 +++- gns3server/modules/iou/iou_vm.py | 36 +++++++++------- gns3server/modules/qemu/qemu_vm.py | 48 +++++++++++++++------ 4 files changed, 65 insertions(+), 31 deletions(-) diff --git a/gns3server/modules/dynamips/__init__.py b/gns3server/modules/dynamips/__init__.py index 16bc67c6..6a5f4c70 100644 --- a/gns3server/modules/dynamips/__init__.py +++ b/gns3server/modules/dynamips/__init__.py @@ -44,7 +44,6 @@ from .hypervisor import Hypervisor from .nodes.router import Router from .dynamips_vm import DynamipsVM from .dynamips_device import DynamipsDevice -from gns3server.config import Config # NIOs from .nios.nio_udp import NIOUDP @@ -312,7 +311,7 @@ class Dynamips(BaseManager): # FIXME: hypervisor should always listen to 127.0.0.1 # See https://github.com/GNS3/dynamips/issues/62 - server_config = Config.instance().get_section_config("Server") + server_config = self.config.get_section_config("Server") server_host = server_config.get("host") try: diff --git a/gns3server/modules/dynamips/nodes/router.py b/gns3server/modules/dynamips/nodes/router.py index 03a8f60c..8cfa23a8 100644 --- a/gns3server/modules/dynamips/nodes/router.py +++ b/gns3server/modules/dynamips/nodes/router.py @@ -34,7 +34,6 @@ from ...base_vm import BaseVM from ..dynamips_error import DynamipsError from ..nios.nio_udp import NIOUDP -from gns3server.config import Config from gns3server.utils.asyncio import wait_run_in_executor @@ -152,6 +151,12 @@ class Router(BaseVM): "mac_addr": self._mac_addr, "system_id": self._system_id} + # return the relative path if the IOS image is in the images_path directory + server_config = self.manager.config.get_section_config("Server") + relative_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "IOS", self._image) + if os.path.exists(relative_image): + router_info["image"] = os.path.basename(self._image) + # add the slots slot_number = 0 for slot in self._slots: @@ -422,7 +427,7 @@ class Router(BaseVM): """ if not os.path.isabs(image): - server_config = Config.instance().get_section_config("Server") + server_config = self.manager.config.get_section_config("Server") image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "IOS", image) if not os.path.isfile(image): diff --git a/gns3server/modules/iou/iou_vm.py b/gns3server/modules/iou/iou_vm.py index 299a9258..bdcb8bb8 100644 --- a/gns3server/modules/iou/iou_vm.py +++ b/gns3server/modules/iou/iou_vm.py @@ -39,7 +39,6 @@ from ..nios.nio_tap import NIOTAP from ..nios.nio_generic_ethernet import NIOGenericEthernet from ..base_vm import BaseVM from .ioucon import start_ioucon -from ...config import Config import gns3server.utils.asyncio @@ -132,7 +131,7 @@ class IOUVM(BaseVM): """ if not os.path.isabs(path): - server_config = Config.instance().get_section_config("Server") + server_config = self.manager.config.get_section_config("Server") path = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "IOU", path) self._path = path @@ -195,19 +194,26 @@ class IOUVM(BaseVM): def __json__(self): - return {"name": self.name, - "vm_id": self.id, - "console": self._console, - "project_id": self.project.id, - "path": self.path, - "ethernet_adapters": len(self._ethernet_adapters), - "serial_adapters": len(self._serial_adapters), - "ram": self._ram, - "nvram": self._nvram, - "l1_keepalives": self._l1_keepalives, - "initial_config": self.relative_initial_config_file, - "use_default_iou_values": self._use_default_iou_values - } + iou_vm_info = {"name": self.name, + "vm_id": self.id, + "console": self._console, + "project_id": self.project.id, + "path": self.path, + "ethernet_adapters": len(self._ethernet_adapters), + "serial_adapters": len(self._serial_adapters), + "ram": self._ram, + "nvram": self._nvram, + "l1_keepalives": self._l1_keepalives, + "initial_config": self.relative_initial_config_file, + "use_default_iou_values": self._use_default_iou_values} + + # return the relative path if the IOU image is in the images_path directory + server_config = self.manager.config.get_section_config("Server") + relative_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "IOU", self.path) + if os.path.exists(relative_image): + iou_vm_info["path"] = os.path.basename(self.path) + + return iou_vm_info @property def iouyap_path(self): diff --git a/gns3server/modules/qemu/qemu_vm.py b/gns3server/modules/qemu/qemu_vm.py index c4c17851..8e8237c6 100644 --- a/gns3server/modules/qemu/qemu_vm.py +++ b/gns3server/modules/qemu/qemu_vm.py @@ -32,7 +32,6 @@ from ..adapters.ethernet_adapter import EthernetAdapter from ..nios.nio_udp import NIOUDP from ..base_vm import BaseVM from ...schemas.qemu import QEMU_OBJECT_SCHEMA -from ...config import Config import logging log = logging.getLogger(__name__) @@ -185,8 +184,8 @@ class QemuVM(BaseVM): :param hda_disk_image: QEMU hda disk image path """ - if os.path.isabs(hda_disk_image): - server_config = Config.instance().get_section_config("Server") + if not os.path.isabs(hda_disk_image): + server_config = self.manager.config.get_section_config("Server") hda_disk_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "QEMU", hda_disk_image) log.info("QEMU VM {name} [id={id}] has set the QEMU hda disk image path to {disk_image}".format(name=self._name, @@ -213,7 +212,7 @@ class QemuVM(BaseVM): """ if not os.path.isabs(hdb_disk_image): - server_config = Config.instance().get_section_config("Server") + server_config = self.manager.config.get_section_config("Server") hdb_disk_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "QEMU", hdb_disk_image) log.info("QEMU VM {name} [id={id}] has set the QEMU hdb disk image path to {disk_image}".format(name=self._name, @@ -239,8 +238,8 @@ class QemuVM(BaseVM): :param hdc_disk_image: QEMU hdc disk image path """ - if os.path.isabs(hdc_disk_image): - server_config = Config.instance().get_section_config("Server") + if not os.path.isabs(hdc_disk_image): + server_config = self.manager.config.get_section_config("Server") hdc_disk_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "QEMU", hdc_disk_image) log.info("QEMU VM {name} [id={id}] has set the QEMU hdc disk image path to {disk_image}".format(name=self._name, @@ -266,8 +265,8 @@ class QemuVM(BaseVM): :param hdd_disk_image: QEMU hdd disk image path """ - if os.path.isabs(hdd_disk_image): - server_config = Config.instance().get_section_config("Server") + if not os.path.isabs(hdd_disk_image): + server_config = self.manager.config.get_section_config("Server") hdd_disk_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "QEMU", hdd_disk_image) log.info("QEMU VM {name} [id={id}] has set the QEMU hdd disk image path to {disk_image}".format(name=self._name, @@ -461,8 +460,8 @@ class QemuVM(BaseVM): :param initrd: QEMU initrd path """ - if os.path.isabs(initrd): - server_config = Config.instance().get_section_config("Server") + if not os.path.isabs(initrd): + server_config = self.manager.config.get_section_config("Server") initrd = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "QEMU", initrd) log.info("QEMU VM {name} [id={id}] has set the QEMU initrd path to {initrd}".format(name=self._name, @@ -488,8 +487,8 @@ class QemuVM(BaseVM): :param kernel_image: QEMU kernel image path """ - if os.path.isabs(kernel_image): - server_config = Config.instance().get_section_config("Server") + if not os.path.isabs(kernel_image): + server_config = self.manager.config.get_section_config("Server") kernel_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "QEMU", kernel_image) log.info("QEMU VM {name} [id={id}] has set the QEMU kernel image path to {kernel_image}".format(name=self._name, @@ -1079,6 +1078,23 @@ class QemuVM(BaseVM): command.extend(self._graphic()) return command + def _get_relative_disk_image_path(self, disk_image): + """ + Returns a relative image path if the disk image is in the images directory. + + :param disk_image: path to the disk image + + :returns: relative or full path + """ + + if disk_image: + # return the relative path if disks images are in the images_path directory + server_config = self.manager.config.get_section_config("Server") + relative_image = os.path.join(os.path.expanduser(server_config.get("images_path", "~/GNS3/images")), "QEMU", disk_image) + if os.path.exists(relative_image): + return os.path.basename(disk_image) + return disk_image + def __json__(self): answer = { "project_id": self.project.id, @@ -1088,4 +1104,12 @@ class QemuVM(BaseVM): for field in QEMU_OBJECT_SCHEMA["required"]: if field not in answer: answer[field] = getattr(self, field) + + answer["hda_disk_image"] = self._get_relative_disk_image_path(self._hda_disk_image) + answer["hdb_disk_image"] = self._get_relative_disk_image_path(self._hdb_disk_image) + answer["hdc_disk_image"] = self._get_relative_disk_image_path(self._hdc_disk_image) + answer["hdd_disk_image"] = self._get_relative_disk_image_path(self._hdd_disk_image) + answer["initrd"] = self._get_relative_disk_image_path(self._initrd) + answer["kernel_image"] = self._get_relative_disk_image_path(self._kernel_image) + return answer