forked from opensearch-project/opensearch-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle_recorder.py
102 lines (88 loc) · 4.29 KB
/
bundle_recorder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Copyright OpenSearch Contributors
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
import os
from typing import Any, Dict
from assemble_workflow.bundle_location import BundleLocation
from assemble_workflow.dists import Dists
from manifests.build_manifest import BuildComponent, BuildManifest
from manifests.bundle_manifest import BundleManifest
from manifests.manifest import Manifest
class BundleRecorder:
def __init__(self, build: BuildManifest.Build, output_dir: str, artifacts_dir: str, bundle_location: BundleLocation) -> None:
self.output_dir = output_dir
self.build_id = build.id
self.bundle_location = bundle_location
self.version = build.version
self.distribution = build.distribution
self.package_name = self.__get_package_name(build)
self.artifacts_dir = artifacts_dir
self.architecture = build.architecture
self.bundle_manifest = self.BundleManifestBuilder(
build.id,
build.name,
build.version,
build.platform,
build.architecture,
build.distribution,
self.__get_package_location(),
)
def __get_package_name(self, build: BuildManifest.Build) -> str:
parts = [
build.filename,
build.version,
build.platform,
build.architecture,
]
extension = Dists.DISTRIBUTIONS_MAP[self.distribution].extension if self.distribution else Dists.DISTRIBUTIONS_MAP['tar'].extension
return "-".join(parts) + extension
# Assembled output are expected to be served from a separate "dist" folder
# Example: https://ci.opensearch.org/ci/dbc/bundle-build/1.2.0/build-id/linux/x64/dist/
def __get_package_location(self) -> str:
return self.bundle_location.get_bundle_location(self.package_name)
# Build artifacts are expected to be served from a "builds" folder
# Example: https://ci.opensearch.org/ci/dbc/bundle-build/1.2.0/build-id/linux/x64/builds/
def __get_component_location(self, component_rel_path: str) -> str:
return self.bundle_location.get_build_location(component_rel_path) if component_rel_path else None
def record_component(self, component: BuildComponent, rel_path: str = None) -> None:
self.bundle_manifest.append_component(
component.name,
component.repository,
component.ref,
component.commit_id,
self.__get_component_location(rel_path),
)
def get_manifest(self) -> BundleManifest:
return self.bundle_manifest.to_manifest()
def write_manifest(self, folder: str) -> None:
manifest_path = os.path.join(folder, "manifest.yml")
self.get_manifest().to_file(manifest_path)
class BundleManifestBuilder:
def __init__(self, build_id: str, name: str, version: str, platform: str, architecture: str, distribution: str, location: str) -> None:
self.data: Dict[str, Any] = {}
self.data["build"] = {}
self.data["build"]["id"] = build_id
self.data["build"]["name"] = name
self.data["build"]["version"] = str(version)
self.data["build"]["platform"] = platform
self.data["build"]["architecture"] = architecture
self.data["build"]["distribution"] = distribution if distribution else "tar"
self.data["build"]["location"] = location
self.data["schema-version"] = "1.1"
# We need to store components as a hash so that we can append artifacts by component name
# When we convert to a BundleManifest this will get converted back into a list
self.data["components"] = []
def append_component(self, name: str, repository_url: str, ref: str, commit_id: str, location: str = None) -> None:
component = Manifest.compact({
"name": name,
"repository": repository_url,
"ref": ref,
"commit_id": commit_id,
"location": location,
})
self.data["components"].append(component)
def to_manifest(self) -> BundleManifest:
return BundleManifest(self.data)