NextWks/update.sh

68 lines
2.3 KiB
Bash
Executable file

#!/bin/bash
# ============================================================
# NextWks — Production Update Script
# Pulls latest code, rebuilds, restarts NextWks only.
# Users & auth are managed by Authelia (untouched).
#
# Run: cd /opt/nextwks && sudo bash update.sh
# cd /opt/nextwks && sudo bash update.sh --wipe-db
# ============================================================
set -euo pipefail
GREEN='\033[0;32m'; BLUE='\033[0;34m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; NC='\033[0m'
info() { echo -e "${BLUE}$1${NC}"; }
ok() { echo -e "${GREEN}$1${NC}"; }
warn() { echo -e "${YELLOW}$1${NC}"; }
error() { echo -e "${RED}$1${NC}"; }
REPO_URL="https://git.lohmar.co.uk/lexton-it/NextWks.git"
REPO_DIR="/tmp/nextwks-update"
INSTALL_DIR="/opt/nextwks"
BIN_DIR="${INSTALL_DIR}/bin"
DATA_DIR="${INSTALL_DIR}/data"
WIPE_DB=false
[ "${1:-}" = "--wipe-db" ] && WIPE_DB=true
info "Updating NextWks..."
[ "$WIPE_DB" = true ] && warn "Database wipe requested — users will be restored from Authelia"
# Clone fresh copy
rm -rf "$REPO_DIR" 2>/dev/null
git clone --depth 1 "$REPO_URL" "$REPO_DIR" --quiet
VERSION=$(cat "$REPO_DIR/VERSION" 2>/dev/null || echo "dev")
COMMIT_SHA=$(cd "$REPO_DIR" && git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# Build
cd "$REPO_DIR/src"
go build -ldflags="-s -w \
-X git.lohmar.co.uk/lexton-it/NextWks/core/version.Version=${VERSION} \
-X git.lohmar.co.uk/lexton-it/NextWks/core/version.BuildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ) \
-X git.lohmar.co.uk/lexton-it/NextWks/core/version.CommitSHA=$(git rev-parse --short HEAD)" \
-o /tmp/nextwks-core .
# Stop, optionally wipe, replace, restart
systemctl stop nextwks
if [ "$WIPE_DB" = true ]; then
find "$DATA_DIR" -name "*.db*" -delete 2>/dev/null || true
ok "Database wiped"
fi
cp /tmp/nextwks-core "$BIN_DIR/core"
chmod 755 "$BIN_DIR/core"
chown nextwks:nextwks "$BIN_DIR/core"
chown -R nextwks:nextwks "$DATA_DIR" 2>/dev/null || true
systemctl start nextwks
sleep 2
# Verify
if curl -s --max-time 3 http://localhost:8080/api/health | grep -q ok; then
ok "NextWks updated to v${VERSION} (${COMMIT_SHA})"
[ "$WIPE_DB" = true ] && info "Users bootstrapped from Authelia"
else
error "Health check failed — check: sudo journalctl -u nextwks -n 20"
fi
# Cleanup
rm -rf "$REPO_DIR" /tmp/nextwks-core