mockapi/fix_remove_backslashes.sh
2026-03-16 18:40:22 +00:00

191 lines
No EOL
6.2 KiB
Bash

#!/bin/bash
# Simple fix: Remove backslashes from $HOST and $PORT in service file
# This allows systemd to expand variables from .env
set -e
echo "=== Fix: Remove backslashes from \$HOST and \$PORT in service file ==="
echo
SERVICE_FILE="/etc/systemd/system/mockapi.service"
APP_DIR="/opt/mockapi"
ENV_FILE="${APP_DIR}/.env"
# Check if running as root
if [[ $EUID -ne 0 ]]; then
echo "ERROR: This script requires sudo privileges."
exit 1
fi
# Check if service file exists
if [[ ! -f "$SERVICE_FILE" ]]; then
echo "ERROR: Service file not found: $SERVICE_FILE"
exit 1
fi
echo "Current service file: $SERVICE_FILE"
echo
# Backup
BACKUP="${SERVICE_FILE}.backup.$(date +%s)"
cp "$SERVICE_FILE" "$BACKUP"
echo "Backup created: $BACKUP"
echo
# Check current ExecStart
echo "Current ExecStart line:"
grep "^ExecStart=" "$SERVICE_FILE"
echo
# Check if we have escaped variables
if grep -q "\\\\\$HOST\|\\\\\$PORT" "$SERVICE_FILE"; then
echo "Found escaped variables (\\\$HOST and/or \\\$PORT). Removing backslashes..."
# Remove backslashes before $HOST and $PORT
sed -i 's/--host=\\\$HOST/--host=$HOST/g' "$SERVICE_FILE"
sed -i 's/--port=\\\$PORT/--port=$PORT/g' "$SERVICE_FILE"
echo "Backslashes removed."
else
echo "No escaped variables found. Checking for other patterns..."
# Check if we have $HOST and $PORT without backslashes
if grep -q "--host=\$HOST --port=\$PORT" "$SERVICE_FILE"; then
echo "Already has unescaped \$HOST and \$PORT. Good."
elif grep -q "--host=[0-9]" "$SERVICE_FILE"; then
echo "Has hardcoded host/port. Need to replace with variables."
# Extract current hardcoded values
CURRENT_LINE=$(grep "^ExecStart=" "$SERVICE_FILE")
HOST_VALUE=$(echo "$CURRENT_LINE" | sed -n 's/.*--host=\([^ ]*\).*/\1/p')
PORT_VALUE=$(echo "$CURRENT_LINE" | sed -n 's/.*--port=\([^ ]*\).*/\1/p')
echo "Current hardcoded values: host=$HOST_VALUE, port=$PORT_VALUE"
# Ensure .env has these values
if [[ ! -f "$ENV_FILE" ]]; then
echo "WARNING: .env file not found: $ENV_FILE"
else
# Update or add PORT in .env
if ! grep -q "^PORT=" "$ENV_FILE"; then
echo "PORT=$PORT_VALUE" >> "$ENV_FILE"
echo "Added PORT=$PORT_VALUE to .env"
fi
# Update or add HOST in .env
if ! grep -q "^HOST=" "$ENV_FILE"; then
echo "HOST=$HOST_VALUE" >> "$ENV_FILE"
echo "Added HOST=$HOST_VALUE to .env"
fi
fi
# Replace hardcoded values with variables
sed -i "s|--host=$HOST_VALUE --port=$PORT_VALUE|--host=\$HOST --port=\$PORT|" "$SERVICE_FILE"
echo "Replaced hardcoded values with variables."
else
echo "Unknown ExecStart format. Manual inspection needed."
fi
fi
echo
echo "Updated ExecStart line:"
grep "^ExecStart=" "$SERVICE_FILE"
echo
# Ensure PORT exists in .env
if [[ -f "$ENV_FILE" ]]; then
echo "Checking .env file..."
if ! grep -q "^PORT=" "$ENV_FILE"; then
echo "WARNING: PORT not found in .env. Adding PORT=8000..."
echo "PORT=8000" >> "$ENV_FILE"
fi
if ! grep -q "^HOST=" "$ENV_FILE"; then
echo "WARNING: HOST not found in .env. Adding HOST=0.0.0.0..."
echo "HOST=0.0.0.0" >> "$ENV_FILE"
fi
PORT_VALUE=$(grep "^PORT=" "$ENV_FILE" | cut -d'=' -f2)
echo "Current PORT in .env: $PORT_VALUE"
else
echo "WARNING: .env file not found at $ENV_FILE"
echo "Creating basic .env file..."
cat > "$ENV_FILE" << EOF
PORT=8000
HOST=0.0.0.0
OAUTH2_ISSUER=http://localhost:8000
EOF
chmod 600 "$ENV_FILE"
echo "Created .env file with default values"
fi
# Reload systemd
echo
echo "Reloading systemd daemon..."
systemctl daemon-reload
# Restart service
echo "Restarting MockAPI service..."
if systemctl restart mockapi; then
echo "Service restart initiated."
sleep 3
if systemctl is-active --quiet mockapi; then
echo "SUCCESS: Service is running!"
# Test health endpoint
PORT_VALUE=$(grep "^PORT=" "$ENV_FILE" 2>/dev/null | cut -d'=' -f2 || echo "8000")
echo
echo "Testing health endpoint on port $PORT_VALUE..."
if curl -f -s --max-time 10 http://localhost:$PORT_VALUE/health > /dev/null 2>&1; then
echo "✓ Health check passed!"
echo
echo "=== FIX SUCCESSFUL ==="
echo "MockAPI now uses PORT from .env file dynamically."
echo
echo "To change port:"
echo " 1. Edit $ENV_FILE"
echo " 2. Update the PORT value"
echo " 3. Run: sudo systemctl restart mockapi"
echo
echo "Current port: $PORT_VALUE"
echo "Access: http://localhost:$PORT_VALUE"
else
echo "WARNING: Service running but health check failed."
echo "Checking logs..."
journalctl -u mockapi -n 10 --no-pager
fi
else
echo "ERROR: Service failed to start after fix."
echo "Checking logs..."
journalctl -u mockapi -n 20 --no-pager
# Try fallback: check if maybe we need different escaping
echo
echo "Trying alternative approach..."
# Check if the issue is with $HOST $PORT not being expanded
# Maybe systemd isn't expanding them. Let's check EnvironmentFile line
if ! grep -q "EnvironmentFile=" "$SERVICE_FILE"; then
echo "Adding EnvironmentFile directive..."
# Add EnvironmentFile after existing Environment lines
sed -i '/^Environment=.*/a EnvironmentFile='"$APP_DIR"'/.env' "$SERVICE_FILE"
systemctl daemon-reload
systemctl restart mockapi
fi
fi
else
echo "ERROR: Failed to restart service."
systemctl status mockapi --no-pager -l
fi
echo
echo "=== Summary ==="
echo "Backup: $BACKUP"
echo "Service file updated to use \$HOST and \$PORT (no backslashes)"
echo "Systemd should now expand these from .env file at runtime"
echo
echo "If port changes don't work, check:"
echo " 1. .env file exists and is readable by service user"
echo " 2. PORT value in .env is numeric"
echo " 3. Service file has 'EnvironmentFile=$APP_DIR/.env' line"