forked from bcgov/von-image
-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_image.py
executable file
·293 lines (277 loc) · 8.83 KB
/
make_image.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
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
284
285
286
287
288
289
290
291
292
293
#!/usr/bin/env python3
import argparse
import re
import subprocess
import sys
import random
VERSIONS = {
"1.7-ew": {
"version": "1.7-ew-0",
"args": {
# bcgov postgres_plugin branch
"indy_sdk_url": "https://codeload.github.com/bcgov/indy-sdk/tar.gz/c4a0249cf9d06dbc1c3c1533e8bf1e0c5946dbb4",
# 0.5.0
"indy_crypto_url": "https://codeload.github.com/hyperledger/indy-crypto/tar.gz/c323bd0046e4e7da936ad1682a401c557c74345b",
},
},
"1.8": {
"version": "1.8-5",
"args": {
# 1.8.3
"indy_sdk_url": "https://codeload.github.com/ianco/indy-sdk/tar.gz/26daafc28da10a8347c52fb2d13817301903b75b",
# 0.5.1
"indy_crypto_url": "https://codeload.github.com/hyperledger/indy-crypto/tar.gz/059e99ac526ad27eb1621c079bba6ebd36f16204",
},
},
"1.9": {
"version": "1.9-0",
"args": {
# 1.9.0
"indy_sdk_url": "https://codeload.github.com/hyperledger/indy-sdk/tar.gz/41d97ab1dc9c8e55f9c3f9e95de70da58f5e384a",
# 0.1.1
"ursa_url": "https://codeload.github.com/hyperledger/ursa/tar.gz/d764981144bce9f5b0f1c085a8ebad222f429690",
},
},
"1.10": {
"version": "1.10-0",
"args": {
# 1.10.1
"indy_sdk_url": "https://codeload.github.com/hyperledger/indy-sdk/tar.gz/0718ac0f2979d5c2ae649663c5e78e2f65f35100"
},
},
"1.11": {
"path": "1.10",
"version": "1.11-0",
"args": {
# 1.11.0
"indy_sdk_url": "https://codeload.github.com/hyperledger/indy-sdk/tar.gz/a583838aad867ad3dbb142b86bd76cadfe294682"
},
},
"1.11.1": {
"path": "1.10",
"version": "1.11-1",
"args": {
# 1.11.1
"indy_sdk_url": "https://codeload.github.com/hyperledger/indy-sdk/tar.gz/453edc895f2278f41e04820911d5a946199a44e4",
"rust_version": "1.37.0",
},
},
}
DEFAULT_NAME = "bcgovimages/von-image"
PY_35_VERSION = "3.5.7"
PY_36_VERSION = "3.6.9"
PY_37_VERSION = "3.7.4"
PY_DEFAULT_VERSION = PY_36_VERSION
parser = argparse.ArgumentParser(description="Generate a von-image Docker image")
parser.add_argument(
"-n", "--name", default=DEFAULT_NAME, help="the base name for the docker image"
)
parser.add_argument("-t", "--tag", help="a custom tag for the docker image")
parser.add_argument("-f", "--file", help="use a custom Dockerfile")
parser.add_argument(
"--build-arg", metavar="ARG=VAL", action="append", help="add docker build arguments"
)
parser.add_argument(
"--debug", action="store_true", help="produce a debug build of libindy"
)
parser.add_argument(
"--dry-run",
action="store_true",
help="print docker command line instead of executing",
)
parser.add_argument("--no-cache", action="store_true", help="ignore docker image cache")
parser.add_argument(
"-o",
"--output",
help="output an updated Dockerfile with the build arguments replaced",
)
parser.add_argument(
"--py35",
dest="python",
action="store_const",
const=PY_35_VERSION,
help="build with the default python 3.5 version",
)
parser.add_argument(
"--py36",
dest="python",
action="store_const",
const=PY_36_VERSION,
help="build with the default python 3.6 version",
)
parser.add_argument(
"--py37",
dest="python",
action="store_const",
const=PY_37_VERSION,
help="build with the default python 3.7 version",
)
parser.add_argument("--python", help="use a specific python version")
parser.add_argument("--push", action="store_true", help="push the resulting image")
parser.add_argument(
"-q", "--quiet", action="store_true", help="suppress output from docker build"
)
parser.add_argument(
"--release",
dest="debug",
action="store_false",
help="produce a release build of libindy",
)
parser.add_argument("--platform", help="build for a specific platform")
parser.add_argument(
"--postgres",
action="store_true",
help="force re-install of postgres plug-in from github",
)
parser.add_argument(
"--vonx",
action="store_true",
help="force re-install of von-x from library in requirements-vonx.txt",
)
parser.add_argument(
"--s2i", action="store_true", help="build the s2i image for this version"
)
parser.add_argument("--squash", action="store_true", help="produce a smaller image")
parser.add_argument("--test", action="store_true", help="perform tests on docker image")
parser.add_argument(
"version", choices=VERSIONS.keys(), help="the predefined release version"
)
args = parser.parse_args()
ver = VERSIONS[args.version]
py_ver = args.python or ver.get("python_version", PY_DEFAULT_VERSION)
target = ver.get("path", args.version)
dockerfile = target + "/Dockerfile.ubuntu"
if args.file:
dockerfile = args.file
tag = args.tag
tag_name = args.name
if tag:
tag_name, tag_version = tag.split(":", 2)
else:
pfx = "py" + py_ver[0:1] + py_ver[2:3] + "-"
tag_version = pfx + ver.get("version", args.version)
if args.debug:
tag_version += "-debug"
tag = tag_name + ":" + tag_version
build_args = {}
build_args.update(ver["args"])
build_args["python_version"] = py_ver
build_args["tag_name"] = tag_name
build_args["tag_version"] = tag_version
if not args.debug:
build_args["indy_build_flags"] = "--release"
if args.build_arg:
for arg in args.build_arg:
key, val = arg.split("=", 2)
build_args[key] = val
if args.output:
src_path = dockerfile
src_replace = build_args
if args.test:
src_path = target + "/Dockerfile.test"
src_replace = {"base_image": tag}
elif args.s2i:
src_path = target + "/Dockerfile.s2i"
src_replace = {"base_image": tag}
with open(args.output, "w") as out:
with open(src_path) as src:
for line in src:
m = re.match(r"^ARG\s+(\w+)=?(.*)$", line)
if m:
line = "ARG {}={}\n".format(
m.group(1), src_replace.get(m.group(1), m.group(2))
)
out.write(line)
sys.exit(0)
cmd_args = []
for k, v in build_args.items():
cmd_args.extend(["--build-arg", "{}={}".format(k, v)])
if dockerfile:
cmd_args.extend(["-f", dockerfile])
if args.no_cache:
cmd_args.append("--no-cache")
if args.squash:
cmd_args.append("--squash")
cmd_args.extend(["-t", tag])
if args.platform:
cmd_args.extend(["--platform", args.platform])
if args.postgres:
cmd_args.extend(
["--build-arg", "CACHEBUSTPX=" + str(random.randint(100000, 999999)) + ""]
)
if args.vonx:
cmd_args.extend(
["--build-arg", "CACHEBUSTVX=" + str(random.randint(100000, 999999)) + ""]
)
cmd_args.extend(["--build-arg", "VONX_FORCE=True"])
cmd_args.append(target)
cmd = ["docker", "build"] + cmd_args
if args.dry_run:
print(" ".join(cmd))
else:
print("Building docker image...")
proc = subprocess.run(cmd, stdout=(subprocess.PIPE if args.quiet else None))
if proc.returncode:
print("build failed")
sys.exit(1)
if args.quiet:
print("Successfully tagged {}".format(tag))
proc_sz = subprocess.run(
["docker", "image", "inspect", tag, "--format={{.Size}}"],
stdout=subprocess.PIPE,
)
size = int(proc_sz.stdout.decode("ascii").strip()) / 1024.0 / 1024.0
print("%0.2f%s" % (size, "MB"))
if args.s2i:
s2i_tag = tag + "-s2i"
s2i_cmd = [
"docker",
"build",
"--build-arg",
"base_image=" + tag,
"-t",
s2i_tag,
"-f",
target + "/Dockerfile.s2i",
target,
]
if args.dry_run:
print(" ".join(s2i_cmd))
else:
proc = subprocess.run(s2i_cmd, stdout=(subprocess.PIPE if args.quiet else None))
if proc.returncode:
print("s2i build failed")
sys.exit(1)
if args.quiet:
print("Successfully tagged {}".format(s2i_tag))
if not args.dry_run:
if args.test or args.push:
test_path = target + "/Dockerfile.test"
test_tag = tag + "-test"
proc_bt = subprocess.run(
[
"docker",
"build",
"--build-arg",
"base_image=" + tag,
"-t",
test_tag,
"-f",
test_path,
target,
]
)
if proc_bt.returncode:
print("test image build failed")
sys.exit(1)
proc_test = subprocess.run(["docker", "run", "--rm", "-i", test_tag])
if proc_test.returncode:
print("One or more tests failed")
sys.exit(1)
print("All tests passed")
if args.push:
print("Pushing docker image...")
proc = subprocess.run(["docker", "push", s2i_tag if args.s2i else tag])
if proc.returncode:
print("push failed")
sys.exit(1)