Esempio n. 1
0
    def find_all_items(self,
                       resource_type=None,
                       properties=None,
                       profile_list=None):
        """Find all items matching the given type, properties, and profiles.

        Args:
          resource_type (str): Resource type string like 'scsi' or 'serial'
          properties (dict): Properties and their values to match
          profile_list (list): List of profiles to filter on

        Returns:
          list: Matching OVFItem instances
        """
        items = [
            self.item_dict[instance]
            for instance in natural_sort(self.item_dict)
        ]
        filtered_items = []
        if properties is None:
            properties = {}
        for item in items:
            if self.item_match(item, resource_type, properties, profile_list):
                filtered_items.append(item)
        logger.spam(
            "Found %s Items of type %s with properties %s and"
            " profiles %s", len(filtered_items), resource_type, properties,
            profile_list)
        return filtered_items
Esempio n. 2
0
File: item.py Progetto: sentrium/cot
    def get_nonintersecting_set_list(self):
        """Identify the minimal non-intersecting set of profiles.

        Returns:
          list: List of profile-set strings.
        """
        set_list = []
        for name in self.property_names:
            for (_, new_set) in list(self.properties[name].items()):
                new_set_list = []
                for existing_set in set_list:
                    # If the sets are identical or do not intersect, do nothing
                    if (new_set == existing_set
                            or not new_set.intersection(existing_set)):
                        new_set_list.append(frozenset(existing_set))
                        continue
                    # Otherwise, need to re-partition!
                    set_a = existing_set.difference(new_set)
                    new_set_list.append(frozenset(set_a))

                    set_b = existing_set.intersection(new_set)
                    new_set_list.append(frozenset(set_b))

                    new_set = new_set.difference(existing_set)

                new_set_list.append(frozenset(new_set))
                # Remove duplicate and empty entries
                set_list = [x for x in set(new_set_list) if x]

        logger.spam("Final set list is %s", set_list)

        # Construct a list of profile strings
        set_string_list = []
        for final_set in set_list:
            if None in final_set:
                set_string_list.append("")
            else:
                set_string_list.append(" ".join(natural_sort(final_set)))
        set_string_list = natural_sort(set_string_list)

        logger.spam("set string list: %s", set_string_list)

        return set_string_list
Esempio n. 3
0
    def get_nonintersecting_set_list(self):
        """Identify the minimal non-intersecting set of profiles.

        Returns:
          list: List of profile-set strings.
        """
        set_list = []
        for name in self.property_names:
            for (_, new_set) in list(self.properties[name].items()):
                new_set_list = []
                for existing_set in set_list:
                    # If the sets are identical or do not intersect, do nothing
                    if (new_set == existing_set or
                            not new_set.intersection(existing_set)):
                        new_set_list.append(frozenset(existing_set))
                        continue
                    # Otherwise, need to re-partition!
                    set_a = existing_set.difference(new_set)
                    new_set_list.append(frozenset(set_a))

                    set_b = existing_set.intersection(new_set)
                    new_set_list.append(frozenset(set_b))

                    new_set = new_set.difference(existing_set)

                new_set_list.append(frozenset(new_set))
                # Remove duplicate and empty entries
                set_list = [x for x in set(new_set_list) if x]

        logger.spam("Final set list is %s", set_list)

        # Construct a list of profile strings
        set_string_list = []
        for final_set in set_list:
            if None in final_set:
                set_string_list.append("")
            else:
                set_string_list.append(" ".join(natural_sort(final_set)))
        set_string_list = natural_sort(set_string_list)

        logger.spam("set string list: %s", set_string_list)

        return set_string_list
Esempio n. 4
0
    def update_xml(self):
        """Regenerate all Items under the VirtualHardwareSection, if needed.

        Will do nothing if no Items have been changed.
        """
        modified = False
        if len(self.item_dict) != len(
                XML.find_all_children(
                    self.ovf.virtual_hw_section,
                    set([
                        self.ovf.ITEM, self.ovf.STORAGE_ITEM,
                        self.ovf.ETHERNET_PORT_ITEM
                    ]))):
            modified = True
        else:
            for ovfitem in self.item_dict.values():
                if ovfitem.modified:
                    modified = True
                    break
        if not modified:
            logger.verbose("No changes to hardware definition, "
                           "so no XML update is required")
            return
        # Delete the existing Items:
        delete_count = 0
        for item in list(self.ovf.virtual_hw_section):
            if (item.tag == self.ovf.ITEM or item.tag == self.ovf.STORAGE_ITEM
                    or item.tag == self.ovf.ETHERNET_PORT_ITEM):
                self.ovf.virtual_hw_section.remove(item)
                delete_count += 1
        logger.debug("Cleared %d existing items from VirtualHWSection",
                     delete_count)
        # Generate the new XML Items, in appropriately sorted order by Instance
        ordering = [self.ovf.INFO, self.ovf.SYSTEM, self.ovf.ITEM]
        for instance in natural_sort(self.item_dict):
            logger.debug("Writing Item(s) with InstanceID %s", instance)
            ovfitem = self.item_dict[instance]
            new_items = ovfitem.generate_items()
            logger.spam("Generated %d items", len(new_items))
            for item in new_items:
                XML.add_child(self.ovf.virtual_hw_section, item, ordering)
        logger.verbose(
            "Updated XML VirtualHardwareSection, now contains %d "
            "Items representing %d devices",
            len(self.ovf.virtual_hw_section.findall(self.ovf.ITEM)),
            len(self.item_dict))
Esempio n. 5
0
    def update_xml(self):
        """Regenerate all Items under the VirtualHardwareSection, if needed.

        Will do nothing if no Items have been changed.
        """
        modified = False
        if len(self.item_dict) != len(XML.find_all_children(
                self.ovf.virtual_hw_section,
                set([self.ovf.ITEM, self.ovf.STORAGE_ITEM,
                     self.ovf.ETHERNET_PORT_ITEM]))):
            modified = True
        else:
            for ovfitem in self.item_dict.values():
                if ovfitem.modified:
                    modified = True
                    break
        if not modified:
            logger.verbose("No changes to hardware definition, "
                           "so no XML update is required")
            return
        # Delete the existing Items:
        delete_count = 0
        for item in list(self.ovf.virtual_hw_section):
            if (item.tag == self.ovf.ITEM or
                    item.tag == self.ovf.STORAGE_ITEM or
                    item.tag == self.ovf.ETHERNET_PORT_ITEM):
                self.ovf.virtual_hw_section.remove(item)
                delete_count += 1
        logger.debug("Cleared %d existing items from VirtualHWSection",
                     delete_count)
        # Generate the new XML Items, in appropriately sorted order by Instance
        ordering = [self.ovf.INFO, self.ovf.SYSTEM, self.ovf.ITEM]
        for instance in natural_sort(self.item_dict):
            logger.debug("Writing Item(s) with InstanceID %s", instance)
            ovfitem = self.item_dict[instance]
            new_items = ovfitem.generate_items()
            logger.spam("Generated %d items", len(new_items))
            for item in new_items:
                XML.add_child(self.ovf.virtual_hw_section, item, ordering)
        logger.verbose("Updated XML VirtualHardwareSection, now contains %d "
                       "Items representing %d devices",
                       len(self.ovf.virtual_hw_section.findall(self.ovf.ITEM)),
                       len(self.item_dict))
Esempio n. 6
0
    def find_all_items(self, resource_type=None, properties=None,
                       profile_list=None):
        """Find all items matching the given type, properties, and profiles.

        Args:
          resource_type (str): Resource type string like 'scsi' or 'serial'
          properties (dict): Properties and their values to match
          profile_list (list): List of profiles to filter on

        Returns:
          list: Matching OVFItem instances
        """
        items = [self.item_dict[instance] for instance in
                 natural_sort(self.item_dict)]
        filtered_items = []
        if properties is None:
            properties = {}
        for item in items:
            if self.item_match(item, resource_type, properties, profile_list):
                filtered_items.append(item)
        logger.spam("Found %s Items of type %s with properties %s and"
                    " profiles %s", len(filtered_items), resource_type,
                    properties, profile_list)
        return filtered_items