Skip to content

Commit 198e107

Browse files
hack: support custom container build config files
I constantly rely on my shell history to make custom modifications to create images based on work-in-progress package builds. Adding a customizations file allows me to store things in a file so I can run: ./hack/build-image -i quay.io/phlogistonjohn/samba-server:custom-centos-amd64-x2 -C foo.ini instead of a long line of embedded junk on the shell. No docs yet because this is for me. :-) Signed-off-by: John Mulligan <[email protected]>
1 parent a17c3b4 commit 198e107

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

hack/build-image

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Usage:
3636
"""
3737

3838
import argparse
39+
import configparser
3940
import logging
4041
import os
4142
import pathlib
@@ -280,8 +281,7 @@ def create_common_container_engine_args(cli, target):
280281
"ctdb_rados_mutex_skip_reg"
281282
)
282283

283-
if cli.extra_build_arg:
284-
args.extend(cli.extra_build_arg)
284+
args.extend(cli.extra_build_arguments())
285285

286286
for tname in target.all_names(baseless=cli.without_repo_bases):
287287
args.append("-t")
@@ -600,6 +600,61 @@ def print_tags(cli, target):
600600
print(f"{prefix}{name}")
601601

602602

603+
class CLIContext:
604+
def __init__(self, cli):
605+
self._cli = cli
606+
self._customizations = None
607+
608+
def __getattr__(self, key):
609+
return getattr(self._cli, key)
610+
611+
def read_customizations(self):
612+
if not self._cli.customizations:
613+
return
614+
self._customizations = configparser.ConfigParser()
615+
files = self._customizations.read(self._cli.customizations)
616+
if not files:
617+
raise ValueError("no customization files could be read")
618+
619+
def customizations(self):
620+
if self._customizations is None:
621+
self.read_customizations()
622+
return self._customizations
623+
624+
def extra_build_arguments(self):
625+
args = []
626+
# extra args from customizations file
627+
if "image" in self.customizations():
628+
cimg = self.customizations()["image"]
629+
for build_arg, vtype, names in self._args_custom_vars():
630+
for name in names:
631+
cvalue = cimg.get(name, None)
632+
if cvalue is None:
633+
continue
634+
if vtype is list:
635+
cvalue = " ".join(cvalue.strip().split())
636+
args.append(f"--build-arg={build_arg}={cvalue}")
637+
# CLI extra args
638+
if self._cli.extra_build_arg:
639+
args.extend(self._cli.extra_build_arg)
640+
return args
641+
642+
def _args_custom_vars(self):
643+
return [
644+
("INSTALL_PACKAGES_FROM", str, ("install_packages_from",)),
645+
("SAMBA_VERSION_SUFFIX", str, ("samba_version_suffix",)),
646+
("SAMBACC_VERSION_SUFFIX", str, ("sambacc_version_suffix",)),
647+
("SAMBA_SPECIFICS", str, ("samba_specifics",)),
648+
(
649+
"INSTALL_CUSTOM_REPO",
650+
str,
651+
("install_custom_repo", "samba_custom_repo"),
652+
),
653+
("PACKAGE_SELECTION", str, ("package_selection",)),
654+
("CUSTOM_REPOS", list, ("custom_repos",)),
655+
]
656+
657+
603658
def main():
604659
parser = argparse.ArgumentParser()
605660
parser.add_argument(
@@ -719,6 +774,12 @@ def main():
719774
" without the repo base"
720775
),
721776
)
777+
parser.add_argument(
778+
"--customizations",
779+
"-C",
780+
type=pathlib.Path,
781+
help="",
782+
)
722783
parser.add_argument(
723784
"--distro-qualified",
724785
action=argparse.BooleanOptionalAction,
@@ -767,7 +828,7 @@ def main():
767828
" for a given FQIN. Requires FQIN to already exist locally."
768829
),
769830
)
770-
cli = parser.parse_args()
831+
cli = CLIContext(parser.parse_args())
771832

772833
if os.environ.get("BUILD_IMAGE_DEBUG") in ("1", "yes"):
773834
cli.log_level = logging.DEBUG

0 commit comments

Comments
 (0)