|
|
@@ -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]:
|
|
|
@@ -30,6 +31,26 @@ class Configuration:
|
|
|
else:
|
|
|
return Path.home() / ".config" / "pc_backup"
|
|
|
|
|
|
+ def __str__(self) -> str:
|
|
|
+ ret = []
|
|
|
+ ret += ["secret_sources:"]
|
|
|
+ for s in self.secret_sources:
|
|
|
+ ret += [f" {s.name}: {s}"]
|
|
|
+
|
|
|
+ ret += ["data_sources:"]
|
|
|
+ for d in self.data_sources:
|
|
|
+ ret += [f" {d}"]
|
|
|
+
|
|
|
+ ret += ["borgmatic setups:"]
|
|
|
+ for f in self.borgmatic_d_path.iterdir():
|
|
|
+ ret += [f" {f.name}"]
|
|
|
+
|
|
|
+ ret += ["paths:"]
|
|
|
+ for p in ["borgmatic_d_path", "borgmatic_path", "history_file"]:
|
|
|
+ ret += [f" {p}: {getattr(self, p)}"]
|
|
|
+
|
|
|
+ return "\n".join(ret)
|
|
|
+
|
|
|
@classmethod
|
|
|
def read(cls, hostname: str, login: str, config_dir: Path):
|
|
|
# The configuration directory has to be organized like this :
|
|
|
@@ -98,54 +119,26 @@ class BorgmaticContainer:
|
|
|
"borgmatic_state:/root/.local/state/borgmatic",
|
|
|
"borgmatic_log:/root/.local/share/borgmatic",
|
|
|
]
|
|
|
- if config.ssh_auth_sock:
|
|
|
- volumes += [f"{config.ssh_auth_sock}:{config.ssh_auth_sock}:Z"]
|
|
|
|
|
|
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 config.secret_sources
|
|
|
- for a in ["--secret", f"{s.name},mode=0{s.mode:o}"]
|
|
|
- ]
|
|
|
-
|
|
|
- args = (
|
|
|
- [
|
|
|
- "podman",
|
|
|
- "run",
|
|
|
- "-h",
|
|
|
- self.hostname,
|
|
|
- "--detach",
|
|
|
- "--name",
|
|
|
- container_name,
|
|
|
- "-e",
|
|
|
- "SSH_AUTH_SOCK",
|
|
|
- "-e",
|
|
|
- "TZ=Europe/Paris",
|
|
|
- "-e",
|
|
|
- "SSH_KEY_NAME",
|
|
|
- "-e",
|
|
|
- f"HOST_LOGIN={self.login}",
|
|
|
- "--security-opt=label=disable",
|
|
|
- ]
|
|
|
- + volume_args
|
|
|
- + secrets_args
|
|
|
- + [self.image]
|
|
|
+ Podman.run(
|
|
|
+ self.image,
|
|
|
+ container_name,
|
|
|
+ hostname=self.hostname,
|
|
|
+ env=["TZ=Europe/Paris", f"HOST_LOGIN={self.login}"],
|
|
|
+ ssh_auth_sock=config.ssh_auth_sock,
|
|
|
+ volumes=volumes,
|
|
|
+ secrets=config.secret_sources,
|
|
|
)
|
|
|
- print(" ".join(args))
|
|
|
- subprocess.run(args)
|
|
|
|
|
|
def rm(self):
|
|
|
- subprocess.run(["podman", "rm", "-f", self.name])
|
|
|
+ Podman.rm(self.name)
|
|
|
|
|
|
- def exec(self, cmd: list[str], env_vars: list[str] = []):
|
|
|
- args = ["podman", "exec", "-ti"]
|
|
|
- args += [a for var in env_vars for a in ["-e", var]]
|
|
|
- subprocess.run(args + [self.name] + cmd)
|
|
|
+ def exec(self, cmd: list[str]):
|
|
|
+ Podman.exec(self.name, *cmd)
|
|
|
|
|
|
@staticmethod
|
|
|
def to_source_path(path: Path):
|