start.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. storage_box_env = credentials_path / "storage_box_env"
  22. ssh_config_path = Path.home() / ".ssh"
  23. volumes = [
  24. f"{config_path}:/etc/borgmatic.d/",
  25. f"{credentials_path}:/credentials/",
  26. f"{ssh_config_path}:/root/.ssh",
  27. "borg_config:/root/.config/borg",
  28. "borg_cache:/root/.cache/borg",
  29. "borgmatic_state:/root/.local/state/borgmatic",
  30. ]
  31. if ssh_auth_sock:
  32. volumes += [f"{ssh_auth_sock}:{ssh_auth_sock}:Z"]
  33. if is_windows:
  34. ssh_key_name = "theenglishway-windows"
  35. else:
  36. ssh_key_name = "theenglishway.pub"
  37. volumes += [
  38. f"{vol}:{to_source_path(vol)}:ro" for vol in data_sources
  39. ]
  40. volume_args = [a for vol in volumes for a in ["-v", vol]]
  41. image_name = "ghcr.io/borgmatic-collective/borgmatic"
  42. args = [
  43. "podman",
  44. "run",
  45. "-h",
  46. hostname,
  47. "--detach",
  48. "--name",
  49. container_name,
  50. "-e",
  51. "SSH_AUTH_SOCK",
  52. "-e",
  53. "TZ=Europe/Paris",
  54. "-e",
  55. f"SSH_KEY_NAME={ssh_key_name}",
  56. "--env-file",
  57. storage_box_env,
  58. "--security-opt=label=disable"
  59. ] + volume_args + [image_name]
  60. print(args)
  61. subprocess.run(args)
  62. if __name__ == "__main__":
  63. main()