List all available VMware VMs.

This commit is contained in:
grossmj 2015-05-01 18:47:46 -06:00
parent 57f5e7a7d9
commit a260377f0e
4 changed files with 56 additions and 14 deletions

View File

@ -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)

View File

@ -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

View File

@ -16,7 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Custom exceptions for the VirtualBox module.
Custom exceptions for the VMware module.
"""
from ..vm_error import VMError

View File

@ -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