From a260377f0e05a1f93376205415b14eb7d94592cd Mon Sep 17 00:00:00 2001 From: grossmj Date: Fri, 1 May 2015 18:47:46 -0600 Subject: [PATCH] List all available VMware VMs. --- gns3server/handlers/api/vmware_handler.py | 27 ++++++++++++++------ gns3server/modules/vmware/__init__.py | 30 +++++++++++++++++++++-- gns3server/modules/vmware/vmware_error.py | 2 +- gns3server/modules/vmware/vmware_vm.py | 11 ++++++--- 4 files changed, 56 insertions(+), 14 deletions(-) diff --git a/gns3server/handlers/api/vmware_handler.py b/gns3server/handlers/api/vmware_handler.py index e8bf35e0..65b39316 100644 --- a/gns3server/handlers/api/vmware_handler.py +++ b/gns3server/handlers/api/vmware_handler.py @@ -29,6 +29,19 @@ class VMwareHandler: API entry points for VMware. """ + @classmethod + @Route.get( + r"/vmware/vms", + status_codes={ + 200: "Success", + }, + description="Get all VMware VMs available") + def show(request, response): + + vmware_manager = VMware.instance() + vms = vmware_manager.list_vms() + response.json(vms) + @classmethod @Route.post( r"/projects/{project_id}/vmware/vms", @@ -53,10 +66,10 @@ class VMwareHandler: request.json.pop("linked_clone"), console=request.json.get("console", None)) - # for name, value in request.json.items(): - # if name != "vm_id": - # if hasattr(vm, name) and getattr(vm, name) != value: - # setattr(vm, name, value) + for name, value in request.json.items(): + if name != "vm_id": + if hasattr(vm, name) and getattr(vm, name) != value: + setattr(vm, name, value) response.set_status(201) response.json(vm) @@ -102,9 +115,9 @@ class VMwareHandler: vmware_manager = VMware.instance() vm = vmware_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) - # for name, value in request.json.items(): - # if hasattr(vm, name) and getattr(vm, name) != value: - # setattr(vm, name, value) + for name, value in request.json.items(): + if hasattr(vm, name) and getattr(vm, name) != value: + setattr(vm, name, value) response.json(vm) diff --git a/gns3server/modules/vmware/__init__.py b/gns3server/modules/vmware/__init__.py index 6d876430..6263eaab 100644 --- a/gns3server/modules/vmware/__init__.py +++ b/gns3server/modules/vmware/__init__.py @@ -41,7 +41,7 @@ class VMware(BaseManager): super().__init__() self._vmrun_path = None - self._host_type = "player" + self._default_vm_path = "/home/grossmj/vmware" @property def vmrun_path(self): @@ -84,7 +84,10 @@ class VMware(BaseManager): if not vmrun_path: vmrun_path = self.find_vmrun() if host_type is None: - host_type = self._host_type + if sys.platform.startswith("darwin"): + host_type = "fusion" + else: + host_type = self.config.get_section_config("VMware").get("host_type", "ws") command = [vmrun_path, "-T", host_type, subcommand] command.extend(args) log.debug("Executing vmrun with command: {}".format(command)) @@ -104,3 +107,26 @@ class VMware(BaseManager): raise VMwareError("vmrun has returned an error: {}".format(vmrun_error)) return stdout_data.decode("utf-8", errors="ignore").splitlines() + + def list_vms(self): + """ + Gets VMware VM list. + """ + + vms = [] + for path, _, filenames in os.walk(os.path.expanduser("~/vmware")): + for filename in filenames: + if os.path.splitext(filename)[1] == ".vmx": + vmx_path = os.path.join(path, filename) + try: + with open(vmx_path, encoding="utf-8") as f: + for line in f.read().splitlines(): + name, value = line.split('=', 1) + if name.strip() == "displayName": + vmname = value.strip('" ') + vms.append({"vmname": vmname, "vmx_path": vmx_path}) + break + except (OSError, ValueError) as e: + log.warning("Could not read VMware vmx file {}: {}".format(vmx_path, e)) + continue + return vms diff --git a/gns3server/modules/vmware/vmware_error.py b/gns3server/modules/vmware/vmware_error.py index 8a254030..88ea393c 100644 --- a/gns3server/modules/vmware/vmware_error.py +++ b/gns3server/modules/vmware/vmware_error.py @@ -16,7 +16,7 @@ # along with this program. If not, see . """ -Custom exceptions for the VirtualBox module. +Custom exceptions for the VMware module. """ from ..vm_error import VMError diff --git a/gns3server/modules/vmware/vmware_vm.py b/gns3server/modules/vmware/vmware_vm.py index 1bc1feee..89a0554c 100644 --- a/gns3server/modules/vmware/vmware_vm.py +++ b/gns3server/modules/vmware/vmware_vm.py @@ -99,8 +99,8 @@ class VMwareVM(BaseVM): Suspends this VMware VM. """ - yield from self._control_vm("suspend") - log.info("VMware VM '{name}' [{id}] stopped".format(name=self.name, id=self.id)) + yield from self._control_vm("pause") + log.info("VMware VM '{name}' [{id}] paused".format(name=self.name, id=self.id)) @asyncio.coroutine def resume(self): @@ -108,7 +108,7 @@ class VMwareVM(BaseVM): Resumes this VMware VM. """ - yield from self.start() + yield from self._control_vm("unpause") log.info("VMware VM '{name}' [{id}] resumed".format(name=self.name, id=self.id)) @asyncio.coroutine @@ -141,7 +141,10 @@ class VMwareVM(BaseVM): # if nio and isinstance(nio, NIOUDP): # self.manager.port_manager.release_udp_port(nio.lport, self._project) - yield from self.stop() + try: + yield from self.stop() + except VMwareError: + pass log.info("VirtualBox VM '{name}' [{id}] closed".format(name=self.name, id=self.id)) self._closed = True