Merge ~d0ugal/maas:scriptset-refactor into maas:master

Proposed by Dougal Matthews
Status: Merged
Approved by: Dougal Matthews
Approved revision: 4a48bce47aab22a521175f1818df4f5488a1b72f
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~d0ugal/maas:scriptset-refactor
Merge into: maas:master
Diff against target: 93 lines (+32/-28)
1 file modified
src/metadataserver/models/scriptset.py (+32/-28)
Reviewer Review Type Date Requested Status
Alberto Donato (community) Approve
MAAS Lander Approve
Review via email: mp+394452@code.launchpad.net

Commit message

Refactor of the script selection code

To post a comment you must log in.
Revision history for this message
Dougal Matthews (d0ugal) wrote :

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b scriptset-refactor lp:~d0ugal/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas/job/branch-tester/8753/console
COMMIT: dbaee7fb1b7a3e9d3d14c3d4a285a3d73503a6d1

review: Needs Fixing
Revision history for this message
Dougal Matthews (d0ugal) wrote :

jenkins: !test

Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b scriptset-refactor lp:~d0ugal/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: cbc9f743ff734b723862eb18da3339ed773e71d3

review: Approve
Revision history for this message
Alberto Donato (ack) wrote :

Nice cleanup, +1

A couple of nits inline

review: Approve
Revision history for this message
Adam Collard (adam-collard) :
~d0ugal/maas:scriptset-refactor updated
4a48bce... by Dougal Matthews

Updates from feedback

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/metadataserver/models/scriptset.py b/src/metadataserver/models/scriptset.py
2index d18027c..4e8983d 100644
3--- a/src/metadataserver/models/scriptset.py
4+++ b/src/metadataserver/models/scriptset.py
5@@ -45,6 +45,19 @@ from provisioningserver.events import EVENT_TYPES
6 from provisioningserver.refresh.node_info_scripts import NODE_INFO_SCRIPTS
7
8
9+def _get_hw_pairs(script_set):
10+ """
11+ Given a ScriptSet return hardware pairs as a list of "key:value" strings.
12+ """
13+ hw_pairs_qs = script_set.node.nodemetadata_set.filter(
14+ Q(key__startswith="system_")
15+ | Q(key__startswith="mainboard_")
16+ | Q(key__startswith="firmware_")
17+ | Q(key__startswith="chassis_")
18+ ).values_list("key", "value")
19+ return [":".join(pair) for pair in hw_pairs_qs]
20+
21+
22 def get_status_from_qs(qs):
23 """Given a QuerySet or list of ScriptResults return the set's status."""
24 # If no tests have been run the QuerySet or list has no status.
25@@ -207,6 +220,22 @@ class ScriptSetManager(Manager):
26 self._clean_old(node, RESULT_TYPE.INSTALLATION, script_set)
27 return script_set
28
29+ def _find_scripts(self, script_qs, hw_pairs, modaliases):
30+ for script in script_qs:
31+ # If a script is not for specific hardware, it is always included
32+ if not script.for_hardware:
33+ yield script
34+ continue
35+
36+ # If a script with the for_hardware field is selected by tag only
37+ # add it if matching hardware is found.
38+ for hardware in script.for_hardware:
39+ if hardware in hw_pairs:
40+ yield script
41+ break
42+ if filter_modaliases(modaliases, *script.ForHardware):
43+ yield script
44+
45 def _add_user_selected_scripts(
46 self, script_set, scripts=None, script_input=None
47 ):
48@@ -226,27 +255,9 @@ class ScriptSetManager(Manager):
49 Q(name__in=scripts) | Q(tags__overlap=scripts) | Q(id__in=ids),
50 script_type=script_type,
51 )
52+ hw_pairs = _get_hw_pairs(script_set)
53 modaliases = script_set.node.modaliases
54- hw_pairs_qs = script_set.node.nodemetadata_set.filter(
55- Q(key__startswith="system_")
56- | Q(key__startswith="mainboard_")
57- | Q(key__startswith="firmware_")
58- | Q(key__startswith="chassis_")
59- ).values_list("key", "value")
60- hw_pairs = [":".join(t) for t in hw_pairs_qs]
61- for script in qs:
62- # If a script with the for_hardware field is selected by tag only
63- # add it if matching hardware is found.
64- if script.for_hardware:
65- found_hw_match = False
66- if hw_pairs:
67- for hardware in script.for_hardware:
68- if hardware in hw_pairs:
69- found_hw_match = True
70- break
71- matches = filter_modaliases(modaliases, *script.ForHardware)
72- if len(matches) == 0 and not found_hw_match:
73- continue
74+ for script in self._find_scripts(qs, hw_pairs, modaliases):
75 try:
76 script_set.add_pending_script(script, script_input)
77 except ValidationError:
78@@ -464,14 +475,7 @@ class ScriptSet(CleanSave, Model):
79
80 if modaliases is None:
81 modaliases = self.node.modaliases
82-
83- hw_pairs_qs = self.node.nodemetadata_set.filter(
84- Q(key__startswith="system_")
85- | Q(key__startswith="mainboard_")
86- | Q(key__startswith="firmware_")
87- | Q(key__startswith="chassis_")
88- ).values_list("key", "value")
89- hw_pairs = [":".join(t) for t in hw_pairs_qs]
90+ hw_pairs = _get_hw_pairs(self)
91
92 # Remove scripts autoselected at the start of commissioning but updated
93 # commissioning data shows the Script is no longer applicable.

Subscribers

People subscribed via source and target branches