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,46 +1,50 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
# NextWorkspace Deploy — clean-slate deployment
HEALTH_CHECK_RETRIES=10
HEALTH_CHECK_INTERVAL=2
REPO_DIR="/opt/NextWks" REPO_DIR="/opt/NextWks"
TARGET_DIR="/opt/workspace" TARGET_DIR="/opt/workspace"
SERVICE_NAME="nextworkspace" SERVICE_NAME="nextworkspace"
BINARY_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" cd "$REPO_DIR"
echo "[1/9] Pulling latest code..." echo "[1/8] Pulling latest code..."
git pull git pull
# 2. Build echo "[2/8] Building binary..."
echo "[2/9] Building binary..."
export PATH=$PATH:/usr/local/go/bin export PATH=$PATH:/usr/local/go/bin
go build -o "$BINARY_NAME" . go build -o "$BINARY_NAME" .
# 3. Remove old deployment # --- Greenfield path ---
echo "[3/9] Removing old deployment..." if [ "$GREENFIELD" = true ]; then
echo "[3/8] Removing old deployment..."
rm -rf "$TARGET_DIR" rm -rf "$TARGET_DIR"
# 4. Create target directory structure echo "[4/8] Creating target directories..."
echo "[4/9] Creating target directories..."
mkdir -p "$TARGET_DIR/configs/core/certs" mkdir -p "$TARGET_DIR/configs/core/certs"
# 5. Copy binary echo "[5/8] Copying binary..."
echo "[5/9] Copying binary..."
cp "$BINARY_NAME" "$TARGET_DIR/$BINARY_NAME" cp "$BINARY_NAME" "$TARGET_DIR/$BINARY_NAME"
# 6. Copy config files echo "[6/8] Copying config files..."
echo "[6/9] Copying config files..."
cp configs/core/config.yaml "$TARGET_DIR/configs/core/config.yaml" cp configs/core/config.yaml "$TARGET_DIR/configs/core/config.yaml"
cp configs/core/proxies.yaml "$TARGET_DIR/configs/core/proxies.yaml" cp configs/core/proxies.yaml "$TARGET_DIR/configs/core/proxies.yaml"
# 7. Write systemd service echo "[7/8] Writing systemd service..."
echo "[7/9] Writing systemd service..."
cat > /etc/systemd/system/$SERVICE_NAME.service <<UNIT cat > /etc/systemd/system/$SERVICE_NAME.service <<UNIT
[Unit] [Unit]
Description=NextWorkspace Description=NextWorkspace
@ -58,14 +62,28 @@ Group=root
WantedBy=multi-user.target WantedBy=multi-user.target
UNIT UNIT
# 8. Reload systemd and restart echo "[8/8] Starting service..."
echo "[8/9] Reloading systemd and restarting service..."
systemctl daemon-reload systemctl daemon-reload
systemctl enable $SERVICE_NAME systemctl enable --now $SERVICE_NAME
systemctl restart $SERVICE_NAME
# 9. Health check # --- Smart update path ---
echo "[9/9] Running health check..." 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 for i in $(seq 1 $HEALTH_CHECK_RETRIES); do
if curl -sf http://localhost:80/ > /dev/null 2>&1; then if curl -sf http://localhost:80/ > /dev/null 2>&1; then
echo "[OK] NextWorkspace is serving on http://localhost:80/" echo "[OK] NextWorkspace is serving on http://localhost:80/"

View file

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