blob: 1b99fa95bef5522b877e99ec626cce3e44738b81 [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
24import sys
Patrick Williamsb16f3e22021-02-06 08:16:47 -060025import threading
Patrick Williamsa18d9c52021-02-05 09:52:26 -060026from datetime import date
27from hashlib import sha256
Patrick Williamse08ffba2022-12-05 10:33:46 -060028
29# typing.Dict is used for type-hints.
30from typing import Any, Callable, Dict, Iterable, Optional # noqa: F401
Patrick Williams02871c92021-02-01 20:57:19 -060031
Patrick Williams41d86212022-11-25 18:28:43 -060032from sh import docker, git, nproc, uname # type: ignore
33
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060034try:
35 # Python before 3.8 doesn't have TypedDict, so reroute to standard 'dict'.
36 from typing import TypedDict
Patrick Williams41d86212022-11-25 18:28:43 -060037except Exception:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060038
39 class TypedDict(dict): # type: ignore
40 # We need to do this to eat the 'total' argument.
Patrick Williams41d86212022-11-25 18:28:43 -060041 def __init_subclass__(cls, **kwargs: Any) -> None:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060042 super().__init_subclass__()
43
44
45# Declare some variables used in package definitions.
Patrick Williamsaae36d12021-02-04 16:30:04 -060046prefix = "/usr/local"
Patrick Williams02871c92021-02-01 20:57:19 -060047proc_count = nproc().strip()
Patrick Williams02871c92021-02-01 20:57:19 -060048
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060049
50class PackageDef(TypedDict, total=False):
Patrick Williams05fb2a02022-10-11 17:22:33 -050051 """Package Definition for packages dictionary."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060052
53 # rev [optional]: Revision of package to use.
54 rev: str
55 # url [optional]: lambda function to create URL: (package, rev) -> url.
56 url: Callable[[str, str], str]
57 # depends [optional]: List of package dependencies.
58 depends: Iterable[str]
59 # build_type [required]: Build type used for package.
60 # Currently supported: autoconf, cmake, custom, make, meson
61 build_type: str
62 # build_steps [optional]: Steps to run for 'custom' build_type.
63 build_steps: Iterable[str]
64 # config_flags [optional]: List of options to pass configuration tool.
65 config_flags: Iterable[str]
66 # config_env [optional]: List of environment variables to set for config.
67 config_env: Iterable[str]
68 # custom_post_dl [optional]: List of steps to run after download, but
69 # before config / build / install.
70 custom_post_dl: Iterable[str]
Patrick Williams6bce2ca2021-02-12 21:13:37 -060071 # custom_post_install [optional]: List of steps to run after install.
72 custom_post_install: Iterable[str]
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060073
74 # __tag [private]: Generated Docker tag name for package stage.
75 __tag: str
76 # __package [private]: Package object associated with this package.
77 __package: Any # Type is Package, but not defined yet.
78
Patrick Williams02871c92021-02-01 20:57:19 -060079
Patrick Williams72043242021-02-02 10:31:45 -060080# Packages to include in image.
81packages = {
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060082 "boost": PackageDef(
Ed Tanous42ff4322023-10-04 17:39:08 -070083 rev="1.83.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060084 url=(
Ed Tanous45bfd1f2022-11-30 15:50:28 -080085 lambda pkg, rev: f"https://boostorg.jfrog.io/artifactory/main/release/{rev}/source/{pkg}_{rev.replace('.', '_')}.tar.gz" # noqa: E501
Patrick Williams2abc4a42021-02-03 06:11:40 -060086 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -060087 build_type="custom",
88 build_steps=[
Patrick Williamse08ffba2022-12-05 10:33:46 -060089 (
Brad Bishop782f41f2023-05-17 15:36:16 -040090 "curl --remote-name"
Patrick Williams876ea1e2023-05-11 16:32:27 -050091 " https://github.com/williamspatrick/beast/commit/98f8b1fbd059a35754c2c7b2841769cf8d021272.patch"
92 " && patch -p2 <"
93 " 98f8b1fbd059a35754c2c7b2841769cf8d021272.patch &&"
94 " ./bootstrap.sh"
Ed Tanous42ff4322023-10-04 17:39:08 -070095 f" --prefix={prefix} --with-libraries=context,coroutine,url"
Patrick Williamse08ffba2022-12-05 10:33:46 -060096 ),
Patrick Williamsaae36d12021-02-04 16:30:04 -060097 "./b2",
98 f"./b2 install --prefix={prefix}",
99 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600100 ),
101 "USCiLab/cereal": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -0500102 rev="v1.3.2",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600103 build_type="custom",
104 build_steps=[f"cp -a include/cereal/ {prefix}/include/"],
105 ),
Ed Tanousc7198552022-07-01 08:15:50 -0700106 "danmar/cppcheck": PackageDef(
Patrick Williams51021782023-12-05 19:10:44 -0600107 rev="2.12.1",
Ed Tanousc7198552022-07-01 08:15:50 -0700108 build_type="cmake",
109 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600110 "CLIUtils/CLI11": PackageDef(
Patrick Williamsfc397332023-07-17 11:35:43 -0500111 rev="v2.3.2",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600112 build_type="cmake",
113 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600114 "-DBUILD_TESTING=OFF",
115 "-DCLI11_BUILD_DOCS=OFF",
116 "-DCLI11_BUILD_EXAMPLES=OFF",
117 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600118 ),
119 "fmtlib/fmt": PackageDef(
Patrick Williamsc061e072023-12-05 19:11:21 -0600120 rev="10.1.1",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600121 build_type="cmake",
122 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600123 "-DFMT_DOC=OFF",
124 "-DFMT_TEST=OFF",
125 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600126 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600127 "Naios/function2": PackageDef(
Patrick Williamscb099742023-12-05 19:12:09 -0600128 rev="4.2.4",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600129 build_type="custom",
130 build_steps=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600131 f"mkdir {prefix}/include/function2",
132 f"cp include/function2/function2.hpp {prefix}/include/function2/",
133 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600134 ),
135 "google/googletest": PackageDef(
Patrick Williamsfdf243b2023-12-05 19:13:50 -0600136 rev="v1.14.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600137 build_type="cmake",
William A. Kennington III4dd32c02021-05-28 01:58:13 -0700138 config_env=["CXXFLAGS=-std=c++20"],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600139 config_flags=["-DTHREADS_PREFER_PTHREAD_FLAG=ON"],
140 ),
Ed Tanous178b4b22023-06-15 09:03:11 -0700141 "nghttp2/nghttp2": PackageDef(
Patrick Williams2b5df8b2023-12-05 19:15:03 -0600142 rev="v1.57.0",
Ed Tanous178b4b22023-06-15 09:03:11 -0700143 build_type="cmake",
144 config_env=["CXXFLAGS=-std=c++20"],
145 config_flags=[
146 "-DENABLE_LIB_ONLY=ON",
147 "-DENABLE_STATIC_LIB=ON",
148 ],
149 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600150 "nlohmann/json": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -0500151 rev="v3.11.2",
Patrick Williams6bce2ca2021-02-12 21:13:37 -0600152 build_type="cmake",
153 config_flags=["-DJSON_BuildTests=OFF"],
154 custom_post_install=[
Patrick Williamse08ffba2022-12-05 10:33:46 -0600155 (
156 f"ln -s {prefix}/include/nlohmann/json.hpp"
157 f" {prefix}/include/json.hpp"
158 ),
Patrick Williamsaae36d12021-02-04 16:30:04 -0600159 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600160 ),
Przemyslaw Czarnowski058e3a32022-12-21 14:13:23 +0100161 "json-c/json-c": PackageDef(
Patrick Williamseee65be2023-12-05 19:17:01 -0600162 rev="json-c-0.17-20230812",
Przemyslaw Czarnowski058e3a32022-12-21 14:13:23 +0100163 build_type="cmake",
164 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600165 "linux-test-project/lcov": PackageDef(
Patrick Williamsf01a7242023-12-05 19:17:53 -0600166 rev="v1.16",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600167 build_type="make",
168 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600169 "LibVNC/libvncserver": PackageDef(
Patrick Williamsc0421322023-12-05 19:18:57 -0600170 rev="LibVNCServer-0.9.14",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600171 build_type="cmake",
172 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600173 "leethomason/tinyxml2": PackageDef(
Patrick Williamsc1977832022-09-27 16:54:34 -0500174 rev="9.0.0",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600175 build_type="cmake",
176 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600177 "tristanpenman/valijson": PackageDef(
Patrick Williams5a2c1132023-12-05 19:20:36 -0600178 rev="v1.0.1",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600179 build_type="cmake",
180 config_flags=[
Patrick Williams0eedeed2021-02-06 19:06:09 -0600181 "-Dvalijson_BUILD_TESTS=0",
182 "-Dvalijson_INSTALL_HEADERS=1",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600183 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600184 ),
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600185 "open-power/pdbg": PackageDef(build_type="autoconf"),
186 "openbmc/gpioplus": PackageDef(
187 depends=["openbmc/stdplus"],
188 build_type="meson",
189 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600190 "-Dexamples=false",
191 "-Dtests=disabled",
192 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600193 ),
194 "openbmc/phosphor-dbus-interfaces": PackageDef(
195 depends=["openbmc/sdbusplus"],
196 build_type="meson",
William A. Kennington III4fe87772022-02-11 15:44:29 -0800197 config_flags=["-Dgenerate_md=false"],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600198 ),
199 "openbmc/phosphor-logging": PackageDef(
200 depends=[
Patrick Williams83394612021-02-03 07:12:50 -0600201 "USCiLab/cereal",
Patrick Williams83394612021-02-03 07:12:50 -0600202 "openbmc/phosphor-dbus-interfaces",
203 "openbmc/sdbusplus",
204 "openbmc/sdeventplus",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600205 ],
Patrick Williamsf79ce4c2021-04-30 16:00:49 -0500206 build_type="meson",
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600207 config_flags=[
William A. Kennington III6c98f282022-10-05 13:37:04 -0700208 "-Dlibonly=true",
209 "-Dtests=disabled",
Patrick Williams5eabdae2022-04-14 14:34:34 -0500210 f"-Dyamldir={prefix}/share/phosphor-dbus-yaml/yaml",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600211 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600212 ),
213 "openbmc/phosphor-objmgr": PackageDef(
214 depends=[
Brad Bishop11e57622022-09-14 16:10:25 -0400215 "CLIUtils/CLI11",
Patrick Williams70af95c2022-09-27 16:55:41 -0500216 "boost",
Patrick Williams83394612021-02-03 07:12:50 -0600217 "leethomason/tinyxml2",
Patrick Williams70af95c2022-09-27 16:55:41 -0500218 "openbmc/phosphor-dbus-interfaces",
Patrick Williams83394612021-02-03 07:12:50 -0600219 "openbmc/phosphor-logging",
220 "openbmc/sdbusplus",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600221 ],
Brad Bishop1197e352021-08-03 19:25:46 -0400222 build_type="meson",
223 config_flags=[
224 "-Dtests=disabled",
225 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600226 ),
Jason M. Billsc02ff272023-08-02 10:55:22 -0700227 "openbmc/libpeci": PackageDef(
228 build_type="meson",
229 config_flags=[
230 "-Draw-peci=disabled",
231 ],
232 ),
Manojkiran Eda1c19e452022-10-03 11:01:59 +0530233 "openbmc/libpldm": PackageDef(
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600234 build_type="meson",
235 config_flags=[
Andrew Jeffery29d69bb2023-06-06 14:38:24 +0930236 "-Dabi=deprecated,stable",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600237 "-Doem-ibm=enabled",
238 "-Dtests=disabled",
239 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600240 ),
241 "openbmc/sdbusplus": PackageDef(
242 build_type="meson",
243 custom_post_dl=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600244 "cd tools",
245 f"./setup.py install --root=/ --prefix={prefix}",
246 "cd ..",
247 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600248 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600249 "-Dexamples=disabled",
250 "-Dtests=disabled",
251 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600252 ),
253 "openbmc/sdeventplus": PackageDef(
Patrick Williams70af95c2022-09-27 16:55:41 -0500254 depends=[
255 "Naios/function2",
256 "openbmc/stdplus",
257 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600258 build_type="meson",
259 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600260 "-Dexamples=false",
261 "-Dtests=disabled",
262 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600263 ),
264 "openbmc/stdplus": PackageDef(
Patrick Williams70af95c2022-09-27 16:55:41 -0500265 depends=[
Patrick Williams70af95c2022-09-27 16:55:41 -0500266 "fmtlib/fmt",
William A. Kennington IIIca1bf0c2022-10-05 02:23:30 -0700267 "google/googletest",
268 "Naios/function2",
Patrick Williams70af95c2022-09-27 16:55:41 -0500269 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600270 build_type="meson",
271 config_flags=[
Patrick Williamsaae36d12021-02-04 16:30:04 -0600272 "-Dexamples=false",
273 "-Dtests=disabled",
William A. Kennington IIIca1bf0c2022-10-05 02:23:30 -0700274 "-Dgtest=enabled",
Patrick Williamsaae36d12021-02-04 16:30:04 -0600275 ],
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600276 ),
277} # type: Dict[str, PackageDef]
Patrick Williams02871c92021-02-01 20:57:19 -0600278
279# Define common flags used for builds
Patrick Williams02871c92021-02-01 20:57:19 -0600280configure_flags = " ".join(
281 [
282 f"--prefix={prefix}",
283 ]
284)
285cmake_flags = " ".join(
286 [
Patrick Williams02871c92021-02-01 20:57:19 -0600287 "-DBUILD_SHARED_LIBS=ON",
Patrick Williams0f2086b2021-02-05 06:49:49 -0600288 "-DCMAKE_BUILD_TYPE=RelWithDebInfo",
Patrick Williams02871c92021-02-01 20:57:19 -0600289 f"-DCMAKE_INSTALL_PREFIX:PATH={prefix}",
Patrick Williams0f2086b2021-02-05 06:49:49 -0600290 "-GNinja",
291 "-DCMAKE_MAKE_PROGRAM=ninja",
Patrick Williams02871c92021-02-01 20:57:19 -0600292 ]
293)
294meson_flags = " ".join(
295 [
296 "--wrap-mode=nodownload",
297 f"-Dprefix={prefix}",
298 ]
299)
300
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600301
302class Package(threading.Thread):
303 """Class used to build the Docker stages for each package.
304
305 Generally, this class should not be instantiated directly but through
306 Package.generate_all().
307 """
308
309 # Copy the packages dictionary.
310 packages = packages.copy()
311
312 # Lock used for thread-safety.
313 lock = threading.Lock()
314
315 def __init__(self, pkg: str):
Patrick Williams05fb2a02022-10-11 17:22:33 -0500316 """pkg - The name of this package (ex. foo/bar )"""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600317 super(Package, self).__init__()
318
319 self.package = pkg
320 self.exception = None # type: Optional[Exception]
321
322 # Reference to this package's
323 self.pkg_def = Package.packages[pkg]
324 self.pkg_def["__package"] = self
325
326 def run(self) -> None:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500327 """Thread 'run' function. Builds the Docker stage."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600328
329 # In case this package has no rev, fetch it from Github.
330 self._update_rev()
331
332 # Find all the Package objects that this package depends on.
333 # This section is locked because we are looking into another
334 # package's PackageDef dict, which could be being modified.
335 Package.lock.acquire()
336 deps: Iterable[Package] = [
337 Package.packages[deppkg]["__package"]
338 for deppkg in self.pkg_def.get("depends", [])
339 ]
340 Package.lock.release()
341
342 # Wait until all the depends finish building. We need them complete
343 # for the "COPY" commands.
344 for deppkg in deps:
345 deppkg.join()
346
347 # Generate this package's Dockerfile.
348 dockerfile = f"""
349FROM {docker_base_img_name}
350{self._df_copycmds()}
351{self._df_build()}
352"""
353
354 # Generate the resulting tag name and save it to the PackageDef.
355 # This section is locked because we are modifying the PackageDef,
356 # which can be accessed by other threads.
357 Package.lock.acquire()
358 tag = Docker.tagname(self._stagename(), dockerfile)
359 self.pkg_def["__tag"] = tag
360 Package.lock.release()
361
362 # Do the build / save any exceptions.
363 try:
364 Docker.build(self.package, tag, dockerfile)
365 except Exception as e:
366 self.exception = e
367
368 @classmethod
369 def generate_all(cls) -> None:
370 """Ensure a Docker stage is created for all defined packages.
371
372 These are done in parallel but with appropriate blocking per
373 package 'depends' specifications.
374 """
375
376 # Create a Package for each defined package.
377 pkg_threads = [Package(p) for p in cls.packages.keys()]
378
379 # Start building them all.
Patrick Williams6dbd7802021-02-20 08:34:10 -0600380 # This section is locked because threads depend on each other,
381 # based on the packages, and they cannot 'join' on a thread
382 # which is not yet started. Adding a lock here allows all the
383 # threads to start before they 'join' their dependencies.
384 Package.lock.acquire()
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600385 for t in pkg_threads:
386 t.start()
Patrick Williams6dbd7802021-02-20 08:34:10 -0600387 Package.lock.release()
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600388
389 # Wait for completion.
390 for t in pkg_threads:
391 t.join()
392 # Check if the thread saved off its own exception.
393 if t.exception:
394 print(f"Package {t.package} failed!", file=sys.stderr)
395 raise t.exception
396
397 @staticmethod
398 def df_all_copycmds() -> str:
399 """Formulate the Dockerfile snippet necessary to copy all packages
400 into the final image.
401 """
402 return Package.df_copycmds_set(Package.packages.keys())
403
404 @classmethod
405 def depcache(cls) -> str:
406 """Create the contents of the '/tmp/depcache'.
407 This file is a comma-separated list of "<pkg>:<rev>".
408 """
409
410 # This needs to be sorted for consistency.
411 depcache = ""
412 for pkg in sorted(cls.packages.keys()):
413 depcache += "%s:%s," % (pkg, cls.packages[pkg]["rev"])
414 return depcache
415
416 def _update_rev(self) -> None:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500417 """Look up the HEAD for missing a static rev."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600418
419 if "rev" in self.pkg_def:
420 return
421
Patrick Williams65b21fb2021-02-12 21:21:14 -0600422 # Check if Jenkins/Gerrit gave us a revision and use it.
423 if gerrit_project == self.package and gerrit_rev:
424 print(
425 f"Found Gerrit revision for {self.package}: {gerrit_rev}",
426 file=sys.stderr,
427 )
428 self.pkg_def["rev"] = gerrit_rev
429 return
430
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600431 # Ask Github for all the branches.
Patrick Williams05fb2a02022-10-11 17:22:33 -0500432 lookup = git(
433 "ls-remote", "--heads", f"https://github.com/{self.package}"
434 )
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600435
436 # Find the branch matching {branch} (or fallback to master).
437 # This section is locked because we are modifying the PackageDef.
438 Package.lock.acquire()
439 for line in lookup.split("\n"):
440 if f"refs/heads/{branch}" in line:
441 self.pkg_def["rev"] = line.split()[0]
Patrick Williamsc7d73642022-10-11 17:22:06 -0500442 elif (
443 "refs/heads/master" in line or "refs/heads/main" in line
444 ) and "rev" not in self.pkg_def:
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600445 self.pkg_def["rev"] = line.split()[0]
446 Package.lock.release()
447
448 def _stagename(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500449 """Create a name for the Docker stage associated with this pkg."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600450 return self.package.replace("/", "-").lower()
451
452 def _url(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500453 """Get the URL for this package."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600454 rev = self.pkg_def["rev"]
455
456 # If the lambda exists, call it.
457 if "url" in self.pkg_def:
458 return self.pkg_def["url"](self.package, rev)
459
460 # Default to the github archive URL.
461 return f"https://github.com/{self.package}/archive/{rev}.tar.gz"
462
463 def _cmd_download(self) -> str:
464 """Formulate the command necessary to download and unpack to source."""
465
466 url = self._url()
467 if ".tar." not in url:
468 raise NotImplementedError(
469 f"Unhandled download type for {self.package}: {url}"
470 )
471
472 cmd = f"curl -L {url} | tar -x"
473
474 if url.endswith(".bz2"):
475 cmd += "j"
476 elif url.endswith(".gz"):
477 cmd += "z"
478 else:
479 raise NotImplementedError(
480 f"Unknown tar flags needed for {self.package}: {url}"
481 )
482
483 return cmd
484
485 def _cmd_cd_srcdir(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500486 """Formulate the command necessary to 'cd' into the source dir."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600487 return f"cd {self.package.split('/')[-1]}*"
488
489 def _df_copycmds(self) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500490 """Formulate the dockerfile snippet necessary to COPY all depends."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600491
492 if "depends" not in self.pkg_def:
493 return ""
494 return Package.df_copycmds_set(self.pkg_def["depends"])
495
496 @staticmethod
497 def df_copycmds_set(pkgs: Iterable[str]) -> str:
498 """Formulate the Dockerfile snippet necessary to COPY a set of
499 packages into a Docker stage.
500 """
501
502 copy_cmds = ""
503
504 # Sort the packages for consistency.
505 for p in sorted(pkgs):
506 tag = Package.packages[p]["__tag"]
507 copy_cmds += f"COPY --from={tag} {prefix} {prefix}\n"
508 # Workaround for upstream docker bug and multiple COPY cmds
509 # https://github.com/moby/moby/issues/37965
510 copy_cmds += "RUN true\n"
511
512 return copy_cmds
513
514 def _df_build(self) -> str:
515 """Formulate the Dockerfile snippet necessary to download, build, and
516 install a package into a Docker stage.
517 """
518
519 # Download and extract source.
520 result = f"RUN {self._cmd_download()} && {self._cmd_cd_srcdir()} && "
521
522 # Handle 'custom_post_dl' commands.
523 custom_post_dl = self.pkg_def.get("custom_post_dl")
524 if custom_post_dl:
525 result += " && ".join(custom_post_dl) + " && "
526
527 # Build and install package based on 'build_type'.
528 build_type = self.pkg_def["build_type"]
529 if build_type == "autoconf":
530 result += self._cmd_build_autoconf()
531 elif build_type == "cmake":
532 result += self._cmd_build_cmake()
533 elif build_type == "custom":
534 result += self._cmd_build_custom()
535 elif build_type == "make":
536 result += self._cmd_build_make()
537 elif build_type == "meson":
538 result += self._cmd_build_meson()
539 else:
540 raise NotImplementedError(
541 f"Unhandled build type for {self.package}: {build_type}"
542 )
543
Patrick Williams6bce2ca2021-02-12 21:13:37 -0600544 # Handle 'custom_post_install' commands.
545 custom_post_install = self.pkg_def.get("custom_post_install")
546 if custom_post_install:
547 result += " && " + " && ".join(custom_post_install)
548
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600549 return result
550
551 def _cmd_build_autoconf(self) -> str:
552 options = " ".join(self.pkg_def.get("config_flags", []))
553 env = " ".join(self.pkg_def.get("config_env", []))
554 result = "./bootstrap.sh && "
555 result += f"{env} ./configure {configure_flags} {options} && "
556 result += f"make -j{proc_count} && make install"
557 return result
558
559 def _cmd_build_cmake(self) -> str:
560 options = " ".join(self.pkg_def.get("config_flags", []))
561 env = " ".join(self.pkg_def.get("config_env", []))
562 result = "mkdir builddir && cd builddir && "
563 result += f"{env} cmake {cmake_flags} {options} .. && "
564 result += "cmake --build . --target all && "
565 result += "cmake --build . --target install && "
566 result += "cd .."
567 return result
568
569 def _cmd_build_custom(self) -> str:
570 return " && ".join(self.pkg_def.get("build_steps", []))
571
572 def _cmd_build_make(self) -> str:
573 return f"make -j{proc_count} && make install"
574
575 def _cmd_build_meson(self) -> str:
576 options = " ".join(self.pkg_def.get("config_flags", []))
577 env = " ".join(self.pkg_def.get("config_env", []))
Andrew Jefferye2da11a2023-06-15 10:16:37 +0930578 result = f"{env} meson setup builddir {meson_flags} {options} && "
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600579 result += "ninja -C builddir && ninja -C builddir install"
580 return result
581
582
583class Docker:
584 """Class to assist with Docker interactions. All methods are static."""
585
586 @staticmethod
587 def timestamp() -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500588 """Generate a timestamp for today using the ISO week."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600589 today = date.today().isocalendar()
590 return f"{today[0]}-W{today[1]:02}"
591
592 @staticmethod
Patrick Williams41d86212022-11-25 18:28:43 -0600593 def tagname(pkgname: Optional[str], dockerfile: str) -> str:
Patrick Williams05fb2a02022-10-11 17:22:33 -0500594 """Generate a tag name for a package using a hash of the Dockerfile."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600595 result = docker_image_name
596 if pkgname:
597 result += "-" + pkgname
598
599 result += ":" + Docker.timestamp()
600 result += "-" + sha256(dockerfile.encode()).hexdigest()[0:16]
601
602 return result
603
604 @staticmethod
605 def build(pkg: str, tag: str, dockerfile: str) -> None:
Andrew Geissler22e61102023-02-14 14:44:00 -0600606 """Build a docker image using the Dockerfile and tagging it with 'tag'."""
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600607
608 # If we're not forcing builds, check if it already exists and skip.
609 if not force_build:
610 if docker.image.ls(tag, "--format", '"{{.Repository}}:{{.Tag}}"'):
Patrick Williams05fb2a02022-10-11 17:22:33 -0500611 print(
612 f"Image {tag} already exists. Skipping.", file=sys.stderr
613 )
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600614 return
615
616 # Build it.
617 # Capture the output of the 'docker build' command and send it to
618 # stderr (prefixed with the package name). This allows us to see
619 # progress but not polute stdout. Later on we output the final
620 # docker tag to stdout and we want to keep that pristine.
621 #
622 # Other unusual flags:
623 # --no-cache: Bypass the Docker cache if 'force_build'.
624 # --force-rm: Clean up Docker processes if they fail.
625 docker.build(
626 proxy_args,
627 "--network=host",
628 "--force-rm",
629 "--no-cache=true" if force_build else "--no-cache=false",
630 "-t",
631 tag,
632 "-",
633 _in=dockerfile,
634 _out=(
635 lambda line: print(
636 pkg + ":", line, end="", file=sys.stderr, flush=True
637 )
638 ),
639 )
640
641
642# Read a bunch of environment variables.
Patrick Williams05fb2a02022-10-11 17:22:33 -0500643docker_image_name = os.environ.get(
644 "DOCKER_IMAGE_NAME", "openbmc/ubuntu-unit-test"
645)
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600646force_build = os.environ.get("FORCE_DOCKER_BUILD")
647is_automated_ci_build = os.environ.get("BUILD_URL", False)
William A. Kennington IIIc6caa182023-06-07 15:11:51 -0700648distro = os.environ.get("DISTRO", "ubuntu:mantic")
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600649branch = os.environ.get("BRANCH", "master")
650ubuntu_mirror = os.environ.get("UBUNTU_MIRROR")
651http_proxy = os.environ.get("http_proxy")
652
Patrick Williams65b21fb2021-02-12 21:21:14 -0600653gerrit_project = os.environ.get("GERRIT_PROJECT")
654gerrit_rev = os.environ.get("GERRIT_PATCHSET_REVISION")
655
Andrew Geisslerd0dabc32023-04-04 08:09:21 -0600656# Ensure appropriate docker build output to see progress and identify
657# any issues
658os.environ["BUILDKIT_PROGRESS"] = "plain"
659
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600660# Set up some common variables.
661username = os.environ.get("USER", "root")
662homedir = os.environ.get("HOME", "/root")
663gid = os.getgid()
664uid = os.getuid()
665
Josh Lehan6825a012022-03-17 18:31:39 -0700666# Use well-known constants if user is root
667if username == "root":
668 homedir = "/root"
669 gid = 0
670 uid = 0
671
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600672# Determine the architecture for Docker.
673arch = uname("-m").strip()
674if arch == "ppc64le":
675 docker_base = "ppc64le/"
676elif arch == "x86_64":
677 docker_base = ""
Thang Q. Nguyen051b05b2021-12-10 08:30:35 +0000678elif arch == "aarch64":
Thang Q. Nguyenf98f1a82021-12-22 01:59:19 +0000679 docker_base = "arm64v8/"
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600680else:
681 print(
682 f"Unsupported system architecture({arch}) found for docker image",
683 file=sys.stderr,
684 )
685 sys.exit(1)
686
Patrick Williams02871c92021-02-01 20:57:19 -0600687# Special flags if setting up a deb mirror.
688mirror = ""
689if "ubuntu" in distro and ubuntu_mirror:
690 mirror = f"""
Patrick Williamse08ffba2022-12-05 10:33:46 -0600691RUN echo "deb {ubuntu_mirror} \
692 $(. /etc/os-release && echo $VERSION_CODENAME) \
693 main restricted universe multiverse" > /etc/apt/sources.list && \\
694 echo "deb {ubuntu_mirror} \
695 $(. /etc/os-release && echo $VERSION_CODENAME)-updates \
696 main restricted universe multiverse" >> /etc/apt/sources.list && \\
697 echo "deb {ubuntu_mirror} \
698 $(. /etc/os-release && echo $VERSION_CODENAME)-security \
699 main restricted universe multiverse" >> /etc/apt/sources.list && \\
700 echo "deb {ubuntu_mirror} \
701 $(. /etc/os-release && echo $VERSION_CODENAME)-proposed \
702 main restricted universe multiverse" >> /etc/apt/sources.list && \\
703 echo "deb {ubuntu_mirror} \
704 $(. /etc/os-release && echo $VERSION_CODENAME)-backports \
705 main restricted universe multiverse" >> /etc/apt/sources.list
Patrick Williams02871c92021-02-01 20:57:19 -0600706"""
707
708# Special flags for proxying.
709proxy_cmd = ""
Adrian Ambrożewicz34ec77e2021-06-02 10:23:38 +0200710proxy_keyserver = ""
Patrick Williams02871c92021-02-01 20:57:19 -0600711proxy_args = []
712if http_proxy:
713 proxy_cmd = f"""
714RUN echo "[http]" >> {homedir}/.gitconfig && \
715 echo "proxy = {http_proxy}" >> {homedir}/.gitconfig
716"""
Adrian Ambrożewicz34ec77e2021-06-02 10:23:38 +0200717 proxy_keyserver = f"--keyserver-options http-proxy={http_proxy}"
718
Patrick Williams02871c92021-02-01 20:57:19 -0600719 proxy_args.extend(
720 [
721 "--build-arg",
722 f"http_proxy={http_proxy}",
723 "--build-arg",
Lei YUd461cd62021-02-18 14:25:49 +0800724 f"https_proxy={http_proxy}",
Patrick Williams02871c92021-02-01 20:57:19 -0600725 ]
726 )
727
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600728# Create base Dockerfile.
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600729dockerfile_base = f"""
730FROM {docker_base}{distro}
Patrick Williams02871c92021-02-01 20:57:19 -0600731
732{mirror}
733
734ENV DEBIAN_FRONTEND noninteractive
735
Patrick Williams8949d3c2022-04-27 16:41:27 -0500736ENV PYTHONPATH "/usr/local/lib/python3.10/site-packages/"
Patrick Williams02871c92021-02-01 20:57:19 -0600737
Patrick Williamsbb16ac12021-04-12 12:23:51 -0500738# Sometimes the ubuntu key expires and we need a way to force an execution
739# of the apt-get commands for the dbgsym-keyring. When this happens we see
740# an error like: "Release: The following signatures were invalid:"
741# Insert a bogus echo that we can change here when we get this error to force
742# the update.
743RUN echo "ubuntu keyserver rev as of 2021-04-21"
744
Patrick Williams02871c92021-02-01 20:57:19 -0600745# We need the keys to be imported for dbgsym repos
746# New releases have a package, older ones fall back to manual fetching
747# https://wiki.ubuntu.com/Debug%20Symbol%20Packages
Jagpal Singh Gill575b5e42023-04-14 15:52:10 -0700748# Known issue with gpg to get keys via proxy -
749# https://bugs.launchpad.net/ubuntu/+source/gnupg2/+bug/1788190, hence using
750# curl to get keys.
Patrick Williams50837432021-02-06 12:24:05 -0600751RUN apt-get update && apt-get dist-upgrade -yy && \
Jian Zhang938d3032023-07-05 13:35:35 +0800752 ( apt-get install -yy gpgv ubuntu-dbgsym-keyring || \
Jagpal Singh Gill575b5e42023-04-14 15:52:10 -0700753 ( apt-get install -yy dirmngr curl && \
754 curl -sSL \
755 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0xF2EDC64DC5AEE1F6B9C621F0C8CAB6595FDFF622' \
756 | apt-key add - ))
Patrick Williams02871c92021-02-01 20:57:19 -0600757
758# Parse the current repo list into a debug repo list
Patrick Williamse08ffba2022-12-05 10:33:46 -0600759RUN sed -n '/^deb /s,^deb [^ ]* ,deb http://ddebs.ubuntu.com ,p' \
760 /etc/apt/sources.list >/etc/apt/sources.list.d/debug.list
Patrick Williams02871c92021-02-01 20:57:19 -0600761
762# Remove non-existent debug repos
Patrick Williams41d86212022-11-25 18:28:43 -0600763RUN sed -i '/-\\(backports\\|security\\) /d' /etc/apt/sources.list.d/debug.list
Patrick Williams02871c92021-02-01 20:57:19 -0600764
765RUN cat /etc/apt/sources.list.d/debug.list
766
767RUN apt-get update && apt-get dist-upgrade -yy && apt-get install -yy \
Andrew Jeffery58f19152023-05-22 16:41:32 +0930768 abi-compliance-checker \
Andrew Jeffery8b112062023-05-22 20:49:11 +0930769 abi-dumper \
Patrick Williams02871c92021-02-01 20:57:19 -0600770 autoconf \
771 autoconf-archive \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600772 bison \
Patrick Williamse2e62e52023-09-20 16:21:16 -0500773 clang-17 \
774 clang-format-17 \
775 clang-tidy-17 \
776 clang-tools-17 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600777 cmake \
778 curl \
779 dbus \
780 device-tree-compiler \
781 flex \
Patrick Williams961f1482023-05-30 09:24:16 -0500782 g++-13 \
783 gcc-13 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600784 git \
Patrick Williams02871c92021-02-01 20:57:19 -0600785 iproute2 \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600786 iputils-ping \
Manojkiran Eda524a3312023-04-05 15:37:47 +0530787 libaudit-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600788 libc6-dbg \
789 libc6-dev \
790 libconfig++-dev \
791 libcryptsetup-dev \
792 libdbus-1-dev \
793 libevdev-dev \
794 libgpiod-dev \
795 libi2c-dev \
796 libjpeg-dev \
797 libjson-perl \
798 libldap2-dev \
799 libmimetic-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600800 libnl-3-dev \
801 libnl-genl-3-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600802 libpam0g-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600803 libpciaccess-dev \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600804 libperlio-gzip-perl \
805 libpng-dev \
806 libprotobuf-dev \
807 libsnmp-dev \
808 libssl-dev \
809 libsystemd-dev \
810 libtool \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600811 liburing-dev \
Patrick Williams02871c92021-02-01 20:57:19 -0600812 libxml2-utils \
Patrick Williams0eedeed2021-02-06 19:06:09 -0600813 libxml-simple-perl \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600814 ninja-build \
815 npm \
816 pkg-config \
817 protobuf-compiler \
818 python3 \
819 python3-dev\
820 python3-git \
821 python3-mako \
822 python3-pip \
823 python3-setuptools \
824 python3-socks \
825 python3-yaml \
John Wedig9adf68d2021-11-16 14:00:39 -0800826 rsync \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600827 shellcheck \
828 sudo \
829 systemd \
830 valgrind \
Andrew Geisslereef3c372023-09-12 10:41:33 -0400831 valgrind-dbgsym \
Andrew Geisslerb565f822022-12-14 11:43:25 -0600832 vim \
Andrew Geissleraf49ed52022-12-14 11:41:35 -0600833 wget \
834 xxd
Patrick Williams02871c92021-02-01 20:57:19 -0600835
Patrick Williams961f1482023-05-30 09:24:16 -0500836RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13 \
837 --slave /usr/bin/g++ g++ /usr/bin/g++-13 \
838 --slave /usr/bin/gcov gcov /usr/bin/gcov-13 \
839 --slave /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-13 \
840 --slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-13
841RUN update-alternatives --remove cpp /usr/bin/cpp && \
842 update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-13 13
Patrick Williams02871c92021-02-01 20:57:19 -0600843
Patrick Williamse2e62e52023-09-20 16:21:16 -0500844RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-17 1000 \
845 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-17 \
846 --slave /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-17 \
847 --slave /usr/bin/clang-format clang-format /usr/bin/clang-format-17 \
Patrick Williamse08ffba2022-12-05 10:33:46 -0600848 --slave /usr/bin/run-clang-tidy run-clang-tidy.py \
Patrick Williamse2e62e52023-09-20 16:21:16 -0500849 /usr/bin/run-clang-tidy-17 \
850 --slave /usr/bin/scan-build scan-build /usr/bin/scan-build-17
Patrick Williams02871c92021-02-01 20:57:19 -0600851
Patrick Williams50837432021-02-06 12:24:05 -0600852"""
853
854if is_automated_ci_build:
855 dockerfile_base += f"""
856# Run an arbitrary command to polute the docker cache regularly force us
857# to re-run `apt-get update` daily.
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600858RUN echo {Docker.timestamp()}
Patrick Williams50837432021-02-06 12:24:05 -0600859RUN apt-get update && apt-get dist-upgrade -yy
860
861"""
862
Patrick Williams41d86212022-11-25 18:28:43 -0600863dockerfile_base += """
Patrick Williams5e4d8402023-04-11 22:19:30 -0500864RUN pip3 install --break-system-packages \
Patrick Williams818023d2023-04-10 13:07:15 -0500865 beautysh \
866 black \
867 codespell \
868 flake8 \
869 gitlint \
870 inflection \
871 isort \
872 jsonschema \
Patrick Williamsb20d9812023-10-21 08:49:40 -0500873 meson==1.2.3 \
Patrick Williams818023d2023-04-10 13:07:15 -0500874 protobuf \
875 requests
Patrick Williamsb08ddf72022-12-06 08:56:31 -0600876
877RUN npm install -g \
878 eslint@latest eslint-plugin-json@latest \
Patrick Williams7d41f6d2022-12-06 10:19:43 -0600879 markdownlint-cli@latest \
Patrick Williamsb08ddf72022-12-06 08:56:31 -0600880 prettier@latest
Ed Tanousfb9948a2022-06-21 09:10:24 -0700881"""
882
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600883# Build the base and stage docker images.
884docker_base_img_name = Docker.tagname("base", dockerfile_base)
885Docker.build("base", docker_base_img_name, dockerfile_base)
886Package.generate_all()
Patrick Williams02871c92021-02-01 20:57:19 -0600887
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600888# Create the final Dockerfile.
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600889dockerfile = f"""
Patrick Williams02871c92021-02-01 20:57:19 -0600890# Build the final output image
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600891FROM {docker_base_img_name}
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600892{Package.df_all_copycmds()}
Patrick Williams02871c92021-02-01 20:57:19 -0600893
894# Some of our infrastructure still relies on the presence of this file
895# even though it is no longer needed to rebuild the docker environment
896# NOTE: The file is sorted to ensure the ordering is stable.
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600897RUN echo '{Package.depcache()}' > /tmp/depcache
Patrick Williams02871c92021-02-01 20:57:19 -0600898
Patrick Williams67cc0612023-04-11 22:16:46 -0500899# Ensure the group, user, and home directory are created (or rename them if
900# they already exist).
901RUN if grep -q ":{gid}:" /etc/group ; then \
902 groupmod -n {username} $(awk -F : '{{ if ($3 == {gid}) {{ print $1 }} }}' /etc/group) ; \
903 else \
904 groupadd -f -g {gid} {username} ; \
905 fi
Patrick Williams02871c92021-02-01 20:57:19 -0600906RUN mkdir -p "{os.path.dirname(homedir)}"
Patrick Williams67cc0612023-04-11 22:16:46 -0500907RUN if grep -q ":{uid}:" /etc/passwd ; then \
Patrick Williams73b3ee92023-04-24 10:11:01 -0500908 usermod -l {username} -d {homedir} -m $(awk -F : '{{ if ($3 == {uid}) {{ print $1 }} }}' /etc/passwd) ; \
Patrick Williams67cc0612023-04-11 22:16:46 -0500909 else \
910 useradd -d {homedir} -m -u {uid} -g {gid} {username} ; \
911 fi
Patrick Williams02871c92021-02-01 20:57:19 -0600912RUN sed -i '1iDefaults umask=000' /etc/sudoers
913RUN echo "{username} ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
914
Andrew Geissler305a9a52021-04-07 11:08:40 -0500915# Ensure user has ability to write to /usr/local for different tool
916# and data installs
Andrew Geissler7bb00b12021-05-10 15:12:08 -0500917RUN chown -R {username}:{username} /usr/local/share
Andrew Geissler305a9a52021-04-07 11:08:40 -0500918
Patrick Williams02871c92021-02-01 20:57:19 -0600919{proxy_cmd}
920
921RUN /bin/bash
922"""
923
Patrick Williamsa18d9c52021-02-05 09:52:26 -0600924# Do the final docker build
Patrick Williamsee3c9ee2021-02-12 20:56:01 -0600925docker_final_img_name = Docker.tagname(None, dockerfile)
926Docker.build("final", docker_final_img_name, dockerfile)
927
Patrick Williams00536fb2021-02-11 14:28:49 -0600928# Print the tag of the final image.
929print(docker_final_img_name)