예제 #1
0
    def get_vm_in_cache(self, vm_id):
        """ Get information of a VM from cache. The vm state is not
        guaranteed to be up-to-date. Also only name and power_state is
        guaranteed to be not None.

        :return: VmCache for the vm that is found
        :raise VmNotFoundException when vm is not found
        """
        if vm_id not in self._vm_name_to_ref:
            raise VmNotFoundException("VM '%s' not found on host." % vm_id)

        ref = self._vm_name_to_ref[vm_id]
        vm = self._vm_cache[ref]
        if self._validate_vm(vm):
            return copy.copy(vm)
        else:
            raise VmNotFoundException("VM '%s' not found on host." % vm_id)
예제 #2
0
    def get_vm_obj_in_cache(self, vm_id):
        """ Get vim vm object given ID of the vm.

        :return: vim.VirtualMachine object
        :raise VmNotFoundException when vm is not found
        """
        if vm_id not in self._vm_name_to_ref:
            raise VmNotFoundException("VM '%s' not found on host." % vm_id)

        moid = self._vm_name_to_ref[vm_id].split(":")[-1][:-1]
        return vim.VirtualMachine(moid, self._si._stub)
예제 #3
0
    def register_vm(self, datastore_id, vm_id):
        os_path = os_vmx_path(datastore_id, vm_id)
        if not os.path.isfile(os_path):
            raise VmNotFoundException("vmx path %s not found" % os_path)

        folder = self.vim_client.vm_folder
        resource_pool = self.vim_client.root_resource_pool
        vmx_datastore_path = os_to_datastore_path(os_path)

        task = folder.RegisterVM_Task(vmx_datastore_path, vm_id, False,
                                      resource_pool)
        self.vim_client.wait_for_task(task)
        self.vim_client.wait_for_vm_create(vm_id)
예제 #4
0
    def get_vm(self, vm_id):
        """Get the vm reference on a host.

        Args:
            vm_id: The name of the vm.

        Returns:
            A vim vm reference.

        """
        vm = self.find_by_inventory_path(VM_FOLDER_NAME, vm_id)
        if not vm:
            raise VmNotFoundException("VM '%s' not found on host." % vm_id)

        return vm
예제 #5
0
    def get_used_memory_mb(self):
        vms = self.vim_client.get_vms_in_cache()
        if not vms:
            return 0

        memory = 0
        for vm in vms:
            # Vms in cache might include half updated record, e.g. with
            # None memory_mb, for a short time windows. Those Vms in cache
            # could be excluded from total used memory.
            if vm.memory_mb:
                memory += vm.memory_mb

        # This indicates that no values were retrieved from the cache.
        if memory == 0:
            raise VmNotFoundException("No valid VMs were found")

        return memory
예제 #6
0
    def get_configured_cpu_count(self):
        """
        Returns the total number of vCPUs across all VMs
        :return: number of vCPUs - int
        """
        vms = self.vim_client.get_vms_in_cache()
        if not vms:
            return 0

        cpu_count = 0
        for vm in vms:
            if vm.num_cpu:
                cpu_count += vm.num_cpu

        # This indicates that no values were retrieved from the cache.
        if cpu_count == 0:
            raise VmNotFoundException("No valid VMs were found")

        return cpu_count
예제 #7
0
    def get_used_memory_mb(self):
        vms = self.vim_client.get_vms_in_cache()
        if not vms:
            return 0

        memory = 0
        for vm in vms:
            # Vms in cache might include half updated record, e.g. with
            # None memory_mb, for a short time windows. Those Vms in cache
            # could be excluded from total used memory.
            if vm.name and vm.name.startswith(SHADOW_VM_NAME_PREFIX):
                # skip shadow vm, because we never power it on
                self._logger.info("skip shadow vm: %s" % vm.name)
            elif vm.memory_mb:
                memory += vm.memory_mb

        # This indicates that no values were retrieved from the cache.
        if memory == 0:
            raise VmNotFoundException("No valid VMs were found")

        return memory
예제 #8
0
    def get_configured_cpu_count(self):
        """
        Returns the total number of vCPUs across all VMs
        :return: number of vCPUs - int
        """
        vms = self.vim_client.get_vms_in_cache()
        if not vms:
            return 0

        cpu_count = 0
        for vm in vms:
            if vm.name and vm.name.startswith(SHADOW_VM_NAME_PREFIX):
                # skip shadow vm, because we never power it on
                self._logger.info("skip shadow vm: %s" % vm.name)
            elif vm.num_cpu:
                cpu_count += vm.num_cpu

        # This indicates that no values were retrieved from the cache.
        if cpu_count == 0:
            raise VmNotFoundException("No valid VMs were found")

        return cpu_count
예제 #9
0
 def _assert_exists(self, name):
     if name not in self._resources:
         raise VmNotFoundException("ENOENT")