cyberhybridhub/hosting/install-systemd.sh
2026-06-04 19:31:04 +00:00

80 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Install cyberhybridhub-api + cyberhybridhub-web systemd units (requires sudo).
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
ENV_FILE="$ROOT/hosting/hosting.env"
if [ ! -f "$ENV_FILE" ]; then
echo "Missing $ENV_FILE" >&2
echo " cp hosting/hosting.env.example hosting/hosting.env" >&2
echo " # edit paths and API_PUBLIC_URL for this VM" >&2
exit 1
fi
# shellcheck source=/dev/null
source "$ENV_FILE"
: "${APP_ROOT:?APP_ROOT required in hosting/hosting.env}"
: "${APP_USER:?APP_USER required}"
: "${FLUTTER_BIN_DIR:?FLUTTER_BIN_DIR required}"
: "${DART_BIN:=/usr/bin/dart}"
: "${WEB_PORT:=80}"
: "${API_PORT:=3000}"
if [ ! -x "$DART_BIN" ]; then
echo "DART_BIN not executable: $DART_BIN" >&2
echo " Install Flutter (see hosting/README.md) or set DART_BIN in hosting/hosting.env" >&2
exit 1
fi
if [ ! -f "$APP_ROOT/build/web/index.html" ]; then
echo "Missing $APP_ROOT/build/web/index.html" >&2
echo " Run: ./scripts/deploy-production.sh" >&2
exit 1
fi
if [ ! -f "$APP_ROOT/server/.env" ]; then
echo "Missing $APP_ROOT/server/.env" >&2
exit 1
fi
substitute() {
local template=$1
local dest=$2
sed \
-e "s|@APP_ROOT@|${APP_ROOT}|g" \
-e "s|@APP_USER@|${APP_USER}|g" \
-e "s|@FLUTTER_BIN_DIR@|${FLUTTER_BIN_DIR}|g" \
-e "s|@DART_BIN@|${DART_BIN}|g" \
-e "s|@WEB_PORT@|${WEB_PORT}|g" \
-e "s|@API_PORT@|${API_PORT}|g" \
"$template" >"$dest"
}
TMPDIR="$(mktemp -d)"
trap 'rm -rf "$TMPDIR"' EXIT
substitute "$ROOT/hosting/cyberhybridhub-api.service" "$TMPDIR/cyberhybridhub-api.service"
substitute "$ROOT/hosting/cyberhybridhub-web.service" "$TMPDIR/cyberhybridhub-web.service"
echo "Installing systemd units..."
sudo cp "$TMPDIR/cyberhybridhub-api.service" /etc/systemd/system/
sudo cp "$TMPDIR/cyberhybridhub-web.service" /etc/systemd/system/
echo "Disabling legacy Fresh unit (if present)..."
sudo systemctl disable cyberhybridhub 2>/dev/null || true
sudo systemctl stop cyberhybridhub 2>/dev/null || true
sudo systemctl daemon-reload
sudo systemctl enable cyberhybridhub-api cyberhybridhub-web
sudo systemctl restart cyberhybridhub-api cyberhybridhub-web
echo ""
sudo systemctl status cyberhybridhub-api cyberhybridhub-web --no-pager || true
echo ""
echo "Verify:"
echo " sudo ss -tlnp | grep -E ':${WEB_PORT}|:${API_PORT}'"
echo " curl -sI http://127.0.0.1:${WEB_PORT}/"
echo " curl -sI http://127.0.0.1:${API_PORT}/"