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
diff --git a/src/metadataserver/models/scriptset.py b/src/metadataserver/models/scriptset.py
index d18027c..4e8983d 100644
--- a/src/metadataserver/models/scriptset.py
+++ b/src/metadataserver/models/scriptset.py
@@ -45,6 +45,19 @@ from provisioningserver.events import EVENT_TYPES
45from provisioningserver.refresh.node_info_scripts import NODE_INFO_SCRIPTS45from provisioningserver.refresh.node_info_scripts import NODE_INFO_SCRIPTS
4646
4747
48def _get_hw_pairs(script_set):
49 """
50 Given a ScriptSet return hardware pairs as a list of "key:value" strings.
51 """
52 hw_pairs_qs = script_set.node.nodemetadata_set.filter(
53 Q(key__startswith="system_")
54 | Q(key__startswith="mainboard_")
55 | Q(key__startswith="firmware_")
56 | Q(key__startswith="chassis_")
57 ).values_list("key", "value")
58 return [":".join(pair) for pair in hw_pairs_qs]
59
60
48def get_status_from_qs(qs):61def get_status_from_qs(qs):
49 """Given a QuerySet or list of ScriptResults return the set's status."""62 """Given a QuerySet or list of ScriptResults return the set's status."""
50 # If no tests have been run the QuerySet or list has no status.63 # If no tests have been run the QuerySet or list has no status.
@@ -207,6 +220,22 @@ class ScriptSetManager(Manager):
207 self._clean_old(node, RESULT_TYPE.INSTALLATION, script_set)220 self._clean_old(node, RESULT_TYPE.INSTALLATION, script_set)
208 return script_set221 return script_set
209222
223 def _find_scripts(self, script_qs, hw_pairs, modaliases):
224 for script in script_qs:
225 # If a script is not for specific hardware, it is always included
226 if not script.for_hardware:
227 yield script
228 continue
229
230 # If a script with the for_hardware field is selected by tag only
231 # add it if matching hardware is found.
232 for hardware in script.for_hardware:
233 if hardware in hw_pairs:
234 yield script
235 break
236 if filter_modaliases(modaliases, *script.ForHardware):
237 yield script
238
210 def _add_user_selected_scripts(239 def _add_user_selected_scripts(
211 self, script_set, scripts=None, script_input=None240 self, script_set, scripts=None, script_input=None
212 ):241 ):
@@ -226,27 +255,9 @@ class ScriptSetManager(Manager):
226 Q(name__in=scripts) | Q(tags__overlap=scripts) | Q(id__in=ids),255 Q(name__in=scripts) | Q(tags__overlap=scripts) | Q(id__in=ids),
227 script_type=script_type,256 script_type=script_type,
228 )257 )
258 hw_pairs = _get_hw_pairs(script_set)
229 modaliases = script_set.node.modaliases259 modaliases = script_set.node.modaliases
230 hw_pairs_qs = script_set.node.nodemetadata_set.filter(260 for script in self._find_scripts(qs, hw_pairs, modaliases):
231 Q(key__startswith="system_")
232 | Q(key__startswith="mainboard_")
233 | Q(key__startswith="firmware_")
234 | Q(key__startswith="chassis_")
235 ).values_list("key", "value")
236 hw_pairs = [":".join(t) for t in hw_pairs_qs]
237 for script in qs:
238 # If a script with the for_hardware field is selected by tag only
239 # add it if matching hardware is found.
240 if script.for_hardware:
241 found_hw_match = False
242 if hw_pairs:
243 for hardware in script.for_hardware:
244 if hardware in hw_pairs:
245 found_hw_match = True
246 break
247 matches = filter_modaliases(modaliases, *script.ForHardware)
248 if len(matches) == 0 and not found_hw_match:
249 continue
250 try:261 try:
251 script_set.add_pending_script(script, script_input)262 script_set.add_pending_script(script, script_input)
252 except ValidationError:263 except ValidationError:
@@ -464,14 +475,7 @@ class ScriptSet(CleanSave, Model):
464475
465 if modaliases is None:476 if modaliases is None:
466 modaliases = self.node.modaliases477 modaliases = self.node.modaliases
467478 hw_pairs = _get_hw_pairs(self)
468 hw_pairs_qs = self.node.nodemetadata_set.filter(
469 Q(key__startswith="system_")
470 | Q(key__startswith="mainboard_")
471 | Q(key__startswith="firmware_")
472 | Q(key__startswith="chassis_")
473 ).values_list("key", "value")
474 hw_pairs = [":".join(t) for t in hw_pairs_qs]
475479
476 # Remove scripts autoselected at the start of commissioning but updated480 # Remove scripts autoselected at the start of commissioning but updated
477 # commissioning data shows the Script is no longer applicable.481 # commissioning data shows the Script is no longer applicable.

Subscribers

People subscribed via source and target branches