Browse Source

devenv: colima followups (#53901)

Joshua Li 1 year ago
parent
commit
d08bb88bcb

+ 39 - 0
scripts/start-colima.py

@@ -0,0 +1,39 @@
+from __future__ import annotations
+
+import os
+import platform
+import subprocess
+import sys
+from typing import Sequence
+
+
+def main(argv: Sequence[str] | None = None) -> int:
+    # platform.processor() changed at some point between these:
+    # 11.2.3: arm
+    # 12.3.1: arm64
+    APPLE_ARM64 = sys.platform == "darwin" and platform.processor() in {"arm", "arm64"}
+
+    cpus = int(subprocess.run(("sysctl", "-n", "hw.ncpu"), check=True, capture_output=True).stdout)
+    memsize_bytes = int(
+        subprocess.run(("sysctl", "-n", "hw.memsize"), check=True, capture_output=True).stdout
+    )
+    args = [
+        "--cpu",
+        f"{cpus//2}",
+        "--memory",
+        f"{memsize_bytes//(2*1024**3)}",
+    ]
+    if APPLE_ARM64:
+        args = [*args, "--vm-type=vz", "--vz-rosetta", "--mount-type=virtiofs"]
+    return subprocess.call(
+        (
+            "colima",
+            "start",
+            f"--mount=/var/folders:w,/private/tmp/colima:w,{os.path.expanduser('~')}:r",
+            *args,
+        )
+    )
+
+
+if __name__ == "__main__":
+    raise SystemExit(main())

+ 29 - 4
scripts/use-colima.sh

@@ -1,11 +1,23 @@
 #!/bin/bash
 
-echo "Stopping Docker.app. You may ignore a 'process terminated unexpectedly' dialog."
+if [[ "$(sysctl -n machdep.cpu.brand_string)" != Intel* ]]; then
+    case "$(sw_vers -productVersion)" in
+        *12.*|*13.0*|*13.1*|*13.2*)
+            echo "Your ARM Mac is on a version incompatible with colima."
+            echo "Use Docker Desktop for now until you upgrade to at least 13.3."
+            exit 1
+            ;;
+    esac
+fi
 
+echo "Copying your postgres volume for use with colima. Will take a few minutes."
+tmpdir=$(mktemp -d)
+docker context use desktop-linux
+docker run --rm -v sentry_postgres:/from -v "${tmpdir}:/to" alpine ash -c "cd /from ; cp -a . /to" || { echo "You need to start Docker Desktop."; exit 1; }
+
+echo "Stopping Docker.app. If a 'process terminated unexpectedly' dialog appears, dismiss it."
 osascript - <<'EOF' || exit
-tell application "Docker"
-  if it is running then quit it
-end tell
+quit application "Docker"
 EOF
 
 # We aren't uninstalling for now - this makes rolling back to docker desktop faster.
@@ -40,3 +52,16 @@ EOF
 echo "Installing colima."
 brew install colima
 brew link colima
+
+echo "Starting colima."
+python3 -uS scripts/start-colima.py
+
+# The context will be colima, we just want to double make sure.
+docker context use colima
+echo "Recreating your postgres volume for use with colima. May take a few minutes."
+docker volume create --name sentry_postgres
+docker run --rm -v "${tmpdir}:/from" -v sentry_postgres:/to alpine ash -c "cd /from ; cp -a . /to"
+rm -rf "$tmpdir"
+
+echo "-----------------------------------------------"
+echo "All done. Start devservices at your discretion."

+ 7 - 1
scripts/use-docker-desktop.sh

@@ -4,7 +4,7 @@ set -e
 
 colima stop
 
-echo "Using docker cli from cask."
+echo "Using docker cli from cask. You may be prompted for your password."
 # brew --prefix doesn't seem to apply here - it's just /usr/local
 sudo ln -svf /Applications/Docker.app/Contents/Resources/bin/docker "/usr/local/bin/docker"
 
@@ -25,3 +25,9 @@ EOF
 
 echo "Unlinking colima."
 brew unlink colima
+
+echo "Starting Docker."
+open -a /Applications/Docker.app --args --unattended
+
+echo "-----------------------------------------------"
+echo "All done. Start devservices at your discretion."

+ 3 - 22
src/sentry/runner/commands/devservices.py

@@ -45,30 +45,11 @@ def get_docker_client() -> Generator[docker.DockerClient, None, None]:
             if DARWIN:
                 if USE_COLIMA:
                     click.echo("Attempting to start colima...")
-                    cpus = int(
-                        subprocess.run(
-                            ("sysctl", "-n", "hw.ncpu"), check=True, capture_output=True
-                        ).stdout
-                    )
-                    memsize_bytes = int(
-                        subprocess.run(
-                            ("sysctl", "-n", "hw.memsize"), check=True, capture_output=True
-                        ).stdout
-                    )
-                    args = [
-                        "--cpu",
-                        f"{cpus//2}",
-                        "--memory",
-                        f"{memsize_bytes//(2*1024**3)}",
-                    ]
-                    if APPLE_ARM64:
-                        args = [*args, "--vm-type=vz", "--vz-rosetta"]
                     subprocess.check_call(
                         (
-                            "colima",
-                            "start",
-                            f"--mount=/private/tmp/colima:w,{os.path.expanduser('~')}:r",
-                            *args,
+                            "python3",
+                            "-uS",
+                            f"{os.path.dirname(__file__)}/../../../../scripts/start-colima.py",
                         )
                     )
                 else: