Skip to content

Commit b5eaf31

Browse files
lisongminp12tic
authored andcommitted
Support variable substitution with service's environment
This commit introduces the ability to substitute environment variables within the 'environment' section of the service definition. This allows for more dynamic configuration of services. Signed-off-by: Songmin Li <[email protected]>
1 parent dbbd695 commit b5eaf31

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ability to substitute variables with the environment of the service.

podman_compose.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ def rec_subs(value, subs_dict):
264264
do bash-like substitution in value and if list of dictionary do that recursively
265265
"""
266266
if is_dict(value):
267+
if 'environment' in value and is_dict(value['environment']):
268+
# Load service's environment variables
269+
subs_dict = subs_dict.copy()
270+
svc_envs = {k: v for k, v in value['environment'].items() if k not in subs_dict}
271+
# we need to add `svc_envs` to the `subs_dict` so that it can evaluate the
272+
# service environment that reference to another service environment.
273+
subs_dict.update(svc_envs)
274+
svc_envs = rec_subs(svc_envs, subs_dict)
275+
subs_dict.update(svc_envs)
276+
267277
value = {k: rec_subs(v, subs_dict) for k, v in value.items()}
268278
elif is_str(value):
269279

@@ -1823,6 +1833,11 @@ def _parse_compose_file(self):
18231833
"COMPOSE_FILE": pathsep.join(relative_files),
18241834
"COMPOSE_PATH_SEPARATOR": pathsep,
18251835
})
1836+
1837+
if args and 'env' in args and args.env:
1838+
env_vars = norm_as_dict(args.env)
1839+
self.environ.update(env_vars)
1840+
18261841
compose = {}
18271842
# Iterate over files primitively to allow appending to files in-loop
18281843
files_iter = iter(files)
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
version: '3'
1+
version: "3"
22

33
services:
44
env-test:
55
image: busybox
66
command: sh -c "export | grep ZZ"
77
environment:
8-
- ZZVAR1=myval1
9-
8+
ZZVAR1: myval1
9+
ZZVAR2: 2-$ZZVAR1
10+
ZZVAR3: 3-$ZZVAR2

0 commit comments

Comments
 (0)