-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.py
255 lines (203 loc) · 10.5 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
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
#! /usr/bin/env python3
import argparse
import enum
import os
REGISTRY_HOST = "harbor.chaimeleon-eu.i3m.upv.es"
REGISTRY_PATH_FOR_BATCH = "/chaimeleon-library-batch/"
REGISTRY_PATH_FOR_DESKTOP = "/chaimeleon-library/"
ubuntu_python_version = "3.8"
ubuntu_python_tensorflow_version = "3.9"
ubuntu_python_pytorch_version = "3.8"
ubuntu_python_tensorflow_desktop_version = "3.16"
ubuntu_python_pytorch_desktop_version = "3.15"
ubuntu_python_tensorflow_desktop_jupyter_version = "3.16"
ubuntu_python_pytorch_desktop_jupyter_version = "3.15"
def cmd(command, exit_on_error=True):
print(command)
ret = os.system(command)
if exit_on_error and ret != 0: exit(1)
return ret
login_done = False
uploaded_images = []
def upload_image(image: str, latest_tag=False):
global login_done, uploaded_images
if input("Do you want to upload the image? [Y/n] ").lower() == "n": return
while True:
ret = cmd("docker push "+image, exit_on_error=False)
if ret == 0:
uploaded_images.append(image)
break
if input("Uploading error. Do you want to login? [Y/n] ").lower() != "n":
cmd("docker login "+REGISTRY_HOST)
login_done = True
else:
if input("Abort upload? [y/N] ").lower() == "y": return
if latest_tag:
latest_tag = image[0:image.find(':')] + ":latest"
if image.find("cuda") > 0: latest_tag += "-cuda"
if input("Do you want to tag as "+latest_tag+"? [Y/n] ").lower() == "n": return
cmd("docker tag "+image+ " " +latest_tag, exit_on_error=False)
while True:
ret = cmd("docker push " +latest_tag, exit_on_error=False)
if ret == 0:
uploaded_images.append(latest_tag)
break
if input("Uploading error. Do you want to retry? [Y/n] ").lower() == "n": return
def logout():
if input("Do you want to logout? [y/N] ").lower() == "y":
cmd("docker logout "+REGISTRY_HOST)
def remove_local_image(image):
if input("Do you want to remove the local image '"+image+"'? [y/N] ").lower() == "y":
cmd("docker rmi "+image)
def build_ubuntu_python(gpu=None):
if gpu==None: gpu = input("Do you want to build with CUDA? [y/N] ").lower() == "y"
TARGET_VERSION = ubuntu_python_version
IMAGE_BASE = "nvidia/cuda:11.8.0-runtime-ubuntu22.04" if gpu else "ubuntu:22.04"
CUDA_VERSION = "cuda11" if gpu else ""
target_image = REGISTRY_HOST+REGISTRY_PATH_FOR_BATCH+"ubuntu-python:"+TARGET_VERSION+CUDA_VERSION
cmd("docker build -t "+target_image
+" --build-arg IMAGE_NAME="+IMAGE_BASE+" --build-arg CUDA_VERSION="+CUDA_VERSION
+" --build-arg TARGET_VERSION="+TARGET_VERSION
+" ubuntu-python")
if input("Do you want to run the container for testing? [y/N] ").lower() == "y":
print("OK, when you end the testing write 'exit' to stop and remove the container.")
cmd("docker run -it --rm --name testing01 "+target_image+" bash")
upload_image(target_image, latest_tag=True)
class AI_TOOL(enum.Enum):
tensorflow = 1
pytorch = 2
def build_ubuntu_python_aitool(aitool:AI_TOOL, gpu=None):
if gpu==None: gpu = input("Do you want to build with CUDA? [y/N] ").lower() == "y"
if aitool == AI_TOOL.tensorflow:
TARGET_VERSION = ubuntu_python_tensorflow_version
BASE_VERSION = ubuntu_python_version
else: # aitool == AI_TOOL.pytorch
TARGET_VERSION = ubuntu_python_pytorch_version
BASE_VERSION = ubuntu_python_version
CUDA_VERSION="cuda11" if gpu else ""
target_image = REGISTRY_HOST+REGISTRY_PATH_FOR_BATCH+"ubuntu-python-"+aitool.name+":"+TARGET_VERSION+CUDA_VERSION
cmd("docker build -t "+target_image
+" --build-arg BASE_VERSION="+BASE_VERSION+" --build-arg CUDA_VERSION="+CUDA_VERSION
+" --build-arg TARGET_VERSION="+TARGET_VERSION
+" ubuntu-python-"+aitool.name)
if input("Do you want to run the container for testing? [y/N] ").lower() == "y":
print("OK, when you end the testing write 'exit' to stop and remove the container.")
cmd("docker run -it --rm --name testing01 "+target_image+" bash")
upload_image(target_image, latest_tag=True)
def build_ubuntu_python_aitool_desktop(aitool:AI_TOOL, gpu=None):
if gpu==None: gpu = input("Do you want to build with CUDA? [y/N] ").lower() == "y"
if aitool == AI_TOOL.tensorflow:
TARGET_VERSION = ubuntu_python_tensorflow_desktop_version
BASE_VERSION = ubuntu_python_tensorflow_version
else: # aitool == AI_TOOL.pytorch
TARGET_VERSION = ubuntu_python_pytorch_desktop_version
BASE_VERSION = ubuntu_python_pytorch_version
CUDA_VERSION="cuda11" if gpu else ""
target_image = REGISTRY_HOST+REGISTRY_PATH_FOR_DESKTOP+"ubuntu-python-"+aitool.name+"-desktop:"+TARGET_VERSION+CUDA_VERSION
cmd("docker build -t "+target_image
+" --build-arg AI_TOOL="+aitool.name+" --build-arg BASE_VERSION="+BASE_VERSION+" --build-arg CUDA_VERSION="+CUDA_VERSION
+" --build-arg TARGET_VERSION="+TARGET_VERSION
+" ubuntu-python-xxxxx-desktop")
if input("Do you want to run the container for testing? [y/N] ").lower() == "y":
print("OK, when you end the testing write 'exit' to stop and remove the container.")
cmd('docker run -d --rm -p 15900:5900 -p 3322:22'
+' -e VNC_PASSWORD="chaimeleon" '
+' -e PASSWORD="chaimeleon" '
+' -e GUACAMOLE_URL=https://chaimeleon-eu.i3m.upv.es/guacamole/ '
+' -e GUACAMOLE_USER="guacamoleuser" '
+' -e GUACAMOLE_PASSWORD="XXXXXXXXXXXX" '
+' -e GUACD_HOST="10.98.114.250" '
+' -e SSH_ENABLE_PASSWORD_AUTH=true '
+' -e GUACAMOLE_CONNECTION_NAME=testing-ubuntu-python-'+aitool.name+'-desktop '
+' -e GATEWAY_PORTS=true '
+' -e TCP_FORWARDING=true '
+' --name testing01 '
+target_image)
if input("Do you want see the log? [Y/n] ").lower() != "n":
cmd("docker logs testing01")
print("Test VNC service: run a VNC client (tightVNC or tigerVNC) and connect to localhost:15900.")
print("Test file transfer: run SSH client and connect to localhost:3322")
input("Type enter to continue stopping the container")
cmd("docker stop testing01")
upload_image(target_image)
def build_ubuntu_python_aitool_desktop_jupyter(aitool:AI_TOOL, gpu=None):
if gpu==None: gpu = input("Do you want to build with CUDA? [y/N] ").lower() == "y"
if aitool == AI_TOOL.tensorflow:
TARGET_VERSION = ubuntu_python_tensorflow_desktop_jupyter_version
BASE_VERSION = ubuntu_python_tensorflow_desktop_version
else: # aitool == AI_TOOL.pytorch
TARGET_VERSION = ubuntu_python_pytorch_desktop_jupyter_version
BASE_VERSION = ubuntu_python_pytorch_desktop_version
CUDA_VERSION="cuda11" if gpu else ""
target_image = REGISTRY_HOST+REGISTRY_PATH_FOR_DESKTOP+"ubuntu-python-"+aitool.name+"-desktop-jupyter:"+TARGET_VERSION+CUDA_VERSION
cmd("docker build -t "+target_image
+" --build-arg AI_TOOL="+aitool.name+" --build-arg BASE_VERSION="+BASE_VERSION+" --build-arg CUDA_VERSION="+CUDA_VERSION
+" --build-arg TARGET_VERSION="+TARGET_VERSION
+" ubuntu-python-xxxxx-desktop-jupyter")
if input("Do you want to run the container for testing? [y/N] ").lower() == "y":
print("OK, when you end the testing write 'exit' to stop and remove the container.")
cmd('docker run -d --rm -p 15900:5900 -p 3322:22'
+' -e VNC_PASSWORD="chaimeleon" '
+' -e PASSWORD="chaimeleon" '
+' -e GUACAMOLE_URL=https://chaimeleon-eu.i3m.upv.es/guacamole/ '
+' -e GUACAMOLE_USER="guacamoleuser" '
+' -e GUACAMOLE_PASSWORD="XXXXXXXXXXXX" '
+' -e GUACD_HOST="10.98.114.250" '
+' -e SSH_ENABLE_PASSWORD_AUTH=true '
+' -e GUACAMOLE_CONNECTION_NAME=testing-ubuntu-python-'+aitool.name+'-desktop '
+' -e GATEWAY_PORTS=true '
+' -e TCP_FORWARDING=true '
+' --name testing01 '
+target_image)
if input("Do you want see the log? [Y/n] ").lower() != "n":
cmd("docker logs testing01")
print("Test VNC service: run a VNC client (tightVNC or tigerVNC) and connect to localhost:15900.")
print("Test file transfer: run SSH client and connect to localhost:3322")
input("Type enter to continue stopping the container")
cmd("docker stop testing01")
upload_image(target_image)
IMAGES = ["all", "ubuntu-python",
"ubuntu-python-tensorflow", "ubuntu-python-pytorch",
"ubuntu-python-tensorflow-desktop", "ubuntu-python-pytorch-desktop",
"ubuntu-python-tensorflow-desktop-jupyter", "ubuntu-python-pytorch-desktop-jupyter"]
def build(image, gpu=None):
if image == "ubuntu-python":
build_ubuntu_python(gpu)
elif image == "ubuntu-python-tensorflow":
build_ubuntu_python_aitool(AI_TOOL.tensorflow, gpu)
elif image == "ubuntu-python-pytorch":
build_ubuntu_python_aitool(AI_TOOL.pytorch, gpu)
elif image == "ubuntu-python-tensorflow-desktop":
build_ubuntu_python_aitool_desktop(AI_TOOL.tensorflow, gpu)
elif image == "ubuntu-python-pytorch-desktop":
build_ubuntu_python_aitool_desktop(AI_TOOL.pytorch, gpu)
elif image == "ubuntu-python-tensorflow-desktop-jupyter":
build_ubuntu_python_aitool_desktop_jupyter(AI_TOOL.tensorflow, gpu)
elif image == "ubuntu-python-pytorch-desktop-jupyter":
build_ubuntu_python_aitool_desktop_jupyter(AI_TOOL.pytorch, gpu)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("IMAGE", help="Which image to build", nargs="?", default="")
args = parser.parse_args()
image = str(args.IMAGE).lower()
while image not in IMAGES:
print("Unknown image, please select one option:")
n = 0
for img_name in IMAGES:
print("%d - %s" % (n, img_name))
n += 1
try:
image = IMAGES[int(input(""))]
except (ValueError, IndexError): image = ""
print(image)
if image == "all":
for image in IMAGES[1:]: build(image, gpu=False)
for image in IMAGES[1:]:
if not image.find("desktop") > 0: # Skip build GPU images for desktops, currently not used because the GPU is only available for jobs
build(image, gpu=True)
else:
build(image)
if login_done: logout()
for image in uploaded_images:
remove_local_image(image)
exit(0)