feat(infra): port pre-check before every port definition (MITHOME-137)
CI Pipeline with Test Management / 🧪 Run Tests & Generate Reports (push) Canceled after 0s
Test Reporting & Gherkin Analysis / 🧪 Run Tests & Generate Reports (push) Canceled after 0s
CI Pipeline with Test Management / 🐳 Docker Integration Tests (push) Canceled after 0s
CI Pipeline with Test Management / 🏗️ Build Docker Image (push) Canceled after 0s
CI Pipeline with Test Management / 📊 Generate Test Summary (push) Canceled after 0s
Test Reporting & Gherkin Analysis / 📊 Analyze Test Coverage (push) Canceled after 0s
Test Reporting & Gherkin Analysis / 🔄 Sync with Linear (push) Canceled after 0s
Test Reporting & Gherkin Analysis / ⚡ Performance Monitoring (push) Canceled after 0s

scripts/check-ports.sh checks a port list or a compose file (locally or on a
remote host over SSH) before anything is built or started; a port counts as
owned only by the same compose project and service. deploy.sh runs it before
the build; tests are part of the pre-deploy suite. Found: the dev compose
clashes on llmdev (3001: step, 8081: mozdit staging).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Do Siki
2026-09-29 21:38:43 +02:00
co-authored by Claude Opus 5.5
parent b8d1b0ac25
commit 63150ed115
8 changed files with 295 additions and 4 deletions
+148
View File
@@ -0,0 +1,148 @@
#!/usr/bin/env bash
# Port pre-check: every host port must be free (or already held by this
# compose project) BEFORE anything is built or started, so a clash fails in
# seconds instead of after a full build. Rule from the llmdev Docker spec
# (~/Projects/maintenance/docker-hatter-specifikacio.md, section 5).
#
# Usage:
# check-ports.sh [--host user@server] [--project NAME] PORT...
# check-ports.sh [--host user@server] [--env-file FILE] --compose FILE
#
# --host check the listeners on a remote server over SSH (e.g. from the
# Mac against llmdev); default: this machine
# --project compose project allowed to hold the port (it gets recreated)
# --compose read the published ports (and the project name) from a compose
# file; uses `docker compose config` when available, otherwise a
# plain parse of "IP:HOST:CONTAINER" / "HOST:CONTAINER" entries
#
# Exit codes: 0 all free/owned, 1 conflict, 2 usage error.
# Test hooks: CHECK_PORTS_LISTENERS / CHECK_PORTS_DOCKER_PS replace the
# commands that list listening ports / docker containers publishing a port.
set -Euo pipefail
HOST=""
PROJECT=""
COMPOSE=""
ENV_FILE=""
PORTS=()
COMPOSE_JSON=""
SERVICE_MAP="" # "PORT SERVICE" lines (no associative arrays: macOS ships bash 3.2)
usage() {
sed -n '6,18p' "$0" >&2
exit 2
}
while [ $# -gt 0 ]; do
case "$1" in
--host) HOST="${2:-}"; shift 2 ;;
--project) PROJECT="${2:-}"; shift 2 ;;
--compose) COMPOSE="${2:-}"; shift 2 ;;
--env-file) ENV_FILE="${2:-}"; shift 2 ;;
-h|--help) usage ;;
-*) echo "Ismeretlen kapcsoló: $1" >&2; usage ;;
*) PORTS+=("$1"); shift ;;
esac
done
remote() {
if [ -n "$HOST" ]; then
ssh -o BatchMode=yes -o ConnectTimeout=10 "$HOST" "$@"
else
bash -c "$*"
fi
}
# Rendered compose config as JSON, or empty when `docker compose` is not
# available (e.g. the Mac without the compose plugin). Read once, in the main
# shell, so the project name below is not lost in a subshell.
compose_json() {
local args=(-f "$COMPOSE")
[ -n "$ENV_FILE" ] && args=(--env-file "$ENV_FILE" "${args[@]}")
docker compose version >/dev/null 2>&1 && docker compose "${args[@]}" config --format json 2>/dev/null || true
}
# Published host ports of a compose file: "PORT SERVICE" per line.
compose_ports() {
if [ -n "$COMPOSE_JSON" ]; then
printf '%s' "$COMPOSE_JSON" | python3 -c '
import json, sys
cfg = json.load(sys.stdin)
for name, svc in (cfg.get("services") or {}).items():
for p in svc.get("ports") or []:
if p.get("published"):
print(p["published"], name)'
return
fi
# Fallback: "127.0.0.1:8081:3000", "8081:3000", "${APP_PORT:-8081}:3000",
# attributed to the service block (two-space indented key) it appears in.
awk '
/^services:/ { ins = 1; next }
/^[^[:space:]#]/ { ins = 0 }
ins && /^ [A-Za-z0-9_.-]+:[[:space:]]*$/ { svc = $1; sub(":", "", svc); next }
ins && /^[[:space:]]*-[[:space:]]*"?[0-9.${}A-Z_:-]+:[0-9]+/ {
line = $0
sub(/^[[:space:]]*-[[:space:]]*"?/, "", line)
sub(/["[:space:]#].*$/, "", line)
gsub(/\$\{[A-Z0-9_]+:-/, "", line); gsub(/\}/, "", line); sub(/\/(tcp|udp)$/, "", line)
n = split(line, parts, ":")
if (n >= 2 && parts[n - 1] ~ /^[0-9]+$/) print parts[n - 1], svc
}
' "$COMPOSE"
}
listeners() {
if [ -n "${CHECK_PORTS_LISTENERS:-}" ]; then
bash -c "$CHECK_PORTS_LISTENERS"
else
remote 'if command -v ss >/dev/null; then ss -ltnH | awk "{print \$4}"; else lsof -nP -iTCP -sTCP:LISTEN 2>/dev/null | awk "NR>1 {print \$9}"; fi'
fi
}
# "name<TAB>compose-project<TAB>compose-service" of containers publishing a port.
publishers() {
if [ -n "${CHECK_PORTS_DOCKER_PS:-}" ]; then
bash -c "$CHECK_PORTS_DOCKER_PS $1"
else
remote "docker ps --filter publish=$1 --format '{{.Names}}\t{{.Label \"com.docker.compose.project\"}}\t{{.Label \"com.docker.compose.service\"}}' 2>/dev/null || true"
fi
}
if [ -n "$COMPOSE" ]; then
[ -f "$COMPOSE" ] || { echo "Nincs ilyen compose-fájl: $COMPOSE" >&2; exit 2; }
COMPOSE_JSON="$(compose_json)"
if [ -n "$COMPOSE_JSON" ] && [ -z "$PROJECT" ]; then
PROJECT="$(printf '%s' "$COMPOSE_JSON" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("name",""))')"
fi
SERVICE_MAP="$(compose_ports)"
while read -r p _; do [ -n "$p" ] && PORTS+=("$p"); done <<< "$SERVICE_MAP"
fi
[ "${#PORTS[@]}" -gt 0 ] || { echo "Nincs ellenőrizendő port." >&2; exit 2; }
TARGET="${HOST:-helyi gép}"
LISTENING="$(listeners)" || { echo "❌ A port-ellenőrzés nem futott le ($TARGET)." >&2; exit 1; }
CONFLICTS=0
for port in $(printf '%s\n' "${PORTS[@]}" | sort -un); do
if ! printf '%s\n' "$LISTENING" | grep -qE "[:.]${port}\$"; then
echo "✅ ${port}: szabad (${TARGET})"
continue
fi
owners="$(publishers "$port")"
# Owned = same compose project and (in compose mode) the same service that
# publishes this port in the file; anything else is a conflict.
foreign="$(printf '%s\n' "$owners" | awk -F'\t' -v p="$PROJECT" -v s="$(printf '%s\n' "$SERVICE_MAP" | awk -v p="$port" '$1 == p {print $2; exit}')" \
'NF && ($2 == "" || $2 != p || (s != "" && $3 != s)) {print $1}')"
if [ -n "$owners" ] && [ -z "$foreign" ] && [ -n "$PROJECT" ]; then
echo "✅ ${port}: a(z) ${PROJECT} projekt használja, újraindításkor felszabadul"
else
echo "❌ ${port}: foglalt (${TARGET}) — ${foreign:-nem Docker-folyamat}" >&2
CONFLICTS=$((CONFLICTS + 1))
fi
done
if [ "$CONFLICTS" -gt 0 ]; then
echo "❌ ${CONFLICTS} portütközés. Válassz szabad portot, és jegyezd be a portregiszterbe (Docker-háttér specifikáció, 5. pont)." >&2
exit 1
fi