Quellcode durchsuchen

Create a podman module

jherve vor 1 Monat
Ursprung
Commit
af721f6515
3 geänderte Dateien mit 37 neuen und 15 gelöschten Zeilen
  1. 5 6
      pc_backup/container.py
  2. 25 0
      pc_backup/podman.py
  3. 7 9
      pc_backup/secret.py

+ 5 - 6
pc_backup/container.py

@@ -5,6 +5,7 @@ from dataclasses import dataclass
 
 from pc_backup.secret import Secret
 from pc_backup.env import is_windows
+from pc_backup.podman import Podman
 
 
 def read_data_sources(file: Path) -> list[Path]:
@@ -135,8 +136,6 @@ class BorgmaticContainer:
 
         args = (
             [
-                "podman",
-                "run",
                 "-h",
                 self.hostname,
                 "--detach",
@@ -156,16 +155,16 @@ class BorgmaticContainer:
             + secrets_args
             + [self.image]
         )
-        print(" ".join(args))
-        subprocess.run(args)
+        Podman.run(args)
 
     def rm(self):
-        subprocess.run(["podman", "rm", "-f", self.name])
+        Podman.rm(["-f", self.name])
 
     def exec(self, cmd: list[str], env_vars: list[str] = []):
-        args = ["podman", "exec", "-ti"]
+        args = ["-ti"]
         args += [a for var in env_vars for a in ["-e", var]]
         subprocess.run(args + [self.name] + cmd)
+        Podman.exec(args + [self.name] + cmd)
 
     @staticmethod
     def to_source_path(path: Path):

+ 25 - 0
pc_backup/podman.py

@@ -0,0 +1,25 @@
+import subprocess
+
+
+class Podman:
+    @classmethod
+    def run(cls, args: list):
+        cls._call(["run"] + args)
+
+    @classmethod
+    def rm(cls, args: list):
+        cls._call(["rm"] + args)
+
+    @classmethod
+    def exec(cls, args: list):
+        cls._call(["exec"] + args)
+
+    @classmethod
+    def secret_create(cls, args: list, **kwargs):
+        cls._call(["secret", "create"] + args, **kwargs)
+
+    @classmethod
+    def _call(cls, args: list, **kwargs):
+        args = ["podman"] + args
+        print(f"Executing `{" ".join(args)}`")
+        subprocess.run(args, **kwargs)

+ 7 - 9
pc_backup/secret.py

@@ -3,6 +3,7 @@ from pathlib import Path
 from dataclasses import dataclass
 
 from pc_backup.keepass import KeePass
+from pc_backup.podman import Podman
 
 
 @dataclass
@@ -41,9 +42,8 @@ class SecretKeepassAttachment(Secret):
 
     def create(self, keepass: KeePass):
         value = keepass.read_entry_attachment(self.key, self.attachment)
-        args = ["podman", "secret", "create", "--replace", self.name, "-"]
-        print(args)
-        subprocess.run(args, input=value.encode())
+        args = ["--replace", self.name, "-"]
+        Podman.secret_create(args, input=value.encode())
 
     @classmethod
     def from_line(cls, name: str, key: str, attachment: str):
@@ -57,9 +57,8 @@ class SecretKeepassAttribute(Secret):
 
     def create(self, keepass: KeePass):
         value = keepass.read_entry_attribute(self.key, self.attribute)
-        args = ["podman", "secret", "create", "--replace", self.name, "-"]
-        print(args)
-        subprocess.run(args, input=value.encode())
+        args = ["--replace", self.name, "-"]
+        Podman.secret_create(args, input=value.encode())
 
     @classmethod
     def from_line(cls, name: str, key: str, attribute: str):
@@ -71,9 +70,8 @@ class SecretFile(Secret):
     host_path: Path
 
     def create(self, keepass: KeePass):
-        args = ["podman", "secret", "create", "--replace", self.name, self.host_path]
-        print(args)
-        subprocess.run(args)
+        args = ["--replace", self.name, self.host_path]
+        Podman.secret_create(args)
 
     @classmethod
     def from_line(cls, name: str, path: str):