← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~igor-brovtsin/maas:lp-1999064 into maas:master

 

Igor Brovtsin has proposed merging ~igor-brovtsin/maas:lp-1999064 into maas:master.

Commit message:
fix(LP#1999064): add cleanup to `maas_run_scripts.py` when no scripts failed

Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~igor-brovtsin/maas/+git/maas/+merge/434256
-- 
Your team MAAS Maintainers is requested to review the proposed merge of ~igor-brovtsin/maas:lp-1999064 into maas:master.
diff --git a/src/metadataserver/user_data/templates/snippets/maas_run_scripts.py b/src/metadataserver/user_data/templates/snippets/maas_run_scripts.py
index 09e8855..dd4e01e 100644
--- a/src/metadataserver/user_data/templates/snippets/maas_run_scripts.py
+++ b/src/metadataserver/user_data/templates/snippets/maas_run_scripts.py
@@ -49,6 +49,7 @@ class ScriptsPaths:
     def __init__(self, base_path=None):
         if base_path is None:
             base_path = Path(mkdtemp())
+        self.base_path = base_path
         self.scripts = base_path / "scripts"
         self.out = base_path / "out"
         self.downloads = base_path / "downloads"
@@ -61,6 +62,10 @@ class ScriptsPaths:
             directory.mkdir()
         self.resources_file.touch()
 
+    def cleanup(self):
+        if os.path.isdir(self.base_path):
+            shutil.rmtree(self.base_path)
+
 
 ScriptRunResult = namedtuple(
     "ScriptRunResult",
@@ -386,6 +391,7 @@ def action_report_results(ns):
             url=metadata_url, dir=paths.scripts
         )
     )
+    any_script_failed = False
     for script in fetch_scripts(
         maas_url, metadata_url, paths, config.credentials
     ):
@@ -409,6 +415,7 @@ def action_report_results(ns):
                     result=result
                 )
             )
+            any_script_failed = True
         signal(
             metadata_url,
             config.credentials,
@@ -422,6 +429,9 @@ def action_report_results(ns):
             script_version_id=script.info.get("script_version_id"),
         )
 
+    if not any_script_failed:
+        paths.cleanup()
+
 
 def action_register_machine(ns):
     hostname = ns.hostname
diff --git a/src/metadataserver/user_data/templates/snippets/tests/test_maas_run_scripts.py b/src/metadataserver/user_data/templates/snippets/tests/test_maas_run_scripts.py
index a640767..bc28990 100644
--- a/src/metadataserver/user_data/templates/snippets/tests/test_maas_run_scripts.py
+++ b/src/metadataserver/user_data/templates/snippets/tests/test_maas_run_scripts.py
@@ -51,6 +51,12 @@ class TestScriptsPaths(MAASTestCase):
         scripts_paths.ensure()
         self.assertFalse(a_file.exists())
 
+    def test_cleanup_cleans_up(self):
+        base_path = Path(self.useFixture(TempDirectory()).path)
+        scripts_paths = ScriptsPaths(base_path=base_path)
+        scripts_paths.cleanup()
+        self.assertFalse(base_path.exists())
+
 
 class TestScript(MAASTestCase):
     def test_properties(self):

Follow ups