def get_guest_ip_address(self, timeout_seconds=600): start = time.time() self.wait_for_tools_in_guest(timeout_seconds) ip_address = None while not ip_address: job_handle = vixlib.VixVM_ReadVariable( self._vm_handle, vixlib.VIX_VM_GUEST_VARIABLE, "ip", 0, None, None) read_value = ctypes.c_char_p() err = vixlib.VixJob_Wait( job_handle, vixlib.VIX_PROPERTY_JOB_RESULT_VM_VARIABLE_STRING, ctypes.byref(read_value), vixlib.VIX_PROPERTY_NONE) vixlib.Vix_ReleaseHandle(job_handle) _check_job_err_code(err) ip_address = read_value.value vixlib.Vix_FreeBuffer(read_value) if not ip_address or ip_address.startswith('169.254.'): if (timeout_seconds >= 0 and time.time() - start > timeout_seconds): raise utils.VixException( _("Timeout exceeded: %d" % timeout_seconds)) time.sleep(3) ip_address = None return ip_address
def _exec_cmd(self, args): p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate() if p.returncode: raise utils.VixException(_("Command failed: %s") % args) return (out, err)
def get_vnc_console(self, instance): vmx_path = self._pathutils.get_vmx_path(instance['name']) with self._conn.open_vm(vmx_path) as vm: vnc_enabled, vnc_port = vm.get_vnc_settings() if not vnc_enabled: raise utils.VixException(_("VNC is not enabled for this instance")) host = self.get_host_ip_addr() return {'host': host, 'port': vnc_port, 'internal_access_path': None}
def get_vix_host_type(): global _host_type if not _host_type: if sys.platform == "darwin": if os.path.exists("/Applications/VMware Fusion.app"): _host_type = vixlib.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION else: raise utils.VixException(_("VMWare Fusion not installed")) elif sys.platform == "win32": # Ref: http://kb.vmware.com/selfservice/microsites/search.do? # language=en_US&cmd=displayKC&externalId=1308 try: with _winreg.OpenKey( _winreg.HKEY_CLASSES_ROOT, "Installer\\UpgradeCodes\\" "3F935F414A4C79542AD9C8D157A3CC39") as key: product_code = _winreg.EnumValue(key, 0)[0] with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, "Installer\\products\\%s" % product_code) as key: product_name = _winreg.QueryValueEx(key, "ProductName")[0] except WindowsError: raise utils.VixException( _("Workstation or Player not " "installed")) else: # Linux product_name = get_vmx_value('/etc/vmware/config', 'product.name') if not _host_type: if product_name == "VMware Player": _host_type = vixlib.VIX_SERVICEPROVIDER_VMWARE_PLAYER elif product_name == "VMware Workstation": _host_type = vixlib.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION else: raise utils.VixException( _("Unsupported Vix product: %s") % product_name) return _host_type
def get_disk_info(self, disk_path): args = ["qemu-img", "info", disk_path] out, err = self._exec_cmd(args) format = None internal_size = None for line in out.split(os.linesep): m = re.match(r"^file format: (.*)$", line) if m: format = m.groups()[0] continue m = re.match(r"^virtual size: .* \((.*) bytes\)$", line) if m: internal_size = long(m.groups()[0]) continue if not format or internal_size is None: raise utils.VixException(_("Unable to get disk details from: %s") % out) file_size = os.path.getsize(disk_path) return (format, internal_size, file_size)
def _check_job_err_code(err): if err: msg = vixlib.Vix_GetErrorText(err, None) raise utils.VixException(msg)