79 lines
1.9 KiB
Bash
Executable file
79 lines
1.9 KiB
Bash
Executable file
#!/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"
|
|
|
|
echo "=== NextWorkspace Deploy ==="
|
|
|
|
# 1. Navigate to repo and pull latest
|
|
cd "$REPO_DIR"
|
|
echo "[1/9] Pulling latest code..."
|
|
git pull
|
|
|
|
# 2. Build
|
|
echo "[2/9] 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"
|
|
|
|
# 4. Create target directory structure
|
|
echo "[4/9] 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"
|
|
|
|
# 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"
|
|
|
|
# 7. Write systemd service
|
|
echo "[7/9] Writing systemd service..."
|
|
cat > /etc/systemd/system/$SERVICE_NAME.service <<UNIT
|
|
[Unit]
|
|
Description=NextWorkspace
|
|
After=network.target
|
|
|
|
[Service]
|
|
Environment=CONFIG_DIR=$TARGET_DIR/configs/core
|
|
ExecStart=$TARGET_DIR/$BINARY_NAME
|
|
WorkingDirectory=$TARGET_DIR
|
|
Restart=always
|
|
User=root
|
|
Group=root
|
|
|
|
[Install]
|
|
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
|
|
|
|
# 9. Health check
|
|
echo "[9/9] 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
|
|
done
|
|
|
|
echo "[FAIL] Health check failed — service did not respond on port 80"
|
|
exit 1
|