forked from bcgov/von-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_node_image.py
executable file
·248 lines (231 loc) · 7.5 KB
/
make_node_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
#!/usr/bin/env python3
import argparse
import re
import subprocess
import sys
DEFAULT_NAME = "bcgovimages/von-image"
PY_35_VERSION = "3.5.7"
PY_36_VERSION = "3.6.13"
PY_DEFAULT_VERSION = PY_35_VERSION
VERSIONS = {
"1.7": {
"path": "node-1.7",
"version": "node-1.7-0",
"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": {
"path": "node-1.9",
"version": "node-1.9-4",
"args": {
# 1.11.1
"indy_sdk_url": "https://codeload.github.com/hyperledger/indy-sdk/tar.gz/453edc895f2278f41e04820911d5a946199a44e4",
# 0.1.1
"ursa_url": "https://codeload.github.com/hyperledger/ursa/tar.gz/d764981144bce9f5b0f1c085a8ebad222f429690",
"rust_version": "1.37.0",
},
},
"1.12": {
"path": "node-1.12",
"version": "node-1.12-4",
"args": {
# 1.16.0 release
"indy_sdk_url": "https://codeload.github.com/hyperledger/indy-sdk/tar.gz/b4b330ef326958d593ab42e25679c2dcd655494c",
# 0.3.2
"ursa_url": "https://codeload.github.com/hyperledger/ursa/tar.gz/394bcdf1413ac41793e96175d46d745ed6ffd970",
"rocksdb_lib_ver":"5.8.fb",
"rust_version": "1.46.0",
},
"python_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("--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(
"--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:
tag_version = 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])
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)