From 04cf30b92aed3bc0b7a9ee327b377a8ba4ff08a3 Mon Sep 17 00:00:00 2001
From: Martin Mares <mj@ucw.cz>
Date: Thu, 6 Jun 2024 17:33:11 +0200
Subject: [PATCH] Implement create_on_start

---
 etc/container.toml.example | 12 +++++++++++-
 etc/shipcat.toml           | 12 +++++++++---
 shipcat/config.py          | 13 ++++++++++---
 shipcat/main.py            |  3 +++
 4 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/etc/container.toml.example b/etc/container.toml.example
index ef1c321..72b9e1e 100644
--- a/etc/container.toml.example
+++ b/etc/container.toml.example
@@ -1,4 +1,14 @@
-root_dir = "/tmp/root"
+# Example configuration of a container
+
+# Image to pull
 image = "registry.ks.matfyz.cz/gimli/web-checker"
+
+# Root directory where data of the container reside
+root_dir = "/tmp/root"
+
+# Users and groups allowed to operate the container
 allowed_users = ['root', 'mj']
 allowed_groups = []
+
+# Do we re-create the container on every start?
+create_on_start = false
diff --git a/etc/shipcat.toml b/etc/shipcat.toml
index 6d09296..5c62070 100644
--- a/etc/shipcat.toml
+++ b/etc/shipcat.toml
@@ -3,8 +3,14 @@
 # Where to find container configuration files
 container_config_dir = "/etc/shipcat/containers"
 
-# Where to find container roots (can be overridden by container's config)
-container_root_dir = "/aux/containers"
-
 # Overall verbosity
 verbosity = 0
+
+# Default values of container configuration
+[defaults]
+
+# Where to find container root: root_dir/container_name
+root_dir = "/aux/containers"
+
+# Do we re-create the container on every start?
+create_on_start = false
diff --git a/shipcat/config.py b/shipcat/config.py
index f756e64..0393535 100644
--- a/shipcat/config.py
+++ b/shipcat/config.py
@@ -17,8 +17,9 @@ class ConfigError(RuntimeError):
 
 class GlobalConfig:
     container_config_path: Path
-    container_root_path: Path
     verbosity: int
+    default_root_path: Path
+    default_create_on_start: bool
 
     @classmethod
     def load(self, filename: str) -> 'GlobalConfig':
@@ -41,9 +42,12 @@ class GlobalConfig:
     def parse(self, walker: Walker) -> None:
         with walker.enter_object() as w:
             self.container_config_path = Path(w['container_config_dir'].as_str())
-            self.container_root_path = Path(w['container_root_dir'].as_str())
             self.verbosity = w['verbosity'].as_int(0)
 
+            with w['defaults'].enter_object() as d:
+                self.default_root_path = Path(d['root_dir'].as_str())
+                self.default_create_on_start = d['create_on_start'].as_bool(False)
+
 
 class ContainerConfig:
     name: str
@@ -52,6 +56,7 @@ class ContainerConfig:
     image: str
     allowed_users: Set[int]
     allowed_groups: Set[int]
+    create_on_start: bool
     global_config: GlobalConfig
 
     # Automatically generated
@@ -88,7 +93,7 @@ class ContainerConfig:
             if rd.is_present():
                 self.root_path = Path(rd.as_str())
             else:
-                self.root_path = self.global_config.container_root_path / self.name
+                self.root_path = self.global_config.default_root_path / self.name
             self.data_path = self.root_path / 'data'
 
             self.root_dir = w['root_dir'].as_str()
@@ -112,5 +117,7 @@ class ContainerConfig:
                     wu.raise_error(f'Unknown group {name}')
                 self.allowed_groups.add(grp.gr_gid)
 
+            self.create_on_start = w['create_on_start'].as_bool(self.global_config.default_create_on_start)
+
         self.pid_file = f'/run/shc/{self.name}.pid'
         self.user_name = self.name
diff --git a/shipcat/main.py b/shipcat/main.py
index 7c1f2f5..9d17d34 100755
--- a/shipcat/main.py
+++ b/shipcat/main.py
@@ -312,6 +312,9 @@ def main_service_start():
     pid_file = f'/run/shc/{cc.name}.pid'
     Path(pid_file).unlink(missing_ok=True)
 
+    if cc.create_on_start:
+        create_container(cc)
+
     run_command(['podman', 'start', cc.name])
 
 
-- 
GitLab