diff --git a/etc/container.toml.example b/etc/container.toml.example index ef1c321a2a40135ee24a08ed521d230f9ef734f4..72b9e1eee32dbc275235c16caacc7b7d7e893d60 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 6d092968265f327ecc01cb901325e843a3165d44..5c620700e07d3a1e400aa3caa1b7d1752755cc58 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 f756e64696e357d86f3499d2fd41bb8c55f37380..039353584c922fa1242beb839358c4d5d42be67d 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 7c1f2f52cb7c3e27ef5cc4f03ed657b6779170b3..9d17d343bef0bb82efc8652526746e6caa3ef8f2 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])