diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 355d5556..2f4532be 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -221,13 +221,18 @@ jobs: docker tag "$SMOKE_IMAGE" cloudhal:ci env: SMOKE_IMAGE: ${{ steps.ref.outputs.image }} + # Every step after the build is bounded (#306): a hang fails with the script's diagnostics + # instead of running into the job's 90-minute timeout and skipping everything after it. - name: Swarm smoke test + timeout-minutes: 20 run: scripts/swarm-smoke.sh cloudhal:ci - name: Helm smoke test (kind) + timeout-minutes: 20 run: | kind version && helm version --short scripts/helm-smoke.sh cloudhal:ci - name: Trivy scan + timeout-minutes: 15 uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0 with: image-ref: ${{ steps.ref.outputs.image }} diff --git a/deploy/swarm/README.md b/deploy/swarm/README.md index 28bda047..763e726b 100644 --- a/deploy/swarm/README.md +++ b/deploy/swarm/README.md @@ -32,6 +32,7 @@ **Notes:** +- **On the node itself, use `127.0.0.1`, not `localhost`.** The routing mesh publishes IPv4 only. Where `localhost` resolves to `::1` first, a connection to the published port is accepted and never answered (#306). - **Peer traffic** (port 7000) travels on the stack's own overlay, `10.231.0.0/24`, which is `encrypted`. Each task advertises its address there (`CLOUDHAL_CLUSTER_ADVERTISE=auto:10.231.0.0/24`). If that subnet is taken in your swarm, change it in both places. - **Working copies:** each task's `/data` is a volume named `cloudhal-data-` on the node it runs on. It holds only working copies; the store holds the installation, so back up the `cloudhal-store` volume (guide, "Backup and restore"). - **TLS:** put a TLS proxy or load balancer in front of ports 8787 and 8788. It must append the client's address to `X-Forwarded-For`. Then set `CLOUDHAL_BASE_URL` and `CLOUDHAL_BEHIND_PROXY=1` when you deploy. The routing mesh hides the client's address, so without a proxy rate limits share one bucket per path. diff --git a/docs/guides/self-host.md b/docs/guides/self-host.md index 046a4f9c..ab336d90 100644 --- a/docs/guides/self-host.md +++ b/docs/guides/self-host.md @@ -151,6 +151,8 @@ The cluster runs on workerd's experimental cluster mode, so treat several instan - two or more replicas behind the routing mesh, each advertising its address on the stack's encrypted overlay (`CLOUDHAL_CLUSTER_ADVERTISE=auto:`); - a volume per task slot, and `start-first` rolling updates; - SeaweedFS pinned to a labelled node. + + The routing mesh publishes IPv4 only. On the node itself, open `http://127.0.0.1:8787`, not `http://localhost:8787`: on hosts where `localhost` resolves to `::1` first, the connection is accepted and never answered. - **Kubernetes:** the Helm chart in [`deploy/helm/cloudhal`](https://github.com/cloudhal-dev/cloudhal/tree/main/deploy/helm/cloudhal) is published with each release as `oci://ghcr.io/cloudhal-dev/charts/cloudhal`. It has these parts: - a StatefulSet whose pods advertise `.-peers` through a headless Service; - readiness on `/ready`, and a NetworkPolicy that admits the cluster port from the release's own pods only; diff --git a/scripts/swarm-smoke.sh b/scripts/swarm-smoke.sh index 0809d83e..bffdb58d 100755 --- a/scripts/swarm-smoke.sh +++ b/scripts/swarm-smoke.sh @@ -12,71 +12,119 @@ root=$(cd "$(dirname "$0")/.." && pwd) stack="chsmoke$$" work=$(mktemp -d) started_swarm=no +# Every network call and docker command is bounded (#306): a port that accepts a connection and +# never answers, or a docker command that waits on the engine, fails the run with the state below +# instead of hanging until the job's timeout cancels it with nothing printed. +http() { curl --connect-timeout 5 --max-time 15 "$@"; } +d() { timeout 120 docker "$@"; } step() { echo "✓ $*"; } +# /api/health from inside one cloudhal container (bypassing the published port and the routing +# mesh): tells a stuck app from a port that is not forwarded. +inside() { + local ctr + ctr=$(timeout 20 docker ps -q --filter "label=com.docker.swarm.service.name=${stack}_cloudhal" | head -1) + [ -n "$ctr" ] || { echo "no container"; return; } + timeout 20 docker exec "$ctr" node -e "fetch('http://127.0.0.1:8787/api/health',{signal:AbortSignal.timeout(8000)}).then(r=>console.log(r.status),e=>console.log('error',e.cause?.code??e.name))" 2>&1 | tail -1 +} fail() { echo "✗ $*" >&2 - docker service ps "${stack}_cloudhal" --no-trunc >&2 || true - docker service logs --tail 80 "${stack}_cloudhal" >&2 || true + { + echo "--- services" + timeout 30 docker service ls || true + for svc in cloudhal seaweedfs; do + echo "--- tasks: ${stack}_$svc" + timeout 30 docker service ps "${stack}_$svc" --no-trunc || true + echo "--- logs: ${stack}_$svc" + timeout 30 docker service logs --tail 80 --timestamps "${stack}_$svc" || true + done + echo "--- containers" + timeout 30 docker ps -a --filter "label=com.docker.stack.namespace=$stack" || true + echo "--- health inside each cloudhal container" + for ctr in $(timeout 20 docker ps -q --filter "label=com.docker.swarm.service.name=${stack}_cloudhal"); do + echo "$ctr: $(timeout 20 docker exec "$ctr" node -e "fetch('http://127.0.0.1:8787/api/health',{signal:AbortSignal.timeout(8000)}).then(r=>console.log(r.status),e=>console.log('error',e.cause?.code??e.name))" 2>&1 | tail -1)" + done + echo "--- listeners on the published ports" + (ss -ltnp 2>/dev/null || true) | grep -E ":(${port}|$((port + 1)))\b" || echo "none on the host" + echo "--- ingress network" + timeout 30 docker network inspect ingress --format '{{json .IPAM.Config}} peers={{len .Peers}} containers={{len .Containers}}' || true + timeout 30 docker info --format 'docker {{.ServerVersion}}, swarm {{.Swarm.LocalNodeState}}, kernel {{.KernelVersion}}, firewall {{.FirewallBackend}}' 2>/dev/null || timeout 30 docker version --format 'docker {{.Server.Version}}' || true + echo "--- NAT rules for the published ports" + (sudo -n iptables-save -t nat 2>/dev/null || sudo -n nft list ruleset 2>/dev/null || true) | grep -E "${port}|$((port + 1))|DOCKER-INGRESS" | head -20 + echo "--- health, verbose (IPv4 and localhost)" + curl -v --connect-timeout 5 --max-time 10 "http://127.0.0.1:$port/api/health" 2>&1 | tail -20 || true + curl -v --connect-timeout 5 --max-time 10 "http://localhost:$port/api/health" 2>&1 | tail -20 || true + } >&2 exit 1 } cleanup() { - docker stack rm "$stack" >/dev/null 2>&1 || true + timeout 60 docker stack rm "$stack" || true for i in $(seq 1 30); do - [ -z "$(docker ps -q --filter "label=com.docker.stack.namespace=$stack")" ] && break + [ -z "$(timeout 20 docker ps -q --filter "label=com.docker.stack.namespace=$stack")" ] && break sleep 2 done - docker secret ls --format '{{.Name}}' | grep '^cloudhal_' | xargs -r docker secret rm >/dev/null 2>&1 || true - docker volume rm cloudhal-data-1 cloudhal-data-2 cloudhal-data-3 "${stack}_cloudhal-store" >/dev/null 2>&1 || true - [ "$started_swarm" = yes ] && docker swarm leave --force >/dev/null 2>&1 || true + timeout 30 docker secret ls --format '{{.Name}}' | grep '^cloudhal_' | xargs -r timeout 30 docker secret rm >/dev/null || true + timeout 60 docker volume rm cloudhal-data-1 cloudhal-data-2 cloudhal-data-3 "${stack}_cloudhal-store" >/dev/null 2>&1 || true + if [ "$started_swarm" = yes ]; then timeout 60 docker swarm leave --force || true; fi rm -rf "$work" } trap cleanup EXIT -if [ "$(docker info --format '{{.Swarm.LocalNodeState}}')" != active ]; then - docker swarm init >/dev/null +if [ "$(d info --format '{{.Swarm.LocalNodeState}}')" != active ]; then + d swarm init >/dev/null started_swarm=yes fi -if docker secret ls --format '{{.Name}}' | grep -q '^cloudhal_'; then +if d secret ls --format '{{.Name}}' | grep -q '^cloudhal_'; then echo "this swarm already has cloudhal_* secrets: run the smoke test on a scratch engine" >&2 started_swarm=no exit 1 fi -docker node update --label-add cloudhal.store=true "$(docker node ls --format '{{.Self}} {{.ID}}' | awk '$1=="true"{print $2}')" >/dev/null -docker run --rm "$image" secrets > "$work/secrets.env" -"$root/deploy/swarm/create-secrets.sh" < "$work/secrets.env" >/dev/null +d node update --label-add cloudhal.store=true "$(d node ls --format '{{.Self}} {{.ID}}' | awk '$1=="true"{print $2}')" >/dev/null +d run --rm "$image" secrets > "$work/secrets.env" +timeout 120 "$root/deploy/swarm/create-secrets.sh" < "$work/secrets.env" >/dev/null set -a # shellcheck disable=SC1091 . "$work/secrets.env" set +a local_tag="ghcr.io/cloudhal-dev/cloudhal:smoke-$$" -docker tag "$image" "$local_tag" +d tag "$image" "$local_tag" CLOUDHAL_TAG="smoke-$$" CLOUDHAL_PORT=$port CLOUDHAL_SITE_PORT=$((port + 1)) CLOUDHAL_SITE_NAME="Smoke Site" \ - docker stack deploy -c "$root/deploy/swarm/stack.yml" "$stack" >/dev/null 2>&1 -studio="http://localhost:$port" + timeout 120 docker stack deploy -c "$root/deploy/swarm/stack.yml" "$stack" || fail "docker stack deploy" +# 127.0.0.1, never localhost (#306): the routing mesh publishes IPv4 only, but on GitHub's runners +# `localhost` resolves to ::1 first and a connection to [::1]: is accepted and then never +# answered, so curl neither gets a reply nor falls back to IPv4. +studio="http://127.0.0.1:$port" site="http://127.0.0.1:$((port + 1))" -for i in $(seq 1 90); do - [ "$(docker service ls --filter "name=${stack}_cloudhal" --format '{{.Replicas}}')" = "2/2" ] && - curl -sf -o /dev/null "$studio/api/health" && break - [ "$i" = 90 ] && fail "the stack did not come up within 270 s" +# Wall-clock deadlines, well inside the step's timeout-minutes, so `fail` always gets to print. +deadline=$((SECONDS + 240)) +i=0 +while :; do + i=$((i + 1)) + replicas=$(d service ls --filter "name=${stack}_cloudhal" --format '{{.Replicas}}') + health=$(curl --connect-timeout 3 --max-time 5 -s -o /dev/null -w '%{http_code}' "$studio/api/health" || true) + [ "$replicas" = "2/2" ] && [ "$health" = 200 ] && break + # Progress, so a slow start shows where it is: the published port, and the app inside its + # container. + [ $((i % 6)) = 0 ] && echo "… ${SECONDS}s: replicas $replicas, health ${health:-none} / inside $(inside)" + [ "$SECONDS" -ge "$deadline" ] && fail "the stack did not come up within 240 s (replicas $replicas, health ${health:-none})" sleep 3 done step "two instances and the store are up" -for i in $(seq 1 60); do - [ "$(curl -s -o /dev/null -w '%{http_code}' "$site/")" = 503 ] && break - [ "$i" = 60 ] && fail "the site never answered its 503 before the first publish" +deadline=$((SECONDS + 120)) +until [ "$(curl --connect-timeout 3 --max-time 5 -s -o /dev/null -w '%{http_code}' "$site/" || true)" = 503 ]; do + [ "$SECONDS" -ge "$deadline" ] && fail "the site never answered its 503 before the first publish" sleep 2 done pw=$(openssl rand -hex 12) -code=$(curl -s -c "$work/jar" -o /dev/null -w '%{http_code}' "$studio/api/auth/sign-up/email" -H "origin: $studio" \ +code=$(http -s -c "$work/jar" -o /dev/null -w '%{http_code}' "$studio/api/auth/sign-up/email" -H "origin: $studio" \ -H 'content-type: application/json' -H "x-cloudhal-setup-token: $CLOUDHAL_SETUP_TOKEN" \ -d "{\"name\":\"Smoke Owner\",\"email\":\"owner@example.com\",\"password\":\"$pw\"}") [ "$code" = 200 ] || fail "sign-up of the first owner ($code)" -code=$(curl -s -b "$work/jar" -o /dev/null -w '%{http_code}' -X POST "$studio/api/sites/$CLOUDHAL_SITE_ID/snapshots" \ +code=$(http -s -b "$work/jar" -o /dev/null -w '%{http_code}' -X POST "$studio/api/sites/$CLOUDHAL_SITE_ID/snapshots" \ -H "origin: $studio" -H 'content-type: application/json' -d '{"publish":true}') [ "$code" = 201 ] || fail "publish ($code)" -for i in 1 2 3 4; do curl -s "$site/" | grep -q "Smoke Site" || fail "the site after the publish"; done +for i in 1 2 3 4; do http -s "$site/" | grep -q "Smoke Site" || fail "the site after the publish"; done step "the owner signed up; a publish is live on the next request through the routing mesh" -docker rmi "$local_tag" >/dev/null 2>&1 || true +timeout 60 docker rmi "$local_tag" >/dev/null 2>&1 || true echo echo "Swarm smoke test passed."