#!/bin/sh # Builds dist/ and serves it at https://atlibs.socialweb.computer/ from the socialweb.computer gateway's Caddy # (/etc/caddy/sites/.caddy, which its Caddyfile imports; the *.socialweb.computer record points there). # The site it replaces is kept in /var/lib/socialweb-computer/atlibs-editor-.tgz (0600). set -eu cd "$(dirname "$0")/.." site=atlibs.socialweb.computer ssh="ssh -p ${SSH_PORT:-1997} -o BatchMode=yes ${SSH_TARGET:-root@socialweb.computer}" scripts/build.sh "https://$site/" tar -C dist -cf - . | $ssh "rm -rf /var/www/$site.new && mkdir -p /var/www/$site.new && tar -C /var/www/$site.new -xf -" $ssh "SITE=$site sh -s" <<'REMOTE' set -eu d=/var/www/$SITE # the site as it was, before it is replaced b=/var/lib/socialweb-computer/atlibs-editor-$(date -u +%Y%m%dT%H%M%SZ).tgz [ ! -d "$d" ] || { mkdir -p "${b%/*}"; (umask 077; tar -C /var/www -czf "$b" "$SITE"); echo "backup $b"; } rm -rf "$d.old"; [ ! -d "$d" ] || mv "$d" "$d.old"; mv "$d.new" "$d"; rm -rf "$d.old" f=/etc/caddy/sites/$SITE.caddy want=$(printf '%s {\n\troot * %s\n\tfile_server\n}' "$SITE" "$d") if [ "$(cat "$f" 2>/dev/null)" != "$want" ]; then printf '%s\n' "$want" > "$f" caddy validate --config /etc/caddy/Caddyfile >/dev/null 2>&1 || { rm -f "$f"; echo "caddy validate failed: $f removed" >&2; exit 1; } systemctl reload caddy fi REMOTE for i in 1 2 3 4 5 6 7 8 9 10; do curl -sf -o /dev/null "https://$site/oauth/editor/client-metadata.json" && { echo "https://$site/"; exit; }; sleep 3; done echo "https://$site/ does not answer yet (TLS issuance?)" >&2; exit 1