Files
muSRFlow/scripts/deploy-remote-web.sh
T

114 lines
4.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright (C) 2026 Zaher Salman
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Deploy muSRFlow web assets to a remote host (e.g. musruser.psi.ch).
# Copies the required files to a temporary directory on the remote host,
# then runs deploy-web.sh there with sudo so Apache can read/execute them.
#
# Usage:
# ./scripts/deploy-remote-web.sh [path/to/muSRFlow.tar.gz]
#
# If no tarball is given, a temporary tar.gz is created from the current
# working tree (including docs/_build/html if present). Build docs first:
# make -C docs html
#
# Environment:
# MUSRFLOW_REMOTE_HOST Remote host (default: musruser.psi.ch)
# MUSRFLOW_REMOTE_USER SSH user (default: current user)
# MUSRFLOW_REMOTE_SSH Extra ssh/scp options (e.g. "-J jumphost")
# MUSRFLOW_REMOTE_TMP Remote temp base dir (default: /tmp)
# MUSRFLOW_WEB_TARGET Passed through to deploy-web.sh
# MUSRFLOW_CGI_TARGET Passed through to deploy-web.sh
# MUSRFLOW_STARTUP_XML_TARGET Passed through to deploy-web.sh
# MUSRFLOW_DATA_ROOT Passed through to deploy-web.sh
# MUSRFLOW_BUILD_REVISION Passed through to deploy-web.sh
#
# The remote user must be able to run: sudo ./scripts/deploy-web.sh
# (passwordless sudo for that command, or you will be prompted if TTY allows).
set -euo pipefail
project_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
remote_host="${MUSRFLOW_REMOTE_HOST:-musruser.psi.ch}"
remote_user="${MUSRFLOW_REMOTE_USER:-}"
remote_ssh_opts=(${MUSRFLOW_REMOTE_SSH:-})
remote_tmp_base="${MUSRFLOW_REMOTE_TMP:-/tmp}"
if [ -n "$remote_user" ]; then
remote="${remote_user}@${remote_host}"
else
remote="${remote_host}"
fi
tarball="${1:-}"
cleanup_tarball=""
if [ -z "$tarball" ]; then
if [ ! -s "$project_root/docs/_build/html/index.html" ]; then
printf 'HTML manual missing. Run: make -C "%s/docs" html\n' "$project_root" >&2
exit 1
fi
tarball="$(mktemp "${TMPDIR:-/tmp}/musrflow-deploy.XXXXXX.tar.gz")"
cleanup_tarball="$tarball"
printf 'Creating temporary deployment archive: %s\n' "$tarball"
# Include only what deploy-web.sh needs, plus docs build and scripts.
tar -C "$project_root" -czf "$tarball" \
index.html quick-start.html ZGUI.css \
muSRFitweb.js muSRFitRoot.js muSRFitParameterPlot.js muSRFitlib.js \
assets package.json build-revision.txt \
scripts/deploy-web.sh \
cgi/single_musrfit.cgi cgi/musrfit_startup.xml \
docs/_build/html
fi
if [ ! -f "$tarball" ]; then
printf 'Tarball not found: %s\n' "$tarball" >&2
exit 1
fi
remote_dir="${remote_tmp_base}/musrflow-deploy.$$.$(date +%s)"
remote_tar="${remote_dir}/$(basename "$tarball")"
printf 'Copying %s to %s:%s\n' "$tarball" "$remote" "$remote_dir"
ssh "${remote_ssh_opts[@]}" "$remote" "mkdir -p '$remote_dir'"
scp "${remote_ssh_opts[@]}" "$tarball" "$remote:$remote_tar"
# Build env assignments for remote deploy-web.sh
env_args=()
for var in MUSRFLOW_WEB_TARGET MUSRFLOW_CGI_TARGET MUSRFLOW_STARTUP_XML_TARGET \
MUSRFLOW_DATA_ROOT MUSRFLOW_BUILD_REVISION MUSRFLOW_WEB_OWNER MUSRFLOW_WEB_GROUP; do
val="${!var-}"
if [ -n "$val" ]; then
# escape single quotes for remote shell
esc="${val//\'/\'\\\'\'}"
env_args+=("$var='$esc'")
fi
done
printf 'Extracting and deploying on %s (sudo)...\n' "$remote"
ssh "${remote_ssh_opts[@]}" -t "$remote" bash -s -- "$remote_dir" "$remote_tar" "${env_args[*]-}" << 'REMOTE'
set -euo pipefail
remote_dir="$1"; remote_tar="$2"; env_assign="${3-}"
cd "$remote_dir"
tar -xzf "$remote_tar"
# Find extracted root (tar may include top-level dir or files)
if [ -f scripts/deploy-web.sh ]; then
root=.
elif [ -f */scripts/deploy-web.sh ]; then
root="$(dirname "$(echo */scripts/deploy-web.sh | awk '{print $1}')")"
else
echo "deploy-web.sh not found after extraction" >&2; exit 1
fi
cd "$root"
# shellcheck disable=SC2086
sudo env $env_assign ./scripts/deploy-web.sh
cd /
rm -rf "$remote_dir"
REMOTE
if [ -n "$cleanup_tarball" ]; then
rm -f "$cleanup_tarball"
fi
printf 'Remote deployment to %s complete.\n' "$remote"