Skip to content

Commit 3a54630

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 c7cd000 commit 3a54630

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

hack/build-image

Lines changed: 63 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,60 @@ 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 or configparser.ConfigParser()
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, name in self._args_custom_vars():
630+
cvalue = cimg.get(name, None)
631+
if cvalue is None:
632+
continue
633+
if vtype is list:
634+
cvalue = " ".join(cvalue.strip().split())
635+
elif vtype is bool:
636+
cvalue = str(int(bool(cvalue)))
637+
args.append(f"--build-arg={build_arg}={cvalue}")
638+
if xargs := cimg.get("extra_arguments"):
639+
args.extend(shlex.split(xargs.strip()))
640+
# CLI extra args
641+
if self._cli.extra_build_arg:
642+
args.extend(self._cli.extra_build_arg)
643+
return args
644+
645+
def _args_custom_vars(self):
646+
return [
647+
("INSTALL_PACKAGES_FROM", str, "install_packages_from"),
648+
("SAMBA_VERSION_SUFFIX", str, "samba_version_suffix"),
649+
("SAMBACC_VERSION_SUFFIX", str, "sambacc_version_suffix"),
650+
("SAMBA_SPECIFICS", str, "samba_specifics"),
651+
("INSTALL_CUSTOM_REPOS", list, "custom_repos"),
652+
("PACKAGE_SELECTION", str, "package_selection"),
653+
("CEPH_FROM_CUSTOM", bool, "ceph_from_custom"),
654+
]
655+
656+
603657
def main():
604658
parser = argparse.ArgumentParser()
605659
parser.add_argument(
@@ -719,6 +773,12 @@ def main():
719773
" without the repo base"
720774
),
721775
)
776+
parser.add_argument(
777+
"--customizations",
778+
"-C",
779+
type=pathlib.Path,
780+
help="",
781+
)
722782
parser.add_argument(
723783
"--distro-qualified",
724784
action=argparse.BooleanOptionalAction,
@@ -767,7 +827,7 @@ def main():
767827
" for a given FQIN. Requires FQIN to already exist locally."
768828
),
769829
)
770-
cli = parser.parse_args()
830+
cli = CLIContext(parser.parse_args())
771831

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

0 commit comments

Comments
 (0)