start-colima.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from __future__ import annotations
  2. import os
  3. import platform
  4. import subprocess
  5. from collections.abc import Sequence
  6. def main(argv: Sequence[str] | None = None) -> int:
  7. macos_version = platform.mac_ver()[0]
  8. macos_major_version = int(macos_version.split(".")[0])
  9. if macos_major_version < 14:
  10. raise SystemExit(f"macos >= 14 is required to use colima, found {macos_version}")
  11. cpus = os.cpu_count()
  12. if cpus is None:
  13. raise SystemExit("failed to determine cpu count")
  14. # SC_PAGE_SIZE is POSIX 2008
  15. # SC_PHYS_PAGES is a linux addition but also supported by more recent MacOS versions
  16. SC_PAGE_SIZE = os.sysconf("SC_PAGE_SIZE")
  17. SC_PHYS_PAGES = os.sysconf("SC_PHYS_PAGES")
  18. if SC_PAGE_SIZE == -1 or SC_PHYS_PAGES == -1:
  19. raise SystemExit("failed to determine memsize_bytes")
  20. memsize_bytes = os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")
  21. args = [
  22. "--cpu",
  23. f"{cpus//2}",
  24. "--memory",
  25. f"{memsize_bytes//(2*1024**3)}",
  26. ]
  27. if platform.machine() == "arm64":
  28. args = [*args, "--vm-type=vz", "--vz-rosetta", "--mount-type=virtiofs"]
  29. HOME = os.path.expanduser("~")
  30. rc = subprocess.call(
  31. (
  32. f"{HOME}/.local/share/sentry-devenv/bin/colima",
  33. "start",
  34. f"--mount=/var/folders:w,/private/tmp/colima:w,{HOME}:r",
  35. *args,
  36. )
  37. )
  38. if rc != 0:
  39. return rc
  40. return subprocess.call(("docker", "context", "use", "colima"))
  41. if __name__ == "__main__":
  42. raise SystemExit(main())