Add ollama-plagin-swap remote swap control plugin

This commit is contained in:
Andrew Emelyaneko
2026-10-31 19:27:59 +03:00
commit be7932441c
13 changed files with 1549 additions and 0 deletions
+134
View File
@@ -0,0 +1,134 @@
#!/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}" <<PLIST
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.ollama.swap-agent</string>
<key>ProgramArguments</key>
<array>
<string>${PYTHON}</string>
<string>${AGENT}</string>
<string>serve</string>
<string>--config</string>
<string>${CONFIG}</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>${STATE_DIR}/agent.log</string>
<key>StandardErrorPath</key>
<string>${STATE_DIR}/agent.log</string>
</dict>
</plist>
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}" <<UNIT
[Unit]
Description=ollama-plagin-swap remote agent
After=network-online.target ollama.service
Wants=network-online.target
[Service]
Type=simple
ExecStart=${PYTHON} ${AGENT} serve --config ${CONFIG}
Restart=always
RestartSec=5
User=root
StandardOutput=append:${STATE_DIR}/agent.log
StandardError=append:${STATE_DIR}/agent.log
[Install]
WantedBy=multi-user.target
UNIT
systemctl daemon-reload
systemctl enable --now ollama-swap-agent
echo "Installed and started Ubuntu agent."
;;
*)
echo "Unsupported OS: ${OS}" >&2
exit 1
;;
esac