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

Proposed by Igor Brovtsin
Status: Merged
Approved by: Igor Brovtsin
Approved revision: 19593037aa1ad8ce52acaccf92b4ba1efa6d3837
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~igor-brovtsin/maas:lp-1999064
Merge into: maas:master
Diff against target: 117 lines (+42/-42)
1 file modified
src/metadataserver/user_data/templates/snippets/maas_run_scripts.py (+42/-42)
Reviewer Review Type Date Requested Status
Adam Collard (community) Approve
MAAS Lander Approve
Review via email: mp+434256@code.launchpad.net

Commit message

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

To post a comment you must log in.
Revision history for this message
MAAS Lander (maas-lander) wrote :

UNIT TESTS
-b lp-1999064 lp:~igor-brovtsin/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 1dbaa02b76b61b54e5c455ec0aa950ca26d8ec76

review: Approve
Revision history for this message
Adam Collard (adam-collard) :
review: Needs Information
Revision history for this message
Adam Collard (adam-collard) wrote :

Let's refactor that script so that ScriptsPaths is always given a directory that exists, the caller is responsible for making a directory if needed, and cleanup afterwards.

Always clean up the directory, even if scripts fail - MAAS gets told what the errors are, and this is just a scratch directory that we use to download and execute.

~igor-brovtsin/maas:lp-1999064 updated
96028f5... by Igor Brovtsin

Made `base_path` a required argument for `ScriptsPaths`

So that directory creation and deletion is a responsibility of the
called

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

UNIT TESTS
-b lp-1999064 lp:~igor-brovtsin/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: FAILED
LOG: http://maas-ci.internal:8080/job/maas-tester/1575/consoleText
COMMIT: 96028f5acecab2186dfdeb6e3f3c39f033164e28

review: Needs Fixing
~igor-brovtsin/maas:lp-1999064 updated
ed4fefd... by Igor Brovtsin

Fixed missing `Path()` wrapper around temp dir path

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

UNIT TESTS
-b lp-1999064 lp:~igor-brovtsin/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: ed4fefdabab16d1bffc7cb282a298cb7b0adf12b

review: Approve
~igor-brovtsin/maas:lp-1999064 updated
1959303... by Igor Brovtsin

Use `TemporaryDirectory` context manager

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

UNIT TESTS
-b lp-1999064 lp:~igor-brovtsin/maas/+git/maas into -b master lp:~maas-committers/maas

STATUS: SUCCESS
COMMIT: 19593037aa1ad8ce52acaccf92b4ba1efa6d3837

review: Approve
Revision history for this message
Adam Collard (adam-collard) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/metadataserver/user_data/templates/snippets/maas_run_scripts.py b/src/metadataserver/user_data/templates/snippets/maas_run_scripts.py
2index 09e8855..b073771 100644
3--- a/src/metadataserver/user_data/templates/snippets/maas_run_scripts.py
4+++ b/src/metadataserver/user_data/templates/snippets/maas_run_scripts.py
5@@ -18,7 +18,7 @@ import shutil
6 import subprocess
7 import sys
8 import tarfile
9-from tempfile import mkdtemp
10+from tempfile import TemporaryDirectory
11 import time
12 import urllib.error
13
14@@ -46,9 +46,7 @@ class ExitError(Exception):
15
16
17 class ScriptsPaths:
18- def __init__(self, base_path=None):
19- if base_path is None:
20- base_path = Path(mkdtemp())
21+ def __init__(self, base_path):
22 self.scripts = base_path / "scripts"
23 self.out = base_path / "out"
24 self.downloads = base_path / "downloads"
25@@ -375,52 +373,54 @@ def action_report_results(ns):
26 if not config.metadata_url:
27 raise ExitError("No MAAS URL set")
28
29- paths = ScriptsPaths()
30- paths.ensure()
31+ with TemporaryDirectory() as base_path:
32+ paths = ScriptsPaths(Path(base_path))
33+ paths.ensure()
34
35- maas_url = get_base_url(config.metadata_url)
36- metadata_url = maas_url + "/MAAS/metadata/" + MD_VERSION + "/"
37+ maas_url = get_base_url(config.metadata_url)
38+ metadata_url = maas_url + "/MAAS/metadata/" + MD_VERSION + "/"
39
40- print(
41- "* Fetching scripts from {url} to {dir}".format(
42- url=metadata_url, dir=paths.scripts
43- )
44- )
45- for script in fetch_scripts(
46- maas_url, metadata_url, paths, config.credentials
47- ):
48- if not script.should_run():
49- continue
50 print(
51- f"* Running '{script.name}'...",
52- end="\n" if ns.debug else " ",
53+ "* Fetching scripts from {url} to {dir}".format(
54+ url=metadata_url, dir=paths.scripts
55+ )
56 )
57- result = script.run(console_output=ns.debug)
58- if ns.debug:
59+
60+ for script in fetch_scripts(
61+ maas_url, metadata_url, paths, config.credentials
62+ ):
63+ if not script.should_run():
64+ continue
65 print(
66- f"* Finished running '{script.name}': ",
67- end=" ",
68+ f"* Running '{script.name}'...",
69+ end="\n" if ns.debug else " ",
70 )
71- if result.exit_status == 0:
72- print("success")
73- else:
74- print(
75- "FAILED (status {result.exit_status}): {result.error}".format(
76- result=result
77+ result = script.run(console_output=ns.debug)
78+ if ns.debug:
79+ print(
80+ f"* Finished running '{script.name}': ",
81+ end=" ",
82 )
83+ if result.exit_status == 0:
84+ print("success")
85+ else:
86+ print(
87+ "FAILED (status {result.exit_status}): {result.error}".format(
88+ result=result
89+ )
90+ )
91+ signal(
92+ metadata_url,
93+ config.credentials,
94+ result.status,
95+ error=result.error,
96+ script_name=script.name,
97+ script_result_id=script.info.get("script_result_id"),
98+ files=result.result_files,
99+ runtime=result.runtime,
100+ exit_status=result.exit_status,
101+ script_version_id=script.info.get("script_version_id"),
102 )
103- signal(
104- metadata_url,
105- config.credentials,
106- result.status,
107- error=result.error,
108- script_name=script.name,
109- script_result_id=script.info.get("script_result_id"),
110- files=result.result_files,
111- runtime=result.runtime,
112- exit_status=result.exit_status,
113- script_version_id=script.info.get("script_version_id"),
114- )
115
116
117 def action_register_machine(ns):

Subscribers

People subscribed via source and target branches