#!/usr/bin/env bash set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" OS="$(uname -s)" PYTHON="$(command -v python3 || true)" if [ -z "${PYTHON}" ]; then echo "python3 is required" >&2 exit 1 fi case "${OS}" in Darwin) APP_DIR="${HOME}/.local/share/ollama-plagin-swap" CONFIG_DIR="${HOME}/.config/ollama-plagin-swap" STATE_DIR="${HOME}/.local/state/ollama-plagin-swap" AGENT="${APP_DIR}/ollama_swap_agent.py" COMMON="${APP_DIR}/ollama_swap_common.py" CONFIG="${CONFIG_DIR}/config.json" PLIST="${HOME}/Library/LaunchAgents/com.ollama.swap-agent.plist" mkdir -p "${APP_DIR}" "${CONFIG_DIR}" "${STATE_DIR}" "${HOME}/Library/LaunchAgents" install -m 0755 "${ROOT}/src/ollama_swap_agent.py" "${AGENT}" install -m 0755 "${ROOT}/src/ollama_swap_common.py" "${COMMON}" if [ ! -f "${CONFIG}" ]; then cat > "${CONFIG}" <<'JSON' { "bind_host": "0.0.0.0", "port": 8731, "token": "replace-with-strong-token", "allow_insecure": false, "poll_interval_seconds": 60, "swap_threshold_percent": 25, "cooldown_seconds": 600, "max_restarts_per_hour": 2, "restart_force_after_seconds": 300, "health_url": "http://127.0.0.1:11434/api/tags", "ollama_ps_url": "http://127.0.0.1:11434/api/ps", "launch_agent_plist": "~/Library/LaunchAgents/com.ollama.serve.plist", "linux_service": "ollama" } JSON fi cat > "${PLIST}" < Label com.ollama.swap-agent ProgramArguments ${PYTHON} ${AGENT} --config ${CONFIG} serve RunAtLoad KeepAlive StandardOutPath ${STATE_DIR}/agent.log StandardErrorPath ${STATE_DIR}/agent.log PLIST launchctl unload "${PLIST}" >/dev/null 2>&1 || true launchctl load -w "${PLIST}" echo "Installed and started macOS agent." ;; Linux) if [ "$(id -u)" -ne 0 ]; then echo "Run this installer as root on Ubuntu (for example: sudo $0)" >&2 exit 1 fi APP_DIR="/opt/ollama-plagin-swap" CONFIG_DIR="/etc/ollama-plagin-swap" STATE_DIR="/var/lib/ollama-plagin-swap" AGENT="${APP_DIR}/ollama_swap_agent.py" COMMON="${APP_DIR}/ollama_swap_common.py" CONFIG="${CONFIG_DIR}/config.json" UNIT="/etc/systemd/system/ollama-swap-agent.service" mkdir -p "${APP_DIR}" "${CONFIG_DIR}" "${STATE_DIR}" install -m 0755 "${ROOT}/src/ollama_swap_agent.py" "${AGENT}" install -m 0755 "${ROOT}/src/ollama_swap_common.py" "${COMMON}" if [ ! -f "${CONFIG}" ]; then cat > "${CONFIG}" <<'JSON' { "bind_host": "0.0.0.0", "port": 8731, "token": "replace-with-strong-token", "allow_insecure": false, "poll_interval_seconds": 60, "swap_threshold_percent": 25, "cooldown_seconds": 600, "max_restarts_per_hour": 2, "restart_force_after_seconds": 300, "health_url": "http://127.0.0.1:11434/api/tags", "ollama_ps_url": "http://127.0.0.1:11434/api/ps", "launch_agent_plist": "~/Library/LaunchAgents/com.ollama.serve.plist", "linux_service": "ollama" } JSON fi cat > "${UNIT}" <&2 exit 1 ;; esac