Skip to content

Commit

Permalink
Rename min bundle folder name in patch releases.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <dblock@dblock.org>
  • Loading branch information
dblock committed Dec 10, 2021
1 parent 90ad75b commit ac07cd9
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 15 deletions.
5 changes: 3 additions & 2 deletions src/assemble_workflow/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ def __init__(self, build_manifest: BuildManifest, artifacts_dir: str, bundle_rec
:param artifacts_dir: Dir location where build artifacts can be found locally
:param bundle_recorder: The bundle recorder that will capture and build a BundleManifest
"""
self.build = build_manifest.build
self.plugins = self.__get_plugins(build_manifest.components)
self.artifacts_dir = artifacts_dir
self.bundle_recorder = bundle_recorder
self.tmp_dir = TemporaryDirectory(keep=keep)
self.min_dist = self.__get_min_dist(build_manifest.components)
self.installed_plugins: List[str] = []
self.build = build_manifest.build

def install_min(self) -> None:
install_script = ScriptFinder.find_install_script(self.min_dist.name)
Expand Down Expand Up @@ -131,7 +131,8 @@ def __get_min_dist(self, build_components: BuildComponents) -> Dist:
raise ValueError('Missing min "dist" in input artifacts.')
min_dist_path = self._copy_component(min_bundle, "dist")
logging.info(f"Copied min bundle to {min_dist_path}.")
min_dist = Dist.from_path(min_bundle.name, min_dist_path)
min_path = f"{self.build.filename}-{self.build.version}".replace("-SNAPSHOT", "")
min_dist = Dist.from_path(min_bundle.name, min_dist_path, min_path)
logging.info(f"Extracting dist into {self.tmp_dir.name}.")
min_dist.extract(self.tmp_dir.name)
logging.info(f"Extracted dist into {self.tmp_dir.name}.")
Expand Down
39 changes: 31 additions & 8 deletions src/assemble_workflow/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@


class Dist(ABC):
def __init__(self, name: str, path: str) -> None:
def __init__(self, name: str, path: str, min_path: str) -> None:
self.name = name
self.path = path
self.min_path = min_path

@abstractmethod
def __extract__(self, dest: str) -> None:
Expand All @@ -28,10 +29,10 @@ def __extract__(self, dest: str) -> None:
def __build__(self, name: str, dest: str) -> None:
pass

def extract(self, dest: str) -> str:
self.__extract__(dest)

# OpenSearch & Dashboard tars will include only a single folder at the top level of the tar.
def __find_min_archive_path(self, dest: str) -> str:
'''
Return the single folder at the top level of the tar.
'''

for file in os.scandir(dest):
if file.is_dir():
Expand All @@ -40,19 +41,41 @@ def extract(self, dest: str) -> str:

raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), os.path.join(dest, "*"))

def __rename_archive_path(self, path: str) -> str:
'''
Rename the single folder at the top level of the tar that contains the min distribution to match current version.
For example, when OpenSearch 1.1.1 is built using the 1.1.0 artifact, we rename opensearch-1.1.0 to opensearch-1.1.1.
'''
current_name = os.path.basename(path)
target_name = self.min_path
if current_name != target_name:
logging.info(f"Renaming {path} to {target_name}.")
target_path = os.path.join(os.path.dirname(path), target_name)
os.rename(path, target_path)
return target_path
else:
return path

def extract(self, dest: str) -> str:
self.__extract__(dest)
self.archive_path = self.__rename_archive_path(
self.__find_min_archive_path(dest)
)
return self.archive_path

def build(self, name: str, dest: str) -> None:
self.__build__(name, dest)
path = os.path.join(dest, name)
shutil.copyfile(name, path)
logging.info(f"Published {path}.")

@classmethod
def from_path(cls, name: str, path: str) -> 'Dist':
def from_path(cls, name: str, path: str, min_path: str) -> 'Dist':
ext = os.path.splitext(path)[1]
if ext == ".gz":
return DistTar(name, path)
return DistTar(name, path, min_path)
elif ext == ".zip":
return DistZip(name, path)
return DistZip(name, path, min_path)
else:
raise ValueError(f'Invalid min "dist" extension in input artifacts: {ext} ({path}).')

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
119 changes: 119 additions & 0 deletions tests/tests_assemble_workflow/data/opensearch-build-linux-1.1.1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
build:
architecture: x64
id: f49ffcf2c1b341d1b29e147093689c6d
name: OpenSearch
platform: linux
version: 1.1.1-SNAPSHOT
components:
- artifacts:
dist:
- dist/opensearch-min-1.1.0-linux-x64.tar.gz
commit_id: 15e9f137622d878b79103df8f82d78d782b686a1
name: OpenSearch
ref: '1.1'
repository: https://github.com/opensearch-project/OpenSearch.git
version: 1.1.1.0-SNAPSHOT
- commit_id: 8d05d8a55ac0e70ba68569f872ad32f8aa32c0ea
name: common-utils
ref: '1.1'
repository: https://github.com/opensearch-project/common-utils.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-job-scheduler-1.1.0.0.zip
commit_id: 0a4ce10a812165e95801b77ede689064ffee2188
name: job-scheduler
ref: '1.1'
repository: https://github.com/opensearch-project/job-scheduler.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-reports-scheduler-1.1.1.0-SNAPSHOT.zip
commit_id: d33924fb14086ebb47df8cafce528393a4aaa015
name: dashboards-reports
ref: '1.1'
repository: https://github.com/opensearch-project/dashboards-reports.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-sql-1.1.0.0.zip
commit_id: ceedf9a1da2c35f64e5aa95077098baa71df46f0
name: sql
ref: '1.1'
repository: https://github.com/opensearch-project/sql.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-alerting-1.1.0.0.zip
commit_id: 3a8b8748e0dc01c3418f50143cdd651e66b78557
name: alerting
ref: '1.1'
repository: https://github.com/opensearch-project/alerting.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-security-1.1.0.0.zip
commit_id: 6436c155347355292ed91c108eade5f6f119c40b
name: security
ref: '1.1'
repository: https://github.com/opensearch-project/security.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-cross-cluster-replication-1.1.0.0.zip
commit_id: 7e16c39ab0197acd865e1b501955306cb32a0150
name: cross-cluster-replication
ref: '1.1'
repository: https://github.com/opensearch-project/cross-cluster-replication.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-performance-analyzer-1.1.0.0.zip
commit_id: e921d570d9af4a82aafc981e6a41591ded4fa7d3
name: performance-analyzer
ref: '1.1'
repository: https://github.com/opensearch-project/performance-analyzer.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-index-management-1.1.0.0.zip
commit_id: 1d8fbd404a7a4b226cd1d42877b51776ba127cdb
name: index-management
ref: '1.1'
repository: https://github.com/opensearch-project/index-management.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
libs:
- libs/libKNNIndexV2_0_11.so
plugins:
- plugins/opensearch-knn-1.1.0.0.zip
commit_id: 9fa629073365d34b2450428b33405c8d36fb6c5b
name: k-NN
ref: '1.1'
repository: https://github.com/opensearch-project/k-NN.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-anomaly-detection-1.1.0.0.zip
commit_id: 569cedeedab46167614fb0148db37b35cd648712
name: anomaly-detection
ref: '1.1'
repository: https://github.com/opensearch-project/anomaly-detection.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-asynchronous-search-1.1.0.0.zip
commit_id: 83492829b92b222d25551375ba5277c28bf13005
name: asynchronous-search
ref: '1.1'
repository: https://github.com/opensearch-project/asynchronous-search.git
version: 1.1.1.0-SNAPSHOT
- artifacts:
plugins:
- plugins/opensearch-notebooks-1.1.0.0.zip
commit_id: 0d498b10f62f0cc3896b736651af5103132200e1
name: dashboards-notebooks
ref: '1.1'
repository: https://github.com/opensearch-project/dashboards-notebooks.git
version: 1.1.1.0-SNAPSHOT
schema-version: '1.2'
52 changes: 50 additions & 2 deletions tests/tests_assemble_workflow/test_bundle_opensearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,54 @@ def test_bundle_install_min(self) -> None:
]
)

@patch("subprocess.check_call")
@patch("os.rename")
def test_bundle_install_min_no_patch(self, mock_os_rename: Mock, mock_check_call: Mock) -> None:
manifest_path = os.path.join(os.path.dirname(__file__), "data", "opensearch-build-linux-1.1.0.yml")
artifacts_path = os.path.join(os.path.dirname(__file__), "data", "artifacts")
bundle = BundleOpenSearch(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock())
bundle.install_min()
mock_os_rename.assert_not_called()
mock_check_call.assert_called()

@patch("subprocess.check_call")
@patch("os.rename")
def test_bundle_install_min_patch(self, mock_os_rename: Mock, mock_check_call: Mock) -> None:
manifest_path = os.path.join(os.path.dirname(__file__), "data", "opensearch-build-linux-1.1.1.yml")
artifacts_path = os.path.join(os.path.dirname(__file__), "data", "artifacts")
bundle = BundleOpenSearch(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock())

bundle.install_min()

self.assertEqual(mock_check_call.call_count, 1)

mock_os_rename.assert_called_with(
os.path.join(bundle.tmp_dir.name, "opensearch-1.1.0"),
os.path.join(bundle.tmp_dir.name, "opensearch-1.1.1")
)

mock_check_call.assert_has_calls(
[
call(
" ".join(
[
"bash",
ScriptFinder.find_install_script("OpenSearch"),
"-v 1.1.1-SNAPSHOT",
"-p linux",
"-a x64",
"-f",
artifacts_path,
"-o",
bundle.min_dist.archive_path,
]
),
cwd=bundle.min_dist.archive_path,
shell=True,
),
]
)

@patch.object(BundleOpenSearch, "install_plugin")
def test_bundle_install_plugins(self, bundle_install_plugin: Mock) -> None:
manifest_path = os.path.join(os.path.dirname(__file__), "data", "opensearch-build-linux-1.1.0.yml")
Expand Down Expand Up @@ -132,7 +180,7 @@ def test_bundle_package_tar(self) -> None:
with patch("shutil.copyfile") as mock_copyfile:
bundle.package(os.path.dirname(__file__))
mock_tarfile_open.assert_called_with("opensearch.tar", "w:gz")
mock_tarfile_add.assert_called_with(os.path.join(bundle.tmp_dir.name, "bundle"), arcname="bundle")
mock_tarfile_add.assert_called_with(os.path.join(bundle.tmp_dir.name, "opensearch-1.1.0"), arcname="opensearch-1.1.0")
self.assertEqual(mock_copyfile.call_count, 1)

def test_bundle_package_zip(self) -> None:
Expand All @@ -150,5 +198,5 @@ def test_bundle_package_zip(self) -> None:
with patch("shutil.copyfile") as mock_copyfile:
bundle.package(os.path.dirname(__file__))
mock_zipfile_open.assert_called_with("opensearch.zip", "w", zipfile.ZIP_DEFLATED)
mock_zipfile_write.assert_called_with(os.path.join(bundle.tmp_dir.name, "dist", "opensearch.txt"), "opensearch.txt")
mock_zipfile_write.assert_called_with(os.path.join(bundle.tmp_dir.name, "opensearch-1.1.0", "opensearch.txt"), "opensearch.txt")
self.assertEqual(mock_copyfile.call_count, 1)
6 changes: 3 additions & 3 deletions tests/tests_assemble_workflow/test_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

class TestDist(unittest.TestCase):
def test_from_path_tar_gz(self) -> None:
dist = Dist.from_path("opensearch", "filename.tar.gz")
dist = Dist.from_path("opensearch", "filename.tar.gz", "opensearch-1.1.0")
self.assertIs(type(dist), DistTar)

def test_from_path_zip(self) -> None:
dist = Dist.from_path("opensearch", "filename.zip")
dist = Dist.from_path("opensearch", "filename.zip", "opensearch-1.1.0")
self.assertIs(type(dist), DistZip)

def test_from_path_invalid(self) -> None:
with self.assertRaises(ValueError) as ctx:
Dist.from_path("opensearch", "filename.invalid")
Dist.from_path("opensearch", "filename.invalid", "opensearch-1.1.0")
self.assertEqual(str(ctx.exception), 'Invalid min "dist" extension in input artifacts: .invalid (filename.invalid).')

0 comments on commit ac07cd9

Please sign in to comment.