From 2dd3057ea31a31fda4f76268dc2f6f5dcb57bcd8 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Mon, 20 Oct 2025 15:04:49 -0400 Subject: [PATCH] script/build-with-container: optionally source WITH_CRIMSON from env file Add support for optionally sourcing WITH_CRIMSON from the env file that can be passed to BWC on the command line. When auto-detecting the crimson variant we previously only looked at the BWC processes environment. After speaking with Zack we determined that the Jenkinsfile only writes the WITH_CRIMSON param into the env file, so we add support to "peek" in the env file for the WITH_CRIMSON variable. Signed-off-by: John Mulligan --- src/script/build-with-container.py | 56 ++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/src/script/build-with-container.py b/src/script/build-with-container.py index 4c8d47a832f..fda8b341a1e 100755 --- a/src/script/build-with-container.py +++ b/src/script/build-with-container.py @@ -410,6 +410,40 @@ class Context: base = self.cli.image_repo or "ceph-build" return f"{base}:{self.target_tag()}" + @ftcache + def _env_file(self): + if not self.cli.env_file: + return None + with open(self.cli.env_file) as fh: + return fh.readlines() + + @ftcache + def lookup_env_file(self, key): + """Simplistic env file parser/key lookup function. + Finds a value assignment in the env file, returns str unless + the env file parameter is not set or the key is not present, + in that case None will be returned. + """ + # This script has minimal dependencies and so we avoid using + # a 3rd party "env file parser" library. + lines = self._env_file() + if not lines: + return None + prefix = f'{key}=' + found = None + for line in lines: + if line.startswith(prefix): + found = line + if not found: + return None + temp_value = found.strip().split('=', 1)[-1] + # ensure there's only one value on this line, otherwise we could be + # reading garbage, or an arbitary shell command + values = shlex.split(temp_value) + if len(values) != 1: + raise ValueError(f"unexpected value in env file: {found!r}") + return values[0] + def packages_build(self): """Return true if only packages will be build (not make check).""" return self.cli.image_variant in { @@ -418,11 +452,29 @@ class Context: ImageVariant.PACKAGES_AND_CRIMSON, } + @ftcache + def _with_crimson(self): + with_crimson = os.environ.get('WITH_CRIMSON') + log.debug("Environment WITH_CRIMSON=%r", with_crimson) + with_crimson2 = self.lookup_env_file('WITH_CRIMSON') + log.debug("Env file WITH_CRIMSON=%r", with_crimson2) + if ( + with_crimson != with_crimson2 + and (with_crimson is not None) + and (with_crimson2 is not None) + ): + raise ValueError( + 'conflicting WITH_CRIMSON values in env and env file' + ) + elif with_crimson2 is not None: + with_crimson = with_crimson2 + return with_crimson + def variant(self): """Return calculated variant. Checks env vars to select between packages with or without crimson. """ - with_crimson = os.environ.get('WITH_CRIMSON') + with_crimson = self._with_crimson() if ( self.cli.image_variant is ImageVariant.PACKAGES_AUTO and with_crimson @@ -439,7 +491,7 @@ class Context: if self.variant() is ImageVariant.PACKAGES_AND_CRIMSON: return True if self.variant() is ImageVariant.DEFAULT: - with_crimson = os.environ.get('WITH_CRIMSON') + with_crimson = self._with_crimson() return None if with_crimson is None else bool(with_crimson) return False -- 2.39.5