-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·172 lines (121 loc) · 3.8 KB
/
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
#!/usr/bin/env python3
import argparse
import platform
import subprocess
import sys
import yaml
import re
import os
APPS = [
"kujira",
"feeder",
"relayer",
"proxy",
"terra2",
"cosmoshub",
]
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--namespace", default="teamkujira")
parser.add_argument("-a", "--app")
parser.add_argument("-t", "--tag")
parser.add_argument("--push", action="store_true")
parser.add_argument("--podman", action="store_true")
parser.add_argument("--manifest", action="store_true")
parser.add_argument("--archs")
return parser.parse_args()
def build_arg(s):
return ["--build-arg", s]
def get_version(versions, name, tag):
versions = versions["pond"]
version = versions.get(tag, {}).get(name)
if not version:
version = versions["latest"].get(name)
if not version:
raise f"no version found for {name}"
return version
def build(command, namespace, app, tag, versions, push=False):
arch = platform.machine()
if arch == "x86_64":
arch = "amd64"
fulltag = f"{namespace}/{app}:{tag}-{arch}"
version = "-".join(tag.split("-")[:-1])
cmd = [command, "build", "--tag", fulltag]
go_version = versions["go"].get(app, {}).get(version)
if not go_version and app != "proxy":
print(f"no go version defined for {app}:{tag}")
sys.exit(1)
cmd += build_arg(f"{app}_version={version}")
if go_version:
cmd += build_arg(f"go_version={go_version}")
cmd.append(app)
print(" ".join(cmd))
subprocess.run(cmd)
push_cmds = [
[command, "push", fulltag]
]
print(push_cmds)
if push:
for cmd in push_cmds:
print(cmd)
subprocess.run(cmd)
def manifest(command, namespace, app, tag, archs, push=False):
fulltag = f"{namespace}/{app}:{tag}"
commands = []
tags = [f"{fulltag}-{x}" for x in archs.split(",")]
commands.append([command, "manifest", "create", fulltag] + tags)
if push:
if command == "podman":
commands.append([command, "manifest", "push", fulltag, fulltag])
else:
commands.append([command, "manifest", "push", fulltag])
for cmd in commands:
print(cmd)
subprocess.run(cmd)
def check_version(version):
pattern = r"^[a-z\d\._-]+-(rc)?\d+$"
if re.match(pattern, version):
return
# if re.match(r"^v(\d+\.){2}\d+-[a-z\d]+$", version):
# return
print(f"version '{version}' doesn't match 'xyz-n'")
sys.exit(1)
def load_versions(version):
versions = {}
for filename in ["go.yml", f"{version}.yml"]:
if not os.path.exists(f"versions.d/{filename}"):
print(f"versions.d/{filename} not found")
sys.exit(1)
data = yaml.safe_load(open(f"versions.d/{filename}", "r"))
if filename == "go.yml":
versions["go"] = data
else:
for tag in data.values():
check_version(tag)
versions["pond"] = data
return versions
def main():
args = parse_args()
versions = load_versions(args.tag)
command = "docker"
if args.podman:
command = "podman"
apps = APPS
if args.app:
apps = [args.app]
for app in apps:
tag = versions["pond"].get(app)
if not tag:
print(f"no tag defined for {app} ({args.tag})")
sys.exit(1)
build(command, args.namespace, app, tag, versions, args.push)
if not args.manifest:
continue
archs = args.archs
if not archs:
archs = platform.machine()
if archs == "x86_64":
archs = "amd64"
manifest(command, args.namespace, app, tag, archs, args.push)
if __name__ == "__main__":
main()