2015-05-01 04:05:37 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
VMware player/workstation server module.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shutil
|
|
|
|
import asyncio
|
|
|
|
import subprocess
|
|
|
|
import logging
|
2015-05-21 04:05:26 +03:00
|
|
|
from collections import OrderedDict
|
2015-05-01 04:05:37 +03:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
from ..base_manager import BaseManager
|
|
|
|
from .vmware_vm import VMwareVM
|
|
|
|
from .vmware_error import VMwareError
|
|
|
|
|
|
|
|
|
|
|
|
class VMware(BaseManager):
|
|
|
|
|
|
|
|
_VM_CLASS = VMwareVM
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
self._vmrun_path = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def vmrun_path(self):
|
|
|
|
"""
|
|
|
|
Returns the path vmrun utility.
|
|
|
|
|
|
|
|
:returns: path
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._vmrun_path
|
|
|
|
|
|
|
|
def find_vmrun(self):
|
2015-05-11 02:21:31 +03:00
|
|
|
"""
|
|
|
|
Searches for vmrun.
|
|
|
|
|
|
|
|
:returns: path to vmrun
|
|
|
|
"""
|
2015-05-01 04:05:37 +03:00
|
|
|
|
|
|
|
# look for vmrun
|
|
|
|
vmrun_path = self.config.get_section_config("VMware").get("vmrun_path")
|
|
|
|
if not vmrun_path:
|
|
|
|
if sys.platform.startswith("win"):
|
2015-05-11 02:21:31 +03:00
|
|
|
pass # TODO: use registry to find vmrun or search for default location
|
2015-05-01 04:05:37 +03:00
|
|
|
elif sys.platform.startswith("darwin"):
|
|
|
|
vmrun_path = "/Applications/VMware Fusion.app/Contents/Library/vmrun"
|
|
|
|
else:
|
|
|
|
vmrun_path = shutil.which("vmrun")
|
|
|
|
|
|
|
|
if not vmrun_path:
|
|
|
|
raise VMwareError("Could not find vmrun")
|
|
|
|
if not os.path.isfile(vmrun_path):
|
|
|
|
raise VMwareError("vmrun {} is not accessible".format(vmrun_path))
|
|
|
|
if not os.access(vmrun_path, os.X_OK):
|
|
|
|
raise VMwareError("vmrun is not executable")
|
|
|
|
if os.path.basename(vmrun_path) not in ["vmrun", "vmrun.exe"]:
|
|
|
|
raise VMwareError("Invalid vmrun executable name {}".format(os.path.basename(vmrun_path)))
|
|
|
|
|
|
|
|
self._vmrun_path = vmrun_path
|
|
|
|
return vmrun_path
|
|
|
|
|
2015-05-16 04:09:48 +03:00
|
|
|
@property
|
|
|
|
def host_type(self):
|
|
|
|
"""
|
|
|
|
Returns the VMware host type.
|
|
|
|
player = VMware player
|
|
|
|
ws = VMware Workstation
|
|
|
|
fusion = VMware Fusion
|
|
|
|
|
|
|
|
:returns: host type (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.platform.startswith("darwin"):
|
|
|
|
host_type = "fusion"
|
|
|
|
else:
|
|
|
|
host_type = self.config.get_section_config("VMware").get("host_type", "ws")
|
|
|
|
return host_type
|
|
|
|
|
2015-05-01 04:05:37 +03:00
|
|
|
@asyncio.coroutine
|
|
|
|
def execute(self, subcommand, args, timeout=60, host_type=None):
|
|
|
|
|
|
|
|
vmrun_path = self.vmrun_path
|
|
|
|
if not vmrun_path:
|
|
|
|
vmrun_path = self.find_vmrun()
|
|
|
|
if host_type is None:
|
2015-05-16 04:09:48 +03:00
|
|
|
host_type = self.host_type
|
|
|
|
|
2015-05-01 04:05:37 +03:00
|
|
|
command = [vmrun_path, "-T", host_type, subcommand]
|
|
|
|
command.extend(args)
|
|
|
|
log.debug("Executing vmrun with command: {}".format(command))
|
|
|
|
try:
|
|
|
|
process = yield from asyncio.create_subprocess_exec(*command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
|
|
|
|
except (OSError, subprocess.SubprocessError) as e:
|
|
|
|
raise VMwareError("Could not execute vmrun: {}".format(e))
|
|
|
|
|
|
|
|
try:
|
|
|
|
stdout_data, _ = yield from asyncio.wait_for(process.communicate(), timeout=timeout)
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
raise VMwareError("vmrun has timed out after {} seconds!".format(timeout))
|
|
|
|
|
|
|
|
if process.returncode:
|
|
|
|
# vmrun print errors on stdout
|
|
|
|
vmrun_error = stdout_data.decode("utf-8", errors="ignore")
|
|
|
|
raise VMwareError("vmrun has returned an error: {}".format(vmrun_error))
|
|
|
|
|
|
|
|
return stdout_data.decode("utf-8", errors="ignore").splitlines()
|
2015-05-02 03:47:46 +03:00
|
|
|
|
2015-05-11 02:21:31 +03:00
|
|
|
@staticmethod
|
2015-05-15 05:11:57 +03:00
|
|
|
def parse_vmware_file(path):
|
2015-05-11 02:21:31 +03:00
|
|
|
"""
|
|
|
|
Parses a VMware file (VMX, preferences or inventory).
|
|
|
|
|
|
|
|
:param path: path to the VMware file
|
|
|
|
|
|
|
|
:returns: dict
|
|
|
|
"""
|
|
|
|
|
2015-05-21 04:05:26 +03:00
|
|
|
pairs = OrderedDict()
|
2015-05-11 02:21:31 +03:00
|
|
|
with open(path, encoding="utf-8") as f:
|
|
|
|
for line in f.read().splitlines():
|
|
|
|
try:
|
|
|
|
key, value = line.split('=', 1)
|
|
|
|
pairs[key.strip()] = value.strip('" ')
|
|
|
|
except ValueError:
|
|
|
|
continue
|
|
|
|
return pairs
|
|
|
|
|
2015-05-21 04:05:26 +03:00
|
|
|
@staticmethod
|
|
|
|
def write_vmx_file(path, pairs):
|
|
|
|
"""
|
|
|
|
Write a VMware VMX file.
|
|
|
|
|
|
|
|
:param path: path to the VMX file
|
|
|
|
:param pairs: settings to write
|
|
|
|
"""
|
|
|
|
|
|
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
|
|
if sys.platform.startswith("linux"):
|
|
|
|
# write the shebang on the first line on Linux
|
|
|
|
vmware_path = shutil.which("vmware")
|
|
|
|
if vmware_path:
|
|
|
|
f.write("#!{}\n".format(vmware_path))
|
|
|
|
for key, value in pairs.items():
|
|
|
|
entry = '{} = "{}"\n'.format(key, value)
|
|
|
|
f.write(entry)
|
|
|
|
|
2015-05-08 01:50:37 +03:00
|
|
|
def _get_vms_from_inventory(self, inventory_path):
|
2015-05-11 02:21:31 +03:00
|
|
|
"""
|
|
|
|
Searches for VMs by parsing a VMware inventory file.
|
|
|
|
|
|
|
|
:param inventory_path: path to the inventory file
|
|
|
|
|
|
|
|
:returns: list of VMs
|
|
|
|
"""
|
2015-05-02 03:47:46 +03:00
|
|
|
|
2015-05-08 01:50:37 +03:00
|
|
|
vm_entries = {}
|
2015-05-02 03:47:46 +03:00
|
|
|
vms = []
|
2015-05-08 01:50:37 +03:00
|
|
|
try:
|
2015-05-11 02:21:31 +03:00
|
|
|
log.debug('Reading VMware inventory file "{}"'.format(inventory_path))
|
2015-05-15 05:11:57 +03:00
|
|
|
pairs = self.parse_vmware_file(inventory_path)
|
2015-05-11 02:21:31 +03:00
|
|
|
for key, value in pairs.items():
|
|
|
|
if key.startswith("vmlist"):
|
2015-05-08 01:50:37 +03:00
|
|
|
try:
|
2015-05-11 02:21:31 +03:00
|
|
|
vm_entry, variable_name = key.split('.', 1)
|
2015-05-08 01:50:37 +03:00
|
|
|
except ValueError:
|
|
|
|
continue
|
2015-05-11 02:21:31 +03:00
|
|
|
if not vm_entry in vm_entries:
|
|
|
|
vm_entries[vm_entry] = {}
|
|
|
|
vm_entries[vm_entry][variable_name.strip()] = value
|
2015-05-08 01:50:37 +03:00
|
|
|
except OSError as e:
|
|
|
|
log.warning("Could not read VMware inventory file {}: {}".format(inventory_path, e))
|
|
|
|
|
|
|
|
for vm_settings in vm_entries.values():
|
|
|
|
if "DisplayName" in vm_settings and "config" in vm_settings:
|
2015-05-21 04:05:26 +03:00
|
|
|
log.debug('Found VM named "{}" with VMX file "{}"'.format(vm_settings["DisplayName"], vm_settings["config"]))
|
2015-05-08 01:50:37 +03:00
|
|
|
vms.append({"vmname": vm_settings["DisplayName"], "vmx_path": vm_settings["config"]})
|
|
|
|
return vms
|
|
|
|
|
2015-05-11 02:21:31 +03:00
|
|
|
def _get_vms_from_directory(self, directory):
|
|
|
|
"""
|
|
|
|
Searches for VMs in a given directory.
|
|
|
|
|
|
|
|
:param directory: path to the directory
|
|
|
|
|
|
|
|
:returns: list of VMs
|
|
|
|
"""
|
2015-05-08 01:50:37 +03:00
|
|
|
|
|
|
|
vms = []
|
2015-05-11 02:21:31 +03:00
|
|
|
for path, _, filenames in os.walk(directory):
|
2015-05-02 03:47:46 +03:00
|
|
|
for filename in filenames:
|
|
|
|
if os.path.splitext(filename)[1] == ".vmx":
|
|
|
|
vmx_path = os.path.join(path, filename)
|
2015-05-11 02:21:31 +03:00
|
|
|
log.debug('Reading VMware VMX file "{}"'.format(vmx_path))
|
2015-05-02 03:47:46 +03:00
|
|
|
try:
|
2015-05-15 05:11:57 +03:00
|
|
|
pairs = self.parse_vmware_file(vmx_path)
|
2015-05-11 02:21:31 +03:00
|
|
|
if "displayName" in pairs:
|
|
|
|
log.debug('Found VM named "{}"'.format(pairs["displayName"]))
|
|
|
|
vms.append({"vmname": pairs["displayName"], "vmx_path": vmx_path})
|
2015-05-08 01:50:37 +03:00
|
|
|
except OSError as e:
|
2015-05-11 02:21:31 +03:00
|
|
|
log.warning('Could not read VMware VMX file "{}": {}'.format(vmx_path, e))
|
2015-05-02 03:47:46 +03:00
|
|
|
continue
|
|
|
|
return vms
|
2015-05-08 01:50:37 +03:00
|
|
|
|
|
|
|
def list_vms(self):
|
|
|
|
"""
|
|
|
|
Gets VMware VM list.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
inventory_path = os.path.expandvars(r"%APPDATA%\Vmware\Inventory.vmls")
|
|
|
|
elif sys.platform.startswith("darwin"):
|
|
|
|
inventory_path = os.path.expanduser("~/Library/Application\ Support/VMware Fusion/vmInventory")
|
|
|
|
else:
|
|
|
|
inventory_path = os.path.expanduser("~/.vmware/inventory.vmls")
|
|
|
|
|
|
|
|
if os.path.exists(inventory_path):
|
|
|
|
return self._get_vms_from_inventory(inventory_path)
|
|
|
|
else:
|
2015-05-11 02:21:31 +03:00
|
|
|
# VMware player has no inventory file, let's search the default location for VMs.
|
2015-05-08 01:50:37 +03:00
|
|
|
if sys.platform.startswith("win"):
|
2015-05-11 02:21:31 +03:00
|
|
|
vmware_preferences_path = os.path.expandvars(r"%APPDATA%\VMware\preferences.ini")
|
2015-05-08 01:50:37 +03:00
|
|
|
default_vm_path = os.path.expandvars(r"%USERPROFILE%\Documents\Virtual Machines")
|
2015-05-09 03:30:41 +03:00
|
|
|
elif sys.platform.startswith("darwin"):
|
2015-05-11 02:21:31 +03:00
|
|
|
vmware_preferences_path = os.path.expanduser("~/Library/Preferences/VMware Fusion/preferences")
|
2015-05-09 03:30:41 +03:00
|
|
|
default_vm_path = os.path.expanduser("~/Documents/Virtual Machines.localized")
|
2015-05-08 01:50:37 +03:00
|
|
|
else:
|
2015-05-11 02:21:31 +03:00
|
|
|
vmware_preferences_path = os.path.expanduser("~/.vmware/preferences")
|
2015-05-09 03:30:41 +03:00
|
|
|
default_vm_path = os.path.expanduser("~/vmware")
|
2015-05-11 02:21:31 +03:00
|
|
|
|
|
|
|
if os.path.exists(vmware_preferences_path):
|
|
|
|
# the default vm path may be present in VMware preferences file.
|
|
|
|
try:
|
2015-05-15 05:11:57 +03:00
|
|
|
pairs = self.parse_vmware_file(vmware_preferences_path)
|
2015-05-11 02:21:31 +03:00
|
|
|
if "prefvmx.defaultvmpath" in pairs:
|
|
|
|
default_vm_path = pairs["prefvmx.defaultvmpath"]
|
|
|
|
except OSError as e:
|
|
|
|
log.warning('Could not read VMware preferences file "{}": {}'.format(vmware_preferences_path, e))
|
|
|
|
|
|
|
|
if not os.path.isdir(default_vm_path):
|
|
|
|
raise VMwareError('Could not find the default VM directory: "{}"'.format(default_vm_path))
|
|
|
|
return self._get_vms_from_directory(default_vm_path)
|