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 list_running_vms(self):
        vmx_paths = []

        def callback(jobHandle, eventType, moreEventInfo, clientData):
            if vixlib.VIX_EVENTTYPE_FIND_ITEM != eventType:
                return

            url = ctypes.c_char_p()
            err = vixlib.Vix_GetProperties(
                moreEventInfo, vixlib.VIX_PROPERTY_FOUND_ITEM_LOCATION,
                ctypes.byref(url), vixlib.VIX_PROPERTY_NONE)

            vmx_paths.append(url.value)

            vixlib.Vix_FreeBuffer(url)
            _check_job_err_code(err)

        cb = vixlib.VixEventProc(callback)
        job_handle = vixlib.VixHost_FindItems(self._host_handle,
                                              vixlib.VIX_FIND_RUNNING_VMS,
                                              vixlib.VIX_INVALID_HANDLE, -1,
                                              cb, None)
        err = vixlib.VixJob_Wait(job_handle, vixlib.VIX_PROPERTY_NONE)
        vixlib.Vix_ReleaseHandle(job_handle)
        _check_job_err_code(err)

        return vmx_paths
 def wait_for_tools_in_guest(self, timeout_seconds=600):
     job_handle = vixlib.VixVM_WaitForToolsInGuest(self._vm_handle,
                                                   timeout_seconds, None,
                                                   None)
     err = vixlib.VixJob_Wait(job_handle, vixlib.VIX_PROPERTY_NONE)
     vixlib.Vix_ReleaseHandle(job_handle)
     _check_job_err_code(err)
 def unpause(self):
     job_handle = vixlib.VixVM_Unpause(self._vm_handle, 0,
                                       vixlib.VIX_INVALID_HANDLE, None,
                                       None)
     err = vixlib.VixJob_Wait(job_handle, vixlib.VIX_PROPERTY_NONE)
     vixlib.Vix_ReleaseHandle(job_handle)
     _check_job_err_code(err)
    def remove_snapshot(self, snapshot):
        job_handle = vixlib.VixVM_RemoveSnapshot(self._vm_handle,
                                                 snapshot._snapshot_handle, 0,
                                                 None, None)
        err = vixlib.VixJob_Wait(job_handle, vixlib.VIX_PROPERTY_NONE)
        _check_job_err_code(err)

        snapshot.close()
    def reboot(self, soft=False):
        if soft:
            power_op = vixlib.VIX_VMPOWEROP_FROM_GUEST
        else:
            power_op = vixlib.VIX_VMPOWEROP_NORMAL

        job_handle = vixlib.VixVM_Reset(self._vm_handle, power_op, None, None)
        err = vixlib.VixJob_Wait(job_handle, vixlib.VIX_PROPERTY_NONE)
        vixlib.Vix_ReleaseHandle(job_handle)
        _check_job_err_code(err)
    def open_vm(self, vmx_path):
        job_handle = vixlib.VixVM_Open(self._host_handle, vmx_path, None, None)
        vm_handle = vixlib.VixHandle()
        err = vixlib.VixJob_Wait(job_handle,
                                 vixlib.VIX_PROPERTY_JOB_RESULT_HANDLE,
                                 ctypes.byref(vm_handle),
                                 vixlib.VIX_PROPERTY_NONE)
        vixlib.Vix_ReleaseHandle(job_handle)
        _check_job_err_code(err)

        return VixVM(vm_handle)
    def power_on(self, show_gui=True):
        if show_gui:
            options = vixlib.VIX_VMPOWEROP_LAUNCH_GUI
        else:
            options = vixlib.VIX_VMPOWEROP_NORMAL

        job_handle = vixlib.VixVM_PowerOn(self._vm_handle, options,
                                          vixlib.VIX_INVALID_HANDLE, None,
                                          None)
        err = vixlib.VixJob_Wait(job_handle, vixlib.VIX_PROPERTY_NONE)
        vixlib.Vix_ReleaseHandle(job_handle)
        _check_job_err_code(err)
    def delete(self, delete_disk_files=True):
        if delete_disk_files:
            delete_options = vixlib.VIX_VMDELETE_DISK_FILES
        else:
            delete_options = 0

        job_handle = vixlib.VixVM_Delete(self._vm_handle, delete_options, None,
                                         None)
        err = vixlib.VixJob_Wait(job_handle, vixlib.VIX_PROPERTY_NONE)
        vixlib.Vix_ReleaseHandle(job_handle)
        _check_job_err_code(err)

        self.close()
Beispiel #10
0
    def connect(self):
        job_handle = vixlib.VixHost_Connect(vixlib.VIX_API_VERSION,
                                            get_vix_host_type(), None, 0, None,
                                            None, 0, vixlib.VIX_INVALID_HANDLE,
                                            None, None)

        host_handle = vixlib.VixHandle()
        err = vixlib.VixJob_Wait(job_handle,
                                 vixlib.VIX_PROPERTY_JOB_RESULT_HANDLE,
                                 ctypes.byref(host_handle),
                                 vixlib.VIX_PROPERTY_NONE)
        vixlib.Vix_ReleaseHandle(job_handle)
        _check_job_err_code(err)

        self._host_handle = host_handle
Beispiel #11
0
    def clone_vm(self, src_vmx_path, dest_vmx_path, linked_clone=False):
        if linked_clone:
            clone_type = vixlib.VIX_CLONETYPE_LINKED
        else:
            clone_type = vixlib.VIX_CLONETYPE_FULL

        with self.open_vm(src_vmx_path) as vm:
            job_handle = vixlib.VixVM_Clone(vm._vm_handle,
                                            vixlib.VIX_INVALID_HANDLE,
                                            clone_type, dest_vmx_path, 0,
                                            vixlib.VIX_INVALID_HANDLE, None,
                                            None)

            cloned_vm_handle = vixlib.VixHandle()
            err = vixlib.VixJob_Wait(job_handle,
                                     vixlib.VIX_PROPERTY_JOB_RESULT_HANDLE,
                                     ctypes.byref(cloned_vm_handle),
                                     vixlib.VIX_PROPERTY_NONE)
            vixlib.Vix_ReleaseHandle(job_handle)
            _check_job_err_code(err)

            return VixVM(cloned_vm_handle)
Beispiel #12
0
    def create_snapshot(self,
                        include_memory=False,
                        name=None,
                        description=None):
        if include_memory:
            options = vixlib.VIX_SNAPSHOT_INCLUDE_MEMORY
        else:
            options = 0

        job_handle = vixlib.VixVM_CreateSnapshot(self._vm_handle, name,
                                                 description, options,
                                                 vixlib.VIX_INVALID_HANDLE,
                                                 None, None)
        snapshot_handle = vixlib.VixHandle()
        err = vixlib.VixJob_Wait(job_handle,
                                 vixlib.VIX_PROPERTY_JOB_RESULT_HANDLE,
                                 ctypes.byref(snapshot_handle),
                                 vixlib.VIX_PROPERTY_NONE)
        vixlib.Vix_ReleaseHandle(job_handle)
        _check_job_err_code(err)

        return VixSnapshot(snapshot_handle)
Beispiel #13
0
 def _unregister_vm_server(self, vmx_path):
     job_handle = vixlib.VixHost_UnregisterVM(self._host_handle, vmx_path,
                                              None, None)
     err = vixlib.VixJob_Wait(job_handle, vixlib.VIX_PROPERTY_NONE)
     vixlib.Vix_ReleaseHandle(job_handle)
     _check_job_err_code(err)
Beispiel #14
0
 def suspend(self):
     job_handle = vixlib.VixVM_Suspend(self._vm_handle, 0, None, None)
     err = vixlib.VixJob_Wait(job_handle, vixlib.VIX_PROPERTY_NONE)
     vixlib.Vix_ReleaseHandle(job_handle)
     _check_job_err_code(err)