Skip to content

Commit 504ae0c

Browse files
committed
substitute mount and view variables at configuration time
1 parent 8dfc0cf commit 504ae0c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

stackinator/recipe.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ def environment_view_meta(self):
292292
view_meta = {}
293293
for _, env in self.environments.items():
294294
for view in env["views"]:
295+
# recipe authors can substitute the name of the view, the mount
296+
# and view path into environment variables using '$@key@' where
297+
# key is one of view_name, mount and view_path.
298+
substitutions = {
299+
"view_name": view["name"],
300+
"mount": self.mount,
301+
"view_path": view["config"]["root"]
302+
}
303+
fill = lambda s: re.sub(r"\$@(\w+)@", lambda m: substitutions.get(m.group(1), m.group(0)), s,)
304+
295305
ev_inputs = view["extra"]["env_vars"]
296306
env = envvars.EnvVarSet()
297307

@@ -302,6 +312,9 @@ def environment_view_meta(self):
302312

303313
for v in ev_inputs["set"]:
304314
((name, value),) = v.items()
315+
if value is not None:
316+
value = fill(value)
317+
305318
# insist that the only 'set' operation on prefix variables is to unset/reset them
306319
# this requires that users use append and prepend to build up the variables
307320
if envvars.is_list_var(name) and value is not None:
@@ -313,12 +326,16 @@ def environment_view_meta(self):
313326
env.set_scalar(name, value)
314327
for v in ev_inputs["prepend_path"]:
315328
((name, value),) = v.items()
329+
if value is not None:
330+
value = fill(value)
316331
if not envvars.is_list_var(name):
317332
raise RuntimeError(f"{name} in the {view['name']} view is not a known prefix path variable")
318333

319334
env.set_list(name, [value], envvars.EnvVarOp.APPEND)
320335
for v in ev_inputs["append_path"]:
321336
((name, value),) = v.items()
337+
if value is not None:
338+
value = fill(value)
322339
if not envvars.is_list_var(name):
323340
raise RuntimeError(f"{name} in the {view['name']} view is not a known prefix path variable")
324341

0 commit comments

Comments
 (0)