chore: auto-commit 2026-03-16 18:05
This commit is contained in:
parent
eed80d3b80
commit
7ea042e9f3
5 changed files with 650 additions and 6 deletions
140
fix_port_immediate.sh
Normal file
140
fix_port_immediate.sh
Normal file
|
|
@ -0,0 +1,140 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# Immediate fix for Waitress port issue - run on VPS
|
||||||
|
# This script fixes the service file to use hardcoded port from .env
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "=== Immediate Fix for Waitress Port Issue ==="
|
||||||
|
echo
|
||||||
|
|
||||||
|
APP_DIR="/opt/mockapi"
|
||||||
|
SERVICE_FILE="/etc/systemd/system/mockapi.service"
|
||||||
|
ENV_FILE="${APP_DIR}/.env"
|
||||||
|
|
||||||
|
# Check if running as root or with sudo
|
||||||
|
if [[ $EUID -eq 0 ]]; then
|
||||||
|
echo "Running as root."
|
||||||
|
elif sudo -n true 2>/dev/null; then
|
||||||
|
echo "Has sudo privileges."
|
||||||
|
else
|
||||||
|
echo "ERROR: This script needs sudo privileges."
|
||||||
|
echo "Run with: sudo bash $0"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Step 1: Check files exist
|
||||||
|
if [[ ! -f "$ENV_FILE" ]]; then
|
||||||
|
echo "ERROR: .env file not found at $ENV_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ! -f "$SERVICE_FILE" ]]; then
|
||||||
|
echo "ERROR: Service file not found at $SERVICE_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Step 2: Read port from .env
|
||||||
|
PORT=$(grep "^PORT=" "$ENV_FILE" | cut -d'=' -f2 || echo "8000")
|
||||||
|
HOST=$(grep "^HOST=" "$ENV_FILE" | cut -d'=' -f2 || echo "0.0.0.0")
|
||||||
|
|
||||||
|
# Validate port is numeric
|
||||||
|
if [[ ! "$PORT" =~ ^[0-9]+$ ]]; then
|
||||||
|
echo "WARNING: PORT '$PORT' is not numeric. Using 8000."
|
||||||
|
PORT="8000"
|
||||||
|
# Fix .env
|
||||||
|
sed -i "s/^PORT=.*/PORT=8000/" "$ENV_FILE" 2>/dev/null || \
|
||||||
|
echo "PORT=8000" >> "$ENV_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Found in .env: HOST=$HOST, PORT=$PORT"
|
||||||
|
echo
|
||||||
|
|
||||||
|
# Step 3: Backup service file
|
||||||
|
BACKUP="${SERVICE_FILE}.backup.$(date +%s)"
|
||||||
|
cp "$SERVICE_FILE" "$BACKUP"
|
||||||
|
echo "Backup created: $BACKUP"
|
||||||
|
|
||||||
|
# Step 4: Update service file
|
||||||
|
echo "Updating service file..."
|
||||||
|
# Get virtual environment path from current service file or use default
|
||||||
|
VENV_DIR=$(grep "Environment=\"PATH=" "$SERVICE_FILE" | cut -d'=' -f2 | cut -d':' -f1 | sed 's/"//g' || echo "/opt/mockapi/venv")
|
||||||
|
|
||||||
|
# Extract other settings from current file
|
||||||
|
SERVICE_USER=$(grep "^User=" "$SERVICE_FILE" | cut -d'=' -f2)
|
||||||
|
SERVICE_GROUP=$(grep "^Group=" "$SERVICE_FILE" | cut -d'=' -f2)
|
||||||
|
|
||||||
|
# Create fixed service file
|
||||||
|
cat > /tmp/mockapi_fixed.service << EOF
|
||||||
|
[Unit]
|
||||||
|
Description=Mock API Service
|
||||||
|
After=network.target
|
||||||
|
Wants=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=$SERVICE_USER
|
||||||
|
Group=$SERVICE_GROUP
|
||||||
|
WorkingDirectory=$APP_DIR
|
||||||
|
Environment="PATH=$VENV_DIR/bin"
|
||||||
|
Environment="PYTHONPATH=$APP_DIR"
|
||||||
|
EnvironmentFile=$APP_DIR/.env
|
||||||
|
# Fixed: Hardcoded port from .env
|
||||||
|
ExecStart=$VENV_DIR/bin/waitress-serve --host=$HOST --port=$PORT wsgi:wsgi_app
|
||||||
|
Restart=always
|
||||||
|
RestartSec=10
|
||||||
|
StandardOutput=syslog
|
||||||
|
StandardError=syslog
|
||||||
|
SyslogIdentifier=mockapi
|
||||||
|
|
||||||
|
# Security hardening
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ReadWritePaths=$APP_DIR
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
|
||||||
|
# Replace service file
|
||||||
|
cp /tmp/mockapi_fixed.service "$SERVICE_FILE"
|
||||||
|
rm /tmp/mockapi_fixed.service
|
||||||
|
|
||||||
|
echo "Service file updated with hardcoded port $PORT."
|
||||||
|
echo
|
||||||
|
|
||||||
|
# Step 5: Reload and restart
|
||||||
|
echo "Reloading systemd..."
|
||||||
|
systemctl daemon-reload
|
||||||
|
|
||||||
|
echo "Restarting service..."
|
||||||
|
if systemctl restart mockapi; then
|
||||||
|
echo "Service restart initiated."
|
||||||
|
|
||||||
|
# Wait and check
|
||||||
|
sleep 3
|
||||||
|
|
||||||
|
if systemctl is-active --quiet mockapi; then
|
||||||
|
echo "SUCCESS: Service is running!"
|
||||||
|
echo
|
||||||
|
echo "MockAPI should now be accessible at:"
|
||||||
|
echo " http://$(hostname -I | awk '{print $1}'):$PORT"
|
||||||
|
echo " http://localhost:$PORT"
|
||||||
|
echo
|
||||||
|
echo "Test with: curl http://localhost:$PORT/health"
|
||||||
|
else
|
||||||
|
echo "WARNING: Service not active after restart."
|
||||||
|
echo "Checking logs..."
|
||||||
|
journalctl -u mockapi -n 20 --no-pager
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "ERROR: Failed to restart service."
|
||||||
|
systemctl status mockapi --no-pager -l
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "=== Fix completed ==="
|
||||||
|
echo "If issues persist, restore backup:"
|
||||||
|
echo " cp $BACKUP $SERVICE_FILE"
|
||||||
|
echo " systemctl daemon-reload"
|
||||||
|
echo " systemctl restart mockapi"
|
||||||
128
fix_service_port.sh
Normal file
128
fix_service_port.sh
Normal file
|
|
@ -0,0 +1,128 @@
|
||||||
|
#!/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"
|
||||||
|
|
@ -297,6 +297,82 @@ else
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Step 5.5: Fix service file port issue
|
||||||
|
print_status "Step 5.5: Fixing service file port configuration"
|
||||||
|
|
||||||
|
SERVICE_FILE="/etc/systemd/system/mockapi.service"
|
||||||
|
if [[ -f "$SERVICE_FILE" ]]; then
|
||||||
|
print_status "Checking service file for port configuration issues..."
|
||||||
|
|
||||||
|
# Check if service file has the escaped \$PORT issue
|
||||||
|
if grep -q "\\\\\$PORT" "$SERVICE_FILE" || grep -q "--port=\\\$PORT" "$SERVICE_FILE"; then
|
||||||
|
print_warning "Found escaped \$PORT in service file. Fixing..."
|
||||||
|
|
||||||
|
# Read port from .env
|
||||||
|
SERVICE_PORT=$(grep "^PORT=" "$ENV_FILE" | cut -d'=' -f2 || echo "8000")
|
||||||
|
SERVICE_HOST=$(grep "^HOST=" "$ENV_FILE" | cut -d'=' -f2 || echo "0.0.0.0")
|
||||||
|
|
||||||
|
# Validate port
|
||||||
|
if [[ ! "$SERVICE_PORT" =~ ^[0-9]+$ ]]; then
|
||||||
|
print_warning "PORT '$SERVICE_PORT' is not numeric. Using 8000."
|
||||||
|
SERVICE_PORT="8000"
|
||||||
|
sed -i "s/^PORT=.*/PORT=8000/" "$ENV_FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_status "Updating service file with hardcoded port: $SERVICE_PORT"
|
||||||
|
|
||||||
|
# Backup service file
|
||||||
|
SERVICE_BACKUP="${SERVICE_FILE}.backup.$(date +%s)"
|
||||||
|
sudo cp "$SERVICE_FILE" "$SERVICE_BACKUP"
|
||||||
|
print_status "Service file backed up to: $SERVICE_BACKUP"
|
||||||
|
|
||||||
|
# Get VENV_DIR from current service file
|
||||||
|
VENV_DIR=$(grep 'Environment="PATH=' "$SERVICE_FILE" | cut -d'=' -f2 | cut -d':' -f1 | sed 's/"//g' || echo "/opt/mockapi/venv")
|
||||||
|
|
||||||
|
# Create fixed service file
|
||||||
|
sudo tee "$SERVICE_FILE" > /dev/null << EOF
|
||||||
|
[Unit]
|
||||||
|
Description=Mock API Service
|
||||||
|
After=network.target
|
||||||
|
Wants=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=$(grep "^User=" "$SERVICE_FILE" | cut -d'=' -f2)
|
||||||
|
Group=$(grep "^Group=" "$SERVICE_FILE" | cut -d'=' -f2)
|
||||||
|
WorkingDirectory=$APP_DIR
|
||||||
|
Environment="PATH=$VENV_DIR/bin"
|
||||||
|
Environment="PYTHONPATH=$APP_DIR"
|
||||||
|
EnvironmentFile=$APP_DIR/.env
|
||||||
|
# Fixed: Hardcoded port from .env
|
||||||
|
ExecStart=$VENV_DIR/bin/waitress-serve --host=$SERVICE_HOST --port=$SERVICE_PORT wsgi:wsgi_app
|
||||||
|
Restart=always
|
||||||
|
RestartSec=10
|
||||||
|
StandardOutput=syslog
|
||||||
|
StandardError=syslog
|
||||||
|
SyslogIdentifier=mockapi
|
||||||
|
|
||||||
|
# Security hardening
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ReadWritePaths=$APP_DIR
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
|
||||||
|
print_status "Service file updated with hardcoded port $SERVICE_PORT."
|
||||||
|
sudo systemctl daemon-reload
|
||||||
|
print_status "Systemd daemon reloaded."
|
||||||
|
else
|
||||||
|
print_status "Service file port configuration looks correct."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
print_warning "Service file not found: $SERVICE_FILE"
|
||||||
|
print_warning "Service won't be fixed. Install service first with install.sh."
|
||||||
|
fi
|
||||||
|
|
||||||
# Step 6: Restart the service
|
# Step 6: Restart the service
|
||||||
print_status "Step 6: Restarting MockAPI service"
|
print_status "Step 6: Restarting MockAPI service"
|
||||||
sudo systemctl restart mockapi
|
sudo systemctl restart mockapi
|
||||||
|
|
@ -311,13 +387,19 @@ if sudo systemctl is-active --quiet mockapi; then
|
||||||
|
|
||||||
echo
|
echo
|
||||||
print_status "Testing API endpoints..."
|
print_status "Testing API endpoints..."
|
||||||
if curl -f -s http://localhost:8000/health > /dev/null 2>&1; then
|
# Read port from .env for testing
|
||||||
print_status "✓ Health check passed: http://localhost:8000/health"
|
TEST_PORT=$(grep "^PORT=" "$ENV_FILE" 2>/dev/null | cut -d'=' -f2 || echo "8000")
|
||||||
|
if [[ ! "$TEST_PORT" =~ ^[0-9]+$ ]]; then
|
||||||
|
TEST_PORT="8000"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if curl -f -s http://localhost:$TEST_PORT/health > /dev/null 2>&1; then
|
||||||
|
print_status "✓ Health check passed: http://localhost:$TEST_PORT/health"
|
||||||
echo
|
echo
|
||||||
echo "Access your MockAPI:"
|
echo "Access your MockAPI:"
|
||||||
echo " Admin UI: http://localhost:8000/admin/login"
|
echo " Admin UI: http://localhost:$TEST_PORT/admin/login"
|
||||||
echo " API Docs: http://localhost:8000/docs"
|
echo " API Docs: http://localhost:$TEST_PORT/docs"
|
||||||
echo " Health check: http://localhost:8000/health"
|
echo " Health check: http://localhost:$TEST_PORT/health"
|
||||||
else
|
else
|
||||||
print_warning "Health check failed. Checking logs..."
|
print_warning "Health check failed. Checking logs..."
|
||||||
sudo journalctl -u mockapi -n 10 --no-pager
|
sudo journalctl -u mockapi -n 10 --no-pager
|
||||||
|
|
|
||||||
262
fix_waitress_port.sh
Normal file
262
fix_waitress_port.sh
Normal file
|
|
@ -0,0 +1,262 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# Comprehensive fix for Waitress port parsing issue on VPS
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "=== Fixing Waitress Port Parsing Issue ==="
|
||||||
|
echo
|
||||||
|
|
||||||
|
APP_DIR="/opt/mockapi"
|
||||||
|
SERVICE_FILE="/etc/systemd/system/mockapi.service"
|
||||||
|
ENV_FILE="${APP_DIR}/.env"
|
||||||
|
|
||||||
|
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 environment
|
||||||
|
print_status "Step 1: Checking environment"
|
||||||
|
if [[ ! -d "$APP_DIR" ]]; then
|
||||||
|
print_error "Application directory not found: $APP_DIR"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ! -f "$SERVICE_FILE" ]]; then
|
||||||
|
print_error "Service file not found: $SERVICE_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
cd "$APP_DIR"
|
||||||
|
|
||||||
|
# Step 2: Read current port from .env
|
||||||
|
print_status "Step 2: Reading current configuration"
|
||||||
|
PORT="8000"
|
||||||
|
HOST="0.0.0.0"
|
||||||
|
|
||||||
|
if [[ -f "$ENV_FILE" ]]; then
|
||||||
|
if grep -q "^PORT=" "$ENV_FILE"; then
|
||||||
|
PORT=$(grep "^PORT=" "$ENV_FILE" | cut -d'=' -f2)
|
||||||
|
print_status "Found PORT in .env: $PORT"
|
||||||
|
|
||||||
|
# Validate PORT is numeric
|
||||||
|
if [[ ! "$PORT" =~ ^[0-9]+$ ]]; then
|
||||||
|
print_error "PORT is not numeric: '$PORT'. Setting to 8000."
|
||||||
|
PORT="8000"
|
||||||
|
sudo sed -i "s/^PORT=.*/PORT=8000/" "$ENV_FILE"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
print_warning "PORT not found in .env, adding PORT=8000"
|
||||||
|
echo "PORT=8000" | sudo tee -a "$ENV_FILE" > /dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
if grep -q "^HOST=" "$ENV_FILE"; then
|
||||||
|
HOST=$(grep "^HOST=" "$ENV_FILE" | cut -d'=' -f2)
|
||||||
|
print_status "Found HOST in .env: $HOST"
|
||||||
|
else
|
||||||
|
print_warning "HOST not found in .env, adding HOST=0.0.0.0"
|
||||||
|
echo "HOST=0.0.0.0" | sudo tee -a "$ENV_FILE" > /dev/null
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
print_error ".env file not found: $ENV_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Step 3: Backup service file
|
||||||
|
print_status "Step 3: Backing up service file"
|
||||||
|
BACKUP_FILE="${SERVICE_FILE}.backup.$(date +%s)"
|
||||||
|
sudo cp "$SERVICE_FILE" "$BACKUP_FILE"
|
||||||
|
print_status "Backup created: $BACKUP_FILE"
|
||||||
|
|
||||||
|
# Step 4: Fix service file - OPTION 1: Use shell expansion
|
||||||
|
print_status "Step 4: Fixing service file ExecStart command"
|
||||||
|
print_warning "Current issue: Waitress receives literal string '\$PORT' instead of expanded value"
|
||||||
|
print_status "Solution 1: Use /bin/sh -c to enable shell variable expansion"
|
||||||
|
|
||||||
|
# Get the virtual environment path
|
||||||
|
VENV_DIR="${APP_DIR}/venv"
|
||||||
|
if [[ ! -d "$VENV_DIR" ]]; then
|
||||||
|
print_error "Virtual environment not found: $VENV_DIR"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Read current service file to get other settings
|
||||||
|
CURRENT_USER=$(grep "^User=" "$SERVICE_FILE" | cut -d'=' -f2)
|
||||||
|
CURRENT_GROUP=$(grep "^Group=" "$SERVICE_FILE" | cut -d'=' -f2)
|
||||||
|
CURRENT_WORKDIR=$(grep "^WorkingDirectory=" "$SERVICE_FILE" | cut -d'=' -f2)
|
||||||
|
|
||||||
|
print_status "Current service settings:"
|
||||||
|
print_status " User: $CURRENT_USER"
|
||||||
|
print_status " Group: $CURRENT_GROUP"
|
||||||
|
print_status " WorkingDirectory: $CURRENT_WORKDIR"
|
||||||
|
|
||||||
|
# Create new service file with fixed ExecStart
|
||||||
|
print_status "Creating fixed service file..."
|
||||||
|
sudo tee "$SERVICE_FILE" > /dev/null << EOF
|
||||||
|
[Unit]
|
||||||
|
Description=Mock API Service
|
||||||
|
After=network.target
|
||||||
|
Wants=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=$CURRENT_USER
|
||||||
|
Group=$CURRENT_GROUP
|
||||||
|
WorkingDirectory=$CURRENT_WORKDIR
|
||||||
|
Environment="PATH=$VENV_DIR/bin"
|
||||||
|
Environment="PYTHONPATH=$APP_DIR"
|
||||||
|
EnvironmentFile=$APP_DIR/.env
|
||||||
|
# Use shell to expand environment variables
|
||||||
|
ExecStart=/bin/sh -c "$VENV_DIR/bin/waitress-serve --host=\$HOST --port=\$PORT wsgi:wsgi_app"
|
||||||
|
Restart=always
|
||||||
|
RestartSec=10
|
||||||
|
StandardOutput=syslog
|
||||||
|
StandardError=syslog
|
||||||
|
SyslogIdentifier=mockapi
|
||||||
|
|
||||||
|
# Security hardening
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ReadWritePaths=$APP_DIR
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
|
||||||
|
print_status "Service file updated with shell expansion."
|
||||||
|
|
||||||
|
# Step 5: Alternative simpler fix - hardcode port based on .env value
|
||||||
|
print_status "Alternative approach available: Hardcode port in service file"
|
||||||
|
print_warning "Would you like to hardcode port $PORT in service file instead? (y/N)"
|
||||||
|
read -p "Choice: " -n 1 -r
|
||||||
|
echo
|
||||||
|
|
||||||
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||||
|
print_status "Hardcoding port $PORT in service file..."
|
||||||
|
sudo tee "$SERVICE_FILE" > /dev/null << EOF
|
||||||
|
[Unit]
|
||||||
|
Description=Mock API Service
|
||||||
|
After=network.target
|
||||||
|
Wants=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=$CURRENT_USER
|
||||||
|
Group=$CURRENT_GROUP
|
||||||
|
WorkingDirectory=$CURRENT_WORKDIR
|
||||||
|
Environment="PATH=$VENV_DIR/bin"
|
||||||
|
Environment="PYTHONPATH=$APP_DIR"
|
||||||
|
EnvironmentFile=$APP_DIR/.env
|
||||||
|
# Hardcoded port based on .env value
|
||||||
|
ExecStart=$VENV_DIR/bin/waitress-serve --host=$HOST --port=$PORT wsgi:wsgi_app
|
||||||
|
Restart=always
|
||||||
|
RestartSec=10
|
||||||
|
StandardOutput=syslog
|
||||||
|
StandardError=syslog
|
||||||
|
SyslogIdentifier=mockapi
|
||||||
|
|
||||||
|
# Security hardening
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ReadWritePaths=$APP_DIR
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
print_status "Service file updated with hardcoded port $PORT."
|
||||||
|
print_warning "Note: If you change PORT in .env, you must update service file manually."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Step 6: Reload and restart
|
||||||
|
print_status "Step 5: Reloading systemd daemon..."
|
||||||
|
sudo systemctl daemon-reload
|
||||||
|
|
||||||
|
print_status "Step 6: Restarting service..."
|
||||||
|
sudo systemctl restart mockapi
|
||||||
|
|
||||||
|
sleep 3
|
||||||
|
|
||||||
|
# Step 7: Verify
|
||||||
|
print_status "Step 7: Verifying service status..."
|
||||||
|
if sudo systemctl is-active --quiet mockapi; then
|
||||||
|
print_status "✓ Service is running!"
|
||||||
|
|
||||||
|
echo
|
||||||
|
print_status "Testing health endpoint on port $PORT..."
|
||||||
|
if curl -f -s --max-time 10 http://localhost:$PORT/health > /dev/null 2>&1; then
|
||||||
|
print_status "✓ Health check passed!"
|
||||||
|
echo
|
||||||
|
echo "=== Service Fixed Successfully ==="
|
||||||
|
echo "MockAPI is now running on:"
|
||||||
|
echo " URL: http://localhost:$PORT"
|
||||||
|
echo " Admin: http://localhost:$PORT/admin/login"
|
||||||
|
echo " Docs: http://localhost:$PORT/docs"
|
||||||
|
echo " Health: http://localhost:$PORT/health"
|
||||||
|
else
|
||||||
|
print_warning "Health check failed (might be starting up). Checking logs..."
|
||||||
|
sleep 2
|
||||||
|
if curl -f -s --max-time 5 http://localhost:$PORT/health > /dev/null 2>&1; then
|
||||||
|
print_status "✓ Health check passed on second attempt!"
|
||||||
|
else
|
||||||
|
print_warning "Service running but health check failing. Checking logs..."
|
||||||
|
sudo journalctl -u mockapi -n 20 --no-pager
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
print_error "Service failed to start after fix."
|
||||||
|
echo
|
||||||
|
print_warning "Checking service logs..."
|
||||||
|
sudo journalctl -u mockapi -n 30 --no-pager
|
||||||
|
echo
|
||||||
|
print_warning "Trying alternative approach with simplified service file..."
|
||||||
|
|
||||||
|
# Try a simpler approach - just hardcode common values
|
||||||
|
print_status "Creating minimal service file..."
|
||||||
|
sudo tee "$SERVICE_FILE" > /dev/null << EOF
|
||||||
|
[Unit]
|
||||||
|
Description=Mock API Service
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=$CURRENT_USER
|
||||||
|
Group=$CURRENT_GROUP
|
||||||
|
WorkingDirectory=$APP_DIR
|
||||||
|
Environment="PATH=$VENV_DIR/bin"
|
||||||
|
ExecStart=$VENV_DIR/bin/waitress-serve --host=0.0.0.0 --port=8000 wsgi:wsgi_app
|
||||||
|
Restart=always
|
||||||
|
RestartSec=10
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
|
EOF
|
||||||
|
|
||||||
|
sudo systemctl daemon-reload
|
||||||
|
sudo systemctl restart mockapi
|
||||||
|
|
||||||
|
sleep 3
|
||||||
|
|
||||||
|
if sudo systemctl is-active --quiet mockapi; then
|
||||||
|
print_status "✓ Service started with hardcoded values!"
|
||||||
|
print_warning "Running on default port 8000. Update .env to match."
|
||||||
|
sudo sed -i "s/^PORT=.*/PORT=8000/" "$ENV_FILE"
|
||||||
|
else
|
||||||
|
print_error "All fixes failed. Please check manual configuration."
|
||||||
|
sudo journalctl -u mockapi -n 30 --no-pager
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo
|
||||||
|
print_status "=== Fix script completed ==="
|
||||||
|
echo "Backup service file: $BACKUP_FILE"
|
||||||
|
echo "Current .env PORT: $PORT"
|
||||||
|
echo "Current .env HOST: $HOST"
|
||||||
34
install.sh
34
install.sh
|
|
@ -299,6 +299,36 @@ setup_systemd_service() {
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Read HOST and PORT from .env for service file
|
||||||
|
SERVICE_HOST="0.0.0.0"
|
||||||
|
SERVICE_PORT="8000"
|
||||||
|
|
||||||
|
if [[ -f "${APP_DIR}/.env" ]]; then
|
||||||
|
# Read HOST from .env
|
||||||
|
if grep -q "^HOST=" "${APP_DIR}/.env"; then
|
||||||
|
SERVICE_HOST=$(grep "^HOST=" "${APP_DIR}/.env" | cut -d'=' -f2)
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Read PORT from .env (use current PORT variable as fallback)
|
||||||
|
if grep -q "^PORT=" "${APP_DIR}/.env"; then
|
||||||
|
SERVICE_PORT=$(grep "^PORT=" "${APP_DIR}/.env" | cut -d'=' -f2)
|
||||||
|
elif [[ -n "$PORT" ]]; then
|
||||||
|
SERVICE_PORT="$PORT"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Validate PORT is numeric
|
||||||
|
if [[ ! "$SERVICE_PORT" =~ ^[0-9]+$ ]]; then
|
||||||
|
print_error "PORT in .env is not numeric: '$SERVICE_PORT'. Using 8000."
|
||||||
|
SERVICE_PORT="8000"
|
||||||
|
# Fix .env
|
||||||
|
sed -i "s/^PORT=.*/PORT=8000/" "${APP_DIR}/.env"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
SERVICE_PORT="$PORT"
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_status "Service will run on: $SERVICE_HOST:$SERVICE_PORT"
|
||||||
|
|
||||||
# Create systemd service file
|
# Create systemd service file
|
||||||
print_status "Creating systemd service file at $SERVICE_FILE..."
|
print_status "Creating systemd service file at $SERVICE_FILE..."
|
||||||
|
|
||||||
|
|
@ -316,7 +346,9 @@ WorkingDirectory=$APP_DIR
|
||||||
Environment="PATH=$VENV_DIR/bin"
|
Environment="PATH=$VENV_DIR/bin"
|
||||||
Environment="PYTHONPATH=$APP_DIR"
|
Environment="PYTHONPATH=$APP_DIR"
|
||||||
EnvironmentFile=$APP_DIR/.env
|
EnvironmentFile=$APP_DIR/.env
|
||||||
ExecStart=$VENV_DIR/bin/waitress-serve --host=\$HOST --port=\$PORT wsgi:wsgi_app
|
# Note: HOST and PORT are hardcoded from .env values at install time
|
||||||
|
# If you change PORT in .env, you must update this service file
|
||||||
|
ExecStart=$VENV_DIR/bin/waitress-serve --host=$SERVICE_HOST --port=$SERVICE_PORT wsgi:wsgi_app
|
||||||
Restart=always
|
Restart=always
|
||||||
RestartSec=10
|
RestartSec=10
|
||||||
StandardOutput=syslog
|
StandardOutput=syslog
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue