-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild.py
62 lines (55 loc) · 2.53 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
import click
import os
from subprocess import Popen, PIPE
@click.command(help="Build a singularity image from a docker image")
@click.option(
"--target",
default=["docker", "singularity"],
help="Build targets",
type=click.Choice(["base", "docker", "singularity"]),
multiple=True,
)
@click.option("--tag", default="latest", help="Tag for the docker and singularity images")
@click.option("--base-tag", default="latest", help="Base image tag to use to build the murfi docker image")
@click.option("--build-arg", multiple=True, help="Args to pass to docker build, e.g, MAKE_TARGET=debug")
def main(tag, build_arg, target, base_tag):
"""
Build the murfi docker and singularity images.
"""
build_args = ""
if build_arg:
build_args = " ".join([f"--build-arg {arg}" for arg in build_arg])
build_args += f" --build-arg BASE_IMAGE_TAG={base_tag}"
if "base" in target:
docker_cmd = ["docker", "build", "-t", f"ghcr.io/gablab/murfi-base:{tag}", "-f", "docker/Dockerfile.base", "."]
print(docker_cmd)
proc = Popen(docker_cmd, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
if proc.returncode != 0:
print(f"Docker base image build failed with error: {err.decode()}")
print(f"stdout: {out.decode()}")
print(f"stderr: {err.decode()}")
raise Exception("Docker base image build failed")
if "docker" in target:
docker_cmd = ["docker", "build", "-t", f"ghcr.io/gablab/murfi:{tag}"] + build_args.split() + ["-f", "docker/Dockerfile", "."]
print(docker_cmd)
proc = Popen(docker_cmd, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
if proc.returncode != 0:
print(f"Docker build failed with error: {err.decode()}")
print(f"stdout: {out.decode()}")
print(f"stderr: {err.decode()}")
raise Exception("Docker build failed")
if "singularity" in target:
os.makedirs("bin", exist_ok=True)
singularity_cmd = ["singularity", "build", "--fakeroot", "-F", "bin/murfi.sif", f"docker-daemon://ghcr.io/gablab/murfi:{tag}"]
print(singularity_cmd)
proc = Popen(singularity_cmd, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
if proc.returncode != 0:
print(f"Singularity build failed with error: {err.decode()}")
print(f"stdout: {out.decode()}")
print(f"stderr: {err.decode()}")
raise Exception("Singularity build failed")
if __name__ == "__main__":
main()