jherve 1 mesiac pred
rodič
commit
e7d86c79a2
1 zmenil súbory, kde vykonal 31 pridanie a 18 odobranie
  1. 31 18
      start.py

+ 31 - 18
start.py

@@ -20,17 +20,22 @@ class Secret:
     name: str
     mode: int
 
+    def create(self):
+        args = ["podman", "secret", "create", "--replace", self.name, self.host_path]
+        print(args)
+        subprocess.run(args)
+
     @classmethod
     def from_line(cls, line: str):
         path = Path(line).expanduser()
         return cls(host_path=path, name=path.name, mode=0o0400)
 
-
-def read_secret_sources(hostname: str, login: str) -> list[Secret]:
-    file = Path(f"./secret_sources_{hostname}_{login}")
-    with open(file) as f:
-        lines = f.readlines()
-        return [Secret.from_line(l.strip()) for l in lines]
+    @classmethod
+    def read_sources(cls, hostname: str, login: str) -> list["Secret"]:
+        file = Path(f"./secret_sources_{hostname}_{login}")
+        with open(file) as f:
+            lines = f.readlines()
+            return [cls.from_line(l.strip()) for l in lines]
 
 
 def to_source_path(path: Path):
@@ -40,11 +45,13 @@ def to_source_path(path: Path):
     return mount_base / with_drive.relative_to(with_drive.anchor)
 
 
-def main():
-    login = os.getlogin()
-    hostname = socket.gethostname()
+def create_secrets(secret_sources: list[Secret]):
+    for s in secret_sources:
+        s.create()
+
+
+def start_borgmatic_container(hostname: str, login: str, secret_sources: list[Secret]):
     data_sources = read_data_sources(hostname, login)
-    secret_sources = read_secret_sources(hostname, login)
     container_name = f"borgmatic_{login}"
     ssh_auth_sock = os.getenv("SSH_AUTH_SOCK")
 
@@ -72,14 +79,6 @@ def main():
 
     volume_args = [a for vol in volumes for a in ["-v", vol]]
 
-    if not secret_sources:
-        print("no secret required ?")
-
-    for s in secret_sources:
-        args = ["podman", "secret", "create", "--replace", s.name, s.host_path]
-        print(args)
-        subprocess.run(args)
-
     secrets_args = [a for s in secret_sources for a in ["--secret", f"{s.name},mode=0{s.mode:o}"]]
     image_name = "ghcr.io/borgmatic-collective/borgmatic"
 
@@ -104,5 +103,19 @@ def main():
     print(args)
     subprocess.run(args)
 
+
+def main():
+    login = os.getlogin()
+    hostname = socket.gethostname()
+
+    secret_sources = Secret.read_sources(hostname, login)
+
+    if not secret_sources:
+        print("no secret required ?")
+
+    create_secrets(secret_sources)
+    start_borgmatic_container(hostname, login, secret_sources)
+
+
 if __name__ == "__main__":
     main()