-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy.py
executable file
·109 lines (95 loc) · 4.3 KB
/
deploy.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
#!/usr/bin/env python3
import argparse
import os
import shutil
import subprocess
import gitlab
REGISTRY = "registry-ex.sandia.gov/trilinos-project/trilinos-containers"
DEPLOYS = [
{"dockerfile": "gnu-openmpi",
"build_args": {"compiler_version": "@12.1.0", "mpi_version": "@4.1.6"},
"production": False,
"image_name": "ubi8-gcc-12.1.0-openmpi-4.1.6"},
{"dockerfile": "gnu-openmpi",
"build_args": {"compiler_version": "@10.3.0", "mpi_version": "@4.1.6"},
"production": False,
"image_name": "ubi8-gcc-10.3.0-openmpi-4.1.6"},
{"dockerfile": "cuda-gnu-openmpi",
"build_args": {"compiler_version": "@10.3.0", "mpi_version": "@4.1.6", "cuda_version": "@11.4.2"},
"production": False,
"image_name": "ubi8-cuda-11.4.2-gcc-10.3.0-openmpi-4.1.6"},
{"dockerfile": "cuda-gnu-openmpi",
"build_args": {"compiler_version": "@10.3.0", "mpi_version": "@4.1.6", "cuda_version": "@12.4.1"},
"production": False,
"image_name": "ubi8-cuda-12.4.1-gcc-10.3.0-openmpi-4.1.6"},
{"dockerfile": "gnu-serial",
"build_args": {"compiler_version": "@8.3.0"},
"production": False,
"image_name": "ubi8-gcc-8.3.0-serial"},
{"dockerfile": "python",
"build_args": {},
"production": False,
"image_name": "ubi8-python-3.9"},
{"dockerfile": "intel-intelmpi",
"build_args": {},
"production": True,
"image_name": "ubi8-intel-intelmpi"}
]
def list_container_images(gitlab_url="https://gitlab-ex.sandia.gov", project_id=3512):
"""
List container images in a private GitLab repository.
Args:
gitlab_url (str): The URL of your GitLab instance.
project_id (int): The ID of your project.
Returns:
list: A list of container image locations for all tags.
"""
gl = gitlab.Gitlab(gitlab_url)
project = gl.projects.get(project_id)
repos = project.repositories.list()
image_list = [tag.location for repo in repos for tag in repo.tags.list()]
return image_list
parser = argparse.ArgumentParser()
parser.add_argument("IMAGES", nargs="*")
parser.add_argument("--skip-login", action="store_true", help="Skip registry login")
args = parser.parse_args()
repo_root = os.path.abspath(os.path.dirname(__file__))
if not args.skip_login:
subprocess.check_call(['docker', 'login', '-u', 'gitlab+deploy-token-23', '-p', os.environ['AT2_BUILD_TOKEN'], 'registry-ex.sandia.gov'])
date_format = "%Y%m%d"
os.chdir(repo_root)
if args.IMAGES:
deploys = [x for x in DEPLOYS if x["image_name"] in args.IMAGES]
else:
deploys = DEPLOYS
for image in deploys:
dockerfile = os.path.join(repo_root, "dockerfiles", image["dockerfile"])
path = dockerfile + "/Dockerfile"
# get the timestamp from the Dockerfile commit
dockerfile_ts = subprocess.check_output(["git", "--no-pager", "log", "-1", "--format=%cd", f"--date=format:{date_format}", "--", path]).decode().strip()
build_args = [k + "=" + v for k, v in image["build_args"].items()]
tag = REGISTRY + ("/production/" if image["production"] else "/experimental/") + image["image_name"] + ":" + dockerfile_ts
if tag in list_container_images():
print(f"tag {tag} already exists in container registry, skipping build")
continue
build_args.append(f"AT2_image_fullpath={tag}")
build_args.append(f"AT2_image={image['image_name']}")
print(f"Building Dockerfile '{dockerfile}' with args {build_args}")
print(f"Tagging as {tag}")
f = []
for e in build_args:
f.append("--build-arg")
f.append(e)
if os.path.exists(os.path.join(dockerfile, "GenConfig")):
shutil.rmtree(os.path.join(dockerfile, "GenConfig"))
if not os.path.exists(os.path.join(repo_root, "GenConfig", "ini_files")):
raise SystemExit("The GenConfig dependency does not appear to exist! Did you initialize git submodules?")
shutil.copytree(os.path.join(repo_root, "GenConfig"), os.path.join(dockerfile, "GenConfig"), symlinks=True)
try:
subprocess.check_call(["podman", "build", "--tag", tag] + f + [dockerfile])
except subprocess.CalledProcessError as e:
print(f"check_call() returned {e.returncode}")
continue
subprocess.check_call(["podman", "push", tag])
latest_tag = REGISTRY + ("/production/" if image["production"] else "/experimental/") + image["image_name"] + ":latest"
subprocess.check_call(["podman", "push", tag, latest_tag])