start.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import os
  2. import subprocess
  3. import socket
  4. from pathlib import Path, PurePosixPath
  5. from dataclasses import dataclass
  6. is_windows = os.name == "nt"
  7. def read_data_sources(hostname: str, login: str) -> list[Path]:
  8. file = Path(f"./data_sources_{hostname}_{login}")
  9. with open(file) as f:
  10. paths = f.readlines()
  11. return [Path(p_str.strip()).expanduser() for p_str in paths]
  12. @dataclass
  13. class Secret:
  14. host_path: Path
  15. name: str
  16. mode: int
  17. def create(self):
  18. args = ["podman", "secret", "create", "--replace", self.name, self.host_path]
  19. print(args)
  20. subprocess.run(args)
  21. @classmethod
  22. def from_line(cls, line: str):
  23. path = Path(line).expanduser()
  24. return cls(host_path=path, name=path.name, mode=0o0400)
  25. @classmethod
  26. def read_sources(cls, hostname: str, login: str) -> list["Secret"]:
  27. file = Path(f"./secret_sources_{hostname}_{login}")
  28. with open(file) as f:
  29. lines = f.readlines()
  30. return [cls.from_line(l.strip()) for l in lines]
  31. def to_source_path(path: Path):
  32. mount_base = PurePosixPath("/mnt") / "source"
  33. inner_path = PurePosixPath(path)
  34. with_drive = PurePosixPath(inner_path.parts[0].replace(":", "")).joinpath(*inner_path.parts[1:])
  35. return mount_base / with_drive.relative_to(with_drive.anchor)
  36. def create_secrets(secret_sources: list[Secret]):
  37. for s in secret_sources:
  38. s.create()
  39. def start_borgmatic_container(hostname: str, login: str, secret_sources: list[Secret]):
  40. data_sources = read_data_sources(hostname, login)
  41. container_name = f"borgmatic_{login}"
  42. ssh_auth_sock = os.getenv("SSH_AUTH_SOCK")
  43. data_path = Path.cwd() / "data"
  44. config_d_path = data_path / "borgmatic.d"
  45. config_path = data_path / "borgmatic"
  46. history_file = data_path / ".bash_history"
  47. history_file.touch()
  48. ssh_config_path = Path.home() / ".ssh"
  49. volumes = [
  50. f"{config_d_path}:/etc/borgmatic.d/",
  51. f"{config_path}:/etc/borgmatic/",
  52. f"{ssh_config_path}:/root/.ssh",
  53. f"{history_file}:/root/.bash_history",
  54. "borg_config:/root/.config/borg",
  55. "borg_cache:/root/.cache/borg",
  56. "borgmatic_state:/root/.local/state/borgmatic",
  57. ]
  58. if ssh_auth_sock:
  59. volumes += [f"{ssh_auth_sock}:{ssh_auth_sock}:Z"]
  60. volumes += [
  61. f"{vol}:{to_source_path(vol)}:ro" for vol in data_sources
  62. ]
  63. volume_args = [a for vol in volumes for a in ["-v", vol]]
  64. secrets_args = [a for s in secret_sources for a in ["--secret", f"{s.name},mode=0{s.mode:o}"]]
  65. image_name = "ghcr.io/borgmatic-collective/borgmatic"
  66. args = [
  67. "podman",
  68. "run",
  69. "-h",
  70. hostname,
  71. "--detach",
  72. "--name",
  73. container_name,
  74. "-e",
  75. "SSH_AUTH_SOCK",
  76. "-e",
  77. "TZ=Europe/Paris",
  78. "-e",
  79. "SSH_KEY_NAME",
  80. "-e",
  81. f"HOST_LOGIN={login}",
  82. "--security-opt=label=disable"
  83. ] + volume_args + secrets_args + [image_name]
  84. print(args)
  85. subprocess.run(args)
  86. def main():
  87. login = os.getlogin()
  88. hostname = socket.gethostname()
  89. secret_sources = Secret.read_sources(hostname, login)
  90. if not secret_sources:
  91. print("no secret required ?")
  92. create_secrets(secret_sources)
  93. start_borgmatic_container(hostname, login, secret_sources)
  94. if __name__ == "__main__":
  95. main()