-
Notifications
You must be signed in to change notification settings - Fork 14
/
dodo.py
206 lines (177 loc) · 4.96 KB
/
dodo.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
import json
import platform
from pathlib import Path
import docker
from doit.tools import title_with_actions
from dunamai import Version
_DOCKER_CLIENT = docker.from_env()
_DOCKER_ARCHITECTURE = _DOCKER_CLIENT.info()["Architecture"]
DOIT_CONFIG = {"default_tasks": ["build"]}
VERSION = Version.from_git().serialize().replace("+", "_")
_ARCH_MAPPING = {
"x86_64": "amd64",
"amd64": "amd64",
"aarch64": "arm64",
}
ARCH = _ARCH_MAPPING.get(_DOCKER_ARCHITECTURE)
if ARCH is None:
print(
f"Unsupported architecture {_DOCKER_ARCHITECTURE} on platform {platform.system()}."
)
exit(1)
_REGISTRY_PARAM = {
"name": "registry",
"short": "r",
"long": "registry",
"type": str,
"default": "",
"help": "Specify the docker image registry (including the trailing slash).",
}
_ORGANIZATION_PARAM = {
"name": "organization",
"short": "o",
"long": "organization",
"type": str,
"default": "aiidalab",
"help": "Specify the docker image organization.",
}
_VERSION_PARAM = {
"name": "version",
"long": "version",
"type": str,
"default": VERSION,
"help": (
"Specify the version of the stack for building / testing. Defaults to a "
"version determined from the state of the local git repository."
),
}
_ARCH_PARAM = {
"name": "architecture",
"long": "arch",
"type": str,
"default": ARCH,
"help": "Specify the platform to build for. Examples: arm64, amd64.",
}
_TARGET_PARAM = {
"name": "target",
"long": "target",
"short": "t",
"type": str,
"choices": (
("base", ""),
("base-with-services", ""),
("lab", ""),
("full-stack", ""),
),
# If the target is not provided, all images will be build
"default": "",
"help": "Specify the target to build.",
}
_AIIDALAB_PORT_PARAM = {
"name": "port",
"short": "p",
"long": "port",
"type": int,
"default": 8888,
"help": "Specify the AiiDAlab host port.",
}
_COMPOSE_CMD_PARAM = {
"name": "compose-command",
"long": "compose-cmd",
"type": str,
"default": "docker compose",
"help": "Specify alternative docker compose command (e.g. podman-compose).",
}
def target_required(target: str) -> bool:
if not target:
print("ERROR: Target image must be provided with '-t/--target' option")
return False
return True
def task_build():
"""Build all docker images."""
def generate_version_override(
version, registry, targets, architecture, organization
):
platforms = [f"linux/{architecture}"]
overrides = {
"VERSION": f":{version}",
"REGISTRY": registry,
"ORGANIZATION": organization,
"PLATFORMS": platforms,
}
# If no targets are specifies via cmdline, we'll build all images,
# as specified in docker-bake.hcl
if targets:
overrides["TARGETS"] = targets
Path("docker-bake.override.json").write_text(json.dumps(overrides))
return {
"actions": [
generate_version_override,
"docker buildx bake -f docker-bake.hcl -f build.json "
"-f docker-bake.override.json "
"--load",
],
"title": title_with_actions,
"params": [
_ORGANIZATION_PARAM,
_REGISTRY_PARAM,
_VERSION_PARAM,
_ARCH_PARAM,
_TARGET_PARAM,
],
"verbosity": 2,
}
def task_tests():
"""Run tests with pytest."""
return {
"actions": [
target_required,
"AIIDALAB_PORT=%(port)i REGISTRY=%(registry)s VERSION=:%(version)s "
"pytest -s --target %(target)s --compose-cmd='%(compose-command)s' %(pytest-opts)s",
],
"params": [
_TARGET_PARAM,
_AIIDALAB_PORT_PARAM,
_REGISTRY_PARAM,
_VERSION_PARAM,
{
"name": "pytest-opts",
"long": "pytest-opts",
"type": str,
"default": "",
"help": "Extra options to pytest command.",
},
_COMPOSE_CMD_PARAM,
],
"verbosity": 2,
}
def task_up():
"""Start AiiDAlab server."""
return {
"actions": [
target_required,
"AIIDALAB_PORT=%(port)i REGISTRY=%(registry)s VERSION=:%(version)s "
"%(compose-command)s -f stack/docker-compose.%(target)s.yml up --detach",
],
"params": [
_TARGET_PARAM,
_AIIDALAB_PORT_PARAM,
_REGISTRY_PARAM,
_VERSION_PARAM,
_COMPOSE_CMD_PARAM,
],
"verbosity": 2,
}
def task_down():
"""Stop AiiDAlab server."""
return {
"actions": [
target_required,
"%(compose-command)s -f stack/docker-compose.%(target)s.yml down",
],
"params": [
_TARGET_PARAM,
_COMPOSE_CMD_PARAM,
],
"verbosity": 2,
}