From f214fd02c0bba863e46691f1e98d23d657754439 Mon Sep 17 00:00:00 2001 From: Ricardo Garcia Silva Date: Fri, 6 Dec 2024 17:41:05 +0000 Subject: [PATCH] Use json.dumps to render deployment params that are to be put as environment variables --- deployments/deploy.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/deployments/deploy.py b/deployments/deploy.py index 58d0803..85a5ec0 100644 --- a/deployments/deploy.py +++ b/deployments/deploy.py @@ -348,15 +348,13 @@ def handle(self) -> None: render_context = dataclasses.asdict(self.config) render_kwargs = {} # conf keys that have _env_ in their name are going to be put as env values - # inside the container and will be consumed by pydantic, so we dump them as - # JSON in order to ensure correct handling of parameters that represent - # collections, for example cors origins, which is a list of strings + # inside the container and will be consumed by pydantic. If one of these is + # a list we dump it as JSON in order to ensure correct handling of + # parameters that represent collections, for example cors origins, which + # is a list of strings for k, v in render_context.items(): - if "_env_" in k: - try: - render_kwargs[k] = json.dumps(v) - except TypeError: - render_kwargs[k] = json.dumps(str(v)) + if "env_" in k and isinstance(v, list): + render_kwargs[k] = json.dumps(v) rendered = compose_template.substitute(render_context, **render_kwargs) target_path = Path(self.config.deployment_root / "compose.production.yaml") @@ -381,8 +379,9 @@ def _run_compose_command(self, suffix: str) -> subprocess.CompletedProcess: self.config.deployment_root / "compose.production.yaml", ] compose_files_fragment = " ".join(f"-f {p}" for p in compose_files) + docker_compose_command = f"docker compose {compose_files_fragment} {suffix}" return subprocess.run( - shlex.split(f"docker compose {compose_files_fragment} {suffix}"), + shlex.split(docker_compose_command), cwd=self.config.deployment_root, env=self.environment or os.environ, check=True,