Skip to content

Commit 585d344

Browse files
authored
Merge pull request #1004 from lisongmin/substitution-with-service-environment
Substitution with service environment
2 parents 3aa6d4d + 34f5268 commit 585d344

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-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

tests/unit/test_rec_subs.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# pylint: disable=protected-access
3+
4+
import unittest
5+
6+
from parameterized import parameterized
7+
8+
from podman_compose import rec_subs
9+
10+
11+
class TestRecSubs(unittest.TestCase):
12+
substitutions = [
13+
# dict with environment variables
14+
(
15+
"service's environment is low priority",
16+
{"environment": {"v1": "low priority", "actual-v1": "$v1"}},
17+
{"environment": {"v1": "low priority", "actual-v1": "high priority"}},
18+
),
19+
(
20+
"service's environment can be used in other values",
21+
{"environment": {"v100": "v1.0.0", "image": "abc:$v100"}},
22+
{"environment": {"v100": "v1.0.0", "image": "abc:v1.0.0"}},
23+
),
24+
(
25+
"Non-variable should not be substituted",
26+
{"environment": {"non_var": "$$v1", "vx": "$non_var"}, "image": "abc:$non_var"},
27+
{"environment": {"non_var": "$v1", "vx": "$v1"}, "image": "abc:$v1"},
28+
),
29+
# list
30+
(
31+
"Values in list are substituted",
32+
["$v1", "low priority"],
33+
["high priority", "low priority"],
34+
),
35+
# str
36+
(
37+
"Value with ${VARIABLE} format",
38+
"${v1}",
39+
"high priority",
40+
),
41+
(
42+
"Value with ${VARIABLE:-default} format",
43+
["${v1:-default}", "${empty:-default}", "${not_exits:-default}"],
44+
["high priority", "default", "default"],
45+
),
46+
(
47+
"Value with ${VARIABLE-default} format",
48+
["${v1-default}", "${empty-default}", "${not_exits-default}"],
49+
["high priority", "", "default"],
50+
),
51+
(
52+
"Value $$ means $",
53+
"$$v1",
54+
"$v1",
55+
),
56+
]
57+
58+
@parameterized.expand(substitutions)
59+
def test_rec_subs(self, desc, input, expected):
60+
sub_dict = {"v1": "high priority", "empty": ""}
61+
result = rec_subs(input, sub_dict)
62+
self.assertEqual(result, expected, msg=desc)

0 commit comments

Comments
 (0)