Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

226 Zeilen
6.8 KiB

  1. #!/usr/bin/env python3
  2. # Build the Debian packages using Docker images.
  3. #
  4. # This script builds the Docker images and then executes them sequentially, each
  5. # one building a Debian package for the targeted operating system. It is
  6. # designed to be a "single command" to produce all the images.
  7. #
  8. # By default, builds for all known distributions, but a list of distributions
  9. # can be passed on the commandline for debugging.
  10. import argparse
  11. import json
  12. import os
  13. import signal
  14. import subprocess
  15. import sys
  16. import threading
  17. from concurrent.futures import ThreadPoolExecutor
  18. from types import FrameType
  19. from typing import Collection, Optional, Sequence, Set
  20. # These are expanded inside the dockerfile to be a fully qualified image name.
  21. # e.g. docker.io/library/debian:bullseye
  22. DISTS = (
  23. "debian:bullseye",
  24. "debian:bookworm",
  25. "debian:sid",
  26. "ubuntu:focal", # 20.04 LTS (our EOL forced by Py38 on 2024-10-14)
  27. "ubuntu:jammy", # 22.04 LTS (EOL 2027-04)
  28. "ubuntu:kinetic", # 22.10 (EOL 2023-07-20)
  29. "ubuntu:lunar", # 23.04 (EOL 2024-01)
  30. )
  31. DESC = """\
  32. Builds .debs for synapse, using a Docker image for the build environment.
  33. By default, builds for all known distributions, but a list of distributions
  34. can be passed on the commandline for debugging.
  35. """
  36. projdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  37. class Builder(object):
  38. def __init__(
  39. self,
  40. redirect_stdout: bool = False,
  41. docker_build_args: Optional[Sequence[str]] = None,
  42. ):
  43. self.redirect_stdout = redirect_stdout
  44. self._docker_build_args = tuple(docker_build_args or ())
  45. self.active_containers: Set[str] = set()
  46. self._lock = threading.Lock()
  47. self._failed = False
  48. def run_build(self, dist: str, skip_tests: bool = False) -> None:
  49. """Build deb for a single distribution"""
  50. if self._failed:
  51. print("not building %s due to earlier failure" % (dist,))
  52. raise Exception("failed")
  53. try:
  54. self._inner_build(dist, skip_tests)
  55. except Exception as e:
  56. print("build of %s failed: %s" % (dist, e), file=sys.stderr)
  57. self._failed = True
  58. raise
  59. def _inner_build(self, dist: str, skip_tests: bool = False) -> None:
  60. tag = dist.split(":", 1)[1]
  61. # Make the dir where the debs will live.
  62. #
  63. # Note that we deliberately put this outside the source tree, otherwise
  64. # we tend to get source packages which are full of debs. (We could hack
  65. # around that with more magic in the build_debian.sh script, but that
  66. # doesn't solve the problem for natively-run dpkg-buildpakage).
  67. debsdir = os.path.join(projdir, "../debs")
  68. os.makedirs(debsdir, exist_ok=True)
  69. if self.redirect_stdout:
  70. logfile = os.path.join(debsdir, "%s.buildlog" % (tag,))
  71. print("building %s: directing output to %s" % (dist, logfile))
  72. stdout = open(logfile, "w")
  73. else:
  74. stdout = None
  75. # first build a docker image for the build environment
  76. build_args = (
  77. (
  78. "docker",
  79. "build",
  80. "--tag",
  81. "dh-venv-builder:" + tag,
  82. "--build-arg",
  83. "distro=" + dist,
  84. "-f",
  85. "docker/Dockerfile-dhvirtualenv",
  86. )
  87. + self._docker_build_args
  88. + ("docker",)
  89. )
  90. subprocess.check_call(
  91. build_args,
  92. stdout=stdout,
  93. stderr=subprocess.STDOUT,
  94. cwd=projdir,
  95. )
  96. container_name = "synapse_build_" + tag
  97. with self._lock:
  98. self.active_containers.add(container_name)
  99. # then run the build itself
  100. subprocess.check_call(
  101. [
  102. "docker",
  103. "run",
  104. "--rm",
  105. "--name",
  106. container_name,
  107. "--volume=" + projdir + ":/synapse/source:ro",
  108. "--volume=" + debsdir + ":/debs",
  109. "-e",
  110. "TARGET_USERID=%i" % (os.getuid(),),
  111. "-e",
  112. "TARGET_GROUPID=%i" % (os.getgid(),),
  113. "-e",
  114. "DEB_BUILD_OPTIONS=%s" % ("nocheck" if skip_tests else ""),
  115. "dh-venv-builder:" + tag,
  116. ],
  117. stdout=stdout,
  118. stderr=subprocess.STDOUT,
  119. )
  120. with self._lock:
  121. self.active_containers.remove(container_name)
  122. if stdout is not None:
  123. stdout.close()
  124. print("Completed build of %s" % (dist,))
  125. def kill_containers(self) -> None:
  126. with self._lock:
  127. active = list(self.active_containers)
  128. for c in active:
  129. print("killing container %s" % (c,))
  130. subprocess.run(
  131. [
  132. "docker",
  133. "kill",
  134. c,
  135. ],
  136. stdout=subprocess.DEVNULL,
  137. )
  138. with self._lock:
  139. self.active_containers.remove(c)
  140. def run_builds(
  141. builder: Builder, dists: Collection[str], jobs: int = 1, skip_tests: bool = False
  142. ) -> None:
  143. def sig(signum: int, _frame: Optional[FrameType]) -> None:
  144. print("Caught SIGINT")
  145. builder.kill_containers()
  146. signal.signal(signal.SIGINT, sig)
  147. with ThreadPoolExecutor(max_workers=jobs) as e:
  148. res = e.map(lambda dist: builder.run_build(dist, skip_tests), dists)
  149. # make sure we consume the iterable so that exceptions are raised.
  150. for _ in res:
  151. pass
  152. if __name__ == "__main__":
  153. parser = argparse.ArgumentParser(
  154. description=DESC,
  155. )
  156. parser.add_argument(
  157. "-j",
  158. "--jobs",
  159. type=int,
  160. default=1,
  161. help="specify the number of builds to run in parallel",
  162. )
  163. parser.add_argument(
  164. "--no-check",
  165. action="store_true",
  166. help="skip running tests after building",
  167. )
  168. parser.add_argument(
  169. "--docker-build-arg",
  170. action="append",
  171. help="specify an argument to pass to docker build",
  172. )
  173. parser.add_argument(
  174. "--show-dists-json",
  175. action="store_true",
  176. help="instead of building the packages, just list the dists to build for, as a json array",
  177. )
  178. parser.add_argument(
  179. "dist",
  180. nargs="*",
  181. default=DISTS,
  182. help="a list of distributions to build for. Default: %(default)s",
  183. )
  184. args = parser.parse_args()
  185. if args.show_dists_json:
  186. print(json.dumps(DISTS))
  187. else:
  188. builder = Builder(
  189. redirect_stdout=(args.jobs > 1), docker_build_args=args.docker_build_arg
  190. )
  191. run_builds(
  192. builder,
  193. dists=args.dist,
  194. jobs=args.jobs,
  195. skip_tests=args.no_check,
  196. )