AkurAI Build
Menu

BifrOSt-Web

public

Latest change 9ac9572e64e8969f5438a182985f96ae8d6fa339 - Route production install through Titan host by Ólafur Búi Ólafsson

#!/usr/bin/env bash
set -euo pipefail

ROOT=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
if [[ ${1:-} != --host ]]; then
    printf -v root_q '%q' "$ROOT"
    exec ssh titan "cd $root_q && ./deploy.sh --host"
fi

BUNDLE="$ROOT/target/dx/bifrost-web/release/web"
SERVICE="$ROOT/deploy/bifrost-web.service"
REVISION=$(git -C "$ROOT" rev-parse HEAD)
RELEASE="/opt/bifrost-web/releases/$REVISION"
CURRENT=/opt/bifrost-web/current
PREVIOUS=$(readlink -f "$CURRENT" 2>/dev/null || true)

[[ -x "$BUNDLE/server" ]] || { echo "missing Dioxus server bundle" >&2; exit 1; }
[[ -f "$BUNDLE/public/index.html" ]] || { echo "missing Dioxus public bundle" >&2; exit 1; }
[[ -f "$ROOT/data/content.db" ]] || { echo "missing content database" >&2; exit 1; }

staging="$RELEASE.new"
sudo rm -rf "$staging"
sudo install -d -m 0755 "$staging/public" "$staging/data" /opt/bifrost-web/releases
sudo install -m 0755 "$BUNDLE/server" "$staging/server"
sudo cp -a "$BUNDLE/public/." "$staging/public/"
sudo install -m 0644 "$ROOT/data/content.db" "$staging/data/content.db"
sudo chown -R root:root "$staging"
sudo mv "$staging" "$RELEASE"
sudo ln -sfn "$RELEASE" /opt/bifrost-web/current.new
sudo mv -Tf /opt/bifrost-web/current.new "$CURRENT"
sudo install -m 0644 "$SERVICE" /etc/systemd/system/bifrost-web.service
sudo systemctl daemon-reload
sudo systemctl enable bifrost-web.service
sudo systemctl restart bifrost-web.service

for _ in {1..30}; do
    if curl --fail --silent --show-error --max-time 3 http://100.88.0.9:8092/ >/dev/null; then
        exit 0
    fi
    sleep 1
done

echo "BifrOSt Web health check failed; rolling back" >&2
if [[ -n "$PREVIOUS" && -d "$PREVIOUS" ]]; then
    sudo ln -sfn "$PREVIOUS" /opt/bifrost-web/current.new
    sudo mv -Tf /opt/bifrost-web/current.new "$CURRENT"
    sudo systemctl restart bifrost-web.service
fi
exit 1