start-colima.py 1.5 KB

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