#!/usr/bin/env bash # Renders stats-site (Hugo project) without requiring Hugo, deploys atomically # to /var/www/stats-site/. The Hugo project uses only two template variables; # we substitute them with sed. # # Usage: build.sh [--src DIR] [--dest DIR] # --src default /opt/stats-site # --dest default /var/www/stats-site set -euo pipefail SRC="${SRC:-/opt/stats-site}" DEST="${DEST:-/var/www/stats-site}" # Hugo variables (mirror config.toml). SITE_TITLE='PSP Speedtest Stats' SITE_DESC='Метрики латентности и времени отклика двух geo-распределённых OpenSpeedTest нод. Snapshot обновляется каждые 10 минут.' [ -d "$SRC" ] || { echo "src $SRC not found" >&2; exit 1; } TMP=$(mktemp -d) trap "rm -rf $TMP" EXIT # 1. Copy static/ verbatim cp -r "$SRC/static/." "$TMP/" # 2. Render layouts/index.html with sed-substitution sed \ -e "s|{{ \.Site\.Title }}|${SITE_TITLE}|g" \ -e "s|{{ \.Site\.Params\.description }}|${SITE_DESC}|g" \ "$SRC/layouts/index.html" > "$TMP/index.html" # 3. Set perms in TMP that nginx (www-data) can read. mktemp creates 700; # --archive in rsync preserves them, leading to 403s in nginx logs. find "$TMP" -type d -exec chmod 0755 {} + find "$TMP" -type f -exec chmod 0644 {} + # 4. Atomic rsync, preserving runtime data (snapshot.json) and ownership. # --delete removes files in DEST that aren't in TMP (cleans old assets), # --exclude protects data/ (snapshot generator owns that). rsync -a --delete \ --exclude='data/snapshot.json' \ --exclude='data/snapshot.json.tmp' \ --chown=deploy:www-data \ "$TMP/" "$DEST/" echo "site built: $(find $DEST -type f | wc -l) files in $DEST"