From 606570f3c37082e24fc522adc89eee4ef62fd62d Mon Sep 17 00:00:00 2001
From: Martin Mares <mj@ucw.cz>
Date: Tue, 9 Jul 2024 16:48:44 +0200
Subject: [PATCH] Implement env_path

---
 etc/container.toml.example |  4 ++++
 shipcat/config.py          |  9 ++++++++-
 shipcat/main.py            | 31 +++++++++++++++++--------------
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/etc/container.toml.example b/etc/container.toml.example
index 60403f0..47d659c 100644
--- a/etc/container.toml.example
+++ b/etc/container.toml.example
@@ -19,3 +19,7 @@ create_on_start = false
 
 # Extra options passed to "podman create" (default=none)
 podman_create_options = []
+
+# Environment file (relative to root_dir, default=none)
+# Contains lines of format "KEY=VALUE"
+# env_file = "environment"
diff --git a/shipcat/config.py b/shipcat/config.py
index 3cfe988..f4989c5 100644
--- a/shipcat/config.py
+++ b/shipcat/config.py
@@ -6,7 +6,7 @@ from grp import getgrnam
 from pathlib import Path
 import re
 import tomllib
-from typing import Set, List
+from typing import Set, List, Optional
 
 from .json_walker import Walker, WalkerError
 
@@ -62,6 +62,7 @@ class ContainerConfig:
     create_on_start: bool
     global_config: GlobalConfig
     podman_create_options: List[str]
+    env_path: Optional[Path]
 
     # Automatically generated
     pid_file: str
@@ -125,6 +126,12 @@ class ContainerConfig:
             self.create_on_start = w['create_on_start'].as_bool(self.global_config.default_create_on_start)
             self.podman_create_options = [o.as_str() for o in w['podman_create_options'].default_to([]).array_values()]
 
+            env_file = w['env_file'].as_optional_str()
+            if env_file is None:
+                self.env_path = None
+            else:
+                self.env_path = self.root_path / env_file
+
         self.pid_file = f'/run/shc/{self.name}.pid'
         self.user_name = self.name
         self.service_name = f'shc@{self.name}.service'
diff --git a/shipcat/main.py b/shipcat/main.py
index 9e315eb..1bbee19 100755
--- a/shipcat/main.py
+++ b/shipcat/main.py
@@ -220,20 +220,23 @@ def create_container(cc: ContainerConfig) -> None:
         ['podman', 'rm', '-if', cc.name]
     )
 
-    run_command(
-        [
-            'podman', 'create',
-            '--name', cc.name,
-            '--conmon-pidfile', cc.pid_file,
-            '--log-driver', 'journald',
-            '--hostname', cc.name,
-            '--volume', f'{cc.data_path}:/data',
-            '--network', 'static',
-            '--ip', ip,
-            '--subuidname', cc.user_name,
-            '--subgidname', cc.user_name,
-        ] + cc.podman_create_options + [cc.image]
-    )
+    cmd = [
+        'podman', 'create',
+        '--name', cc.name,
+        '--conmon-pidfile', cc.pid_file,
+        '--log-driver', 'journald',
+        '--hostname', cc.name,
+        '--volume', f'{cc.data_path}:/data',
+        '--network', 'static',
+        '--ip', ip,
+        '--subuidname', cc.user_name,
+        '--subgidname', cc.user_name,
+    ]
+    if cc.env_path is not None:
+        cmd.extend(['--env-file', str(cc.env_path)])
+    cmd.extend(cc.podman_create_options)
+    cmd.append(cc.image)
+    run_command(cmd)
 
 
 def cmd_start(args: argparse.Namespace) -> None:
-- 
GitLab