-
Notifications
You must be signed in to change notification settings - Fork 60
/
hmftools-build.py
222 lines (185 loc) · 10 KB
/
hmftools-build.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"""
This script does the following:
- Whenever a user pushes a git tag in the format '<tool>-<version>' this script will parse that tag.
The 'v' character prefixing the version will be stripped.
- The script will build the tool with the given version and deploy it using Maven with version equal to the 'version'
part of the tag.
- The script will build all direct hmf-tools dependencies of the given tool (i.e. 'hmf-common') and deploy those with
the version set equal to '<tool>-<version>'.
By building and deploying each tool together with their dependencies, we ensure each deployment is isolated.
Example:
- 'git tag neo-v1.0.0' will start building the 'neo' tool and deploy it with version '1.0.0'.
- 'neo' has a dependency on 'hmf-common', so it will deploy 'hmf-common' with version 'neo-1.0.0'.
- The pom of the deployed 'neo' will be updated such that the 'hmf-common' dependency will point towards the correct
version.
"""
import re
import subprocess
import requests
import os.path
import sys
import time
import jwt
from xml.etree import ElementTree
from argparse import ArgumentParser
SEMVER_REGEX = re.compile(
r'^([a-z-]+)-v([0-9]+\.[0-9]+(?:\.[0-9]+)?(?:-(?:alpha|beta|rc)\.[0-9]+)?(?:_(?:[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?)$')
class Maven:
def __init__(self, pom_path, name=''):
self.pom_path = pom_path
self.name = name
def set_property(self, property, value):
subprocess.run(['mvn', '-f', self.pom_path, 'versions:set-property',
'-DgenerateBackupPoms=false', f'-Dproperty={property}', f'-DnewVersion={value}'], check=True)
def set_version(self, version):
subprocess.run(['mvn', '-f', self.pom_path, 'versions:set',
'-DgenerateBackupPoms=false', f'-DnewVersion={version}'], check=True)
@staticmethod
def deploy_all(*modules):
module_str = ','.join([m.name for m in modules])
subprocess.run(['mvn', 'deploy', '-B', '-pl', module_str, '-am', '-DdeployAtEnd=true'], check=True)
class Docker:
def __init__(self, module, version, is_external_release):
self.module = module
self.version = version
self.is_external_release = is_external_release
self.internal_image = f'europe-west4-docker.pkg.dev/hmf-build/hmf-docker/{self.module}:{self.version}'
self.external_image = f'hartwigmedicalfoundation/{self.module}:{self.version}'
def build(self):
with open("/workspace/entrypoint_template.sh", "r") as template, \
open(f"/workspace/{self.module}/target/entrypoint.sh", "w") as output:
for line in template:
output.write(line.rstrip().replace("__JAR_PATH__", f"/usr/share/java/{self.module}_v{self.version}.jar") + '\n')
with open("/workspace/docker.sh", "w") as output:
output.write('set -e\n')
output.write(f'[ ! -f {self.module}/Dockerfile ] && echo "No Dockerfile for {self.module}" && exit 0\n')
output.write('docker buildx create --name builder --driver docker-container --driver-opt network=cloudbuild --use\n')
output.write(f'docker buildx build --add-host metadata.google.internal:169.254.169.254 {self.module} --load -t {self.internal_image} -t {self.external_image} --build-arg VERSION={self.version}\n')
output.write(f'docker push {self.internal_image}\n')
if self.is_external_release:
output.write(f'cat /workspace/dockerhub.password | docker login -u hartwigmedicalfoundation --password-stdin\n')
output.write(f'docker push {self.external_image}\n')
else:
output.write(f'echo Not pushing to external Docker registry as {self.version} is an internal-only version')
class GithubRelease:
def __init__(self, tag_name: str, module: str, version: str, artifact_file: str, private_key: str, github_client_id: str,
github_installation_id: str):
self.tag_name = tag_name
self.module = module
self.version = version
self.artifact_file = artifact_file
self.private_key = private_key
self.github_client_id = github_client_id
self.github_installation_id = github_installation_id
self.release_name = f"{module} v{version}"
def create(self):
jwt = self._construct_jwt()
token = self._refresh_token(jwt)
print("Successfully refreshed token")
id = self._create_release(token)
print(f"Created release with id {id}")
self._upload_artifacts(id, token)
def _create_release(self, token: str):
print(f"Creating release [{self.release_name}]")
request = {"tag_name": self.tag_name,
"target_commitish": "master",
"name": self.release_name,
"body": f"Description of release {self.release_name}",
"prerelease": True,
"generate_release_notes": True
}
response = requests.post(self._construct_url("api"),
json = request,
headers = self._headers(token))
print(f"Response: {response.text}")
response.raise_for_status()
return response.json()["id"]
def _upload_artifacts(self, id: str, token: str):
print(f"Uploading artifact to release {id}")
headers = self._headers(token)
headers["Content-Type"] = "application/octet-stream"
base_url="{}/{}/assets?name".format(self._construct_url("uploads"), id)
response = requests.post(f"{base_url}={self.module}_v{self.version}.jar",
headers = headers,
data = self.artifact_file.read())
response.raise_for_status()
print(f"Uploaded {self.artifact_file.name}")
def _construct_url(self, prefix):
return f"https://{prefix}.github.com/repos/hartwigmedical/hmftools/releases"
# This method gleaned from Github docs/examples
def _construct_jwt(self):
payload = {
'iat': int(time.time()),
# JWT expiration time (10 minutes maximum)
'exp': int(time.time()) + 600,
'iss': self.github_client_id
}
return jwt.encode(payload, self.private_key, algorithm='RS256')
def _refresh_token(self, jwt: str):
response = requests.post(f"https://api.github.com/app/installations/{self.github_installation_id}/access_tokens",
headers = self._headers(jwt))
response.raise_for_status()
return response.json()["token"]
def _headers(self, token: str):
return {"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"X-GitHub-Api-Version": "2022-11-28"
}
def extract_hmftools_dependencies(pom_path):
namespace = {'ns': 'http://maven.apache.org/POM/4.0.0'}
# First, obtain a list of all modules defined in the parent
parsed_parent_pom = ElementTree.parse('pom.xml')
modules = parsed_parent_pom.getroot().findall('.//ns:modules/ns:module', namespace)
module_set = {module.text for module in modules}
# Then, obtain dependencies on these modules from target module
parsed_module_pom = ElementTree.parse(pom_path)
dependencies = parsed_module_pom.getroot().findall('.//ns:dependencies/ns:dependency', namespace)
hmftools_dependencies = set()
for dep in dependencies:
group_id = dep.find('ns:groupId', namespace).text
artifact_id = dep.find('ns:artifactId', namespace).text
if group_id == "com.hartwig" and artifact_id in module_set:
hmftools_dependencies.add(artifact_id)
return hmftools_dependencies
def main():
parser = ArgumentParser(
description="A tool for automatically building and deploying individual modules in HMF-tools.")
parser.add_argument('tag', help="The semantic versioning tag in the following format: <tool-name>-<version>")
parser.add_argument('github_key_path', help="Path to private key for the Github deployment bot")
parser.add_argument('github_client_id', help="Client id for the deployment bot")
parser.add_argument('github_installation_id', help="Installation id of the deployment bot")
args = parser.parse_args()
build_and_release(args.tag, args.github_key_path, args.github_client_id, args.github_installation_id)
def build_and_release(raw_tag: str, github_key: str, github_client_id: str, github_installation_id: str):
match = SEMVER_REGEX.match(raw_tag)
if not match:
print(f"Invalid tag: '{raw_tag}' (it does not match the regex pattern: '{SEMVER_REGEX.pattern}')")
exit(1)
module = match.group(1)
version = match.group(2)
is_external_release = re.compile(r'^[0-9\.]+(-rc\.[0-9]+)*$').match(version) != None
# Clean the raw_tag such that it only includes the groups captured by the regex
# For example: raw_tag = orange-v1.0.0 then tag = orange-1.1.0
tag = f'{module}-{version}'
# parse all the hmftools modules the project depends on from the pom.xml
hmftools_dependencies = extract_hmftools_dependencies(f'{module}/pom.xml')
parent_pom = Maven('pom.xml')
module_pom = Maven(f'{module}/pom.xml', name=module)
dependencies_pom = [Maven(f'{hmf_dep}/pom.xml', name=hmf_dep) for hmf_dep in hmftools_dependencies]
# Set versions in appropriate poms
# For the module we are targeting, we will use only the version part of the semver tag
# For all dependencies, we will use the entire semver tag
parent_pom.set_property(f'{module}.version', version)
for hmf_dep in hmftools_dependencies:
parent_pom.set_property(f'{hmf_dep}.version', tag)
parent_pom.set_version(tag)
module_pom.set_version(version)
Maven.deploy_all(module_pom, *dependencies_pom)
Docker(module, version, is_external_release).build()
if is_external_release:
GithubRelease(raw_tag, module, version, open(f'/workspace/{module}/target/{module}-{version}-jar-with-dependencies.jar', 'rb'),
open(github_key, "r").read(), github_client_id, github_installation_id).create()
else:
print(f'Skipping Github release creation as {version} is an internal-only version')
if __name__ == '__main__':
main()