|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +# Automated setup script for Podman on macOS |
| 7 | +# Handles Podman machine initialization, cgroup delegation, and environment setup |
| 8 | + |
| 9 | +set -euo pipefail |
| 10 | + |
| 11 | +MACHINE_NAME="${PODMAN_MACHINE_NAME:-openshell}" |
| 12 | +MEMORY="${PODMAN_MEMORY:-8192}" |
| 13 | +CPUS="${PODMAN_CPUS:-4}" |
| 14 | + |
| 15 | +echo "=== OpenShell Podman Setup for macOS ===" |
| 16 | +echo "" |
| 17 | + |
| 18 | +# Check if Podman is installed |
| 19 | +if ! command -v podman &>/dev/null; then |
| 20 | + echo "❌ Podman is not installed." |
| 21 | + echo "" |
| 22 | + echo "Install with: brew install podman" |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +echo "✓ Podman is installed ($(podman --version))" |
| 27 | + |
| 28 | +# Check if machine already exists |
| 29 | +if podman machine list --format '{{.Name}}' | grep -q "^${MACHINE_NAME}$"; then |
| 30 | + echo "" |
| 31 | + echo "⚠️ Podman machine '${MACHINE_NAME}' already exists." |
| 32 | + read -p "Do you want to recreate it? This will delete the existing machine. (y/N) " -n 1 -r |
| 33 | + echo |
| 34 | + if [[ $REPLY =~ ^[Yy]$ ]]; then |
| 35 | + echo "Stopping and removing existing machine..." |
| 36 | + podman machine stop "${MACHINE_NAME}" 2>/dev/null || true |
| 37 | + podman machine rm -f "${MACHINE_NAME}" |
| 38 | + else |
| 39 | + echo "Using existing machine. Verifying configuration..." |
| 40 | + EXISTING=true |
| 41 | + fi |
| 42 | +fi |
| 43 | + |
| 44 | +# Initialize machine if needed |
| 45 | +if [[ "${EXISTING:-false}" != "true" ]]; then |
| 46 | + echo "" |
| 47 | + echo "Initializing Podman machine '${MACHINE_NAME}' with ${MEMORY}MB RAM and ${CPUS} CPUs..." |
| 48 | + podman machine init "${MACHINE_NAME}" --memory "${MEMORY}" --cpus "${CPUS}" |
| 49 | +fi |
| 50 | + |
| 51 | +# Stop any other running machines |
| 52 | +echo "" |
| 53 | +echo "Stopping other Podman machines (only one can run at a time)..." |
| 54 | +for machine in $(podman machine list --format '{{.Name}}' --noheading); do |
| 55 | + if [[ "$machine" != "${MACHINE_NAME}" ]]; then |
| 56 | + if podman machine list --format '{{.Name}} {{.Running}}' | grep "^${machine} " | grep -q "true"; then |
| 57 | + echo " Stopping ${machine}..." |
| 58 | + podman machine stop "${machine}" 2>/dev/null || true |
| 59 | + fi |
| 60 | + fi |
| 61 | +done |
| 62 | + |
| 63 | +# Start the machine |
| 64 | +echo "" |
| 65 | +echo "Starting Podman machine '${MACHINE_NAME}'..." |
| 66 | +if ! podman machine list --format '{{.Name}} {{.Running}}' | grep "^${MACHINE_NAME} " | grep -q "true"; then |
| 67 | + podman machine start "${MACHINE_NAME}" |
| 68 | +fi |
| 69 | + |
| 70 | +# Set as default |
| 71 | +echo "Setting '${MACHINE_NAME}' as default connection..." |
| 72 | +podman system connection default "${MACHINE_NAME}" |
| 73 | + |
| 74 | +# Configure cgroup delegation (CRITICAL for rootless k3s) |
| 75 | +echo "" |
| 76 | +echo "Configuring cgroup delegation for rootless k3s..." |
| 77 | +podman machine ssh "${MACHINE_NAME}" 'echo "[Service] |
| 78 | +Delegate=cpu cpuset io memory pids" | sudo tee /etc/systemd/system/[email protected]/delegate.conf' >/dev/null |
| 79 | + |
| 80 | +podman machine ssh "${MACHINE_NAME}" "sudo systemctl daemon-reload" |
| 81 | + |
| 82 | +# Restart for cgroup changes to take effect |
| 83 | +echo "Restarting machine for cgroup changes..." |
| 84 | +podman machine stop "${MACHINE_NAME}" |
| 85 | +podman machine start "${MACHINE_NAME}" |
| 86 | + |
| 87 | +# Get socket path |
| 88 | +SOCKET_PATH=$(podman machine inspect "${MACHINE_NAME}" --format '{{.ConnectionInfo.PodmanSocket.Path}}') |
| 89 | + |
| 90 | +echo "" |
| 91 | +echo "=== Setup Complete ===" |
| 92 | +echo "" |
| 93 | +echo "Podman machine '${MACHINE_NAME}' is ready!" |
| 94 | +echo "" |
| 95 | +echo "Environment variables (add to your shell profile):" |
| 96 | +echo " export CONTAINER_HOST=\"unix://${SOCKET_PATH}\"" |
| 97 | +echo " export OPENSHELL_CONTAINER_RUNTIME=podman" |
| 98 | +echo "" |
| 99 | +echo "To set them now, run:" |
| 100 | +echo " export CONTAINER_HOST=\"unix://${SOCKET_PATH}\"" |
| 101 | +echo " export OPENSHELL_CONTAINER_RUNTIME=podman" |
| 102 | +echo "" |
| 103 | + |
| 104 | +# Offer to add to shell profile |
| 105 | +read -p "Add environment variables to ~/.zshrc? (y/N) " -n 1 -r |
| 106 | +echo |
| 107 | +if [[ $REPLY =~ ^[Yy]$ ]]; then |
| 108 | + if ! grep -q "CONTAINER_HOST.*${MACHINE_NAME}" ~/.zshrc 2>/dev/null; then |
| 109 | + echo "" >> ~/.zshrc |
| 110 | + echo "# OpenShell Podman environment" >> ~/.zshrc |
| 111 | + echo "export CONTAINER_HOST=\"unix://${SOCKET_PATH}\"" >> ~/.zshrc |
| 112 | + echo "export OPENSHELL_CONTAINER_RUNTIME=podman" >> ~/.zshrc |
| 113 | + echo "✓ Added to ~/.zshrc" |
| 114 | + echo " Run: source ~/.zshrc" |
| 115 | + else |
| 116 | + echo "⚠️ Environment variables already in ~/.zshrc" |
| 117 | + fi |
| 118 | +fi |
| 119 | + |
| 120 | +echo "" |
| 121 | +echo "Next steps:" |
| 122 | +echo " 1. Source your shell profile or set environment variables" |
| 123 | +echo " 2. Build cluster image: mise run docker:build:cluster" |
| 124 | +echo " 3. Build CLI: cargo build --release -p openshell-cli" |
| 125 | +echo " 4. Install CLI: cp target/release/openshell ~/.local/bin/" |
0 commit comments