start.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import os
  2. import subprocess
  3. import socket
  4. from pathlib import Path, PurePosixPath
  5. is_windows = os.name == "nt"
  6. data_sources = [
  7. Path.home() / "Documents" / "cv",
  8. Path.home() / "Documents" / "formations" / "5_jours"
  9. ]
  10. def to_source_path(path: Path):
  11. mount_base = PurePosixPath("/mnt") / "source"
  12. inner_path = PurePosixPath(path)
  13. with_drive = PurePosixPath(inner_path.parts[0].replace(":", "")).joinpath(*inner_path.parts[1:])
  14. return mount_base.joinpath(with_drive)
  15. def main():
  16. hostname = socket.gethostname()
  17. container_name = "borgmatic"
  18. ssh_auth_sock = os.getenv("SSH_AUTH_SOCK")
  19. config_path = Path.cwd() / "data" / "borgmatic.d"
  20. credentials_path = Path.cwd() / "data" / "credentials"
  21. ssh_config_path = Path.home() / ".ssh"
  22. volumes = [
  23. f"{config_path}:/etc/borgmatic.d/",
  24. f"{credentials_path}:/credentials/",
  25. f"{ssh_config_path}:/root/.ssh",
  26. "borg_config:/root/.config/borg",
  27. "borg_cache:/root/.cache/borg",
  28. "borgmatic_state:/root/.local/state/borgmatic",
  29. ]
  30. if ssh_auth_sock:
  31. volumes += [f"{ssh_auth_sock}:{ssh_auth_sock}:Z"]
  32. if is_windows:
  33. ssh_key_name = "theenglishway-windows"
  34. else:
  35. ssh_key_name = "theenglishway.pub"
  36. volumes += [
  37. f"{vol}:{to_source_path(vol)}:ro" for vol in data_sources
  38. ]
  39. volume_args = [a for vol in volumes for a in ["-v", vol]]
  40. image_name = "ghcr.io/borgmatic-collective/borgmatic"
  41. args = [
  42. "podman",
  43. "run",
  44. "-h",
  45. hostname,
  46. "--detach",
  47. "--name",
  48. container_name,
  49. "-e SSH_AUTH_SOCK",
  50. "-e TZ=Europe/Paris",
  51. "-e",
  52. f"SSH_KEY_NAME={ssh_key_name}",
  53. "--security-opt=label=disable"
  54. ] + volume_args + [image_name]
  55. print(args)
  56. subprocess.run(args)
  57. if __name__ == "__main__":
  58. main()