blob: 56dd211900a18de6a09918e9bd04ea802657818e [file] [log] [blame]
Patrick Williams02871c92021-02-01 20:57:19 -06001#!/usr/bin/env python3
2#
3# Build the required docker image to run package unit tests
4#
5# Script Variables:
6# DOCKER_IMG_NAME: <optional, the name of the docker image to generate>
7# default is openbmc/ubuntu-unit-test
8# DISTRO: <optional, the distro to build a docker image against>
Patrick Williams50837432021-02-06 12:24:05 -06009# FORCE_DOCKER_BUILD: <optional, a non-zero value with force all Docker
10# images to be rebuilt rather than reusing caches.>
11# BUILD_URL: <optional, used to detect running under CI context
12# (ex. Jenkins)>
Patrick Williams02871c92021-02-01 20:57:19 -060013# BRANCH: <optional, branch to build from each of the openbmc/
14# repositories>
15# default is master, which will be used if input branch not
16# provided or not found
17# UBUNTU_MIRROR: <optional, the URL of a mirror of Ubuntu to override the
18# default ones in /etc/apt/sources.list>
19# default is empty, and no mirror is used.
20# http_proxy The HTTP address of the proxy server to connect to.
21# Default: "", proxy is not setup if this is not set
22
Patrick Williams276bd0e2024-10-02 10:34:32 -040023import json
Patrick Williams02871c92021-02-01 20:57:19 -060024import os
Andrew Geisslerf3d27e62024-04-09 15:24:49 -050025import re
Patrick Williams02871c92021-02-01 20:57:19 -060026import sys
Patrick Williamsb16f3e22021-02-06 08:16:47 -060027import threading
Patrick Williams276bd0e2024-10-02 10:34:32 -040028import urllib.request
Patrick Williamsa18d9c52021-02-05 09:52:26 -060029from datetime import date
30from hashlib import sha256
Patrick Williamse08ffba2022-12-05 10:33:46 -060031
32# typing.Dict is used for type-hints.
33from typing import Any, Callable, Dict, Iterable, Optional # noqa: F401
Patrick Williams02871c92021-02-01 20:57:19 -060034
Patrick Williams41d86212022-11-25 18:28:43 -060035from sh import docker, git, nproc, uname # type: ignore
36
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060037try:
38 # Python before 3.8 doesn't have TypedDict, so reroute to standard 'dict'.
39 from typing import TypedDict
Patrick Williams41d86212022-11-25 18:28:43 -060040except Exception:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060041
42 class TypedDict(dict): # type: ignore
43 # We need to do this to eat the 'total' argument.
Patrick Williams41d86212022-11-25 18:28:43 -060044 def __init_subclass__(cls, **kwargs: Any) -> None:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060045 super().__init_subclass__()
46
47
48# Declare some variables used in package definitions.
Patrick Williamsaae36d12021-02-04 16:30:04 -060049prefix = "/usr/local"
Patrick Williams02871c92021-02-01 20:57:19 -060050proc_count = nproc().strip()
Patrick Williams02871c92021-02-01 20:57:19 -060051
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060052
53class PackageDef(TypedDict, total=False):
Patrick Williams05fb2a02022-10-11 17:22:33 -050054 """Package Definition for packages dictionary."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060055
56 # rev [optional]: Revision of package to use.
57 rev: str
58 # url [optional]: lambda function to create URL: (package, rev) -> url.
59 url: Callable[[str, str], str]
60 # depends [optional]: List of package dependencies.
61 depends: Iterable[str]
62 # build_type [required]: Build type used for package.
63 # Currently supported: autoconf, cmake, custom, make, meson
64 build_type: str
65 # build_steps [optional]: Steps to run for 'custom' build_type.
66 build_steps: Iterable[str]
67 # config_flags [optional]: List of options to pass configuration tool.
68 config_flags: Iterable[str]
69 # config_env [optional]: List of environment variables to set for config.
70 config_env: Iterable[str]
71 # custom_post_dl [optional]: List of steps to run after download, but
72 # before config / build / install.
73 custom_post_dl: Iterable[str]
Patrick Williams6bce2ca2021-02-12 21:13:37 -060074 # custom_post_install [optional]: List of steps to run after install.
75 custom_post_install: Iterable[str]
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060076
77 # __tag [private]: Generated Docker tag name for package stage.
78 __tag: str
79 # __package [private]: Package object associated with this package.
80 __package: Any # Type is Package, but not defined yet.
81
Patrick Williams02871c92021-02-01 20:57:19 -060082
Patrick Williams72043242021-02-02 10:31:45 -060083# Packages to include in image.
84packages = {
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060085 "boost": PackageDef(
Andrew Geissler05806f52024-01-07 08:39:29 -060086 rev="1.84.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060087 url=(
Andrew Geissler38b46872024-01-07 07:20:27 -060088 lambda pkg, rev: f"https://github.com/boostorg/{pkg}/releases/download/{pkg}-{rev}/{pkg}-{rev}.tar.gz"
Patrick Williams2abc4a42021-02-03 06:11:40 -060089 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060090 build_type="custom",
91 build_steps=[
Patrick Williamse08ffba2022-12-05 10:33:46 -060092 (
Andrew Geissler38b46872024-01-07 07:20:27 -060093 "./bootstrap.sh"
Ed Tanous42ff4322023-10-04 17:39:08 -070094 f" --prefix={prefix} --with-libraries=context,coroutine,url"
Patrick Williamse08ffba2022-12-05 10:33:46 -060095 ),
Patrick Williamsaae36d12021-02-04 16:30:04 -060096 "./b2",
Michal Orzel04770cc2024-06-18 10:38:22 +020097 f"./b2 install --prefix={prefix} valgrind=on",
Patrick Williamsaae36d12021-02-04 16:30:04 -060098 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060099 ),
100 "USCiLab/cereal": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -0500101 rev="v1.3.2",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600102 build_type="custom",
103 build_steps=[f"cp -a include/cereal/ {prefix}/include/"],
104 ),
Ed Tanousc7198552022-07-01 08:15:50 -0700105 "danmar/cppcheck": PackageDef(
Patrick Williams51021782023-12-05 19:10:44 -0600106 rev="2.12.1",
Ed Tanousc7198552022-07-01 08:15:50 -0700107 build_type="cmake",
108 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600109 "CLIUtils/CLI11": PackageDef(
Patrick Williamsfc397332023-07-17 11:35:43 -0500110 rev="v2.3.2",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600111 build_type="cmake",
112 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600113 "-DBUILD_TESTING=OFF",
114 "-DCLI11_BUILD_DOCS=OFF",
115 "-DCLI11_BUILD_EXAMPLES=OFF",
116 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600117 ),
118 "fmtlib/fmt": PackageDef(
Patrick Williamsc061e072023-12-05 19:11:21 -0600119 rev="10.1.1",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600120 build_type="cmake",
121 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600122 "-DFMT_DOC=OFF",
123 "-DFMT_TEST=OFF",
124 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600125 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600126 "Naios/function2": PackageDef(
Patrick Williamscb099742023-12-05 19:12:09 -0600127 rev="4.2.4",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600128 build_type="custom",
129 build_steps=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600130 f"mkdir {prefix}/include/function2",
131 f"cp include/function2/function2.hpp {prefix}/include/function2/",
132 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600133 ),
134 "google/googletest": PackageDef(
Patrick Williamsd11e9c72024-08-17 06:44:00 -0400135 rev="v1.15.2",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600136 build_type="cmake",
William A. Kennington III4dd32c02021-05-28 01:58:13 -0700137 config_env=["CXXFLAGS=-std=c++20"],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600138 config_flags=["-DTHREADS_PREFER_PTHREAD_FLAG=ON"],
139 ),
Ed Tanous178b4b22023-06-15 09:03:11 -0700140 "nghttp2/nghttp2": PackageDef(
Ed Tanousabb106a2024-04-04 10:00:02 -0700141 rev="v1.61.0",
Ed Tanous178b4b22023-06-15 09:03:11 -0700142 build_type="cmake",
143 config_env=["CXXFLAGS=-std=c++20"],
144 config_flags=[
145 "-DENABLE_LIB_ONLY=ON",
146 "-DENABLE_STATIC_LIB=ON",
147 ],
148 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600149 "nlohmann/json": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -0500150 rev="v3.11.2",
Patrick Williams6bce2ca2021-02-12 21:13:37 -0600151 build_type="cmake",
152 config_flags=["-DJSON_BuildTests=OFF"],
153 custom_post_install=[
Patrick Williamse08ffba2022-12-05 10:33:46 -0600154 (
155 f"ln -s {prefix}/include/nlohmann/json.hpp"
156 f" {prefix}/include/json.hpp"
157 ),
Patrick Williamsaae36d12021-02-04 16:30:04 -0600158 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600159 ),
Przemyslaw Czarnowski058e3a32022-12-21 14:13:23 +0100160 "json-c/json-c": PackageDef(
Patrick Williamseee65be2023-12-05 19:17:01 -0600161 rev="json-c-0.17-20230812",
Przemyslaw Czarnowski058e3a32022-12-21 14:13:23 +0100162 build_type="cmake",
163 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600164 "LibVNC/libvncserver": PackageDef(
Patrick Williamsc0421322023-12-05 19:18:57 -0600165 rev="LibVNCServer-0.9.14",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600166 build_type="cmake",
167 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600168 "leethomason/tinyxml2": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -0500169 rev="9.0.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600170 build_type="cmake",
171 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600172 "tristanpenman/valijson": PackageDef(
Patrick Williams5a2c1132023-12-05 19:20:36 -0600173 rev="v1.0.1",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600174 build_type="cmake",
175 config_flags=[
Patrick Williams0eedeed2021-02-06 19:06:09 -0600176 "-Dvalijson_BUILD_TESTS=0",
177 "-Dvalijson_INSTALL_HEADERS=1",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600178 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600179 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600180 "open-power/pdbg": PackageDef(build_type="autoconf"),
181 "openbmc/gpioplus": PackageDef(
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600182 build_type="meson",
183 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600184 "-Dexamples=false",
185 "-Dtests=disabled",
186 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600187 ),
188 "openbmc/phosphor-dbus-interfaces": PackageDef(
189 depends=["openbmc/sdbusplus"],
190 build_type="meson",
William A. Kennington III4fe87772022-02-11 15:44:29 -0800191 config_flags=["-Dgenerate_md=false"],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600192 ),
193 "openbmc/phosphor-logging": PackageDef(
194 depends=[
Patrick Williams83394612021-02-03 07:12:50 -0600195 "USCiLab/cereal",
Patrick Williams83394612021-02-03 07:12:50 -0600196 "openbmc/phosphor-dbus-interfaces",
197 "openbmc/sdbusplus",
198 "openbmc/sdeventplus",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600199 ],
Patrick Williamsf79ce4c2021-04-30 16:00:49 -0500200 build_type="meson",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600201 config_flags=[
William A. Kennington III6c98f282022-10-05 13:37:04 -0700202 "-Dlibonly=true",
203 "-Dtests=disabled",
Patrick Williams5eabdae2022-04-14 14:34:34 -0500204 f"-Dyamldir={prefix}/share/phosphor-dbus-yaml/yaml",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600205 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600206 ),
207 "openbmc/phosphor-objmgr": PackageDef(
208 depends=[
Brad Bishop11e57622022-09-14 16:10:25 -0400209 "CLIUtils/CLI11",
Patrick Williams70af95c2022-09-27 16:55:41 -0500210 "boost",
Patrick Williams83394612021-02-03 07:12:50 -0600211 "leethomason/tinyxml2",
Patrick Williams70af95c2022-09-27 16:55:41 -0500212 "openbmc/phosphor-dbus-interfaces",
Patrick Williams83394612021-02-03 07:12:50 -0600213 "openbmc/phosphor-logging",
214 "openbmc/sdbusplus",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600215 ],
Brad Bishop1197e352021-08-03 19:25:46 -0400216 build_type="meson",
217 config_flags=[
218 "-Dtests=disabled",
219 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600220 ),
Jason M. Billsc02ff272023-08-02 10:55:22 -0700221 "openbmc/libpeci": PackageDef(
222 build_type="meson",
223 config_flags=[
224 "-Draw-peci=disabled",
225 ],
226 ),
Manojkiran Eda1c19e452022-10-03 11:01:59 +0530227 "openbmc/libpldm": PackageDef(
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600228 build_type="meson",
229 config_flags=[
Andrew Jeffery29d69bb2023-06-06 14:38:24 +0930230 "-Dabi=deprecated,stable",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600231 "-Doem-ibm=enabled",
232 "-Dtests=disabled",
233 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600234 ),
235 "openbmc/sdbusplus": PackageDef(
Patrick Williams54d01da2024-09-25 06:40:25 -0400236 depends=[
237 "nlohmann/json",
238 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600239 build_type="meson",
240 custom_post_dl=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600241 "cd tools",
242 f"./setup.py install --root=/ --prefix={prefix}",
243 "cd ..",
244 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600245 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600246 "-Dexamples=disabled",
247 "-Dtests=disabled",
248 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600249 ),
250 "openbmc/sdeventplus": PackageDef(
Patrick Williams70af95c2022-09-27 16:55:41 -0500251 depends=[
Patrick Williams70af95c2022-09-27 16:55:41 -0500252 "openbmc/stdplus",
253 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600254 build_type="meson",
255 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600256 "-Dexamples=false",
257 "-Dtests=disabled",
258 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600259 ),
260 "openbmc/stdplus": PackageDef(
Patrick Williams70af95c2022-09-27 16:55:41 -0500261 depends=[
Patrick Williams70af95c2022-09-27 16:55:41 -0500262 "fmtlib/fmt",
William A. Kennington IIIca1bf0c2022-10-05 02:23:30 -0700263 "google/googletest",
264 "Naios/function2",
Patrick Williams70af95c2022-09-27 16:55:41 -0500265 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600266 build_type="meson",
267 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600268 "-Dexamples=false",
269 "-Dtests=disabled",
William A. Kennington IIIca1bf0c2022-10-05 02:23:30 -0700270 "-Dgtest=enabled",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600271 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600272 ),
273} # type: Dict[str, PackageDef]
Patrick Williams02871c92021-02-01 20:57:19 -0600274
275# Define common flags used for builds
Patrick Williams02871c92021-02-01 20:57:19 -0600276configure_flags = " ".join(
277 [
278 f"--prefix={prefix}",
279 ]
280)
281cmake_flags = " ".join(
282 [
Patrick Williams02871c92021-02-01 20:57:19 -0600283 "-DBUILD_SHARED_LIBS=ON",
Patrick Williams0f2086b2021-02-05 06:49:49 -0600284 "-DCMAKE_BUILD_TYPE=RelWithDebInfo",
Patrick Williams02871c92021-02-01 20:57:19 -0600285 f"-DCMAKE_INSTALL_PREFIX:PATH={prefix}",
Patrick Williams0f2086b2021-02-05 06:49:49 -0600286 "-GNinja",
287 "-DCMAKE_MAKE_PROGRAM=ninja",
Patrick Williams02871c92021-02-01 20:57:19 -0600288 ]
289)
290meson_flags = " ".join(
291 [
292 "--wrap-mode=nodownload",
293 f"-Dprefix={prefix}",
294 ]
295)
296
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600297
298class Package(threading.Thread):
299 """Class used to build the Docker stages for each package.
300
301 Generally, this class should not be instantiated directly but through
302 Package.generate_all().
303 """
304
305 # Copy the packages dictionary.
306 packages = packages.copy()
307
308 # Lock used for thread-safety.
309 lock = threading.Lock()
310
311 def __init__(self, pkg: str):
Patrick Williams05fb2a02022-10-11 17:22:33 -0500312 """pkg - The name of this package (ex. foo/bar )"""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600313 super(Package, self).__init__()
314
315 self.package = pkg
316 self.exception = None # type: Optional[Exception]
317
318 # Reference to this package's
319 self.pkg_def = Package.packages[pkg]
320 self.pkg_def["__package"] = self
321
322 def run(self) -> None:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500323 """Thread 'run' function. Builds the Docker stage."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600324
325 # In case this package has no rev, fetch it from Github.
326 self._update_rev()
327
328 # Find all the Package objects that this package depends on.
329 # This section is locked because we are looking into another
330 # package's PackageDef dict, which could be being modified.
331 Package.lock.acquire()
332 deps: Iterable[Package] = [
333 Package.packages[deppkg]["__package"]
334 for deppkg in self.pkg_def.get("depends", [])
335 ]
336 Package.lock.release()
337
338 # Wait until all the depends finish building. We need them complete
339 # for the "COPY" commands.
340 for deppkg in deps:
341 deppkg.join()
342
343 # Generate this package's Dockerfile.
344 dockerfile = f"""
345FROM {docker_base_img_name}
346{self._df_copycmds()}
347{self._df_build()}
348"""
349
350 # Generate the resulting tag name and save it to the PackageDef.
351 # This section is locked because we are modifying the PackageDef,
352 # which can be accessed by other threads.
353 Package.lock.acquire()
354 tag = Docker.tagname(self._stagename(), dockerfile)
355 self.pkg_def["__tag"] = tag
356 Package.lock.release()
357
358 # Do the build / save any exceptions.
359 try:
360 Docker.build(self.package, tag, dockerfile)
361 except Exception as e:
362 self.exception = e
363
364 @classmethod
365 def generate_all(cls) -> None:
366 """Ensure a Docker stage is created for all defined packages.
367
368 These are done in parallel but with appropriate blocking per
369 package 'depends' specifications.
370 """
371
372 # Create a Package for each defined package.
373 pkg_threads = [Package(p) for p in cls.packages.keys()]
374
375 # Start building them all.
Patrick Williams6dbd7802021-02-20 08:34:10 -0600376 # This section is locked because threads depend on each other,
377 # based on the packages, and they cannot 'join' on a thread
378 # which is not yet started. Adding a lock here allows all the
379 # threads to start before they 'join' their dependencies.
380 Package.lock.acquire()
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600381 for t in pkg_threads:
382 t.start()
Patrick Williams6dbd7802021-02-20 08:34:10 -0600383 Package.lock.release()
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600384
385 # Wait for completion.
386 for t in pkg_threads:
387 t.join()
388 # Check if the thread saved off its own exception.
389 if t.exception:
390 print(f"Package {t.package} failed!", file=sys.stderr)
391 raise t.exception
392
393 @staticmethod
394 def df_all_copycmds() -> str:
395 """Formulate the Dockerfile snippet necessary to copy all packages
396 into the final image.
397 """
398 return Package.df_copycmds_set(Package.packages.keys())
399
400 @classmethod
401 def depcache(cls) -> str:
402 """Create the contents of the '/tmp/depcache'.
403 This file is a comma-separated list of "<pkg>:<rev>".
404 """
405
406 # This needs to be sorted for consistency.
407 depcache = ""
408 for pkg in sorted(cls.packages.keys()):
409 depcache += "%s:%s," % (pkg, cls.packages[pkg]["rev"])
410 return depcache
411
Patrick Williams276bd0e2024-10-02 10:34:32 -0400412 def _check_gerrit_topic(self) -> bool:
413 if not gerrit_topic:
414 return False
415 if not self.package.startswith("openbmc/"):
416 return False
417 if gerrit_project == self.package and gerrit_rev:
418 return False
419
420 try:
421 commits = json.loads(
422 urllib.request.urlopen(
423 f"https://gerrit.openbmc.org/changes/?q=status:open+project:{self.package}+topic:{gerrit_topic}"
424 )
425 .read()
426 .splitlines()[-1]
427 )
428
429 if len(commits) == 0:
430 return False
431 if len(commits) > 1:
432 print(
433 f"{self.package} has more than 1 commit under {gerrit_topic}; using lastest upstream: {len(commits)}",
434 file=sys.stderr,
435 )
436 return False
437
438 change_id = commits[0]["id"]
439
440 commit = json.loads(
441 urllib.request.urlopen(
442 f"https://gerrit.openbmc.org/changes/{change_id}/revisions/current/commit"
443 )
444 .read()
445 .splitlines()[-1]
446 )["commit"]
447
448 print(
449 f"Using {commit} from {gerrit_topic} for {self.package}",
450 file=sys.stderr,
451 )
452 self.pkg_def["rev"] = commit
453 return True
454
455 except urllib.error.HTTPError as e:
456 print(
457 f"Error loading topic {gerrit_topic} for {self.package}: ",
458 e,
459 file=sys.stderr,
460 )
461 return False
462
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600463 def _update_rev(self) -> None:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500464 """Look up the HEAD for missing a static rev."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600465
466 if "rev" in self.pkg_def:
467 return
468
Patrick Williams276bd0e2024-10-02 10:34:32 -0400469 if self._check_gerrit_topic():
470 return
471
Patrick Williams65b21fb2021-02-12 21:21:14 -0600472 # Check if Jenkins/Gerrit gave us a revision and use it.
473 if gerrit_project == self.package and gerrit_rev:
474 print(
475 f"Found Gerrit revision for {self.package}: {gerrit_rev}",
476 file=sys.stderr,
477 )
478 self.pkg_def["rev"] = gerrit_rev
479 return
480
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600481 # Ask Github for all the branches.
Patrick Williams05fb2a02022-10-11 17:22:33 -0500482 lookup = git(
483 "ls-remote", "--heads", f"https://github.com/{self.package}"
484 )
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600485
486 # Find the branch matching {branch} (or fallback to master).
487 # This section is locked because we are modifying the PackageDef.
488 Package.lock.acquire()
489 for line in lookup.split("\n"):
Andrew Geisslerf3d27e62024-04-09 15:24:49 -0500490 if re.fullmatch(f".*{branch}$", line.strip()):
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600491 self.pkg_def["rev"] = line.split()[0]
Andrew Geisslerf3d27e62024-04-09 15:24:49 -0500492 break
Patrick Williamsc7d73642022-10-11 17:22:06 -0500493 elif (
494 "refs/heads/master" in line or "refs/heads/main" in line
495 ) and "rev" not in self.pkg_def:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600496 self.pkg_def["rev"] = line.split()[0]
497 Package.lock.release()
498
499 def _stagename(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500500 """Create a name for the Docker stage associated with this pkg."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600501 return self.package.replace("/", "-").lower()
502
503 def _url(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500504 """Get the URL for this package."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600505 rev = self.pkg_def["rev"]
506
507 # If the lambda exists, call it.
508 if "url" in self.pkg_def:
509 return self.pkg_def["url"](self.package, rev)
510
511 # Default to the github archive URL.
512 return f"https://github.com/{self.package}/archive/{rev}.tar.gz"
513
514 def _cmd_download(self) -> str:
515 """Formulate the command necessary to download and unpack to source."""
516
517 url = self._url()
518 if ".tar." not in url:
519 raise NotImplementedError(
520 f"Unhandled download type for {self.package}: {url}"
521 )
522
523 cmd = f"curl -L {url} | tar -x"
524
525 if url.endswith(".bz2"):
526 cmd += "j"
527 elif url.endswith(".gz"):
528 cmd += "z"
529 else:
530 raise NotImplementedError(
531 f"Unknown tar flags needed for {self.package}: {url}"
532 )
533
534 return cmd
535
536 def _cmd_cd_srcdir(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500537 """Formulate the command necessary to 'cd' into the source dir."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600538 return f"cd {self.package.split('/')[-1]}*"
539
540 def _df_copycmds(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500541 """Formulate the dockerfile snippet necessary to COPY all depends."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600542
543 if "depends" not in self.pkg_def:
544 return ""
545 return Package.df_copycmds_set(self.pkg_def["depends"])
546
547 @staticmethod
548 def df_copycmds_set(pkgs: Iterable[str]) -> str:
549 """Formulate the Dockerfile snippet necessary to COPY a set of
550 packages into a Docker stage.
551 """
552
553 copy_cmds = ""
554
555 # Sort the packages for consistency.
556 for p in sorted(pkgs):
557 tag = Package.packages[p]["__tag"]
558 copy_cmds += f"COPY --from={tag} {prefix} {prefix}\n"
559 # Workaround for upstream docker bug and multiple COPY cmds
560 # https://github.com/moby/moby/issues/37965
561 copy_cmds += "RUN true\n"
562
563 return copy_cmds
564
565 def _df_build(self) -> str:
566 """Formulate the Dockerfile snippet necessary to download, build, and
567 install a package into a Docker stage.
568 """
569
570 # Download and extract source.
571 result = f"RUN {self._cmd_download()} && {self._cmd_cd_srcdir()} && "
572
573 # Handle 'custom_post_dl' commands.
574 custom_post_dl = self.pkg_def.get("custom_post_dl")
575 if custom_post_dl:
576 result += " && ".join(custom_post_dl) + " && "
577
578 # Build and install package based on 'build_type'.
579 build_type = self.pkg_def["build_type"]
580 if build_type == "autoconf":
581 result += self._cmd_build_autoconf()
582 elif build_type == "cmake":
583 result += self._cmd_build_cmake()
584 elif build_type == "custom":
585 result += self._cmd_build_custom()
586 elif build_type == "make":
587 result += self._cmd_build_make()
588 elif build_type == "meson":
589 result += self._cmd_build_meson()
590 else:
591 raise NotImplementedError(
592 f"Unhandled build type for {self.package}: {build_type}"
593 )
594
Patrick Williams6bce2ca2021-02-12 21:13:37 -0600595 # Handle 'custom_post_install' commands.
596 custom_post_install = self.pkg_def.get("custom_post_install")
597 if custom_post_install:
598 result += " && " + " && ".join(custom_post_install)
599
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600600 return result
601
602 def _cmd_build_autoconf(self) -> str:
603 options = " ".join(self.pkg_def.get("config_flags", []))
604 env = " ".join(self.pkg_def.get("config_env", []))
605 result = "./bootstrap.sh && "
606 result += f"{env} ./configure {configure_flags} {options} && "
607 result += f"make -j{proc_count} && make install"
608 return result
609
610 def _cmd_build_cmake(self) -> str:
611 options = " ".join(self.pkg_def.get("config_flags", []))
612 env = " ".join(self.pkg_def.get("config_env", []))
613 result = "mkdir builddir && cd builddir && "
614 result += f"{env} cmake {cmake_flags} {options} .. && "
615 result += "cmake --build . --target all && "
616 result += "cmake --build . --target install && "
617 result += "cd .."
618 return result
619
620 def _cmd_build_custom(self) -> str:
621 return " && ".join(self.pkg_def.get("build_steps", []))
622
623 def _cmd_build_make(self) -> str:
624 return f"make -j{proc_count} && make install"
625
626 def _cmd_build_meson(self) -> str:
627 options = " ".join(self.pkg_def.get("config_flags", []))
628 env = " ".join(self.pkg_def.get("config_env", []))
Andrew Jefferye2da11a2023-06-15 10:16:37 +0930629 result = f"{env} meson setup builddir {meson_flags} {options} && "
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600630 result += "ninja -C builddir && ninja -C builddir install"
631 return result
632
633
634class Docker:
635 """Class to assist with Docker interactions. All methods are static."""
636
637 @staticmethod
638 def timestamp() -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500639 """Generate a timestamp for today using the ISO week."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600640 today = date.today().isocalendar()
641 return f"{today[0]}-W{today[1]:02}"
642
643 @staticmethod
Patrick Williams41d86212022-11-25 18:28:43 -0600644 def tagname(pkgname: Optional[str], dockerfile: str) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500645 """Generate a tag name for a package using a hash of the Dockerfile."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600646 result = docker_image_name
647 if pkgname:
648 result += "-" + pkgname
649
650 result += ":" + Docker.timestamp()
651 result += "-" + sha256(dockerfile.encode()).hexdigest()[0:16]
652
653 return result
654
655 @staticmethod
656 def build(pkg: str, tag: str, dockerfile: str) -> None:
Andrew Geissler22e61102023-02-14 14:44:00 -0600657 """Build a docker image using the Dockerfile and tagging it with 'tag'."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600658
659 # If we're not forcing builds, check if it already exists and skip.
660 if not force_build:
661 if docker.image.ls(tag, "--format", '"{{.Repository}}:{{.Tag}}"'):
Patrick Williams05fb2a02022-10-11 17:22:33 -0500662 print(
663 f"Image {tag} already exists. Skipping.", file=sys.stderr
664 )
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600665 return
666
667 # Build it.
668 # Capture the output of the 'docker build' command and send it to
669 # stderr (prefixed with the package name). This allows us to see
Manojkiran Edaa6ebc6e2024-06-17 11:51:45 +0530670 # progress but not pollute stdout. Later on we output the final
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600671 # docker tag to stdout and we want to keep that pristine.
672 #
673 # Other unusual flags:
674 # --no-cache: Bypass the Docker cache if 'force_build'.
675 # --force-rm: Clean up Docker processes if they fail.
676 docker.build(
677 proxy_args,
678 "--network=host",
679 "--force-rm",
680 "--no-cache=true" if force_build else "--no-cache=false",
681 "-t",
682 tag,
683 "-",
684 _in=dockerfile,
685 _out=(
686 lambda line: print(
687 pkg + ":", line, end="", file=sys.stderr, flush=True
688 )
689 ),
Jonathan Doman88dd7922024-05-02 10:34:21 -0700690 _err_to_out=True,
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600691 )
692
693
694# Read a bunch of environment variables.
Patrick Williams05fb2a02022-10-11 17:22:33 -0500695docker_image_name = os.environ.get(
696 "DOCKER_IMAGE_NAME", "openbmc/ubuntu-unit-test"
697)
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600698force_build = os.environ.get("FORCE_DOCKER_BUILD")
699is_automated_ci_build = os.environ.get("BUILD_URL", False)
Patrick Williams7c95a372024-01-05 19:22:58 -0600700distro = os.environ.get("DISTRO", "ubuntu:noble")
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600701branch = os.environ.get("BRANCH", "master")
702ubuntu_mirror = os.environ.get("UBUNTU_MIRROR")
703http_proxy = os.environ.get("http_proxy")
704
Patrick Williams65b21fb2021-02-12 21:21:14 -0600705gerrit_project = os.environ.get("GERRIT_PROJECT")
706gerrit_rev = os.environ.get("GERRIT_PATCHSET_REVISION")
Patrick Williams276bd0e2024-10-02 10:34:32 -0400707gerrit_topic = os.environ.get("GERRIT_TOPIC")
Patrick Williams65b21fb2021-02-12 21:21:14 -0600708
Andrew Geisslerd0dabc32023-04-04 08:09:21 -0600709# Ensure appropriate docker build output to see progress and identify
710# any issues
711os.environ["BUILDKIT_PROGRESS"] = "plain"
712
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600713# Set up some common variables.
714username = os.environ.get("USER", "root")
715homedir = os.environ.get("HOME", "/root")
716gid = os.getgid()
717uid = os.getuid()
718
Josh Lehan6825a012022-03-17 18:31:39 -0700719# Use well-known constants if user is root
720if username == "root":
721 homedir = "/root"
722 gid = 0
723 uid = 0
724
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600725# Determine the architecture for Docker.
726arch = uname("-m").strip()
727if arch == "ppc64le":
728 docker_base = "ppc64le/"
729elif arch == "x86_64":
730 docker_base = ""
Thang Q. Nguyen051b05b2021-12-10 08:30:35 +0000731elif arch == "aarch64":
Thang Q. Nguyenf98f1a82021-12-22 01:59:19 +0000732 docker_base = "arm64v8/"
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600733else:
734 print(
735 f"Unsupported system architecture({arch}) found for docker image",
736 file=sys.stderr,
737 )
738 sys.exit(1)
739
Patrick Williams02871c92021-02-01 20:57:19 -0600740# Special flags if setting up a deb mirror.
741mirror = ""
742if "ubuntu" in distro and ubuntu_mirror:
743 mirror = f"""
Patrick Williamse08ffba2022-12-05 10:33:46 -0600744RUN echo "deb {ubuntu_mirror} \
745 $(. /etc/os-release && echo $VERSION_CODENAME) \
746 main restricted universe multiverse" > /etc/apt/sources.list && \\
747 echo "deb {ubuntu_mirror} \
748 $(. /etc/os-release && echo $VERSION_CODENAME)-updates \
749 main restricted universe multiverse" >> /etc/apt/sources.list && \\
750 echo "deb {ubuntu_mirror} \
751 $(. /etc/os-release && echo $VERSION_CODENAME)-security \
752 main restricted universe multiverse" >> /etc/apt/sources.list && \\
753 echo "deb {ubuntu_mirror} \
754 $(. /etc/os-release && echo $VERSION_CODENAME)-proposed \
755 main restricted universe multiverse" >> /etc/apt/sources.list && \\
756 echo "deb {ubuntu_mirror} \
757 $(. /etc/os-release && echo $VERSION_CODENAME)-backports \
758 main restricted universe multiverse" >> /etc/apt/sources.list
Patrick Williams02871c92021-02-01 20:57:19 -0600759"""
760
761# Special flags for proxying.
762proxy_cmd = ""
Adrian Ambrożewicz34ec77e2021-06-02 10:23:38 +0200763proxy_keyserver = ""
Patrick Williams02871c92021-02-01 20:57:19 -0600764proxy_args = []
765if http_proxy:
766 proxy_cmd = f"""
767RUN echo "[http]" >> {homedir}/.gitconfig && \
768 echo "proxy = {http_proxy}" >> {homedir}/.gitconfig
769"""
Adrian Ambrożewicz34ec77e2021-06-02 10:23:38 +0200770 proxy_keyserver = f"--keyserver-options http-proxy={http_proxy}"
771
Patrick Williams02871c92021-02-01 20:57:19 -0600772 proxy_args.extend(
773 [
774 "--build-arg",
775 f"http_proxy={http_proxy}",
776 "--build-arg",
Lei YUd461cd62021-02-18 14:25:49 +0800777 f"https_proxy={http_proxy}",
Patrick Williams02871c92021-02-01 20:57:19 -0600778 ]
779 )
780
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600781# Create base Dockerfile.
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600782dockerfile_base = f"""
783FROM {docker_base}{distro}
Patrick Williams02871c92021-02-01 20:57:19 -0600784
785{mirror}
786
787ENV DEBIAN_FRONTEND noninteractive
788
Patrick Williams8949d3c2022-04-27 16:41:27 -0500789ENV PYTHONPATH "/usr/local/lib/python3.10/site-packages/"
Patrick Williams02871c92021-02-01 20:57:19 -0600790
Patrick Williamsbb16ac12021-04-12 12:23:51 -0500791# Sometimes the ubuntu key expires and we need a way to force an execution
792# of the apt-get commands for the dbgsym-keyring. When this happens we see
793# an error like: "Release: The following signatures were invalid:"
794# Insert a bogus echo that we can change here when we get this error to force
795# the update.
796RUN echo "ubuntu keyserver rev as of 2021-04-21"
797
Patrick Williams02871c92021-02-01 20:57:19 -0600798# We need the keys to be imported for dbgsym repos
799# New releases have a package, older ones fall back to manual fetching
800# https://wiki.ubuntu.com/Debug%20Symbol%20Packages
Jagpal Singh Gill575b5e42023-04-14 15:52:10 -0700801# Known issue with gpg to get keys via proxy -
802# https://bugs.launchpad.net/ubuntu/+source/gnupg2/+bug/1788190, hence using
803# curl to get keys.
Patrick Williams50837432021-02-06 12:24:05 -0600804RUN apt-get update && apt-get dist-upgrade -yy && \
Jian Zhang938d3032023-07-05 13:35:35 +0800805 ( apt-get install -yy gpgv ubuntu-dbgsym-keyring || \
Jagpal Singh Gill575b5e42023-04-14 15:52:10 -0700806 ( apt-get install -yy dirmngr curl && \
807 curl -sSL \
808 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xF2EDC64DC5AEE1F6B9C621F0C8CAB6595FDFF622' \
809 | apt-key add - ))
Patrick Williams02871c92021-02-01 20:57:19 -0600810
811# Parse the current repo list into a debug repo list
Patrick Williamse08ffba2022-12-05 10:33:46 -0600812RUN sed -n '/^deb /s,^deb [^ ]* ,deb http://ddebs.ubuntu.com ,p' \
813 /etc/apt/sources.list >/etc/apt/sources.list.d/debug.list
Patrick Williams02871c92021-02-01 20:57:19 -0600814
815# Remove non-existent debug repos
Patrick Williams41d86212022-11-25 18:28:43 -0600816RUN sed -i '/-\\(backports\\|security\\) /d' /etc/apt/sources.list.d/debug.list
Patrick Williams02871c92021-02-01 20:57:19 -0600817
818RUN cat /etc/apt/sources.list.d/debug.list
819
820RUN apt-get update && apt-get dist-upgrade -yy && apt-get install -yy \
Andrew Jeffery58f19152023-05-22 16:41:32 +0930821 abi-compliance-checker \
Andrew Jeffery8b112062023-05-22 20:49:11 +0930822 abi-dumper \
Patrick Williams02871c92021-02-01 20:57:19 -0600823 autoconf \
824 autoconf-archive \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600825 bison \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600826 cmake \
827 curl \
828 dbus \
829 device-tree-compiler \
830 flex \
Patrick Williams961f1482023-05-30 09:24:16 -0500831 g++-13 \
832 gcc-13 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600833 git \
Patrick Williams6968e832024-08-16 17:43:24 -0400834 gnupg \
Patrick Williams02871c92021-02-01 20:57:19 -0600835 iproute2 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600836 iputils-ping \
Manojkiran Eda524a3312023-04-05 15:37:47 +0530837 libaudit-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600838 libc6-dbg \
839 libc6-dev \
840 libconfig++-dev \
841 libcryptsetup-dev \
842 libdbus-1-dev \
843 libevdev-dev \
844 libgpiod-dev \
845 libi2c-dev \
846 libjpeg-dev \
847 libjson-perl \
848 libldap2-dev \
849 libmimetic-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600850 libnl-3-dev \
851 libnl-genl-3-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600852 libpam0g-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600853 libpciaccess-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600854 libperlio-gzip-perl \
855 libpng-dev \
856 libprotobuf-dev \
857 libsnmp-dev \
858 libssl-dev \
859 libsystemd-dev \
860 libtool \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600861 liburing-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600862 libxml2-utils \
Patrick Williams0eedeed2021-02-06 19:06:09 -0600863 libxml-simple-perl \
Patrick Williams6968e832024-08-16 17:43:24 -0400864 lsb-release \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600865 ninja-build \
866 npm \
867 pkg-config \
868 protobuf-compiler \
869 python3 \
870 python3-dev\
871 python3-git \
872 python3-mako \
873 python3-pip \
William A. Kennington III25ba1e22024-03-24 15:47:51 -0700874 python3-protobuf \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600875 python3-setuptools \
876 python3-socks \
877 python3-yaml \
John Wedig9adf68d2021-11-16 14:00:39 -0800878 rsync \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600879 shellcheck \
Ewelina Walkusz8dd1bfe2024-05-27 09:34:50 +0200880 socat \
Patrick Williams6968e832024-08-16 17:43:24 -0400881 software-properties-common \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600882 sudo \
883 systemd \
884 valgrind \
Andrew Geisslerb565f822022-12-14 11:43:25 -0600885 vim \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600886 wget \
887 xxd
Patrick Williams02871c92021-02-01 20:57:19 -0600888
Patrick Williams961f1482023-05-30 09:24:16 -0500889RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13 \
890 --slave /usr/bin/g++ g++ /usr/bin/g++-13 \
891 --slave /usr/bin/gcov gcov /usr/bin/gcov-13 \
892 --slave /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-13 \
893 --slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-13
894RUN update-alternatives --remove cpp /usr/bin/cpp && \
895 update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-13 13
Patrick Williams02871c92021-02-01 20:57:19 -0600896
Patrick Williams6968e832024-08-16 17:43:24 -0400897# Set up LLVM apt repository.
898RUN bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" 18
899
900# Install extra clang tools
901RUN apt-get install \
902 clang-18 \
903 clang-format-18 \
904 clang-tidy-18
905
Ed Tanousb84e29c2024-02-22 15:40:34 -0800906RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-18 1000 \
907 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-18 \
908 --slave /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-18 \
909 --slave /usr/bin/clang-format clang-format /usr/bin/clang-format-18 \
Patrick Williamse08ffba2022-12-05 10:33:46 -0600910 --slave /usr/bin/run-clang-tidy run-clang-tidy.py \
Ed Tanousb84e29c2024-02-22 15:40:34 -0800911 /usr/bin/run-clang-tidy-18 \
912 --slave /usr/bin/scan-build scan-build /usr/bin/scan-build-18
Patrick Williams02871c92021-02-01 20:57:19 -0600913
Patrick Williams50837432021-02-06 12:24:05 -0600914"""
915
916if is_automated_ci_build:
917 dockerfile_base += f"""
Manojkiran Edaa6ebc6e2024-06-17 11:51:45 +0530918# Run an arbitrary command to pollute the docker cache regularly force us
Patrick Williams50837432021-02-06 12:24:05 -0600919# to re-run `apt-get update` daily.
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600920RUN echo {Docker.timestamp()}
Patrick Williams50837432021-02-06 12:24:05 -0600921RUN apt-get update && apt-get dist-upgrade -yy
922
923"""
924
Patrick Williams41d86212022-11-25 18:28:43 -0600925dockerfile_base += """
Patrick Williams5e4d8402023-04-11 22:19:30 -0500926RUN pip3 install --break-system-packages \
Patrick Williams818023d2023-04-10 13:07:15 -0500927 beautysh \
928 black \
929 codespell \
930 flake8 \
Ewelina Walkusz2d8c5512024-07-02 10:49:38 +0200931 gcovr \
Patrick Williams818023d2023-04-10 13:07:15 -0500932 gitlint \
933 inflection \
934 isort \
935 jsonschema \
Patrick Williams16baaf72023-12-05 19:21:51 -0600936 meson==1.3.0 \
Patrick Williams818023d2023-04-10 13:07:15 -0500937 requests
Patrick Williamsb08ddf72022-12-06 08:56:31 -0600938
939RUN npm install -g \
Xinnan Xied0757de2024-05-27 14:22:58 +0800940 eslint@v8.56.0 eslint-plugin-json@v3.1.0 \
Patrick Williams7d41f6d2022-12-06 10:19:43 -0600941 markdownlint-cli@latest \
Patrick Williamsb08ddf72022-12-06 08:56:31 -0600942 prettier@latest
Ed Tanousfb9948a2022-06-21 09:10:24 -0700943"""
944
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600945# Build the base and stage docker images.
946docker_base_img_name = Docker.tagname("base", dockerfile_base)
947Docker.build("base", docker_base_img_name, dockerfile_base)
948Package.generate_all()
Patrick Williams02871c92021-02-01 20:57:19 -0600949
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600950# Create the final Dockerfile.
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600951dockerfile = f"""
Patrick Williams02871c92021-02-01 20:57:19 -0600952# Build the final output image
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600953FROM {docker_base_img_name}
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600954{Package.df_all_copycmds()}
Patrick Williams02871c92021-02-01 20:57:19 -0600955
956# Some of our infrastructure still relies on the presence of this file
957# even though it is no longer needed to rebuild the docker environment
958# NOTE: The file is sorted to ensure the ordering is stable.
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600959RUN echo '{Package.depcache()}' > /tmp/depcache
Patrick Williams02871c92021-02-01 20:57:19 -0600960
Patrick Williams67cc0612023-04-11 22:16:46 -0500961# Ensure the group, user, and home directory are created (or rename them if
962# they already exist).
963RUN if grep -q ":{gid}:" /etc/group ; then \
964 groupmod -n {username} $(awk -F : '{{ if ($3 == {gid}) {{ print $1 }} }}' /etc/group) ; \
965 else \
966 groupadd -f -g {gid} {username} ; \
967 fi
Patrick Williams02871c92021-02-01 20:57:19 -0600968RUN mkdir -p "{os.path.dirname(homedir)}"
Patrick Williams67cc0612023-04-11 22:16:46 -0500969RUN if grep -q ":{uid}:" /etc/passwd ; then \
Patrick Williams73b3ee92023-04-24 10:11:01 -0500970 usermod -l {username} -d {homedir} -m $(awk -F : '{{ if ($3 == {uid}) {{ print $1 }} }}' /etc/passwd) ; \
Patrick Williams67cc0612023-04-11 22:16:46 -0500971 else \
972 useradd -d {homedir} -m -u {uid} -g {gid} {username} ; \
973 fi
Patrick Williams02871c92021-02-01 20:57:19 -0600974RUN sed -i '1iDefaults umask=000' /etc/sudoers
975RUN echo "{username} ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
976
Andrew Geissler305a9a52021-04-07 11:08:40 -0500977# Ensure user has ability to write to /usr/local for different tool
978# and data installs
Andrew Geissler7bb00b12021-05-10 15:12:08 -0500979RUN chown -R {username}:{username} /usr/local/share
Andrew Geissler305a9a52021-04-07 11:08:40 -0500980
Jonathan Domanab4fee82024-01-31 15:39:20 -0800981# Update library cache
982RUN ldconfig
983
Patrick Williams02871c92021-02-01 20:57:19 -0600984{proxy_cmd}
985
986RUN /bin/bash
987"""
988
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600989# Do the final docker build
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600990docker_final_img_name = Docker.tagname(None, dockerfile)
991Docker.build("final", docker_final_img_name, dockerfile)
992
Patrick Williams00536fb2021-02-11 14:28:49 -0600993# Print the tag of the final image.
994print(docker_final_img_name)