Exemple #1
0
    def select_for_hardware_scripts(self, modaliases=None):
        """Select for_hardware scripts for the given node and user input.

        Goes through an existing ScriptSet and adds any for_hardware tagged
        Script and removes those that were autoselected but the hardware has
        been removed.
        """
        # Only the builtin commissioning scripts run on controllers.
        if self.node.is_controller:
            return

        if modaliases is None:
            modaliases = self.node.modaliases

        # Remove scripts autoselected at the start of commissioning but updated
        # commissioning data shows the Script is no longer applicable.
        script_results = self.scriptresult_set.exclude(script=None)
        script_results = script_results.filter(status=SCRIPT_STATUS.PENDING)
        script_results = script_results.exclude(script__for_hardware=[])
        for script_result in script_results:
            # User selected the specific script, always do what the user says.
            if (script_result.name in self.requested_scripts
                    or str(script_result.script.id) in self.requested_scripts):
                continue
            matches = filter_modaliases(modaliases,
                                        *script_result.script.ForHardware)
            if len(matches) == 0:
                script_result.delete()

        # Add Scripts which match the node with current commissioning data.
        scripts = Script.objects.all()
        if self.result_type == RESULT_TYPE.COMMISSIONING:
            scripts = scripts.filter(script_type=SCRIPT_TYPE.COMMISSIONING)
        else:
            scripts = scripts.filter(script_type=SCRIPT_TYPE.TESTING)
        scripts = scripts.filter(tags__overlap=self.requested_scripts)
        scripts = scripts.exclude(for_hardware=[])
        scripts = scripts.exclude(name__in=[s.name for s in self])
        for script in scripts:
            matches = filter_modaliases(modaliases, *script.ForHardware)
            if len(matches) != 0:
                try:
                    self.add_pending_script(script)
                except ValidationError as e:
                    err_msg = (
                        "Error adding for_hardware Script %s due to error - %s"
                        % (script.name, str(e)))
                    logger.error(err_msg)
                    Event.objects.create_node_event(
                        system_id=self.node.system_id,
                        event_type=EVENT_TYPES.SCRIPT_RESULT_ERROR,
                        event_description=err_msg)
Exemple #2
0
 def _add_user_selected_scripts(self, script_set, scripts=None, input=None):
     """Add user selected scripts to the ScriptSet."""
     if scripts is None:
         scripts = []
     if input is None:
         input = {}
     ids = [
         int(id) for id in scripts if isinstance(id, int) or id.isdigit()
     ]
     if script_set.result_type == RESULT_TYPE.COMMISSIONING:
         script_type = SCRIPT_TYPE.COMMISSIONING
     else:
         script_type = SCRIPT_TYPE.TESTING
     qs = Script.objects.filter(Q(name__in=scripts)
                                | Q(tags__overlap=scripts) | Q(id__in=ids),
                                script_type=script_type)
     modaliases = script_set.node.modaliases
     for script in qs:
         # If a script with the for_hardware field is selected by tag only
         # add it if matching hardware is found.
         if script.for_hardware and script.name not in scripts:
             matches = filter_modaliases(modaliases, *script.ForHardware)
             if len(matches) == 0:
                 continue
         try:
             script_set.add_pending_script(script, input)
         except ValidationError:
             script_set.delete()
             raise
Exemple #3
0
 def _add_user_selected_scripts(self,
                                script_set,
                                scripts=None,
                                script_input=None):
     """Add user selected scripts to the ScriptSet."""
     if scripts is None:
         scripts = []
     if script_input is None:
         script_input = {}
     ids = [
         int(id) for id in scripts if isinstance(id, int) or id.isdigit()
     ]
     if script_set.result_type == RESULT_TYPE.COMMISSIONING:
         script_type = SCRIPT_TYPE.COMMISSIONING
     else:
         script_type = SCRIPT_TYPE.TESTING
     qs = Script.objects.filter(
         Q(name__in=scripts) | Q(tags__overlap=scripts) | Q(id__in=ids),
         script_type=script_type,
     )
     modaliases = script_set.node.modaliases
     regexes = []
     for nmd in script_set.node.nodemetadata_set.all():
         if nmd.key in [
                 "system_vendor",
                 "system_product",
                 "system_version",
                 "mainboard_vendor",
                 "mainboard_product",
         ]:
             regexes.append("%s:%s" %
                            (nmd.key, fnmatch.translate(nmd.value)))
     if len(regexes) > 0:
         node_hw_regex = re.compile("^%s$" % "|".join(regexes), re.I)
     else:
         node_hw_regex = None
     for script in qs:
         # If a script with the for_hardware field is selected by tag only
         # add it if matching hardware is found.
         if script.for_hardware:
             found_hw_match = False
             if node_hw_regex is not None:
                 for hardware in script.for_hardware:
                     if node_hw_regex.search(hardware) is not None:
                         found_hw_match = True
                         break
             matches = filter_modaliases(modaliases, *script.ForHardware)
             if len(matches) == 0 and not found_hw_match:
                 continue
         try:
             script_set.add_pending_script(script, script_input)
         except ValidationError:
             script_set.delete()
             raise
Exemple #4
0
    def _find_scripts(self, script_qs, hw_pairs, modaliases):
        for script in script_qs:
            # If a script is not for specific hardware, it is always included
            if not script.for_hardware:
                yield script
                continue

            # If a script with the for_hardware field is selected by tag only
            # add it if matching hardware is found.
            for hardware in script.for_hardware:
                if hardware in hw_pairs:
                    yield script
                    break
            if filter_modaliases(modaliases, *script.ForHardware):
                yield script
Exemple #5
0
 def _add_user_selected_scripts(self,
                                script_set,
                                scripts=None,
                                script_input=None):
     """Add user selected scripts to the ScriptSet."""
     if scripts is None:
         scripts = []
     if script_input is None:
         script_input = {}
     ids = [
         int(id) for id in scripts if isinstance(id, int) or id.isdigit()
     ]
     if script_set.result_type == RESULT_TYPE.COMMISSIONING:
         script_type = SCRIPT_TYPE.COMMISSIONING
     else:
         script_type = SCRIPT_TYPE.TESTING
     qs = Script.objects.filter(
         Q(name__in=scripts) | Q(tags__overlap=scripts) | Q(id__in=ids),
         script_type=script_type,
     )
     modaliases = script_set.node.modaliases
     hw_pairs_qs = script_set.node.nodemetadata_set.filter(
         Q(key__startswith="system_")
         | Q(key__startswith="mainboard_")
         | Q(key__startswith="firmware_")
         | Q(key__startswith="chassis_")).values_list("key", "value")
     hw_pairs = [":".join(t) for t in hw_pairs_qs]
     for script in qs:
         # If a script with the for_hardware field is selected by tag only
         # add it if matching hardware is found.
         if script.for_hardware:
             found_hw_match = False
             if hw_pairs:
                 for hardware in script.for_hardware:
                     if hardware in hw_pairs:
                         found_hw_match = True
                         break
             matches = filter_modaliases(modaliases, *script.ForHardware)
             if len(matches) == 0 and not found_hw_match:
                 continue
         try:
             script_set.add_pending_script(script, script_input)
         except ValidationError:
             script_set.delete()
             raise
Exemple #6
0
    def select_for_hardware_scripts(self, modaliases=None):
        """Select for_hardware scripts for the given node and user input.

        Goes through an existing ScriptSet and adds any for_hardware tagged
        Script and removes those that were autoselected but the hardware has
        been removed.
        """
        # Only the builtin commissioning scripts run on controllers.
        if self.node.is_controller:
            return

        if modaliases is None:
            modaliases = self.node.modaliases

        regexes = []
        for nmd in self.node.nodemetadata_set.all():
            if nmd.key in [
                    'system_vendor', 'system_product', 'system_version',
                    'mainboard_vendor', 'mainboard_product'
            ]:
                regexes.append('%s:%s' %
                               (nmd.key, fnmatch.translate(nmd.value)))
        if len(regexes) > 0:
            node_hw_regex = re.compile('^%s$' % '|'.join(regexes), re.I)
        else:
            node_hw_regex = None

        # Remove scripts autoselected at the start of commissioning but updated
        # commissioning data shows the Script is no longer applicable.
        script_results = self.scriptresult_set.exclude(script=None)
        script_results = script_results.filter(status=SCRIPT_STATUS.PENDING)
        script_results = script_results.exclude(script__for_hardware=[])
        script_results = script_results.prefetch_related('script')
        script_results = script_results.only('script__for_hardware',
                                             'script_set_id')
        for script_result in script_results:
            matches = filter_modaliases(modaliases,
                                        *script_result.script.ForHardware)
            found_hw_match = False
            if node_hw_regex is not None:
                for hardware in script_result.script.for_hardware:
                    if node_hw_regex.search(hardware) is not None:
                        found_hw_match = True
                        break
            matches = filter_modaliases(modaliases,
                                        *script_result.script.ForHardware)
            if len(matches) == 0 and not found_hw_match:
                script_result.delete()

        # Add Scripts which match the node with current commissioning data.
        scripts = Script.objects.all()
        if self.result_type == RESULT_TYPE.COMMISSIONING:
            scripts = scripts.filter(script_type=SCRIPT_TYPE.COMMISSIONING)
        else:
            scripts = scripts.filter(script_type=SCRIPT_TYPE.TESTING)
        scripts = scripts.filter(tags__overlap=self.requested_scripts)
        scripts = scripts.exclude(for_hardware=[])
        scripts = scripts.exclude(name__in=[s.name for s in self])
        for script in scripts:
            found_hw_match = False
            if node_hw_regex is not None:
                for hardware in script.for_hardware:
                    if node_hw_regex.search(hardware) is not None:
                        found_hw_match = True
                        break
            matches = filter_modaliases(modaliases, *script.ForHardware)
            if len(matches) != 0 or found_hw_match:
                try:
                    self.add_pending_script(script)
                except ValidationError as e:
                    err_msg = (
                        "Error adding for_hardware Script %s due to error - %s"
                        % (script.name, str(e)))
                    logger.error(err_msg)
                    Event.objects.create_node_event(
                        system_id=self.node.system_id,
                        event_type=EVENT_TYPES.SCRIPT_RESULT_ERROR,
                        event_description=err_msg)
Exemple #7
0
    def select_for_hardware_scripts(self, modaliases=None):
        """Select for_hardware scripts for the given node and user input.

        Goes through an existing ScriptSet and adds any for_hardware tagged
        Script and removes those that were autoselected but the hardware has
        been removed.
        """
        # Only the builtin commissioning scripts run on controllers.
        if self.node.is_controller:
            return

        if modaliases is None:
            modaliases = self.node.modaliases

        hw_pairs_qs = self.node.nodemetadata_set.filter(
            Q(key__startswith="system_")
            | Q(key__startswith="mainboard_")
            | Q(key__startswith="firmware_")
            | Q(key__startswith="chassis_")).values_list("key", "value")
        hw_pairs = [":".join(t) for t in hw_pairs_qs]

        # Remove scripts autoselected at the start of commissioning but updated
        # commissioning data shows the Script is no longer applicable.
        script_results = self.scriptresult_set.exclude(script=None)
        script_results = script_results.filter(status=SCRIPT_STATUS.PENDING)
        script_results = script_results.exclude(script__for_hardware=[])
        script_results = script_results.prefetch_related("script")
        script_results = script_results.only("status", "script__for_hardware",
                                             "script_set_id")
        for script_result in script_results:
            matches = filter_modaliases(modaliases,
                                        *script_result.script.ForHardware)
            found_hw_match = False
            if hw_pairs:
                for hardware in script_result.script.for_hardware:
                    if hardware in hw_pairs:
                        found_hw_match = True
                        break
            matches = filter_modaliases(modaliases,
                                        *script_result.script.ForHardware)
            if len(matches) == 0 and not found_hw_match:
                script_result.delete()

        # Add Scripts which match the node with current commissioning data.
        scripts = Script.objects.all()
        if self.result_type == RESULT_TYPE.COMMISSIONING:
            scripts = scripts.filter(script_type=SCRIPT_TYPE.COMMISSIONING)
        else:
            scripts = scripts.filter(script_type=SCRIPT_TYPE.TESTING)
        scripts = scripts.filter(tags__overlap=self.requested_scripts)
        scripts = scripts.exclude(for_hardware=[])
        scripts = scripts.exclude(name__in=[s.name for s in self])
        for script in scripts:
            found_hw_match = False
            if hw_pairs:
                for hardware in script.for_hardware:
                    if hardware in hw_pairs:
                        found_hw_match = True
                        break
            matches = filter_modaliases(modaliases, *script.ForHardware)
            if len(matches) != 0 or found_hw_match:
                try:
                    self.add_pending_script(script)
                except ValidationError as e:
                    err_msg = (
                        "Error adding for_hardware Script %s due to error - %s"
                        % (script.name, str(e)))
                    logger.error(err_msg)
                    Event.objects.create_node_event(
                        system_id=self.node.system_id,
                        event_type=EVENT_TYPES.SCRIPT_RESULT_ERROR,
                        event_description=err_msg,
                    )