Quellcode durchsuchen

Read config paths in one pass and one place

jherve vor 1 Monat
Ursprung
Commit
63365d40d0
1 geänderte Dateien mit 40 neuen und 22 gelöschten Zeilen
  1. 40 22
      pc_backup/start.py

+ 40 - 22
pc_backup/start.py

@@ -120,6 +120,28 @@ class SecretFile(Secret):
         return cls(host_path=path, name=name, mode=0o0400)
 
 
+@dataclass
+class Configuration:
+    secret_sources: list[Secret]
+    data_sources: list[Path]
+    borgmatic_d_path: Path
+    borgmatic_path: Path
+    history_file: Path
+
+    @classmethod
+    def read(cls, hostname: str, login: str, config_dir: Path, data_path: Path):
+        secret_sources_file = config_dir / f"secret_sources_{hostname}_{login}"
+        data_sources_file = config_dir / f"data_sources_{hostname}_{login}"
+
+        return cls(
+            secret_sources=Secret.read_sources(secret_sources_file),
+            data_sources=read_data_sources(data_sources_file),
+            borgmatic_d_path=data_path / "borgmatic.d",
+            borgmatic_path=data_path / "borgmatic",
+            history_file=data_path / ".bash_history",
+        )
+
+
 @dataclass
 class BorgmaticContainer:
     hostname: str
@@ -127,19 +149,15 @@ class BorgmaticContainer:
     name: str
     image: str = "ghcr.io/borgmatic-collective/borgmatic"
 
-    def run(self, data_sources: list[Path], secret_sources: list[Secret]):
+    def run(self, config: Configuration):
         container_name = f"borgmatic_{self.login}"
         ssh_auth_sock = os.getenv("SSH_AUTH_SOCK")
 
-        data_path = Path.cwd() / "pc_backup" / "data"
-        config_d_path = data_path / "borgmatic.d"
-        config_path = data_path / "borgmatic"
-        history_file = data_path / ".bash_history"
-        history_file.touch()
+        config.history_file.touch()
         volumes = [
-            f"{config_d_path}:/etc/borgmatic.d/",
-            f"{config_path}:/etc/borgmatic/",
-            f"{history_file}:/root/.bash_history",
+            f"{config.borgmatic_d_path}:/etc/borgmatic.d/",
+            f"{config.borgmatic_path}:/etc/borgmatic/",
+            f"{config.history_file}:/root/.bash_history",
             "borg_ssh_dir:/root/.ssh",
             "borg_config:/root/.config/borg",
             "borg_cache:/root/.cache/borg",
@@ -148,13 +166,15 @@ class BorgmaticContainer:
         if ssh_auth_sock:
             volumes += [f"{ssh_auth_sock}:{ssh_auth_sock}:Z"]
 
-        volumes += [f"{vol}:{self.to_source_path(vol)}:ro" for vol in data_sources]
+        volumes += [
+            f"{vol}:{self.to_source_path(vol)}:ro" for vol in config.data_sources
+        ]
 
         volume_args = [a for vol in volumes for a in ["-v", vol]]
 
         secrets_args = [
             a
-            for s in secret_sources
+            for s in config.secret_sources
             for a in ["--secret", f"{s.name},mode=0{s.mode:o}"]
         ]
 
@@ -250,11 +270,10 @@ class CommandStart(Command):
         self,
         *,
         container: BorgmaticContainer,
-        data_sources: list[Path],
-        secret_sources: list[Secret],
+        config: Configuration,
         **kwargs,
     ):
-        container.run(data_sources, secret_sources)
+        container.run(config)
 
 
 class CommandRm(Command):
@@ -311,13 +330,11 @@ def main():
     login = os.getlogin()
     hostname = socket.gethostname()
 
-    secret_sources_file = Path(f"./secret_sources_{hostname}_{login}")
-    secret_sources = Secret.read_sources(secret_sources_file)
-
-    data_sources_file = Path(f"./data_sources_{hostname}_{login}")
-    data_sources = read_data_sources(data_sources_file)
+    config = Configuration.read(
+        hostname, login, Path("."), data_path=Path.cwd() / "pc_backup" / "data"
+    )
 
-    if not secret_sources:
+    if not config.secret_sources:
         print("no secret required ?")
 
     container = BorgmaticContainer.new(hostname, login)
@@ -325,8 +342,9 @@ def main():
     parser = CliArguments.new()
     command = CliArguments.read_command(parser)
     command.run(
-        secret_sources=secret_sources,
-        data_sources=data_sources,
+        config=config,
+        secret_sources=config.secret_sources,
+        data_sources=config.data_sources,
         container=container,
     )