From ef8b7a4da706b41f310f732f2652428d6648fd7b Mon Sep 17 00:00:00 2001 From: Andy Piper Date: Mon, 22 Nov 2021 20:13:42 -0500 Subject: app.py: Add `--variable` param for user-supplied jinja2 variables The `--variable` command line parameter can be used to supply values for Jinja2 variables in vmdb2 build specification YAML files using the format `name=value`. This is useful for builds which need to be parameterized, e.g they use remote services in different AWS regions and/or accounts. --- vmdb/app.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/vmdb/app.py b/vmdb/app.py index 83a8810..65bc568 100644 --- a/vmdb/app.py +++ b/vmdb/app.py @@ -68,6 +68,14 @@ class Vmdb2: p.add_argument("-v", "--verbose", action="store_true") p.add_argument("--log") p.add_argument("--version", action="store_true") + p.add_argument( + "--variable", + action="append", + default=[], + help="Specify the value for a Jinja2 template variable in the " + 'build spec file in the format "name=value". This parameter can ' + "be specified multiple times to define multiple variables.", + ) p.add_argument("specfile") return p.parse_args() @@ -88,12 +96,27 @@ class Vmdb2: logging.info(f"Starting vmdb2 version {self._version}") def template_vars_from_args(self, args): - return { + vars = { "image": args.image, "output": args.output, "rootfs_tarball": args.rootfs_tarball, } + for var_spec in args.variable: + name, value = var_spec.split("=") + if name in vars: + raise RuntimeError(f'Duplicate definition of variable "{name}"') + + # Remove quoes from the value if required + if value.startswith("'"): + value = value.strip("'") + elif value.startswith('"'): + value = value.strip('"') + + vars[name] = value + + return vars + class Command: def run(self): -- cgit v1.2.1