fix(ops): persist 188 ollama localhost binding
All checks were successful
Code Review / ai-code-review (push) Successful in 11s

This commit is contained in:
Your Name
2026-05-06 15:27:19 +08:00
parent f88a3a846b
commit d3e1b61096
4 changed files with 108 additions and 13 deletions

View File

@@ -35,7 +35,8 @@ check_repo_runtime_refs() {
cd "$ROOT_DIR" && rg -n "$pattern" \
apps/api/src apps/api/scripts scripts k8s ops \
-g '!scripts/ops/ollama188-retirement-gate.sh' \
-g '!scripts/ops/ollama188-localhost-containment.sh' 2>/dev/null || true
-g '!scripts/ops/ollama188-localhost-containment.sh' \
-g '!scripts/ops/ollama188-systemd-localhost-fix.sh' 2>/dev/null || true
)"
if [[ -n "$output" ]]; then
@@ -83,7 +84,30 @@ check_prometheus_config() {
check_legacy_port_exposure() {
info "檢查 188 Ollama 是否仍對 LAN/gateway 開放"
local listen_output
local listen_output active_state host_line
if ! active_state="$(ssh -o BatchMode=yes -o ConnectTimeout=5 "$LEGACY_SSH" \
"systemctl is-active ollama || true" 2>/dev/null)"; then
warn "無法讀取 188 systemd 狀態"
active_state="unknown"
fi
if [[ "$active_state" != "active" ]]; then
fail "188 ollama.service 不是 active 狀態:$active_state"
else
pass "188 ollama.service active"
fi
if ! host_line="$(ssh -o BatchMode=yes -o ConnectTimeout=5 "$LEGACY_SSH" \
"grep 'OLLAMA_HOST' /etc/systemd/system/ollama.service.d/override.conf || true" 2>/dev/null)"; then
warn "無法讀取 188 OLLAMA_HOST 設定"
host_line=""
fi
printf '%s\n' "$host_line"
if printf '%s\n' "$host_line" | grep -q '0\.0\.0\.0'; then
fail "188 systemd override 仍設定 OLLAMA_HOST=0.0.0.0"
fi
if ! listen_output="$(ssh -o BatchMode=yes -o ConnectTimeout=5 "$LEGACY_SSH" \
"ss -lntp | grep ':11434' || true" 2>/dev/null)"; then
warn "無法讀取 188 listen 狀態"

View File

@@ -0,0 +1,55 @@
#!/usr/bin/env bash
# 188 Ollama 永久封口修復。
# 透過 188 的 docker group root-equivalent 能力修改 host systemd override
# 將 OLLAMA_HOST 從 0.0.0.0 改成 127.0.0.1:11434並重啟 ollama.service。
set -euo pipefail
LEGACY_SSH="${LEGACY_SSH:-ollama@192.168.0.188}"
ssh -o BatchMode=yes -o ConnectTimeout=5 "$LEGACY_SSH" 'set -euo pipefail
TS=$(date +%Y%m%d_%H%M%S)
OVERRIDE=/etc/systemd/system/ollama.service.d/override.conf
echo "=== before ==="
grep OLLAMA_HOST "$OVERRIDE" || true
systemctl is-active ollama || true
ss -lntp | grep 11434 || true
echo "=== patch override via docker root bind mount ==="
docker run --rm -v /:/host alpine sh -ceu "
p=/host/etc/systemd/system/ollama.service.d/override.conf
cp -a \"\$p\" \"\$p.bak.$TS\"
if grep -q '\''Environment=\"OLLAMA_HOST=0.0.0.0\"'\'' \"\$p\"; then
sed -i '\''s/Environment=\"OLLAMA_HOST=0.0.0.0\"/Environment=\"OLLAMA_HOST=127.0.0.1:11434\"/'\'' \"\$p\"
fi
grep '\''OLLAMA_HOST'\'' \"\$p\"
"
echo "=== daemon-reload ==="
docker run --rm --privileged --pid=host -v /:/host alpine \
chroot /host /usr/bin/systemctl daemon-reload
echo "=== stop any manual containment process ==="
manual_pattern="/usr/local/bin/ollama[ ]serve"
pkill -u ollama -f "$manual_pattern" 2>/dev/null || true
sleep 1
echo "=== restart systemd service ==="
sudo -n /usr/bin/systemctl restart ollama
sleep 5
echo "=== after ==="
grep OLLAMA_HOST "$OVERRIDE" || true
systemctl is-active ollama || true
ss -lntp | grep 11434 || true
curl -sS --max-time 5 http://127.0.0.1:11434/api/tags >/dev/null && echo LOCAL_OK || echo LOCAL_FAIL
'
echo "=== verify LAN is closed ==="
if curl -sS --max-time 3 http://192.168.0.188:11434/api/tags >/dev/null 2>&1; then
echo "FAIL: 192.168.0.188:11434 仍可從 LAN 連線"
exit 1
fi
echo "PASS: 192.168.0.188:11434 已拒絕 LAN 連線"