-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
generate_docker_images.py
102 lines (92 loc) · 3.19 KB
/
generate_docker_images.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
import requests
import os
import hashlib
import re
from jsonpath_ng import parse
def generate_image(image):
version = image[0]["version"].replace("go1","go-1")
wildcard = version
if version.count('.') > 1:
wildcard = re.sub("\.\d+$", ".x", version)
else:
version = version + ".0"
wildcard = wildcard + ".x"
try:
os.mkdir("docker/"+version)
except:
print("version folder already exists")
try:
os.mkdir("docker/"+wildcard)
except:
print("wildcard folder already exists")
# now that folders have been created, the Dockerfile needs to be created
f = open("docker/"+version+"/Dockerfile", "w")
f.write("## GENERATED. DO NOT EDIT DIRECTLY.\n")
f.write("FROM toolchain\n\n")
f.write("ARG TARGETPLATFORM\n")
f.write("ENV GO_VERSION "+version.replace("go-","").replace(".","")+"\n\n")
f.write("RUN \\\n")
f.write('if [ "$TARGETPLATFORM" = "linux/amd64" ]; then \\\n')
f.write(" export ROOT_DIST=https://dl.google.com/go/"+image[0]["filename"]+" && \\\n")
f.write(" export ROOT_DIST_SHA="+image[0]["sha256"]+";\\\n")
f.write('elif [ "$TARGETPLATFORM" = "linux/arm64" ]; then \\\n')
f.write("export ROOT_DIST=https://dl.google.com/go/"+image[1]["filename"]+" && \\\n")
f.write("export ROOT_DIST_SHA="+image[1]["sha256"]+";\\\n")
f.write(" else \\\n")
f.write('echo "Unsupported architecture: $TARGETPLATFORM" && exit 1; \\\n')
f.write("fi && \\\n")
f.write("$BOOTSTRAP_PURE\n")
f.close()
# now wildcard version
f = open("docker/"+wildcard+"/Dockerfile", "w")
f.write("## GENERATED. DO NOT EDIT DIRECTLY.\n")
f.write("FROM "+version+"\n")
f.close()
r = requests.get('https://go.dev/dl/?mode=json')
if r.status_code != requests.codes.ok:
print("error fetching golang versions")
exit(1)
try:
golangJson = r.json()
except:
print("failed to parse json")
exit(1)
if len(golangJson) != 2:
# the script below assumes only two stable versions
print("unexpected number of golang versions returned")
exit(1)
fileExpr = parse('$.[*].files')
files = [match.value for match in fileExpr.find(golangJson)]
versionExpr = parse('$.[*].version')
versions = [match.value for match in versionExpr.find(golangJson)]
docker_images = {}
for file in files:
x = [f for f in file if (f['os'] == "linux" and f['arch'] == "amd64" ) ][0]
y = [f for f in file if (f['os'] == "linux" and f['arch'] == "arm64" ) ][0]
docker_images[x['version']] = [x, y]
# loop through each key in dict and pass value to generate_image function
first = {}
for docker_image in docker_images:
if len(first) < 1:
first = docker_images[docker_image]
generate_image(docker_images[docker_image])
# write latest image
if first[0]["version"].count('.') > 1:
wildcard = re.sub("\.\d+$", ".x", first[0]["version"])
else:
wildcard = first[0]["version"] + ".x"
try:
os.mkdir("docker/go-latest")
except:
print("go-latest folder already exists")
f = open("docker/go-latest/Dockerfile", "w")
f.write("## GENERATED. DO NOT EDIT DIRECTLY.\n")
f.write("FROM techknowlogick/xgo:"+wildcard.replace("go1", "go-1")+"\n")
f.close()
hs = hashlib.sha256(r.text.encode('utf-8')).hexdigest()
f = open(".golang_hash", "w")
f.write(hs)
f.close()
f = open(".golang_version", "w")
f.write(",".join(versions))
f.close()