From 532940c56ebe855996bf4fd14dbc16527ba501db Mon Sep 17 00:00:00 2001
From: Martin Mares <mj@ucw.cz>
Date: Tue, 9 Jul 2024 16:33:22 +0200
Subject: [PATCH] Pulling of images of now configurable and overridable in both
 ways

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

diff --git a/etc/container.toml.example b/etc/container.toml.example
index fb7b590..41bbf5e 100644
--- a/etc/container.toml.example
+++ b/etc/container.toml.example
@@ -1,8 +1,12 @@
 # Example configuration of a container
+# Defaults for some options can be specified in shipcat.toml.
 
-# Image to pull
+# Container image
 image = "registry.ks.matfyz.cz/gimli/web-checker"
 
+# Pull the image automatically when creating the container
+pull_image = true
+
 # Root directory where data of the container reside
 root_dir = "/tmp/root"
 
diff --git a/etc/shipcat.toml b/etc/shipcat.toml
index 5c62070..b63b314 100644
--- a/etc/shipcat.toml
+++ b/etc/shipcat.toml
@@ -12,5 +12,8 @@ verbosity = 0
 # Where to find container root: root_dir/container_name
 root_dir = "/aux/containers"
 
+# Pull the image automatically when creating the container
+pull_image = true
+
 # Do we re-create the container on every start?
 create_on_start = false
diff --git a/shipcat/config.py b/shipcat/config.py
index 4484dd7..3cfe988 100644
--- a/shipcat/config.py
+++ b/shipcat/config.py
@@ -20,6 +20,7 @@ class GlobalConfig:
     verbosity: int
     default_root_path: Path
     default_create_on_start: bool
+    default_pull_image: bool
 
     @classmethod
     def load(self, filename: str) -> 'GlobalConfig':
@@ -47,6 +48,7 @@ class GlobalConfig:
             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)
+                self.default_pull_image = d['pull_image'].as_bool(True)
 
 
 class ContainerConfig:
@@ -54,6 +56,7 @@ class ContainerConfig:
     root_path: Path
     data_path: Path
     image: str
+    pull_image: bool
     allowed_users: Set[int]
     allowed_groups: Set[int]
     create_on_start: bool
@@ -99,6 +102,7 @@ class ContainerConfig:
             self.data_path = self.root_path / 'data'
 
             self.image = w['image'].as_str()
+            self.pull_image = w['pull_image'].as_bool(self.global_config.default_pull_image)
 
             self.allowed_users = set()
             for wu in w['allowed_users'].default_to([]).array_values():
diff --git a/shipcat/main.py b/shipcat/main.py
index 1f46a84..9e315eb 100755
--- a/shipcat/main.py
+++ b/shipcat/main.py
@@ -192,7 +192,12 @@ def service_action(cc: ContainerConfig, action: str) -> None:
 def cmd_update(args: argparse.Namespace) -> None:
     cc = setup_container(args, False)
 
-    if not args.no_pull:
+    if args.pull is not None:
+        should_pull = args.pull
+    else:
+        should_pull = cc.pull_image
+
+    if should_pull:
         progress('Pulling new image')
         run_command(
             ['podman', 'pull', cc.image]
@@ -441,7 +446,7 @@ def main() -> None:
 
     update_parser = subparsers.add_parser('update', help='update a container from an image', description='Update a container from an image and start it.')
     update_parser.add_argument('name', help='name of the container')
-    update_parser.add_argument('--no-pull', default=False, action='store_true', help='do not pull a new image')
+    update_parser.add_argument('--pull', action=argparse.BooleanOptionalAction, help='pull a new image (use --pull or --no-pull to override behavior specified by container config) ')
 
     args = parser.parse_args()
     get_caller_credentials()
-- 
GitLab