-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild_img.py
More file actions
283 lines (228 loc) · 8.15 KB
/
Copy pathbuild_img.py
File metadata and controls
283 lines (228 loc) · 8.15 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import sys
import subprocess
import datetime
import argparse
import re
import json
import platform
options = None
push_log = {"versions":{}}
versions = [
# Precise
# "gcc-4.4", "gcc-4.5",
# Trusty
# "clang-2.9", "clang-3.0", "clang-3.1", "clang-3.2", "clang-3.3",
# "clang-3.4", "clang-3.5", "clang-3.6", "clang-3.7", "clang-3.8",
# "gcc-4.6", "gcc-4.7", "gcc-4.8", "gcc-4.9", "gcc-5", "gcc-6",
# Xenial
"clang-3.9", "clang-4", "clang-5", "clang-6",
"gcc-7",
# Bionic
"clang-7", "clang-8", "clang-9", "clang-10",
"gcc-8",
# Focal
"clang-11", "clang-12", "clang-13", "clang-14",
"gcc-9", "gcc-10", "gcc-11",
# Jammy
"clang-15", "clang-16", "clang-16", "clang-17"
"gcc-12", "gcc-13",
# Noble
"clang-18", "clang-19", "clang-20", "clang-21", "clang-22", "clang-23",
"gcc-14", "gcc-15",
"filc-0"
]
test_versions = {}
class Image(object):
def __init__(self, repo, tag):
self.repo = repo
self.tag = tag
@property
def image(self):
return f"{self.repo}:{self.tag}"
def run_my_cmd(cmd):
try:
print(cmd)
subprocess.check_call(cmd, shell=True)
except Exception:
print("Failure in command: " + cmd)
raise
def build(version):
image = Image(options.repo, version)
force = "--no-cache"
if options.no_force:
force = ""
pull = "--pull"
if options.no_pull_base:
pull = ""
cmd = f"docker build {pull} {force} --tag {image.image} {version}"
run_my_cmd(cmd)
return image
def test(image, test_version):
pass
def tag_timestamp(base_image, version):
timestamp = datetime.datetime.utcnow().strftime("%Y%m%d_%H%M")
arch_str = f"_{options.arch}" if options.arch else ""
tag = f"{version}{arch_str}_{timestamp}"
image = Image(options.repo, tag)
cmd = f"docker tag {base_image.image} {image.image}"
run_my_cmd(cmd)
return image
def tag_latest(base_image):
image = Image(options.repo,"latest")
cmd = f"docker tag {base_image.image} {image.image}"
run_my_cmd(cmd)
return image
def push_image(image):
cmd = f"docker push {image.image}"
run_my_cmd(cmd)
def create_and_push_manifest(version_tag, amend_tags):
manifest_image = Image(options.repo, version_tag)
cmd = f"docker manifest rm {manifest_image.image}"
try:
run_my_cmd(cmd)
except subprocess.CalledProcessError:
pass
cmd = f"docker manifest create {manifest_image.image}"
for tag in amend_tags:
cmd += f" --amend {options.repo}:{tag}"
run_my_cmd(cmd)
cmd = f"docker manifest push {manifest_image.image}"
run_my_cmd(cmd)
def remove_image(image):
cmd = f"docker rmi {image.image}"
run_my_cmd(cmd)
def all():
for version in versions:
latest = False
if options.latest and version == versions[-1]:
latest = True
build_one(version, latest)
def build_one(version, push_latest=False):
tags = []
base_image = None
time_image = None
latest_image = None
if options.manifest_only:
amend_tags = options.manifest_only
timestamp = datetime.datetime.utcnow().strftime("%Y%m%d_%H%M")
time_tag = f"{version}_{timestamp}"
create_and_push_manifest(time_tag, amend_tags)
create_and_push_manifest(version, amend_tags)
if push_latest:
create_and_push_manifest("latest", amend_tags)
pushes = {}
pushes["timestamp"] = time_tag
if push_latest:
pushes["latest"] = True
push_log["versions"][version] = pushes
return
if not options.no_build:
base_image = build(version)
if not options.no_test:
tv = version
if version in test_versions:
tv = test_versions[version]
test(base_image, tv)
if not options.no_tag_timestamp:
time_image = tag_timestamp(base_image, version)
if push_latest:
if not options.manifest_add:
latest_image = tag_latest(base_image)
if options.no_push_tag or options.manifest_add:
base_image = None
if options.push:
for img in (base_image, time_image, latest_image):
if img:
push_image(img)
pushes = {}
if base_image:
pushes["base"] = base_image.tag
if time_image:
pushes["timestamp"] = time_image.tag
if latest_image:
pushes["latest"] = True
push_log["versions"][version] = pushes
if options.manifest_add:
amend_tags = [time_image.tag] + options.manifest_add
create_and_push_manifest(version, amend_tags)
if push_latest:
create_and_push_manifest("latest", amend_tags)
if options.delete_timestamp_tag:
remove_image(time_image)
def set_options():
parser = argparse.ArgumentParser(
description="Build one or more docker images for boost-cpp-docker")
parser.add_argument(
"-v", "--version", action="append",
help="Use one of more times to specify the versions to run, skip"
+ " for all")
parser.add_argument(
"--no-build", action="store_true", help="skip build step")
parser.add_argument(
"--no-force", action="store_true",
help="don't force an update, use existing layers")
parser.add_argument(
"--no-pull-base", action="store_true",
help="Don't require pulling the base image")
parser.add_argument(
"--no-test", action="store_true", help="skip the test step")
parser.add_argument(
"--no-tag-timestamp", action="store_true", help="only version tag")
parser.add_argument(
"--latest", action="store_true",
help="Update latest tag. If multiple versions, applies to last one." +
" If --manifest-add specified will create a latest manifest")
parser.add_argument(
"-T", "--no-push-tag", action="store_true",
help="Do not apply the tag for the version, only the timestamp tag")
parser.add_argument(
"-r", "--repo", default="test/boost-cpp",
help="repo to build for and push to. Defaults to test/boost-cpp, " +
"use teeks99/boost-cpp-docker for dockerhub")
parser.add_argument(
"-p", "--push", action="store_true", help="push to dockerhub")
parser.add_argument(
"-d", "--delete-timestamp-tag", action="store_true",
help="remove the timestamp tag from the local machine")
parser.add_argument(
"-m", "--manifest-add", action="append",
help="Generate a manifest for the version supplied, using the" +
" timestamp upload as the first version add the timestamp(s)" +
" specified here as additional versions. Used for generating" +
" multiarch images on different machines.")
parser.add_argument(
"--manifest-only", nargs="+",
help="Create a manifest from the provided timestamp tags, without building." +
" Will create a manifest for the version and a new timestamp.")
parser.add_argument(
"--arch", nargs='?', const='auto', default="",
help="Architecture string to include in the timestamp tag (e.g. amd64, arm64). If passed without value, it autodetects.")
parser.add_argument(
"-l", "--log-file", default="",
help="json file to log pushes into")
global options
options = parser.parse_args()
if options.manifest_add and len(options.version) > 1:
raise RuntimeError("Cannot support manifest with multiple versions")
if options.manifest_only and len(options.version) > 1:
raise RuntimeError("Cannot support manifest-only with multiple versions")
def run():
set_options()
push_log["repo"] = options.repo
if options.arch == 'auto':
machine = platform.machine().lower()
if machine in ['x86_64', 'amd64']:
options.arch = 'amd64'
elif machine in ['aarch64', 'arm64']:
options.arch = 'arm64'
else:
options.arch = machine
if options.version:
global versions
versions = options.version
all()
if options.log_file:
with open(options.log_file, "w") as f:
json.dump(push_log, f)
if __name__ == "__main__":
run()