blob: 2b391fd59711a08f229008dcbe9f4fba9ab5d960 [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
23import os
Andrew Geisslerf3d27e62024-04-09 15:24:49 -050024import re
Patrick Williams02871c92021-02-01 20:57:19 -060025import sys
Patrick Williamsb16f3e22021-02-06 08:16:47 -060026import threading
Patrick Williamsa18d9c52021-02-05 09:52:26 -060027from datetime import date
28from hashlib import sha256
Patrick Williamse08ffba2022-12-05 10:33:46 -060029
30# typing.Dict is used for type-hints.
31from typing import Any, Callable, Dict, Iterable, Optional # noqa: F401
Patrick Williams02871c92021-02-01 20:57:19 -060032
Patrick Williams41d86212022-11-25 18:28:43 -060033from sh import docker, git, nproc, uname # type: ignore
34
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060035try:
36 # Python before 3.8 doesn't have TypedDict, so reroute to standard 'dict'.
37 from typing import TypedDict
Patrick Williams41d86212022-11-25 18:28:43 -060038except Exception:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060039
40 class TypedDict(dict): # type: ignore
41 # We need to do this to eat the 'total' argument.
Patrick Williams41d86212022-11-25 18:28:43 -060042 def __init_subclass__(cls, **kwargs: Any) -> None:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060043 super().__init_subclass__()
44
45
46# Declare some variables used in package definitions.
Patrick Williamsaae36d12021-02-04 16:30:04 -060047prefix = "/usr/local"
Patrick Williams02871c92021-02-01 20:57:19 -060048proc_count = nproc().strip()
Patrick Williams02871c92021-02-01 20:57:19 -060049
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060050
51class PackageDef(TypedDict, total=False):
Patrick Williams05fb2a02022-10-11 17:22:33 -050052 """Package Definition for packages dictionary."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060053
54 # rev [optional]: Revision of package to use.
55 rev: str
56 # url [optional]: lambda function to create URL: (package, rev) -> url.
57 url: Callable[[str, str], str]
58 # depends [optional]: List of package dependencies.
59 depends: Iterable[str]
60 # build_type [required]: Build type used for package.
61 # Currently supported: autoconf, cmake, custom, make, meson
62 build_type: str
63 # build_steps [optional]: Steps to run for 'custom' build_type.
64 build_steps: Iterable[str]
65 # config_flags [optional]: List of options to pass configuration tool.
66 config_flags: Iterable[str]
67 # config_env [optional]: List of environment variables to set for config.
68 config_env: Iterable[str]
69 # custom_post_dl [optional]: List of steps to run after download, but
70 # before config / build / install.
71 custom_post_dl: Iterable[str]
Patrick Williams6bce2ca2021-02-12 21:13:37 -060072 # custom_post_install [optional]: List of steps to run after install.
73 custom_post_install: Iterable[str]
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060074
75 # __tag [private]: Generated Docker tag name for package stage.
76 __tag: str
77 # __package [private]: Package object associated with this package.
78 __package: Any # Type is Package, but not defined yet.
79
Patrick Williams02871c92021-02-01 20:57:19 -060080
Patrick Williams72043242021-02-02 10:31:45 -060081# Packages to include in image.
82packages = {
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060083 "boost": PackageDef(
Andrew Geissler05806f52024-01-07 08:39:29 -060084 rev="1.84.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060085 url=(
Andrew Geissler38b46872024-01-07 07:20:27 -060086 lambda pkg, rev: f"https://github.com/boostorg/{pkg}/releases/download/{pkg}-{rev}/{pkg}-{rev}.tar.gz"
Patrick Williams2abc4a42021-02-03 06:11:40 -060087 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060088 build_type="custom",
89 build_steps=[
Patrick Williamse08ffba2022-12-05 10:33:46 -060090 (
Andrew Geissler38b46872024-01-07 07:20:27 -060091 "./bootstrap.sh"
Ed Tanous42ff4322023-10-04 17:39:08 -070092 f" --prefix={prefix} --with-libraries=context,coroutine,url"
Patrick Williamse08ffba2022-12-05 10:33:46 -060093 ),
Patrick Williamsaae36d12021-02-04 16:30:04 -060094 "./b2",
95 f"./b2 install --prefix={prefix}",
96 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060097 ),
98 "USCiLab/cereal": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -050099 rev="v1.3.2",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600100 build_type="custom",
101 build_steps=[f"cp -a include/cereal/ {prefix}/include/"],
102 ),
Ed Tanousc7198552022-07-01 08:15:50 -0700103 "danmar/cppcheck": PackageDef(
Patrick Williams51021782023-12-05 19:10:44 -0600104 rev="2.12.1",
Ed Tanousc7198552022-07-01 08:15:50 -0700105 build_type="cmake",
106 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600107 "CLIUtils/CLI11": PackageDef(
Patrick Williamsfc397332023-07-17 11:35:43 -0500108 rev="v2.3.2",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600109 build_type="cmake",
110 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600111 "-DBUILD_TESTING=OFF",
112 "-DCLI11_BUILD_DOCS=OFF",
113 "-DCLI11_BUILD_EXAMPLES=OFF",
114 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600115 ),
116 "fmtlib/fmt": PackageDef(
Patrick Williamsc061e072023-12-05 19:11:21 -0600117 rev="10.1.1",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600118 build_type="cmake",
119 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600120 "-DFMT_DOC=OFF",
121 "-DFMT_TEST=OFF",
122 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600123 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600124 "Naios/function2": PackageDef(
Patrick Williamscb099742023-12-05 19:12:09 -0600125 rev="4.2.4",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600126 build_type="custom",
127 build_steps=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600128 f"mkdir {prefix}/include/function2",
129 f"cp include/function2/function2.hpp {prefix}/include/function2/",
130 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600131 ),
132 "google/googletest": PackageDef(
Patrick Williamsfdf243b2023-12-05 19:13:50 -0600133 rev="v1.14.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600134 build_type="cmake",
William A. Kennington III4dd32c02021-05-28 01:58:13 -0700135 config_env=["CXXFLAGS=-std=c++20"],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600136 config_flags=["-DTHREADS_PREFER_PTHREAD_FLAG=ON"],
137 ),
Ed Tanous178b4b22023-06-15 09:03:11 -0700138 "nghttp2/nghttp2": PackageDef(
Ed Tanousabb106a2024-04-04 10:00:02 -0700139 rev="v1.61.0",
Ed Tanous178b4b22023-06-15 09:03:11 -0700140 build_type="cmake",
141 config_env=["CXXFLAGS=-std=c++20"],
142 config_flags=[
143 "-DENABLE_LIB_ONLY=ON",
144 "-DENABLE_STATIC_LIB=ON",
145 ],
146 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600147 "nlohmann/json": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -0500148 rev="v3.11.2",
Patrick Williams6bce2ca2021-02-12 21:13:37 -0600149 build_type="cmake",
150 config_flags=["-DJSON_BuildTests=OFF"],
151 custom_post_install=[
Patrick Williamse08ffba2022-12-05 10:33:46 -0600152 (
153 f"ln -s {prefix}/include/nlohmann/json.hpp"
154 f" {prefix}/include/json.hpp"
155 ),
Patrick Williamsaae36d12021-02-04 16:30:04 -0600156 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600157 ),
Przemyslaw Czarnowski058e3a32022-12-21 14:13:23 +0100158 "json-c/json-c": PackageDef(
Patrick Williamseee65be2023-12-05 19:17:01 -0600159 rev="json-c-0.17-20230812",
Przemyslaw Czarnowski058e3a32022-12-21 14:13:23 +0100160 build_type="cmake",
161 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600162 "LibVNC/libvncserver": PackageDef(
Patrick Williamsc0421322023-12-05 19:18:57 -0600163 rev="LibVNCServer-0.9.14",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600164 build_type="cmake",
165 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600166 "leethomason/tinyxml2": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -0500167 rev="9.0.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600168 build_type="cmake",
169 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600170 "tristanpenman/valijson": PackageDef(
Patrick Williams5a2c1132023-12-05 19:20:36 -0600171 rev="v1.0.1",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600172 build_type="cmake",
173 config_flags=[
Patrick Williams0eedeed2021-02-06 19:06:09 -0600174 "-Dvalijson_BUILD_TESTS=0",
175 "-Dvalijson_INSTALL_HEADERS=1",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600176 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600177 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600178 "open-power/pdbg": PackageDef(build_type="autoconf"),
179 "openbmc/gpioplus": PackageDef(
180 depends=["openbmc/stdplus"],
181 build_type="meson",
182 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600183 "-Dexamples=false",
184 "-Dtests=disabled",
185 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600186 ),
187 "openbmc/phosphor-dbus-interfaces": PackageDef(
188 depends=["openbmc/sdbusplus"],
189 build_type="meson",
William A. Kennington III4fe87772022-02-11 15:44:29 -0800190 config_flags=["-Dgenerate_md=false"],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600191 ),
192 "openbmc/phosphor-logging": PackageDef(
193 depends=[
Patrick Williams83394612021-02-03 07:12:50 -0600194 "USCiLab/cereal",
Patrick Williams83394612021-02-03 07:12:50 -0600195 "openbmc/phosphor-dbus-interfaces",
196 "openbmc/sdbusplus",
197 "openbmc/sdeventplus",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600198 ],
Patrick Williamsf79ce4c2021-04-30 16:00:49 -0500199 build_type="meson",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600200 config_flags=[
William A. Kennington III6c98f282022-10-05 13:37:04 -0700201 "-Dlibonly=true",
202 "-Dtests=disabled",
Patrick Williams5eabdae2022-04-14 14:34:34 -0500203 f"-Dyamldir={prefix}/share/phosphor-dbus-yaml/yaml",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600204 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600205 ),
206 "openbmc/phosphor-objmgr": PackageDef(
207 depends=[
Brad Bishop11e57622022-09-14 16:10:25 -0400208 "CLIUtils/CLI11",
Patrick Williams70af95c2022-09-27 16:55:41 -0500209 "boost",
Patrick Williams83394612021-02-03 07:12:50 -0600210 "leethomason/tinyxml2",
Patrick Williams70af95c2022-09-27 16:55:41 -0500211 "openbmc/phosphor-dbus-interfaces",
Patrick Williams83394612021-02-03 07:12:50 -0600212 "openbmc/phosphor-logging",
213 "openbmc/sdbusplus",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600214 ],
Brad Bishop1197e352021-08-03 19:25:46 -0400215 build_type="meson",
216 config_flags=[
217 "-Dtests=disabled",
218 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600219 ),
Jason M. Billsc02ff272023-08-02 10:55:22 -0700220 "openbmc/libpeci": PackageDef(
221 build_type="meson",
222 config_flags=[
223 "-Draw-peci=disabled",
224 ],
225 ),
Manojkiran Eda1c19e452022-10-03 11:01:59 +0530226 "openbmc/libpldm": PackageDef(
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600227 build_type="meson",
228 config_flags=[
Andrew Jeffery29d69bb2023-06-06 14:38:24 +0930229 "-Dabi=deprecated,stable",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600230 "-Doem-ibm=enabled",
231 "-Dtests=disabled",
232 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600233 ),
234 "openbmc/sdbusplus": PackageDef(
235 build_type="meson",
236 custom_post_dl=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600237 "cd tools",
238 f"./setup.py install --root=/ --prefix={prefix}",
239 "cd ..",
240 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600241 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600242 "-Dexamples=disabled",
243 "-Dtests=disabled",
244 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600245 ),
246 "openbmc/sdeventplus": PackageDef(
Patrick Williams70af95c2022-09-27 16:55:41 -0500247 depends=[
248 "Naios/function2",
249 "openbmc/stdplus",
250 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600251 build_type="meson",
252 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600253 "-Dexamples=false",
254 "-Dtests=disabled",
255 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600256 ),
257 "openbmc/stdplus": PackageDef(
Patrick Williams70af95c2022-09-27 16:55:41 -0500258 depends=[
Patrick Williams70af95c2022-09-27 16:55:41 -0500259 "fmtlib/fmt",
William A. Kennington IIIca1bf0c2022-10-05 02:23:30 -0700260 "google/googletest",
261 "Naios/function2",
Patrick Williams70af95c2022-09-27 16:55:41 -0500262 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600263 build_type="meson",
264 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600265 "-Dexamples=false",
266 "-Dtests=disabled",
William A. Kennington IIIca1bf0c2022-10-05 02:23:30 -0700267 "-Dgtest=enabled",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600268 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600269 ),
270} # type: Dict[str, PackageDef]
Patrick Williams02871c92021-02-01 20:57:19 -0600271
272# Define common flags used for builds
Patrick Williams02871c92021-02-01 20:57:19 -0600273configure_flags = " ".join(
274 [
275 f"--prefix={prefix}",
276 ]
277)
278cmake_flags = " ".join(
279 [
Patrick Williams02871c92021-02-01 20:57:19 -0600280 "-DBUILD_SHARED_LIBS=ON",
Patrick Williams0f2086b2021-02-05 06:49:49 -0600281 "-DCMAKE_BUILD_TYPE=RelWithDebInfo",
Patrick Williams02871c92021-02-01 20:57:19 -0600282 f"-DCMAKE_INSTALL_PREFIX:PATH={prefix}",
Patrick Williams0f2086b2021-02-05 06:49:49 -0600283 "-GNinja",
284 "-DCMAKE_MAKE_PROGRAM=ninja",
Patrick Williams02871c92021-02-01 20:57:19 -0600285 ]
286)
287meson_flags = " ".join(
288 [
289 "--wrap-mode=nodownload",
290 f"-Dprefix={prefix}",
291 ]
292)
293
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600294
295class Package(threading.Thread):
296 """Class used to build the Docker stages for each package.
297
298 Generally, this class should not be instantiated directly but through
299 Package.generate_all().
300 """
301
302 # Copy the packages dictionary.
303 packages = packages.copy()
304
305 # Lock used for thread-safety.
306 lock = threading.Lock()
307
308 def __init__(self, pkg: str):
Patrick Williams05fb2a02022-10-11 17:22:33 -0500309 """pkg - The name of this package (ex. foo/bar )"""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600310 super(Package, self).__init__()
311
312 self.package = pkg
313 self.exception = None # type: Optional[Exception]
314
315 # Reference to this package's
316 self.pkg_def = Package.packages[pkg]
317 self.pkg_def["__package"] = self
318
319 def run(self) -> None:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500320 """Thread 'run' function. Builds the Docker stage."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600321
322 # In case this package has no rev, fetch it from Github.
323 self._update_rev()
324
325 # Find all the Package objects that this package depends on.
326 # This section is locked because we are looking into another
327 # package's PackageDef dict, which could be being modified.
328 Package.lock.acquire()
329 deps: Iterable[Package] = [
330 Package.packages[deppkg]["__package"]
331 for deppkg in self.pkg_def.get("depends", [])
332 ]
333 Package.lock.release()
334
335 # Wait until all the depends finish building. We need them complete
336 # for the "COPY" commands.
337 for deppkg in deps:
338 deppkg.join()
339
340 # Generate this package's Dockerfile.
341 dockerfile = f"""
342FROM {docker_base_img_name}
343{self._df_copycmds()}
344{self._df_build()}
345"""
346
347 # Generate the resulting tag name and save it to the PackageDef.
348 # This section is locked because we are modifying the PackageDef,
349 # which can be accessed by other threads.
350 Package.lock.acquire()
351 tag = Docker.tagname(self._stagename(), dockerfile)
352 self.pkg_def["__tag"] = tag
353 Package.lock.release()
354
355 # Do the build / save any exceptions.
356 try:
357 Docker.build(self.package, tag, dockerfile)
358 except Exception as e:
359 self.exception = e
360
361 @classmethod
362 def generate_all(cls) -> None:
363 """Ensure a Docker stage is created for all defined packages.
364
365 These are done in parallel but with appropriate blocking per
366 package 'depends' specifications.
367 """
368
369 # Create a Package for each defined package.
370 pkg_threads = [Package(p) for p in cls.packages.keys()]
371
372 # Start building them all.
Patrick Williams6dbd7802021-02-20 08:34:10 -0600373 # This section is locked because threads depend on each other,
374 # based on the packages, and they cannot 'join' on a thread
375 # which is not yet started. Adding a lock here allows all the
376 # threads to start before they 'join' their dependencies.
377 Package.lock.acquire()
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600378 for t in pkg_threads:
379 t.start()
Patrick Williams6dbd7802021-02-20 08:34:10 -0600380 Package.lock.release()
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600381
382 # Wait for completion.
383 for t in pkg_threads:
384 t.join()
385 # Check if the thread saved off its own exception.
386 if t.exception:
387 print(f"Package {t.package} failed!", file=sys.stderr)
388 raise t.exception
389
390 @staticmethod
391 def df_all_copycmds() -> str:
392 """Formulate the Dockerfile snippet necessary to copy all packages
393 into the final image.
394 """
395 return Package.df_copycmds_set(Package.packages.keys())
396
397 @classmethod
398 def depcache(cls) -> str:
399 """Create the contents of the '/tmp/depcache'.
400 This file is a comma-separated list of "<pkg>:<rev>".
401 """
402
403 # This needs to be sorted for consistency.
404 depcache = ""
405 for pkg in sorted(cls.packages.keys()):
406 depcache += "%s:%s," % (pkg, cls.packages[pkg]["rev"])
407 return depcache
408
409 def _update_rev(self) -> None:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500410 """Look up the HEAD for missing a static rev."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600411
412 if "rev" in self.pkg_def:
413 return
414
Patrick Williams65b21fb2021-02-12 21:21:14 -0600415 # Check if Jenkins/Gerrit gave us a revision and use it.
416 if gerrit_project == self.package and gerrit_rev:
417 print(
418 f"Found Gerrit revision for {self.package}: {gerrit_rev}",
419 file=sys.stderr,
420 )
421 self.pkg_def["rev"] = gerrit_rev
422 return
423
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600424 # Ask Github for all the branches.
Patrick Williams05fb2a02022-10-11 17:22:33 -0500425 lookup = git(
426 "ls-remote", "--heads", f"https://github.com/{self.package}"
427 )
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600428
429 # Find the branch matching {branch} (or fallback to master).
430 # This section is locked because we are modifying the PackageDef.
431 Package.lock.acquire()
432 for line in lookup.split("\n"):
Andrew Geisslerf3d27e62024-04-09 15:24:49 -0500433 if re.fullmatch(f".*{branch}$", line.strip()):
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600434 self.pkg_def["rev"] = line.split()[0]
Andrew Geisslerf3d27e62024-04-09 15:24:49 -0500435 break
Patrick Williamsc7d73642022-10-11 17:22:06 -0500436 elif (
437 "refs/heads/master" in line or "refs/heads/main" in line
438 ) and "rev" not in self.pkg_def:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600439 self.pkg_def["rev"] = line.split()[0]
440 Package.lock.release()
441
442 def _stagename(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500443 """Create a name for the Docker stage associated with this pkg."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600444 return self.package.replace("/", "-").lower()
445
446 def _url(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500447 """Get the URL for this package."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600448 rev = self.pkg_def["rev"]
449
450 # If the lambda exists, call it.
451 if "url" in self.pkg_def:
452 return self.pkg_def["url"](self.package, rev)
453
454 # Default to the github archive URL.
455 return f"https://github.com/{self.package}/archive/{rev}.tar.gz"
456
457 def _cmd_download(self) -> str:
458 """Formulate the command necessary to download and unpack to source."""
459
460 url = self._url()
461 if ".tar." not in url:
462 raise NotImplementedError(
463 f"Unhandled download type for {self.package}: {url}"
464 )
465
466 cmd = f"curl -L {url} | tar -x"
467
468 if url.endswith(".bz2"):
469 cmd += "j"
470 elif url.endswith(".gz"):
471 cmd += "z"
472 else:
473 raise NotImplementedError(
474 f"Unknown tar flags needed for {self.package}: {url}"
475 )
476
477 return cmd
478
479 def _cmd_cd_srcdir(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500480 """Formulate the command necessary to 'cd' into the source dir."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600481 return f"cd {self.package.split('/')[-1]}*"
482
483 def _df_copycmds(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500484 """Formulate the dockerfile snippet necessary to COPY all depends."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600485
486 if "depends" not in self.pkg_def:
487 return ""
488 return Package.df_copycmds_set(self.pkg_def["depends"])
489
490 @staticmethod
491 def df_copycmds_set(pkgs: Iterable[str]) -> str:
492 """Formulate the Dockerfile snippet necessary to COPY a set of
493 packages into a Docker stage.
494 """
495
496 copy_cmds = ""
497
498 # Sort the packages for consistency.
499 for p in sorted(pkgs):
500 tag = Package.packages[p]["__tag"]
501 copy_cmds += f"COPY --from={tag} {prefix} {prefix}\n"
502 # Workaround for upstream docker bug and multiple COPY cmds
503 # https://github.com/moby/moby/issues/37965
504 copy_cmds += "RUN true\n"
505
506 return copy_cmds
507
508 def _df_build(self) -> str:
509 """Formulate the Dockerfile snippet necessary to download, build, and
510 install a package into a Docker stage.
511 """
512
513 # Download and extract source.
514 result = f"RUN {self._cmd_download()} && {self._cmd_cd_srcdir()} && "
515
516 # Handle 'custom_post_dl' commands.
517 custom_post_dl = self.pkg_def.get("custom_post_dl")
518 if custom_post_dl:
519 result += " && ".join(custom_post_dl) + " && "
520
521 # Build and install package based on 'build_type'.
522 build_type = self.pkg_def["build_type"]
523 if build_type == "autoconf":
524 result += self._cmd_build_autoconf()
525 elif build_type == "cmake":
526 result += self._cmd_build_cmake()
527 elif build_type == "custom":
528 result += self._cmd_build_custom()
529 elif build_type == "make":
530 result += self._cmd_build_make()
531 elif build_type == "meson":
532 result += self._cmd_build_meson()
533 else:
534 raise NotImplementedError(
535 f"Unhandled build type for {self.package}: {build_type}"
536 )
537
Patrick Williams6bce2ca2021-02-12 21:13:37 -0600538 # Handle 'custom_post_install' commands.
539 custom_post_install = self.pkg_def.get("custom_post_install")
540 if custom_post_install:
541 result += " && " + " && ".join(custom_post_install)
542
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600543 return result
544
545 def _cmd_build_autoconf(self) -> str:
546 options = " ".join(self.pkg_def.get("config_flags", []))
547 env = " ".join(self.pkg_def.get("config_env", []))
548 result = "./bootstrap.sh && "
549 result += f"{env} ./configure {configure_flags} {options} && "
550 result += f"make -j{proc_count} && make install"
551 return result
552
553 def _cmd_build_cmake(self) -> str:
554 options = " ".join(self.pkg_def.get("config_flags", []))
555 env = " ".join(self.pkg_def.get("config_env", []))
556 result = "mkdir builddir && cd builddir && "
557 result += f"{env} cmake {cmake_flags} {options} .. && "
558 result += "cmake --build . --target all && "
559 result += "cmake --build . --target install && "
560 result += "cd .."
561 return result
562
563 def _cmd_build_custom(self) -> str:
564 return " && ".join(self.pkg_def.get("build_steps", []))
565
566 def _cmd_build_make(self) -> str:
567 return f"make -j{proc_count} && make install"
568
569 def _cmd_build_meson(self) -> str:
570 options = " ".join(self.pkg_def.get("config_flags", []))
571 env = " ".join(self.pkg_def.get("config_env", []))
Andrew Jefferye2da11a2023-06-15 10:16:37 +0930572 result = f"{env} meson setup builddir {meson_flags} {options} && "
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600573 result += "ninja -C builddir && ninja -C builddir install"
574 return result
575
576
577class Docker:
578 """Class to assist with Docker interactions. All methods are static."""
579
580 @staticmethod
581 def timestamp() -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500582 """Generate a timestamp for today using the ISO week."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600583 today = date.today().isocalendar()
584 return f"{today[0]}-W{today[1]:02}"
585
586 @staticmethod
Patrick Williams41d86212022-11-25 18:28:43 -0600587 def tagname(pkgname: Optional[str], dockerfile: str) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500588 """Generate a tag name for a package using a hash of the Dockerfile."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600589 result = docker_image_name
590 if pkgname:
591 result += "-" + pkgname
592
593 result += ":" + Docker.timestamp()
594 result += "-" + sha256(dockerfile.encode()).hexdigest()[0:16]
595
596 return result
597
598 @staticmethod
599 def build(pkg: str, tag: str, dockerfile: str) -> None:
Andrew Geissler22e61102023-02-14 14:44:00 -0600600 """Build a docker image using the Dockerfile and tagging it with 'tag'."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600601
602 # If we're not forcing builds, check if it already exists and skip.
603 if not force_build:
604 if docker.image.ls(tag, "--format", '"{{.Repository}}:{{.Tag}}"'):
Patrick Williams05fb2a02022-10-11 17:22:33 -0500605 print(
606 f"Image {tag} already exists. Skipping.", file=sys.stderr
607 )
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600608 return
609
610 # Build it.
611 # Capture the output of the 'docker build' command and send it to
612 # stderr (prefixed with the package name). This allows us to see
Manojkiran Edaa6ebc6e2024-06-17 11:51:45 +0530613 # progress but not pollute stdout. Later on we output the final
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600614 # docker tag to stdout and we want to keep that pristine.
615 #
616 # Other unusual flags:
617 # --no-cache: Bypass the Docker cache if 'force_build'.
618 # --force-rm: Clean up Docker processes if they fail.
619 docker.build(
620 proxy_args,
621 "--network=host",
622 "--force-rm",
623 "--no-cache=true" if force_build else "--no-cache=false",
624 "-t",
625 tag,
626 "-",
627 _in=dockerfile,
628 _out=(
629 lambda line: print(
630 pkg + ":", line, end="", file=sys.stderr, flush=True
631 )
632 ),
Jonathan Doman88dd7922024-05-02 10:34:21 -0700633 _err_to_out=True,
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600634 )
635
636
637# Read a bunch of environment variables.
Patrick Williams05fb2a02022-10-11 17:22:33 -0500638docker_image_name = os.environ.get(
639 "DOCKER_IMAGE_NAME", "openbmc/ubuntu-unit-test"
640)
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600641force_build = os.environ.get("FORCE_DOCKER_BUILD")
642is_automated_ci_build = os.environ.get("BUILD_URL", False)
Patrick Williams7c95a372024-01-05 19:22:58 -0600643distro = os.environ.get("DISTRO", "ubuntu:noble")
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600644branch = os.environ.get("BRANCH", "master")
645ubuntu_mirror = os.environ.get("UBUNTU_MIRROR")
646http_proxy = os.environ.get("http_proxy")
647
Patrick Williams65b21fb2021-02-12 21:21:14 -0600648gerrit_project = os.environ.get("GERRIT_PROJECT")
649gerrit_rev = os.environ.get("GERRIT_PATCHSET_REVISION")
650
Andrew Geisslerd0dabc32023-04-04 08:09:21 -0600651# Ensure appropriate docker build output to see progress and identify
652# any issues
653os.environ["BUILDKIT_PROGRESS"] = "plain"
654
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600655# Set up some common variables.
656username = os.environ.get("USER", "root")
657homedir = os.environ.get("HOME", "/root")
658gid = os.getgid()
659uid = os.getuid()
660
Josh Lehan6825a012022-03-17 18:31:39 -0700661# Use well-known constants if user is root
662if username == "root":
663 homedir = "/root"
664 gid = 0
665 uid = 0
666
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600667# Determine the architecture for Docker.
668arch = uname("-m").strip()
669if arch == "ppc64le":
670 docker_base = "ppc64le/"
671elif arch == "x86_64":
672 docker_base = ""
Thang Q. Nguyen051b05b2021-12-10 08:30:35 +0000673elif arch == "aarch64":
Thang Q. Nguyenf98f1a82021-12-22 01:59:19 +0000674 docker_base = "arm64v8/"
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600675else:
676 print(
677 f"Unsupported system architecture({arch}) found for docker image",
678 file=sys.stderr,
679 )
680 sys.exit(1)
681
Patrick Williams02871c92021-02-01 20:57:19 -0600682# Special flags if setting up a deb mirror.
683mirror = ""
684if "ubuntu" in distro and ubuntu_mirror:
685 mirror = f"""
Patrick Williamse08ffba2022-12-05 10:33:46 -0600686RUN echo "deb {ubuntu_mirror} \
687 $(. /etc/os-release && echo $VERSION_CODENAME) \
688 main restricted universe multiverse" > /etc/apt/sources.list && \\
689 echo "deb {ubuntu_mirror} \
690 $(. /etc/os-release && echo $VERSION_CODENAME)-updates \
691 main restricted universe multiverse" >> /etc/apt/sources.list && \\
692 echo "deb {ubuntu_mirror} \
693 $(. /etc/os-release && echo $VERSION_CODENAME)-security \
694 main restricted universe multiverse" >> /etc/apt/sources.list && \\
695 echo "deb {ubuntu_mirror} \
696 $(. /etc/os-release && echo $VERSION_CODENAME)-proposed \
697 main restricted universe multiverse" >> /etc/apt/sources.list && \\
698 echo "deb {ubuntu_mirror} \
699 $(. /etc/os-release && echo $VERSION_CODENAME)-backports \
700 main restricted universe multiverse" >> /etc/apt/sources.list
Patrick Williams02871c92021-02-01 20:57:19 -0600701"""
702
703# Special flags for proxying.
704proxy_cmd = ""
Adrian Ambrożewicz34ec77e2021-06-02 10:23:38 +0200705proxy_keyserver = ""
Patrick Williams02871c92021-02-01 20:57:19 -0600706proxy_args = []
707if http_proxy:
708 proxy_cmd = f"""
709RUN echo "[http]" >> {homedir}/.gitconfig && \
710 echo "proxy = {http_proxy}" >> {homedir}/.gitconfig
711"""
Adrian Ambrożewicz34ec77e2021-06-02 10:23:38 +0200712 proxy_keyserver = f"--keyserver-options http-proxy={http_proxy}"
713
Patrick Williams02871c92021-02-01 20:57:19 -0600714 proxy_args.extend(
715 [
716 "--build-arg",
717 f"http_proxy={http_proxy}",
718 "--build-arg",
Lei YUd461cd62021-02-18 14:25:49 +0800719 f"https_proxy={http_proxy}",
Patrick Williams02871c92021-02-01 20:57:19 -0600720 ]
721 )
722
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600723# Create base Dockerfile.
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600724dockerfile_base = f"""
725FROM {docker_base}{distro}
Patrick Williams02871c92021-02-01 20:57:19 -0600726
727{mirror}
728
729ENV DEBIAN_FRONTEND noninteractive
730
Patrick Williams8949d3c2022-04-27 16:41:27 -0500731ENV PYTHONPATH "/usr/local/lib/python3.10/site-packages/"
Patrick Williams02871c92021-02-01 20:57:19 -0600732
Patrick Williamsbb16ac12021-04-12 12:23:51 -0500733# Sometimes the ubuntu key expires and we need a way to force an execution
734# of the apt-get commands for the dbgsym-keyring. When this happens we see
735# an error like: "Release: The following signatures were invalid:"
736# Insert a bogus echo that we can change here when we get this error to force
737# the update.
738RUN echo "ubuntu keyserver rev as of 2021-04-21"
739
Patrick Williams02871c92021-02-01 20:57:19 -0600740# We need the keys to be imported for dbgsym repos
741# New releases have a package, older ones fall back to manual fetching
742# https://wiki.ubuntu.com/Debug%20Symbol%20Packages
Jagpal Singh Gill575b5e42023-04-14 15:52:10 -0700743# Known issue with gpg to get keys via proxy -
744# https://bugs.launchpad.net/ubuntu/+source/gnupg2/+bug/1788190, hence using
745# curl to get keys.
Patrick Williams50837432021-02-06 12:24:05 -0600746RUN apt-get update && apt-get dist-upgrade -yy && \
Jian Zhang938d3032023-07-05 13:35:35 +0800747 ( apt-get install -yy gpgv ubuntu-dbgsym-keyring || \
Jagpal Singh Gill575b5e42023-04-14 15:52:10 -0700748 ( apt-get install -yy dirmngr curl && \
749 curl -sSL \
750 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xF2EDC64DC5AEE1F6B9C621F0C8CAB6595FDFF622' \
751 | apt-key add - ))
Patrick Williams02871c92021-02-01 20:57:19 -0600752
753# Parse the current repo list into a debug repo list
Patrick Williamse08ffba2022-12-05 10:33:46 -0600754RUN sed -n '/^deb /s,^deb [^ ]* ,deb http://ddebs.ubuntu.com ,p' \
755 /etc/apt/sources.list >/etc/apt/sources.list.d/debug.list
Patrick Williams02871c92021-02-01 20:57:19 -0600756
757# Remove non-existent debug repos
Patrick Williams41d86212022-11-25 18:28:43 -0600758RUN sed -i '/-\\(backports\\|security\\) /d' /etc/apt/sources.list.d/debug.list
Patrick Williams02871c92021-02-01 20:57:19 -0600759
760RUN cat /etc/apt/sources.list.d/debug.list
761
762RUN apt-get update && apt-get dist-upgrade -yy && apt-get install -yy \
Andrew Jeffery58f19152023-05-22 16:41:32 +0930763 abi-compliance-checker \
Andrew Jeffery8b112062023-05-22 20:49:11 +0930764 abi-dumper \
Patrick Williams02871c92021-02-01 20:57:19 -0600765 autoconf \
766 autoconf-archive \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600767 bison \
Patrick Williamse2e62e52023-09-20 16:21:16 -0500768 clang-17 \
769 clang-format-17 \
770 clang-tidy-17 \
771 clang-tools-17 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600772 cmake \
773 curl \
774 dbus \
775 device-tree-compiler \
776 flex \
Patrick Williams961f1482023-05-30 09:24:16 -0500777 g++-13 \
778 gcc-13 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600779 git \
Patrick Williams02871c92021-02-01 20:57:19 -0600780 iproute2 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600781 iputils-ping \
Manojkiran Eda524a3312023-04-05 15:37:47 +0530782 libaudit-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600783 libc6-dbg \
784 libc6-dev \
785 libconfig++-dev \
786 libcryptsetup-dev \
787 libdbus-1-dev \
788 libevdev-dev \
789 libgpiod-dev \
790 libi2c-dev \
791 libjpeg-dev \
792 libjson-perl \
793 libldap2-dev \
794 libmimetic-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600795 libnl-3-dev \
796 libnl-genl-3-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600797 libpam0g-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600798 libpciaccess-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600799 libperlio-gzip-perl \
800 libpng-dev \
801 libprotobuf-dev \
802 libsnmp-dev \
803 libssl-dev \
804 libsystemd-dev \
805 libtool \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600806 liburing-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600807 libxml2-utils \
Patrick Williams0eedeed2021-02-06 19:06:09 -0600808 libxml-simple-perl \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600809 ninja-build \
810 npm \
811 pkg-config \
812 protobuf-compiler \
813 python3 \
814 python3-dev\
815 python3-git \
816 python3-mako \
817 python3-pip \
William A. Kennington III25ba1e22024-03-24 15:47:51 -0700818 python3-protobuf \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600819 python3-setuptools \
820 python3-socks \
821 python3-yaml \
John Wedig9adf68d2021-11-16 14:00:39 -0800822 rsync \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600823 shellcheck \
Ewelina Walkusz8dd1bfe2024-05-27 09:34:50 +0200824 socat \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600825 sudo \
826 systemd \
827 valgrind \
Andrew Geisslerb565f822022-12-14 11:43:25 -0600828 vim \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600829 wget \
830 xxd
Patrick Williams02871c92021-02-01 20:57:19 -0600831
Patrick Williams961f1482023-05-30 09:24:16 -0500832RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13 \
833 --slave /usr/bin/g++ g++ /usr/bin/g++-13 \
834 --slave /usr/bin/gcov gcov /usr/bin/gcov-13 \
835 --slave /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-13 \
836 --slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-13
837RUN update-alternatives --remove cpp /usr/bin/cpp && \
838 update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-13 13
Patrick Williams02871c92021-02-01 20:57:19 -0600839
Patrick Williamse2e62e52023-09-20 16:21:16 -0500840RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-17 1000 \
841 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-17 \
842 --slave /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-17 \
843 --slave /usr/bin/clang-format clang-format /usr/bin/clang-format-17 \
Patrick Williamse08ffba2022-12-05 10:33:46 -0600844 --slave /usr/bin/run-clang-tidy run-clang-tidy.py \
Patrick Williamse2e62e52023-09-20 16:21:16 -0500845 /usr/bin/run-clang-tidy-17 \
846 --slave /usr/bin/scan-build scan-build /usr/bin/scan-build-17
Patrick Williams02871c92021-02-01 20:57:19 -0600847
Patrick Williams50837432021-02-06 12:24:05 -0600848"""
849
850if is_automated_ci_build:
851 dockerfile_base += f"""
Manojkiran Edaa6ebc6e2024-06-17 11:51:45 +0530852# Run an arbitrary command to pollute the docker cache regularly force us
Patrick Williams50837432021-02-06 12:24:05 -0600853# to re-run `apt-get update` daily.
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600854RUN echo {Docker.timestamp()}
Patrick Williams50837432021-02-06 12:24:05 -0600855RUN apt-get update && apt-get dist-upgrade -yy
856
857"""
858
Patrick Williams41d86212022-11-25 18:28:43 -0600859dockerfile_base += """
Patrick Williams5e4d8402023-04-11 22:19:30 -0500860RUN pip3 install --break-system-packages \
Patrick Williams818023d2023-04-10 13:07:15 -0500861 beautysh \
862 black \
863 codespell \
864 flake8 \
Ewelina Walkusz2d8c5512024-07-02 10:49:38 +0200865 gcovr \
Patrick Williams818023d2023-04-10 13:07:15 -0500866 gitlint \
867 inflection \
868 isort \
869 jsonschema \
Patrick Williams16baaf72023-12-05 19:21:51 -0600870 meson==1.3.0 \
Patrick Williams818023d2023-04-10 13:07:15 -0500871 requests
Patrick Williamsb08ddf72022-12-06 08:56:31 -0600872
873RUN npm install -g \
Xinnan Xied0757de2024-05-27 14:22:58 +0800874 eslint@v8.56.0 eslint-plugin-json@v3.1.0 \
Patrick Williams7d41f6d2022-12-06 10:19:43 -0600875 markdownlint-cli@latest \
Patrick Williamsb08ddf72022-12-06 08:56:31 -0600876 prettier@latest
Ed Tanousfb9948a2022-06-21 09:10:24 -0700877"""
878
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600879# Build the base and stage docker images.
880docker_base_img_name = Docker.tagname("base", dockerfile_base)
881Docker.build("base", docker_base_img_name, dockerfile_base)
882Package.generate_all()
Patrick Williams02871c92021-02-01 20:57:19 -0600883
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600884# Create the final Dockerfile.
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600885dockerfile = f"""
Patrick Williams02871c92021-02-01 20:57:19 -0600886# Build the final output image
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600887FROM {docker_base_img_name}
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600888{Package.df_all_copycmds()}
Patrick Williams02871c92021-02-01 20:57:19 -0600889
890# Some of our infrastructure still relies on the presence of this file
891# even though it is no longer needed to rebuild the docker environment
892# NOTE: The file is sorted to ensure the ordering is stable.
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600893RUN echo '{Package.depcache()}' > /tmp/depcache
Patrick Williams02871c92021-02-01 20:57:19 -0600894
Patrick Williams67cc0612023-04-11 22:16:46 -0500895# Ensure the group, user, and home directory are created (or rename them if
896# they already exist).
897RUN if grep -q ":{gid}:" /etc/group ; then \
898 groupmod -n {username} $(awk -F : '{{ if ($3 == {gid}) {{ print $1 }} }}' /etc/group) ; \
899 else \
900 groupadd -f -g {gid} {username} ; \
901 fi
Patrick Williams02871c92021-02-01 20:57:19 -0600902RUN mkdir -p "{os.path.dirname(homedir)}"
Patrick Williams67cc0612023-04-11 22:16:46 -0500903RUN if grep -q ":{uid}:" /etc/passwd ; then \
Patrick Williams73b3ee92023-04-24 10:11:01 -0500904 usermod -l {username} -d {homedir} -m $(awk -F : '{{ if ($3 == {uid}) {{ print $1 }} }}' /etc/passwd) ; \
Patrick Williams67cc0612023-04-11 22:16:46 -0500905 else \
906 useradd -d {homedir} -m -u {uid} -g {gid} {username} ; \
907 fi
Patrick Williams02871c92021-02-01 20:57:19 -0600908RUN sed -i '1iDefaults umask=000' /etc/sudoers
909RUN echo "{username} ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
910
Andrew Geissler305a9a52021-04-07 11:08:40 -0500911# Ensure user has ability to write to /usr/local for different tool
912# and data installs
Andrew Geissler7bb00b12021-05-10 15:12:08 -0500913RUN chown -R {username}:{username} /usr/local/share
Andrew Geissler305a9a52021-04-07 11:08:40 -0500914
Jonathan Domanab4fee82024-01-31 15:39:20 -0800915# Update library cache
916RUN ldconfig
917
Patrick Williams02871c92021-02-01 20:57:19 -0600918{proxy_cmd}
919
920RUN /bin/bash
921"""
922
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600923# Do the final docker build
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600924docker_final_img_name = Docker.tagname(None, dockerfile)
925Docker.build("final", docker_final_img_name, dockerfile)
926
Patrick Williams00536fb2021-02-11 14:28:49 -0600927# Print the tag of the final image.
928print(docker_final_img_name)