128 lines
No EOL
4.2 KiB
Bash
128 lines
No EOL
4.2 KiB
Bash
#!/bin/bash
|
|
# Fix service port issue where $PORT is not being expanded properly
|
|
|
|
set -e
|
|
|
|
echo "=== Fixing MockAPI Service Port Issue ==="
|
|
echo
|
|
|
|
APP_DIR="/opt/mockapi"
|
|
SERVICE_FILE="/etc/systemd/system/mockapi.service"
|
|
|
|
print_status() {
|
|
echo -e "\033[0;32m[+]\033[0m $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "\033[1;33m[!]\033[0m $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "\033[0;31m[!]\033[0m $1"
|
|
}
|
|
|
|
# Step 1: Check if service file exists
|
|
if [[ ! -f "$SERVICE_FILE" ]]; then
|
|
print_error "Service file not found: $SERVICE_FILE"
|
|
print_warning "Make sure MockAPI is installed first."
|
|
exit 1
|
|
fi
|
|
|
|
# Step 2: Backup service file
|
|
print_status "Backing up service file..."
|
|
BACKUP_FILE="${SERVICE_FILE}.backup.$(date +%s)"
|
|
sudo cp "$SERVICE_FILE" "$BACKUP_FILE"
|
|
print_status "Backup created: $BACKUP_FILE"
|
|
|
|
# Step 3: Check current ExecStart line
|
|
print_status "Checking current ExecStart configuration..."
|
|
CURRENT_EXEC=$(grep "^ExecStart=" "$SERVICE_FILE")
|
|
echo "Current ExecStart: $CURRENT_EXEC"
|
|
|
|
# Check if we have the escaped \$PORT issue
|
|
if echo "$CURRENT_EXEC" | grep -q "\\\\\$PORT"; then
|
|
print_warning "Found escaped \$PORT in ExecStart line. This causes Waitress to parse '\$PORT' as a string."
|
|
print_status "Fixing ExecStart line..."
|
|
|
|
# Create temporary file with fixed service config
|
|
TMP_SERVICE=$(mktemp)
|
|
|
|
# Read and fix the service file
|
|
sudo cat "$SERVICE_FILE" | while IFS= read -r line; do
|
|
if [[ "$line" =~ ^ExecStart=.*waitress-serve.* ]]; then
|
|
# Remove backslashes before $HOST and $PORT
|
|
fixed_line=$(echo "$line" | sed 's/--host=\\\$HOST/--host=$HOST/g' | sed 's/--port=\\\$PORT/--port=$PORT/g')
|
|
echo "$fixed_line"
|
|
print_status "Fixed: $fixed_line"
|
|
else
|
|
echo "$line"
|
|
fi
|
|
done > "$TMP_SERVICE"
|
|
|
|
# Replace service file
|
|
sudo mv "$TMP_SERVICE" "$SERVICE_FILE"
|
|
sudo chmod 644 "$SERVICE_FILE"
|
|
|
|
print_status "Service file updated."
|
|
else
|
|
print_status "ExecStart line looks correct (no escaped \$PORT)."
|
|
fi
|
|
|
|
# Step 4: Alternative approach - check if we should use environment variables directly
|
|
print_status "Checking if .env has PORT and HOST set..."
|
|
if [[ -f "${APP_DIR}/.env" ]]; then
|
|
ENV_PORT=$(grep "^PORT=" "${APP_DIR}/.env" | cut -d'=' -f2 || echo "8000")
|
|
ENV_HOST=$(grep "^HOST=" "${APP_DIR}/.env" | cut -d'=' -f2 || echo "0.0.0.0")
|
|
print_status "Found in .env: PORT=$ENV_PORT, HOST=$ENV_HOST"
|
|
|
|
# Validate PORT is numeric
|
|
if [[ ! "$ENV_PORT" =~ ^[0-9]+$ ]]; then
|
|
print_error "PORT in .env is not numeric: '$ENV_PORT'"
|
|
print_status "Setting PORT to 8000"
|
|
sudo sed -i "s/^PORT=.*/PORT=8000/" "${APP_DIR}/.env"
|
|
fi
|
|
else
|
|
print_warning ".env file not found at ${APP_DIR}/.env"
|
|
fi
|
|
|
|
# Step 5: Reload systemd and restart service
|
|
print_status "Reloading systemd daemon..."
|
|
sudo systemctl daemon-reload
|
|
|
|
print_status "Restarting MockAPI service..."
|
|
if sudo systemctl restart mockapi; then
|
|
print_status "Service restart initiated."
|
|
|
|
sleep 3
|
|
|
|
if sudo systemctl is-active --quiet mockapi; then
|
|
print_status "✓ Service is running successfully!"
|
|
|
|
echo
|
|
print_status "Testing health endpoint..."
|
|
if curl -f -s http://localhost:${ENV_PORT:-8000}/health > /dev/null 2>&1; then
|
|
print_status "✓ Health check passed!"
|
|
echo
|
|
echo "Access your MockAPI:"
|
|
echo " Admin UI: http://localhost:${ENV_PORT:-8000}/admin/login"
|
|
echo " API Docs: http://localhost:${ENV_PORT:-8000}/docs"
|
|
echo " Health check: http://localhost:${ENV_PORT:-8000}/health"
|
|
else
|
|
print_warning "Health check failed. Checking service status..."
|
|
sudo systemctl status mockapi --no-pager -l
|
|
fi
|
|
else
|
|
print_error "Service failed to start after fix."
|
|
echo
|
|
print_warning "Checking service logs..."
|
|
sudo journalctl -u mockapi -n 30 --no-pager
|
|
fi
|
|
else
|
|
print_error "Failed to restart service."
|
|
sudo systemctl status mockapi --no-pager -l
|
|
fi
|
|
|
|
echo
|
|
print_status "=== Fix completed ==="
|
|
echo "Backup service file: $BACKUP_FILE"
|
|
echo "If you encounter issues, restore with: sudo cp $BACKUP_FILE $SERVICE_FILE && sudo systemctl daemon-reload" |