feat(deploy): add --destroy flag and smart update modes

This commit is contained in:
Claus Lohmar 2026-07-06 15:36:48 +01:00
parent cd15d294a1
commit 29b9f3c159
2 changed files with 57 additions and 39 deletions

View file

@ -1,47 +1,51 @@
#!/usr/bin/env bash
set -euo pipefail
# NextWorkspace Deploy — clean-slate deployment
HEALTH_CHECK_RETRIES=10
HEALTH_CHECK_INTERVAL=2
REPO_DIR="/opt/NextWks"
TARGET_DIR="/opt/workspace"
SERVICE_NAME="nextworkspace"
BINARY_NAME="nextworkspace"
HEALTH_CHECK_RETRIES=10
HEALTH_CHECK_INTERVAL=2
echo "=== NextWorkspace Deploy ==="
# --- Mode detection ---
GREENFIELD=false
if [ "${1:-}" = "--destroy" ]; then
GREENFIELD=true
echo "[MODE] Greenfield deploy (--destroy)"
elif [ ! -d "$TARGET_DIR" ]; then
GREENFIELD=true
echo "[MODE] Greenfield deploy (target missing)"
else
echo "[MODE] Smart update (target exists)"
fi
# 1. Navigate to repo and pull latest
# --- Common: pull + build ---
cd "$REPO_DIR"
echo "[1/9] Pulling latest code..."
echo "[1/8] Pulling latest code..."
git pull
# 2. Build
echo "[2/9] Building binary..."
echo "[2/8] Building binary..."
export PATH=$PATH:/usr/local/go/bin
go build -o "$BINARY_NAME" .
# 3. Remove old deployment
echo "[3/9] Removing old deployment..."
rm -rf "$TARGET_DIR"
# --- Greenfield path ---
if [ "$GREENFIELD" = true ]; then
echo "[3/8] Removing old deployment..."
rm -rf "$TARGET_DIR"
# 4. Create target directory structure
echo "[4/9] Creating target directories..."
mkdir -p "$TARGET_DIR/configs/core/certs"
echo "[4/8] Creating target directories..."
mkdir -p "$TARGET_DIR/configs/core/certs"
# 5. Copy binary
echo "[5/9] Copying binary..."
cp "$BINARY_NAME" "$TARGET_DIR/$BINARY_NAME"
echo "[5/8] Copying binary..."
cp "$BINARY_NAME" "$TARGET_DIR/$BINARY_NAME"
# 6. Copy config files
echo "[6/9] Copying config files..."
cp configs/core/config.yaml "$TARGET_DIR/configs/core/config.yaml"
cp configs/core/proxies.yaml "$TARGET_DIR/configs/core/proxies.yaml"
echo "[6/8] Copying config files..."
cp configs/core/config.yaml "$TARGET_DIR/configs/core/config.yaml"
cp configs/core/proxies.yaml "$TARGET_DIR/configs/core/proxies.yaml"
# 7. Write systemd service
echo "[7/9] Writing systemd service..."
cat > /etc/systemd/system/$SERVICE_NAME.service <<UNIT
echo "[7/8] Writing systemd service..."
cat > /etc/systemd/system/$SERVICE_NAME.service <<UNIT
[Unit]
Description=NextWorkspace
After=network.target
@ -58,21 +62,35 @@ Group=root
WantedBy=multi-user.target
UNIT
# 8. Reload systemd and restart
echo "[8/9] Reloading systemd and restarting service..."
systemctl daemon-reload
systemctl enable $SERVICE_NAME
systemctl restart $SERVICE_NAME
echo "[8/8] Starting service..."
systemctl daemon-reload
systemctl enable --now $SERVICE_NAME
# 9. Health check
echo "[9/9] Running health check..."
# --- Smart update path ---
else
echo "[3/8] Stopping service..."
systemctl stop $SERVICE_NAME
echo "[4/8] Swapping binary..."
cp "$BINARY_NAME" "$TARGET_DIR/$BINARY_NAME"
echo "[5/8] Refreshing configs..."
cp configs/core/config.yaml "$TARGET_DIR/configs/core/config.yaml"
cp configs/core/proxies.yaml "$TARGET_DIR/configs/core/proxies.yaml"
echo "[6/8] Restarting service..."
systemctl restart $SERVICE_NAME
fi
# --- Health check (both modes) ---
echo "[*] Running health check..."
for i in $(seq 1 $HEALTH_CHECK_RETRIES); do
if curl -sf http://localhost:80/ > /dev/null 2>&1; then
echo "[OK] NextWorkspace is serving on http://localhost:80/"
exit 0
fi
echo " Attempt $i/$HEALTH_CHECK_RETRIES — not ready yet..."
sleep $HEALTH_CHECK_INTERVAL
if curl -sf http://localhost:80/ > /dev/null 2>&1; then
echo "[OK] NextWorkspace is serving on http://localhost:80/"
exit 0
fi
echo " Attempt $i/$HEALTH_CHECK_RETRIES — not ready yet..."
sleep $HEALTH_CHECK_INTERVAL
done
echo "[FAIL] Health check failed — service did not respond on port 80"

View file

@ -41,4 +41,4 @@ else
fi
echo "[DONE] Bootstrapping complete. Running first deploy..."
"$REPO_DIR/deploy.sh"
"$REPO_DIR/deploy.sh" --destroy