start.py 3.0 KB

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