Skip to content

Commit 33ef834

Browse files
authored
Refactor and simplify assembly workflow. (#827)
* Extracted assemble args into a class, add keep. Signed-off-by: dblock <[email protected]> * Remove TemporaryDirectory inside another TemporaryDirectory. Signed-off-by: dblock <[email protected]>
1 parent 27f390e commit 33ef834

File tree

7 files changed

+119
-38
lines changed

7 files changed

+119
-38
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# The OpenSearch Contributors require contributions made to
4+
# this file be licensed under the Apache-2.0 license or a
5+
# compatible open source license.
6+
7+
import argparse
8+
import logging
9+
10+
11+
class AssembleArgs:
12+
manifest: str
13+
keep: bool
14+
15+
def __init__(self):
16+
parser = argparse.ArgumentParser(description="Assemble an OpenSearch Distribution")
17+
parser.add_argument("manifest", type=argparse.FileType("r"), help="Manifest file.")
18+
parser.add_argument("-b", "--base-url", dest="base_url", help="The base url to download the artifacts.")
19+
parser.add_argument(
20+
"--keep",
21+
dest="keep",
22+
action="store_true",
23+
help="Do not delete the working temporary directory.",
24+
)
25+
parser.add_argument(
26+
"-v",
27+
"--verbose",
28+
help="Show more verbose output.",
29+
action="store_const",
30+
default=logging.INFO,
31+
const=logging.DEBUG,
32+
dest="logging_level",
33+
)
34+
35+
args = parser.parse_args()
36+
self.logging_level = args.logging_level
37+
self.manifest = args.manifest
38+
self.keep = args.keep
39+
self.base_url = args.base_url

src/assemble_workflow/bundle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __enter__(self):
2929
def __exit__(self, exc_type, exc_value, exc_traceback):
3030
self.tmp_dir.__exit__(exc_type, exc_value, exc_traceback)
3131

32-
def __init__(self, build_manifest, artifacts_dir, bundle_recorder):
32+
def __init__(self, build_manifest, artifacts_dir, bundle_recorder, keep=False):
3333
"""
3434
Construct a new Bundle instance.
3535
:param build_manifest: A BuildManifest created from the build workflow.
@@ -39,7 +39,7 @@ def __init__(self, build_manifest, artifacts_dir, bundle_recorder):
3939
self.plugins = self.__get_plugins(build_manifest.components.values())
4040
self.artifacts_dir = artifacts_dir
4141
self.bundle_recorder = bundle_recorder
42-
self.tmp_dir = TemporaryDirectory()
42+
self.tmp_dir = TemporaryDirectory(keep=keep)
4343
self.min_dist = self.__get_min_dist(build_manifest.components.values())
4444
self.installed_plugins = []
4545

src/assemble_workflow/bundles.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ def from_name(cls, name):
2222
return klass
2323

2424
@classmethod
25-
def create(cls, build_manifest, artifacts_dir, bundle_recorder):
25+
def create(cls, build_manifest, artifacts_dir, bundle_recorder, keep):
2626
klass = cls.from_name(build_manifest.build.name)
27-
return klass(build_manifest, artifacts_dir, bundle_recorder)
27+
return klass(build_manifest, artifacts_dir, bundle_recorder, keep)

src/run_assemble.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,19 @@
66
# this file be licensed under the Apache-2.0 license or a
77
# compatible open source license.
88

9-
import argparse
109
import logging
1110
import os
1211
import sys
1312

13+
from assemble_workflow.assemble_args import AssembleArgs
1414
from assemble_workflow.bundle_recorder import BundleRecorder
1515
from assemble_workflow.bundles import Bundles
1616
from manifests.build_manifest import BuildManifest
1717
from system import console
18-
from system.temporary_directory import TemporaryDirectory
1918

2019

2120
def main():
22-
parser = argparse.ArgumentParser(description="Assemble an OpenSearch Bundle")
23-
parser.add_argument("manifest", type=argparse.FileType("r"), help="Manifest file.")
24-
parser.add_argument(
25-
"-v",
26-
"--verbose",
27-
help="Show more verbose output.",
28-
action="store_const",
29-
default=logging.INFO,
30-
const=logging.DEBUG,
31-
dest="logging_level",
32-
)
33-
parser.add_argument("-b", "--base-url", dest="base_url", help="The base url to download the artifacts.")
34-
args = parser.parse_args()
21+
args = AssembleArgs()
3522

3623
console.configure(level=args.logging_level)
3724

@@ -41,21 +28,20 @@ def main():
4128
output_dir = os.path.join(os.getcwd(), "dist")
4229
os.makedirs(output_dir, exist_ok=True)
4330

44-
with TemporaryDirectory(chdir=True):
45-
logging.info(f"Bundling {build.name} ({build.architecture}) on {build.platform} into {output_dir} ...")
31+
logging.info(f"Bundling {build.name} ({build.architecture}) on {build.platform} into {output_dir} ...")
4632

47-
bundle_recorder = BundleRecorder(build, output_dir, artifacts_dir, args.base_url)
33+
bundle_recorder = BundleRecorder(build, output_dir, artifacts_dir, args.base_url)
4834

49-
with Bundles.create(build_manifest, artifacts_dir, bundle_recorder) as bundle:
50-
bundle.install_min()
51-
bundle.install_plugins()
52-
logging.info(f"Installed plugins: {bundle.installed_plugins}")
35+
with Bundles.create(build_manifest, artifacts_dir, bundle_recorder, args.keep) as bundle:
36+
bundle.install_min()
37+
bundle.install_plugins()
38+
logging.info(f"Installed plugins: {bundle.installed_plugins}")
5339

54-
# Save a copy of the manifest inside of the tar
55-
bundle_recorder.write_manifest(bundle.min_dist.archive_path)
56-
bundle.package(output_dir)
40+
# Save a copy of the manifest inside of the tar
41+
bundle_recorder.write_manifest(bundle.min_dist.archive_path)
42+
bundle.package(output_dir)
5743

58-
bundle_recorder.write_manifest(output_dir)
44+
bundle_recorder.write_manifest(output_dir)
5945

6046
logging.info("Done.")
6147

tests/test_run_assemble.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# compatible open source license.
66

77
import os
8-
import tempfile
98
import unittest
109
from unittest.mock import MagicMock, call, patch
1110

@@ -31,14 +30,12 @@ def test_usage(self, *mocks):
3130

3231
@patch("os.chdir")
3332
@patch("os.makedirs")
33+
@patch("shutil.copy2")
3434
@patch("os.getcwd", return_value="curdir")
3535
@patch("argparse._sys.argv", ["run_assemble.py", BUILD_MANIFEST])
3636
@patch("run_assemble.Bundles.create")
3737
@patch("run_assemble.BundleRecorder", return_value=MagicMock())
38-
@patch("run_assemble.TemporaryDirectory")
39-
@patch("shutil.copy2")
40-
def test_main(self, mock_copy, mock_temp, mock_recorder, mock_bundles, *mocks):
41-
mock_temp.return_value.__enter__.return_value.name = tempfile.gettempdir()
38+
def test_main(self, mock_recorder, mock_bundles, *mocks):
4239
mock_bundle = MagicMock(min_dist=MagicMock(archive_path="path"))
4340
mock_bundles.return_value.__enter__.return_value = mock_bundle
4441

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# The OpenSearch Contributors require contributions made to
4+
# this file be licensed under the Apache-2.0 license or a
5+
# compatible open source license.
6+
7+
import logging
8+
import os
9+
import unittest
10+
from unittest.mock import patch
11+
12+
from assemble_workflow.assemble_args import AssembleArgs
13+
14+
15+
class TestAssembleArgs(unittest.TestCase):
16+
17+
ASSEMBLE_PY = "./src/run_assembly.py"
18+
19+
OPENSEARCH_MANIFEST = os.path.realpath(
20+
os.path.join(
21+
os.path.dirname(__file__),
22+
"..",
23+
"..",
24+
"manifests",
25+
"1.1.0",
26+
"opensearch-1.1.0.yml",
27+
)
28+
)
29+
30+
@patch("argparse._sys.argv", [ASSEMBLE_PY, OPENSEARCH_MANIFEST])
31+
def test_manifest(self):
32+
self.assertEqual(AssembleArgs().manifest.name, TestAssembleArgs.OPENSEARCH_MANIFEST)
33+
34+
@patch("argparse._sys.argv", [ASSEMBLE_PY, OPENSEARCH_MANIFEST])
35+
def test_keep_default(self):
36+
self.assertFalse(AssembleArgs().keep)
37+
38+
@patch("argparse._sys.argv", [ASSEMBLE_PY, OPENSEARCH_MANIFEST, "--keep"])
39+
def test_keep_true(self):
40+
self.assertTrue(AssembleArgs().keep)
41+
42+
@patch("argparse._sys.argv", [ASSEMBLE_PY, OPENSEARCH_MANIFEST])
43+
def test_verbose_default(self):
44+
self.assertEqual(AssembleArgs().logging_level, logging.INFO)
45+
46+
@patch("argparse._sys.argv", [ASSEMBLE_PY, OPENSEARCH_MANIFEST, "--verbose"])
47+
def test_verbose_true(self):
48+
self.assertTrue(AssembleArgs().logging_level, logging.DEBUG)
49+
50+
@patch("argparse._sys.argv", [ASSEMBLE_PY, OPENSEARCH_MANIFEST, "--base-url", "url"])
51+
def test_base_url(self):
52+
self.assertEqual(AssembleArgs().base_url, "url")

tests/tests_assemble_workflow/test_bundles.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ class TestBundles(unittest.TestCase):
1818
def test_bundle_opensearch(self):
1919
manifest_path = os.path.join(os.path.dirname(__file__), "data", "opensearch-build-linux-1.1.0.yml")
2020
artifacts_path = os.path.join(os.path.dirname(__file__), "data", "artifacts")
21-
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock())
21+
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock(), False)
2222
self.assertIs(type(bundle), BundleOpenSearch)
2323

2424
def test_bundle_opensearch_dashboards(self):
2525
manifest_path = os.path.join(os.path.dirname(__file__), "data", "opensearch-dashboards-build-1.1.0.yml")
2626
artifacts_path = os.path.join(os.path.dirname(__file__), "data", "artifacts")
27-
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock())
27+
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock(), False)
2828
self.assertIs(type(bundle), BundleOpenSearchDashboards)
29+
self.assertFalse(bundle.tmp_dir.keep)
30+
31+
def test_bundle_keep(self):
32+
manifest_path = os.path.join(os.path.dirname(__file__), "data", "opensearch-build-linux-1.1.0.yml")
33+
artifacts_path = os.path.join(os.path.dirname(__file__), "data", "artifacts")
34+
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock(), True)
35+
self.assertTrue(bundle.tmp_dir.keep)
2936

3037
def test_bundle_opensearch_invalid(self):
3138
manifest = BuildManifest(
@@ -41,5 +48,5 @@ def test_bundle_opensearch_invalid(self):
4148
}
4249
)
4350
with self.assertRaises(ValueError) as ctx:
44-
Bundles.create(manifest, "path", MagicMock())
51+
Bundles.create(manifest, "path", MagicMock(), False)
4552
self.assertEqual(str(ctx.exception), "Unsupported bundle: invalid")

0 commit comments

Comments
 (0)