#!/usr/bin/env bash # One-command publish of the Web Blender build to https://:58014/. # # tools/web/deploy-58014.sh # build, upload, switch, verify # tools/web/deploy-58014.sh --skip-build # publish the existing web/dist # tools/web/deploy-58014.sh --release-id X # explicit release directory name # tools/web/deploy-58014.sh --rollback ID # switch `current` to an existing release # # Credentials: a working SSH key for the deploy user, or the password in # BW_DEPLOY_PASSWORD, or a 0600 file at ~/.config/blender-web/deploy.env # containing `BW_DEPLOY_PASSWORD=...`. set -euo pipefail HOST="${BW_DEPLOY_HOST:-49.232.250.35}" SSH_PORT="${BW_DEPLOY_SSH_PORT:-22}" DEPLOY_USER="${BW_DEPLOY_USER:-ubuntu}" WEB_PORT="${BW_DEPLOY_WEB_PORT:-58014}" SERVICE="${BW_DEPLOY_SERVICE:-blender-web-58014}" WEBROOT="${BW_DEPLOY_ROOT:-/var/www/blender-web}" repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" cd "$repo_root" skip_build=0 release_id="" rollback_id="" while [ $# -gt 0 ]; do case "$1" in --skip-build) skip_build=1; shift ;; --release-id) release_id="${2:?--release-id needs a value}"; shift 2 ;; --rollback) rollback_id="${2:?--rollback needs a value}"; shift 2 ;; -h|--help) sed -n '2,12p' "$0"; exit 0 ;; *) echo "unknown option: $1" >&2; exit 2 ;; esac done # ---- ssh plumbing (password via askpass when no key is authorised) ---------- ssh_base=(-o StrictHostKeyChecking=no -o UserKnownHostsFile=/tmp/blender-web-deploy-known-hosts -o PreferredAuthentications=publickey,password -o PubkeyAuthentication=yes -o ConnectTimeout=15 -p "$SSH_PORT") askpass="" cleanup() { [ -n "$askpass" ] && rm -f "$askpass"; } trap cleanup EXIT if ! ssh "${ssh_base[@]}" -o BatchMode=yes -o PasswordAuthentication=no "$DEPLOY_USER@$HOST" true 2>/dev/null; then if [ -z "${BW_DEPLOY_PASSWORD:-}" ] && [ -f "$HOME/.config/blender-web/deploy.env" ]; then # shellcheck disable=SC1090 . "$HOME/.config/blender-web/deploy.env" fi if [ -z "${BW_DEPLOY_PASSWORD:-}" ]; then echo "error: no SSH key and BW_DEPLOY_PASSWORD is unset" >&2 echo " export BW_DEPLOY_PASSWORD=... or write ~/.config/blender-web/deploy.env" >&2 exit 2 fi askpass="$(mktemp)" # Quoted format: an unquoted `\n` would be eaten by the shell and corrupt the # password with a trailing "n". printf '#!/bin/sh\nexec printf "%%s\\n" %q\n' "$BW_DEPLOY_PASSWORD" > "$askpass" chmod 700 "$askpass" export SSH_ASKPASS="$askpass" SSH_ASKPASS_REQUIRE=force ssh_run() { setsid -w ssh "${ssh_base[@]}" "$@"; } scp_run() { setsid -w scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/tmp/blender-web-deploy-known-hosts \ -o PreferredAuthentications=publickey,password -P "$SSH_PORT" "$@"; } else ssh_run() { ssh "${ssh_base[@]}" "$@"; } scp_run() { scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/tmp/blender-web-deploy-known-hosts -P "$SSH_PORT" "$@"; } fi # ---- rollback fast path ----------------------------------------------------- if [ -n "$rollback_id" ]; then ssh_run "$DEPLOY_USER@$HOST" " set -euo pipefail target='$WEBROOT/releases/$rollback_id' test -d \"\$target\" sudo ln -sfn \"\$target\" '$WEBROOT/current' sudo systemctl restart '$SERVICE' sleep 3 systemctl is-active '$SERVICE' echo current=\$(readlink -f '$WEBROOT/current') curl -sk -o /dev/null -w 'health=%{http_code}\\n' https://127.0.0.1:$WEB_PORT/" exit $? fi # ---- build ------------------------------------------------------------------ if [ "$skip_build" -eq 0 ]; then echo "== build" ( cd web && npm run build ) fi test -f web/dist/index.html test -f docs/web/deployment-contract.json short_sha="$(git rev-parse --short HEAD 2>/dev/null || echo local)" [ -n "$release_id" ] || release_id="$(date +%Y%m%d)-$(basename "$repo_root" | tr '[:upper:]' '[:lower:]')-$short_sha" # ---- assemble the release tree (app/, tools/, docs/, metadata) -------------- echo "== assemble $release_id" stage="$(mktemp -d)" trap 'rm -rf "$stage"; cleanup' EXIT mkdir -p "$stage/app" "$stage/tools" "$stage/docs/web" cp -a web/dist/. "$stage/app/" cp -a tools/web "$stage/tools/" cp docs/web/deployment-contract.json "$stage/docs/web/" node - "$stage" "$release_id" "$short_sha" <<'NODE' const fs = require("node:fs"), path = require("node:path"), crypto = require("node:crypto"); const [stage, releaseId, shortSha] = process.argv.slice(2); const sha = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); const app = path.join(stage, "app"); const html = fs.readFileSync(path.join(app, "index.html"), "utf8"); const css = /href="(\/assets\/[^"]+\.css)"/.exec(html)?.[1]; const js = /src="(\/assets\/[^"]+\.js)"/.exec(html)?.[1]; const engine = JSON.parse(fs.readFileSync(path.join(app, "engine-manifest.json"), "utf8")); fs.writeFileSync(path.join(stage, "release-metadata.json"), JSON.stringify({ schemaVersion: 1, product: "Web Blender Modeler V1", gitCommit: shortSha, releaseId, engineReleaseId: engine.releaseId ?? engine.engineVersion ?? "unknown", webEngineWasmSha256: sha(path.join(app, "vendor/blender/web_engine.wasm")), entryDocumentSha256: sha(path.join(app, "index.html")), cssSha256: css ? sha(path.join(app, css.slice(1))) : "", javascriptSha256: js ? sha(path.join(app, js.slice(1))) : "", manual: "/manuals/index.html", manualChapters: 28, }, null, 2) + "\n"); NODE archive="/tmp/blender-web-$release_id.tar.gz" tar -czf "$archive" -C "$stage" . # ---- upload ----------------------------------------------------------------- echo "== upload $(du -h "$archive" | cut -f1)" scp_run "$archive" "$DEPLOY_USER@$HOST:/tmp/" # ---- switch + restart + health --------------------------------------------- echo "== deploy" ssh_run "$DEPLOY_USER@$HOST" " set -euo pipefail target='$WEBROOT/releases/$release_id' sudo mkdir -p \"\$target\" sudo tar -xzf '/tmp/blender-web-$release_id.tar.gz' -C \"\$target\" sudo chown -R root:root \"\$target\" sudo chmod -R a+rX \"\$target\" test -f \"\$target/app/index.html\" test -f \"\$target/tools/web/deployment-http-server.mjs\" test -f \"\$target/docs/web/deployment-contract.json\" previous=\"\$(readlink -f '$WEBROOT/current' || true)\" sudo ln -sfn \"\$target\" '$WEBROOT/current' sudo systemctl restart '$SERVICE' sleep 3 systemctl is-active '$SERVICE' echo previous=\"\$previous\" echo current=\"\$(readlink -f '$WEBROOT/current')\" curl -sk -o /dev/null -w 'health=%{http_code}\\n' https://127.0.0.1:$WEB_PORT/" # ---- verify the public endpoint -------------------------------------------- echo "== verify https://$HOST:$WEB_PORT/" origin="https://$HOST:$WEB_PORT" index_status="$(curl -sk -o /dev/null -w '%{http_code}' "$origin/")" wasm_local="$(sha256sum web/dist/vendor/blender/web_engine.wasm | cut -d' ' -f1)" wasm_remote="$(curl -sk "$origin/vendor/blender/web_engine.wasm" | sha256sum | cut -d' ' -f1)" bin_status="$(curl -sk -o /dev/null -w '%{http_code}' "$origin/lights/forest.bin")" echo "index=$index_status wasm_local=$wasm_local wasm_remote=$wasm_remote studios=$bin_status" [ "$index_status" = "200" ] && [ "$wasm_local" = "$wasm_remote" ] && [ "$bin_status" = "200" ] || { echo "verification failed; roll back with: $0 --rollback " >&2; exit 1; } echo "published $release_id -> $origin/"