Files
soleprint/rig/ctrl/docs.sh
2026-09-30 10:12:32 -03:00

83 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Documentation: render the diagrams, and serve the pages from a throwaway nginx container.
# Usage: docs.sh serve | graphs
# Whose: DOCS_DIR, the docs/ of the folder `make docs` runs from (a project's launcher sets
# it); unset, rig's own.
# Notes: docs/notes/docs.md
set -euo pipefail
cd "$(dirname "$0")"
source ./lib/config.sh
load_config
REPO="$(cd .. && pwd)"
DOCS="${DOCS_DIR:-$REPO/docs}"
[ -d "$DOCS" ] && DOCS="$(cd "$DOCS" && pwd)"
DOCS_PORT="${DOCS_PORT:-$((HTTP_PORT + 4))}" # +4 sits inside this env's block
serve() {
if [ ! -f "$DOCS/index.html" ]; then
echo "no index.html in $DOCS" >&2
exit 1
fi
# One worker and no access log: a few static pages need no more, and the
# terminal then shows only real errors. The config is a temp file, gone on exit.
# nginx runs as the caller, not as its own user: it reads what the caller can read,
# whatever the umask (a 027 one leaves files unreadable to "others", which is a 403).
# So no port below 1024 inside, and pid and temp files go to /tmp.
# Global, not local: the trap runs after serve() has returned.
DOCS_CONF=$(mktemp)
trap 'rm -f "$DOCS_CONF"' EXIT
cat > "$DOCS_CONF" <<'NGINX'
worker_processes 1;
error_log stderr error;
pid /tmp/nginx.pid;
events { worker_connections 64; }
http {
include /etc/nginx/mime.types;
access_log off;
client_body_temp_path /tmp/client_body;
proxy_temp_path /tmp/proxy;
fastcgi_temp_path /tmp/fastcgi;
uwsgi_temp_path /tmp/uwsgi;
scgi_temp_path /tmp/scgi;
server { listen 8080; root /usr/share/nginx/html; }
}
NGINX
chmod 644 "$DOCS_CONF"
echo "docs of $DOCS"
echo " for '$CLUSTER' on http://localhost:${DOCS_PORT}"
echo " (ctrl-c to stop; nothing is installed and nothing persists)"
docker run --rm \
--name "${CLUSTER}-docs" \
--user "$(id -u):$(id -g)" \
--entrypoint nginx \
-p "${DOCS_PORT}:8080" \
-v "$DOCS:/usr/share/nginx/html:ro" \
-v "$DOCS_CONF:/etc/nginx/nginx.conf:ro" \
nginx:alpine -g 'daemon off;'
}
graphs() {
if ! command -v dot >/dev/null 2>&1; then
echo "graphviz not found — install with: sudo apt install graphviz" >&2
echo "(only needed to re-render; the committed .svg files already work)" >&2
exit 1
fi
shopt -s nullglob
local found=0 f out
for f in "$DOCS"/graphs/*.dot; do
out="${f%.dot}.svg"
echo " graphviz $(basename "$f") → $(basename "$out")"
dot -Tsvg "$f" -o "$out"
found=1
done
[ "$found" -eq 1 ] || echo " no .dot files in $DOCS/graphs/"
}
case "${1:-serve}" in
serve) serve ;;
graphs) graphs ;;
*) echo "usage: $0 [serve|graphs]" >&2; exit 1 ;;
esac