#!/usr/bin/env bash # Reads what knotless logs on the host (deploy/README.md, Logs): # # deploy/logs.sh [serve|run|runner|pages|deploys] [--since 30m] [--follow] [jq filter] # # deploy/logs.sh serve 'select(.duration_ms > 1000)' # deploy/logs.sh serve --since 2h 'select(.level == "WARN" or .level == "ERROR")' # deploy/logs.sh runner --since 1h 'select(.message == "batch started")' # deploy/logs.sh deploys # # They are read from the host's journal, so a container a rollout removed # is read too. serve and the runners log JSON lines, each run through the # jq filter (`.` if none); lines that are not JSON are left out. pages # logs plain text, and deploys is the host's /var/log/knotless-deploy.log, # each shown as is. # The host is KNOTLESS_HOST, from deploy/local.env, else knotless.dev: run # is a runner there. runner is the runner host's instead # (deploy/runner-host): KNOTLESS_RUNNER_HOST, else runner.knotless.dev, # its runner named by KNOTLESS_RUNNER_NAME (knotless-runner). As # deploy/runner-host/deploy.sh does, KNOTLESS_RUNNER_ENV names a file # read in place of deploy/local.env, for another runner such as the # trusted one: `KNOTLESS_RUNNER_ENV=deploy/ci.env deploy/logs.sh runner`. set -euo pipefail cd "$(dirname "$0")/.." SVC="${1:-serve}" if [ "$SVC" = runner ] && [ -n "${KNOTLESS_RUNNER_ENV:-}" ]; then set -a; . "$KNOTLESS_RUNNER_ENV"; set +a elif [ -f deploy/local.env ]; then set -a; . deploy/local.env; set +a fi DEST="${KNOTLESS_HOST:-root@knotless.dev}" if [ "$SVC" = runner ]; then DEST="${KNOTLESS_RUNNER_HOST:-root@runner.knotless.dev}" fi [ $# -eq 0 ] || shift SINCE=30m FOLLOW= while [ $# -gt 0 ]; do case "$1" in --since) SINCE=$2 && shift 2 ;; --follow) FOLLOW=--follow && shift ;; *) break ;; esac done FILTER="${1:-.}" SSH="ssh -o StrictHostKeyChecking=accept-new $DEST" # This project's lines, as written, since then: journalctl takes `-30m`. JOURNAL="journalctl --no-pager -o cat --since '-$SINCE' ${FOLLOW:+--follow} COM_DOCKER_COMPOSE_PROJECT=knotless" case "$SVC" in deploys) # shellcheck disable=SC2029 $SSH "tail -n 100 $FOLLOW /var/log/knotless-deploy.log" ;; serve | run) # Every container of the service, gone or running, both while a # rollout overlaps them. # shellcheck disable=SC2029 $SSH "$JOURNAL COM_DOCKER_COMPOSE_SERVICE='$SVC'" | jq -R -c --unbuffered "fromjson? | $FILTER" ;; runner) # The runner host tags each line with its container's name, which a # deploy that replaces the container keeps, and the host's clock may # not be in UTC: `--since` is relative, so it reads the same anywhere. # shellcheck disable=SC2029 $SSH "journalctl --no-pager -o cat --since '-$SINCE' ${FOLLOW:+--follow} CONTAINER_TAG='${KNOTLESS_RUNNER_NAME:-knotless-runner}-run-1'" | jq -R -c --unbuffered "fromjson? | $FILTER" ;; pages) # shellcheck disable=SC2029 $SSH "$JOURNAL COM_DOCKER_COMPOSE_SERVICE=pages" ;; *) sed -n '4p' "$0" >&2 exit 2 ;; esac