start.py 1.6 KB

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