Пример #1
0
    def create_vm_group(self, cluster):
        """Create the VmGroup in the DRS.
        :param cluster: vim.ClusterComputeResource representing the cluster where the VM is contained.
        :return: True in case of success, else False.
        """
        # check if VmGroup Exists
        windows_group = self.get_vm_group_by_name(self.vm_group_name, cluster)
        if len(windows_group) > 0:
            if isinstance(windows_group[0], vim.cluster.VmGroup):
                self.logger.warning("VmGroup: {} exists. Skipping creation.".format(self.vm_group_name))
                return True
        spec = vim.cluster.ConfigSpecEx()
        operation = Operation()
        self.logger.info("Creating VmGroup: {}.".format(self.vm_group_name))
        operation.operation = operation.add
        group_vm = vim.cluster.GroupSpec()
        group_vm.info = vim.cluster.VmGroup()
        group_vm.operation = operation.operation
        group_vm.info.name = self.vm_group_name
        spec.groupSpec.append(group_vm)
        if self.test_mode:
            from pprint import pprint

            pprint(spec)
        else:
            task = cluster.ReconfigureComputeResource_Task(spec, True)
            # wait_for_task(task)
        self.logger.info("VmGroup: {} Created.".format(self.vm_group_name))
        return True
Пример #2
0
    def create_host_group(self, cluster):
        """Create the HostGroup in the DRS Configuration.
        :param cluster: vim.ClusterComputeResource representing the cluster containing the HostGroup.
        :return True in case of success, else False
        """
        # I will take the first result returned by the function.
        windows_host_group = self.get_host_group_by_name(self.host_group_name, cluster)
        operation = Operation()
        # Assuring that the group doesn't exists
        if len(windows_host_group) > 0:
            if isinstance(windows_host_group[0], vim.cluster.HostGroup):
                self.logger.warning("HostGroup: {} exists. Skipping creation.".format(self.host_group_name))
                return True
        self.logger.warning('Creating HostGroup: {}.'.format(self.host_group_name))
        operation.operation = operation.add
        spec = vim.cluster.ConfigSpecEx()
        group = vim.cluster.GroupSpec()
        group.operation = operation.operation
        group.info = vim.cluster.HostGroup()
        group.info.name = self.host_group_name
        spec.groupSpec.append(group)
        if self.test_mode:
            from pprint import pprint

            pprint(spec)
        else:
            task = cluster.ReconfigureComputeResource_Task(spec, True)
            wait_for_task(task)
        return True
Пример #3
0
    def create_host_group(self, cluster):
        """Create the HostGroup in the DRS Configuration.
        :param cluster: vim.ClusterComputeResource representing the cluster containing the HostGroup.
        :return True in case of success, else False
        """
        # I will take the first result returned by the function.
        windows_host_group = self.get_host_group_by_name(
            self.host_group_name, cluster)
        operation = Operation()
        # Assuring that the group doesn't exists
        if len(windows_host_group) > 0:
            if isinstance(windows_host_group[0], vim.cluster.HostGroup):
                self.logger.warning(
                    "HostGroup: {} exists. Skipping creation.".format(
                        self.host_group_name))
                return True
        self.logger.warning('Creating HostGroup: {}.'.format(
            self.host_group_name))
        operation.operation = operation.add
        spec = vim.cluster.ConfigSpecEx()
        group = vim.cluster.GroupSpec()
        group.operation = operation.operation
        group.info = vim.cluster.HostGroup()
        group.info.name = self.host_group_name
        spec.groupSpec.append(group)
        if self.test_mode:
            from pprint import pprint

            pprint(spec)
        else:
            task = cluster.ReconfigureComputeResource_Task(spec, True)
            wait_for_task(task)
        return True
Пример #4
0
    def create_vm_group(self, cluster):
        """Create the VmGroup in the DRS.
        :param cluster: vim.ClusterComputeResource representing the cluster where the VM is contained.
        :return: True in case of success, else False.
        """
        # check if VmGroup Exists
        windows_group = self.get_vm_group_by_name(self.vm_group_name, cluster)
        if len(windows_group) > 0:
            if isinstance(windows_group[0], vim.cluster.VmGroup):
                self.logger.warning(
                    "VmGroup: {} exists. Skipping creation.".format(
                        self.vm_group_name))
                return True
        spec = vim.cluster.ConfigSpecEx()
        operation = Operation()
        self.logger.info("Creating VmGroup: {}.".format(self.vm_group_name))
        operation.operation = operation.add
        group_vm = vim.cluster.GroupSpec()
        group_vm.info = vim.cluster.VmGroup()
        group_vm.operation = operation.operation
        group_vm.info.name = self.vm_group_name
        spec.groupSpec.append(group_vm)
        if self.test_mode:
            from pprint import pprint

            pprint(spec)
        else:
            task = cluster.ReconfigureComputeResource_Task(spec, True)
            # wait_for_task(task)
        self.logger.info("VmGroup: {} Created.".format(self.vm_group_name))
        return True
Пример #5
0
    def add_host_to_host_group(self, cluster):
        """Add a new host entity to an HostGroup spec.
        :param cluster: vim.ClusterComputeResource representing the cluster containing the HostGroup.
        :return True in case of success, else False
        """
        active_hosts = []
        # I will take the first result returned by the function.
        windows_host_group = self.get_host_group_by_name(
            self.host_group_name, cluster)
        # Assuring that the group exists.
        operation = Operation()
        if windows_host_group and len(windows_host_group) > 0:
            # Extrapolate the first element of the list (we should have only one record here if everything is fine.
            try:
                windows_host_group = windows_host_group[0]
            except IndexError:
                # Something really strange is happening... Let's raise an error.
                raise HostGroupNotExists(name=self.host_group_name)
            # I take the first host available in the cluster
            operation.operation = operation.edit
            # If the HostGroup is empty, I will add the first host of the cluster (sorted by name)
            if len(windows_host_group.host) == 0:
                sorted_list = sorted(map(lambda x: x, cluster.host),
                                     key=lambda x: x.name,
                                     reverse=False)
                first_usable_host = sorted_list[0]
            else:
                sorted_list = sorted(filter(
                    lambda x: x not in windows_host_group.host, cluster.host),
                                     key=lambda x: x.name,
                                     reverse=False)
                first_usable_host = sorted_list[0]
            active_hosts = windows_host_group.host
            active_hosts.append(first_usable_host)
            self.logger.debug('Picked host: {}'.format(first_usable_host.name))
        else:
            self.logger.critical(
                "HostGroup doesn't exists. We shouldn't be here...")
            raise HostGroupNotExists(name=self.host_group_name)
        spec = vim.cluster.ConfigSpecEx()
        group = vim.cluster.GroupSpec()
        group.operation = operation.operation
        group.info = vim.cluster.HostGroup()
        group.info.name = self.host_group_name
        # if the group exists and the object is really an HostGroup I will add the current list of available
        # hosts to it.
        if windows_host_group and isinstance(windows_host_group,
                                             vim.cluster.HostGroup):
            group.info.host = active_hosts
        spec.groupSpec.append(group)
        if self.test_mode:
            from pprint import pprint

            pprint(spec)
        else:
            task = cluster.ReconfigureComputeResource_Task(spec, True)
            # wait_for_task(task)
        return True
Пример #6
0
    def add_host_to_host_group(self, cluster):
        """Add a new host entity to an HostGroup spec.
        :param cluster: vim.ClusterComputeResource representing the cluster containing the HostGroup.
        :return True in case of success, else False
        """
        active_hosts = []
        # I will take the first result returned by the function.
        windows_host_group = self.get_host_group_by_name(self.host_group_name, cluster)
        # Assuring that the group exists.
        operation = Operation()
        if windows_host_group and len(windows_host_group) > 0:
            # Extrapolate the first element of the list (we should have only one record here if everything is fine.
            try:
                windows_host_group = windows_host_group[0]
            except IndexError:
                # Something really strange is happening... Let's raise an error.
                raise HostGroupNotExists(name=self.host_group_name)
            # I take the first host available in the cluster
            operation.operation = operation.edit
            # If the HostGroup is empty, I will add the first host of the cluster (sorted by name)
            if len(windows_host_group.host) == 0:
                sorted_list = sorted(
                    map(lambda x: x, cluster.host),
                    key=lambda x: x.name, reverse=False
                )
                first_usable_host = sorted_list[0]
            else:
                sorted_list = sorted(
                    filter(lambda x: x not in windows_host_group.host, cluster.host),
                    key=lambda x: x.name, reverse=False
                )
                first_usable_host = sorted_list[0]
            active_hosts = windows_host_group.host
            active_hosts.append(first_usable_host)
            self.logger.debug('Picked host: {}'.format(first_usable_host.name))
        else:
            self.logger.critical("HostGroup doesn't exists. We shouldn't be here...")
            raise HostGroupNotExists(name=self.host_group_name)
        spec = vim.cluster.ConfigSpecEx()
        group = vim.cluster.GroupSpec()
        group.operation = operation.operation
        group.info = vim.cluster.HostGroup()
        group.info.name = self.host_group_name
        # if the group exists and the object is really an HostGroup I will add the current list of available
        # hosts to it.
        if windows_host_group and isinstance(windows_host_group, vim.cluster.HostGroup):
            group.info.host = active_hosts
        spec.groupSpec.append(group)
        if self.test_mode:
            from pprint import pprint

            pprint(spec)
        else:
            task = cluster.ReconfigureComputeResource_Task(spec, True)
            # wait_for_task(task)
        return True
Пример #7
0
    def add_vm_to_vm_group(self, vm, cluster):
        """Add a VM entity vo a VmGroup spec.
        :param vm: vim.VirtualMachine representing the VM to add.
        :param cluster: vim.ClusterComputeResource representing the cluster where the VM is contained.
        :return: True if the operation is successful else False.
        """
        # check if VmGroup Exists
        vm_name = vm.name
        windows_group = self.get_vm_group_by_name(self.vm_group_name, cluster)
        spec = vim.cluster.ConfigSpecEx()
        operation = Operation()
        if windows_group and len(windows_group) > 0:
            self.logger.info('VmGroup: {} exists. Adding VM {} to it.'.format(
                self.vm_group_name, vm_name))
            operation.operation = operation.edit
            group_vm = vim.cluster.GroupSpec()
            group_vm.info = vim.cluster.VmGroup()
            # if there are some vm already configured I add it now to the spec.
            if len(windows_group[0].vm) > 0:
                for _vm in windows_group[0].vm:
                    # going defensive here to avoid bad spec.
                    if isinstance(_vm, vim.VirtualMachine):
                        group_vm.info.vm.append(_vm)
        else:
            raise VmGroupNotExists(name=self.vm_group_name)
        group_vm.operation = operation.operation
        group_vm.info.name = self.vm_group_name
        group_vm.info.vm.append(vm)
        spec.groupSpec.append(group_vm)
        if self.test_mode:
            from pprint import pprint

            pprint(spec)
        else:
            task = cluster.ReconfigureComputeResource_Task(spec, True)
            # wait_for_task(task)
        self.logger.info('VM: {} finally plugged into VmGroup: {}'.format(
            vm_name, self.vm_group_name))
        return True
Пример #8
0
    def add_vm_to_vm_group(self, vm, cluster):
        """Add a VM entity vo a VmGroup spec.
        :param vm: vim.VirtualMachine representing the VM to add.
        :param cluster: vim.ClusterComputeResource representing the cluster where the VM is contained.
        :return: True if the operation is successful else False.
        """
        # check if VmGroup Exists
        vm_name = vm.name
        windows_group = self.get_vm_group_by_name(self.vm_group_name, cluster)
        spec = vim.cluster.ConfigSpecEx()
        operation = Operation()
        if windows_group and len(windows_group) > 0:
            self.logger.info('VmGroup: {} exists. Adding VM {} to it.'.format(self.vm_group_name, vm_name))
            operation.operation = operation.edit
            group_vm = vim.cluster.GroupSpec()
            group_vm.info = vim.cluster.VmGroup()
            # if there are some vm already configured I add it now to the spec.
            if len(windows_group[0].vm) > 0:
                for _vm in windows_group[0].vm:
                    # going defensive here to avoid bad spec.
                    if isinstance(_vm, vim.VirtualMachine):
                        group_vm.info.vm.append(_vm)
        else:
            raise VmGroupNotExists(name=self.vm_group_name)
        group_vm.operation = operation.operation
        group_vm.info.name = self.vm_group_name
        group_vm.info.vm.append(vm)
        spec.groupSpec.append(group_vm)
        if self.test_mode:
            from pprint import pprint

            pprint(spec)
        else:
            task = cluster.ReconfigureComputeResource_Task(spec, True)
            # wait_for_task(task)
        self.logger.info('VM: {} finally plugged into VmGroup: {}'.format(vm_name, self.vm_group_name))
        return True
Пример #9
0
 def add_affinity_rule(self, cluster):
     """Add the new Affinity Rule to associate VmGroup with the HostGroup.
     :param cluster: vim.clusterComputeResource representing the cluster to reconfigure.
     :return True if success, else False.
     """
     operation = Operation()
     spec = vim.cluster.ConfigSpecEx()
     rule = vim.cluster.RuleSpec()
     rule.operation = operation.add
     rule.info = vim.cluster.VmHostRuleInfo()
     rule.info.enabled = True
     rule.info.name = self.windows_affinity_rule_name
     rule.info.vmGroupName = self.vm_group_name
     rule.info.affineHostGroupName = self.host_group_name
     spec.rulesSpec.append(rule)
     if self.test_mode:
         from pprint import pprint
         pprint(spec)
     else:
         task = cluster.ReconfigureComputeResource_Task(spec, True)
         # wait_for_task(task)
     return True