當前位置: 首頁>>代碼示例>>Python>>正文


Python VMware.list_vms方法代碼示例

本文整理匯總了Python中gns3server.compute.vmware.VMware.list_vms方法的典型用法代碼示例。如果您正苦於以下問題:Python VMware.list_vms方法的具體用法?Python VMware.list_vms怎麽用?Python VMware.list_vms使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在gns3server.compute.vmware.VMware的用法示例。


在下文中一共展示了VMware.list_vms方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: VMwareGNS3VM

# 需要導入模塊: from gns3server.compute.vmware import VMware [as 別名]
# 或者: from gns3server.compute.vmware.VMware import list_vms [as 別名]
class VMwareGNS3VM(BaseGNS3VM):

    def __init__(self, controller):

        self._engine = "vmware"
        super().__init__(controller)
        self._vmware_manager = VMware()
        self._vmx_path = None

    @property
    def vmx_path(self):
        return self._vmx_path

    @asyncio.coroutine
    def _execute(self, subcommand, args, timeout=60, log_level=logging.INFO):

        try:
            result = yield from self._vmware_manager.execute(subcommand, args, timeout, log_level=log_level)
            return (''.join(result))
        except VMwareError as e:
            raise GNS3VMError("Error while executing VMware command: {}".format(e))

    @asyncio.coroutine
    def _is_running(self):
        result = yield from self._vmware_manager.execute("list", [])
        if self._vmx_path in result:
            return True
        return False

    @asyncio.coroutine
    def _set_vcpus_ram(self, vcpus, ram):
        """
        Set the number of vCPU cores and amount of RAM for the GNS3 VM.

        :param vcpus: number of vCPU cores
        :param ram: amount of RAM
        """

        # memory must be a multiple of 4 (VMware requirement)
        if ram % 4 != 0:
            raise GNS3VMError("Allocated memory {} for the GNS3 VM must be a multiple of 4".format(ram))

        available_vcpus = psutil.cpu_count(logical=False)
        if vcpus > available_vcpus:
            raise GNS3VMError("You have allocated too many vCPUs for the GNS3 VM! (max available is {} vCPUs)".format(available_vcpus))

        try:
            pairs = VMware.parse_vmware_file(self._vmx_path)
            pairs["numvcpus"] = str(vcpus)
            pairs["memsize"] = str(ram)
            VMware.write_vmx_file(self._vmx_path, pairs)
            log.info("GNS3 VM vCPU count set to {} and RAM amount set to {}".format(vcpus, ram))
        except OSError as e:
            raise GNS3VMError('Could not read/write VMware VMX file "{}": {}'.format(self._vmx_path, e))

    @asyncio.coroutine
    def list(self):
        """
        List all VMware VMs
        """
        try:
            return (yield from self._vmware_manager.list_vms())
        except VMwareError as e:
            raise GNS3VMError("Could not list VMware VMs: {}".format(str(e)))

    @asyncio.coroutine
    def start(self):
        """
        Starts the GNS3 VM.
        """

        vms = yield from self.list()
        for vm in vms:
            if vm["vmname"] == self.vmname:
                self._vmx_path = vm["vmx_path"]
                break

        # check we have a valid VMX file path
        if not self._vmx_path:
            raise GNS3VMError("VMWare VM {} not found".format(self.vmname))
        if not os.path.exists(self._vmx_path):
            raise GNS3VMError("VMware VMX file {} doesn't exist".format(self._vmx_path))

        # check if the VMware guest tools are installed
        vmware_tools_state = yield from self._execute("checkToolsState", [self._vmx_path])
        if vmware_tools_state not in ("installed", "running"):
            raise GNS3VMError("VMware tools are not installed in {}".format(self.vmname))

        try:
            running = yield from self._is_running()
        except VMwareError as e:
            raise GNS3VMError("Could not list VMware VMs: {}".format(str(e)))
        if not running:
            log.info("Update GNS3 VM settings")
            # set the number of vCPUs and amount of RAM
            yield from self._set_vcpus_ram(self.vcpus, self.ram)

            # start the VM
            args = [self._vmx_path]
            if self._headless:
#.........這裏部分代碼省略.........
開發者ID:AJNOURI,項目名稱:gns3-server,代碼行數:103,代碼來源:vmware_gns3_vm.py


注:本文中的gns3server.compute.vmware.VMware.list_vms方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。