2019-01-12 13:26:53 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# Build the Debian packages using Docker images.
|
|
|
|
#
|
|
|
|
# This script builds the Docker images and then executes them sequentially, each
|
|
|
|
# one building a Debian package for the targeted operating system. It is
|
|
|
|
# designed to be a "single command" to produce all the images.
|
|
|
|
#
|
|
|
|
# By default, builds for all known distributions, but a list of distributions
|
|
|
|
# can be passed on the commandline for debugging.
|
|
|
|
|
|
|
|
import argparse
|
2021-07-12 20:03:14 +02:00
|
|
|
import json
|
2019-01-12 13:26:53 +01:00
|
|
|
import os
|
|
|
|
import signal
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import threading
|
2019-01-23 09:39:06 +01:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2021-07-26 12:36:01 +02:00
|
|
|
from typing import Optional, Sequence
|
2019-01-12 13:26:53 +01:00
|
|
|
|
|
|
|
DISTS = (
|
|
|
|
"debian:buster",
|
2019-11-06 14:35:56 +01:00
|
|
|
"debian:bullseye",
|
2019-01-12 13:26:53 +01:00
|
|
|
"debian:sid",
|
2021-05-14 12:46:35 +02:00
|
|
|
"ubuntu:bionic", # 18.04 LTS (our EOL forced by Py36 on 2021-12-23)
|
|
|
|
"ubuntu:focal", # 20.04 LTS (our EOL forced by Py38 on 2024-10-14)
|
|
|
|
"ubuntu:groovy", # 20.10 (EOL 2021-07-07)
|
2021-04-30 16:36:05 +02:00
|
|
|
"ubuntu:hirsute", # 21.04 (EOL 2022-01-05)
|
2019-01-12 13:26:53 +01:00
|
|
|
)
|
|
|
|
|
2021-05-14 12:46:35 +02:00
|
|
|
DESC = """\
|
2019-01-12 13:26:53 +01:00
|
|
|
Builds .debs for synapse, using a Docker image for the build environment.
|
|
|
|
|
|
|
|
By default, builds for all known distributions, but a list of distributions
|
|
|
|
can be passed on the commandline for debugging.
|
2021-05-14 12:46:35 +02:00
|
|
|
"""
|
2019-01-12 13:26:53 +01:00
|
|
|
|
2021-07-12 20:03:14 +02:00
|
|
|
projdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
|
|
|
|
2019-01-12 13:26:53 +01:00
|
|
|
|
|
|
|
class Builder(object):
|
2021-07-26 12:36:01 +02:00
|
|
|
def __init__(
|
|
|
|
self, redirect_stdout=False, docker_build_args: Optional[Sequence[str]] = None
|
|
|
|
):
|
2019-01-12 13:26:53 +01:00
|
|
|
self.redirect_stdout = redirect_stdout
|
2021-07-26 12:36:01 +02:00
|
|
|
self._docker_build_args = tuple(docker_build_args or ())
|
2019-01-12 13:26:53 +01:00
|
|
|
self.active_containers = set()
|
|
|
|
self._lock = threading.Lock()
|
|
|
|
self._failed = False
|
|
|
|
|
2021-04-12 16:27:05 +02:00
|
|
|
def run_build(self, dist, skip_tests=False):
|
2019-01-12 13:26:53 +01:00
|
|
|
"""Build deb for a single distribution"""
|
|
|
|
|
|
|
|
if self._failed:
|
2021-05-14 12:46:35 +02:00
|
|
|
print("not building %s due to earlier failure" % (dist,))
|
2019-01-12 13:26:53 +01:00
|
|
|
raise Exception("failed")
|
|
|
|
|
|
|
|
try:
|
2021-04-12 16:27:05 +02:00
|
|
|
self._inner_build(dist, skip_tests)
|
2019-01-12 13:26:53 +01:00
|
|
|
except Exception as e:
|
|
|
|
print("build of %s failed: %s" % (dist, e), file=sys.stderr)
|
|
|
|
self._failed = True
|
|
|
|
raise
|
|
|
|
|
2021-04-12 16:27:05 +02:00
|
|
|
def _inner_build(self, dist, skip_tests=False):
|
2019-01-12 13:26:53 +01:00
|
|
|
tag = dist.split(":", 1)[1]
|
|
|
|
|
|
|
|
# Make the dir where the debs will live.
|
|
|
|
#
|
|
|
|
# Note that we deliberately put this outside the source tree, otherwise
|
|
|
|
# we tend to get source packages which are full of debs. (We could hack
|
|
|
|
# around that with more magic in the build_debian.sh script, but that
|
|
|
|
# doesn't solve the problem for natively-run dpkg-buildpakage).
|
2021-05-14 12:46:35 +02:00
|
|
|
debsdir = os.path.join(projdir, "../debs")
|
2019-01-12 13:26:53 +01:00
|
|
|
os.makedirs(debsdir, exist_ok=True)
|
|
|
|
|
|
|
|
if self.redirect_stdout:
|
2021-05-14 12:46:35 +02:00
|
|
|
logfile = os.path.join(debsdir, "%s.buildlog" % (tag,))
|
2019-01-12 13:26:53 +01:00
|
|
|
print("building %s: directing output to %s" % (dist, logfile))
|
|
|
|
stdout = open(logfile, "w")
|
|
|
|
else:
|
|
|
|
stdout = None
|
|
|
|
|
|
|
|
# first build a docker image for the build environment
|
2021-07-26 12:36:01 +02:00
|
|
|
build_args = (
|
|
|
|
(
|
2021-05-14 12:46:35 +02:00
|
|
|
"docker",
|
|
|
|
"build",
|
|
|
|
"--tag",
|
|
|
|
"dh-venv-builder:" + tag,
|
|
|
|
"--build-arg",
|
|
|
|
"distro=" + dist,
|
|
|
|
"-f",
|
|
|
|
"docker/Dockerfile-dhvirtualenv",
|
2021-07-26 12:36:01 +02:00
|
|
|
)
|
|
|
|
+ self._docker_build_args
|
|
|
|
+ ("docker",)
|
|
|
|
)
|
|
|
|
|
|
|
|
subprocess.check_call(
|
|
|
|
build_args,
|
2021-05-14 12:46:35 +02:00
|
|
|
stdout=stdout,
|
|
|
|
stderr=subprocess.STDOUT,
|
2021-07-12 20:03:14 +02:00
|
|
|
cwd=projdir,
|
2021-05-14 12:46:35 +02:00
|
|
|
)
|
2019-01-12 13:26:53 +01:00
|
|
|
|
|
|
|
container_name = "synapse_build_" + tag
|
|
|
|
with self._lock:
|
|
|
|
self.active_containers.add(container_name)
|
|
|
|
|
|
|
|
# then run the build itself
|
2021-05-14 12:46:35 +02:00
|
|
|
subprocess.check_call(
|
|
|
|
[
|
|
|
|
"docker",
|
|
|
|
"run",
|
|
|
|
"--rm",
|
|
|
|
"--name",
|
|
|
|
container_name,
|
|
|
|
"--volume=" + projdir + ":/synapse/source:ro",
|
|
|
|
"--volume=" + debsdir + ":/debs",
|
|
|
|
"-e",
|
|
|
|
"TARGET_USERID=%i" % (os.getuid(),),
|
|
|
|
"-e",
|
|
|
|
"TARGET_GROUPID=%i" % (os.getgid(),),
|
|
|
|
"-e",
|
|
|
|
"DEB_BUILD_OPTIONS=%s" % ("nocheck" if skip_tests else ""),
|
|
|
|
"dh-venv-builder:" + tag,
|
|
|
|
],
|
|
|
|
stdout=stdout,
|
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
)
|
2019-01-12 13:26:53 +01:00
|
|
|
|
|
|
|
with self._lock:
|
|
|
|
self.active_containers.remove(container_name)
|
|
|
|
|
|
|
|
if stdout is not None:
|
|
|
|
stdout.close()
|
2021-05-14 12:46:35 +02:00
|
|
|
print("Completed build of %s" % (dist,))
|
2019-01-12 13:26:53 +01:00
|
|
|
|
|
|
|
def kill_containers(self):
|
|
|
|
with self._lock:
|
|
|
|
active = list(self.active_containers)
|
|
|
|
|
|
|
|
for c in active:
|
|
|
|
print("killing container %s" % (c,))
|
2021-05-14 12:46:35 +02:00
|
|
|
subprocess.run(
|
|
|
|
[
|
|
|
|
"docker",
|
|
|
|
"kill",
|
|
|
|
c,
|
|
|
|
],
|
|
|
|
stdout=subprocess.DEVNULL,
|
|
|
|
)
|
2019-01-12 13:26:53 +01:00
|
|
|
with self._lock:
|
|
|
|
self.active_containers.remove(c)
|
|
|
|
|
|
|
|
|
2021-07-26 12:36:01 +02:00
|
|
|
def run_builds(builder, dists, jobs=1, skip_tests=False):
|
2019-01-12 13:26:53 +01:00
|
|
|
def sig(signum, _frame):
|
|
|
|
print("Caught SIGINT")
|
|
|
|
builder.kill_containers()
|
2021-05-14 12:46:35 +02:00
|
|
|
|
2019-01-12 13:26:53 +01:00
|
|
|
signal.signal(signal.SIGINT, sig)
|
|
|
|
|
|
|
|
with ThreadPoolExecutor(max_workers=jobs) as e:
|
2021-04-12 16:27:05 +02:00
|
|
|
res = e.map(lambda dist: builder.run_build(dist, skip_tests), dists)
|
2019-01-12 13:26:53 +01:00
|
|
|
|
|
|
|
# make sure we consume the iterable so that exceptions are raised.
|
2021-05-14 12:46:35 +02:00
|
|
|
for _ in res:
|
2019-01-12 13:26:53 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-05-14 12:46:35 +02:00
|
|
|
if __name__ == "__main__":
|
2019-01-12 13:26:53 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description=DESC,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2021-05-14 12:46:35 +02:00
|
|
|
"-j",
|
|
|
|
"--jobs",
|
|
|
|
type=int,
|
|
|
|
default=1,
|
|
|
|
help="specify the number of builds to run in parallel",
|
2019-01-12 13:26:53 +01:00
|
|
|
)
|
2021-04-12 16:27:05 +02:00
|
|
|
parser.add_argument(
|
2021-05-14 12:46:35 +02:00
|
|
|
"--no-check",
|
|
|
|
action="store_true",
|
|
|
|
help="skip running tests after building",
|
2021-04-12 16:27:05 +02:00
|
|
|
)
|
2021-07-26 12:36:01 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--docker-build-arg",
|
|
|
|
action="append",
|
|
|
|
help="specify an argument to pass to docker build",
|
|
|
|
)
|
2021-07-12 20:03:14 +02:00
|
|
|
parser.add_argument(
|
|
|
|
"--show-dists-json",
|
|
|
|
action="store_true",
|
|
|
|
help="instead of building the packages, just list the dists to build for, as a json array",
|
|
|
|
)
|
2019-01-12 13:26:53 +01:00
|
|
|
parser.add_argument(
|
2021-05-14 12:46:35 +02:00
|
|
|
"dist",
|
|
|
|
nargs="*",
|
|
|
|
default=DISTS,
|
|
|
|
help="a list of distributions to build for. Default: %(default)s",
|
2019-01-12 13:26:53 +01:00
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
2021-07-12 20:03:14 +02:00
|
|
|
if args.show_dists_json:
|
|
|
|
print(json.dumps(DISTS))
|
|
|
|
else:
|
2021-07-26 12:36:01 +02:00
|
|
|
builder = Builder(
|
|
|
|
redirect_stdout=(args.jobs > 1), docker_build_args=args.docker_build_arg
|
|
|
|
)
|
|
|
|
run_builds(
|
|
|
|
builder,
|
|
|
|
dists=args.dist,
|
|
|
|
jobs=args.jobs,
|
|
|
|
skip_tests=args.no_check,
|
|
|
|
)
|