Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-25 08:29:11

0001 #!/bin/bash
0002 # Require bash (fail fast if invoked under another shell)
0003 if [ -z "${BASH_VERSION:-}" ]; then
0004     echo "This script must be run with bash. Try: bash $0 \"$@\"" >&2
0005     exit 1
0006 fi
0007 # Script to start Django with both HTTP and HTTPS support
0008 # HTTP on port 8002 for REST logging (no auth)
0009 # HTTPS on port 8443 for authenticated API calls
0010 
0011 echo "Starting Django with dual HTTP/HTTPS support..."
0012 
0013 # Robustly kill any process listening on a TCP port (TERM then KILL)
0014 kill_port() {
0015     local port="$1"
0016     echo "Ensuring no process is listening on port ${port}..."
0017 
0018     # Collect PIDs via lsof if available
0019     local pids=""
0020     if command -v lsof >/dev/null 2>&1; then
0021         pids=$(lsof -ti tcp:"${port}" 2>/dev/null || true)
0022     fi
0023 
0024     # If still empty, try ss to parse PIDs
0025     if [ -z "${pids}" ] && command -v ss >/dev/null 2>&1; then
0026         # Example line contains users:("python",pid=1234,fd=5)
0027         pids=$(ss -ltnp "( sport = :${port} )" 2>/dev/null | awk -F',' '/pid=/ { for (i=1;i<=NF;i++) if ($i ~ /pid=/) { gsub(/.*pid=/, "", $i); gsub(/[^0-9].*/, "", $i); if ($i != "") print $i } }' | sort -u)
0028     fi
0029 
0030     # If still nothing, try fuser to kill directly
0031     if [ -z "${pids}" ] && command -v fuser >/dev/null 2>&1; then
0032         echo "Using fuser to kill listeners on ${port}/tcp (if any)"
0033         fuser -k "${port}/tcp" 2>/dev/null || true
0034         sleep 1
0035         # Re-check after fuser
0036         if command -v lsof >/dev/null 2>&1; then
0037             pids=$(lsof -ti tcp:"${port}" 2>/dev/null || true)
0038         fi
0039     fi
0040 
0041     if [ -n "${pids}" ]; then
0042         echo "Found PIDs on port ${port}: ${pids} — sending SIGTERM"
0043         kill -TERM ${pids} 2>/dev/null || true
0044         sleep 1
0045         # If still present, escalate to SIGKILL
0046         local remaining="${pids}"
0047         if command -v lsof >/dev/null 2>&1; then
0048             remaining=$(lsof -ti tcp:"${port}" 2>/dev/null || true)
0049         elif command -v ss >/dev/null 2>&1; then
0050             remaining=$(ss -ltnp "( sport = :${port} )" 2>/dev/null | awk -F',' '/pid=/ { for (i=1;i<=NF;i++) if ($i ~ /pid=/) { gsub(/.*pid=/, "", $i); gsub(/[^0-9].*/, "", $i); if ($i != "") print $i } }' | sort -u)
0051         fi
0052         if [ -n "${remaining}" ]; then
0053             echo "Processes still listening on ${port}: ${remaining} — sending SIGKILL"
0054             kill -KILL ${remaining} 2>/dev/null || true
0055             sleep 1
0056         fi
0057     fi
0058 
0059     # Final check
0060     local check=""
0061     if command -v lsof >/dev/null 2>&1; then
0062         check=$(lsof -ti tcp:"${port}" 2>/dev/null || true)
0063     elif command -v ss >/dev/null 2>&1; then
0064         check=$(ss -ltnp "( sport = :${port} )" 2>/dev/null | grep -v "State" || true)
0065     fi
0066     if [ -z "${check}" ]; then
0067         echo "Port ${port} is free."
0068     else
0069         echo "Warning: Port ${port} still appears in use. You may need elevated permissions to terminate owning processes."
0070     fi
0071 }
0072 
0073 # Source ~/.env if it exists
0074 if [[ -f "$HOME/.env" ]]; then
0075     source "$HOME/.env"
0076 fi
0077 
0078 # Navigate to swf-monitor source directory
0079 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
0080 SWF_MONITOR_DIR="${SWF_MONITOR_DIR:-$SCRIPT_DIR}"
0081 cd "$SWF_MONITOR_DIR/src"
0082 
0083 # Set up virtual environment paths
0084 SWF_TESTBED_DIR="${SWF_TESTBED_DIR:-$SCRIPT_DIR/../swf-testbed}"
0085 if [[ -f "$SWF_TESTBED_DIR/.venv/bin/activate" ]]; then
0086     source "$SWF_TESTBED_DIR/.venv/bin/activate"
0087     PYTHON_CMD="$SWF_TESTBED_DIR/.venv/bin/python"
0088     DAPHNE_CMD="$SWF_TESTBED_DIR/.venv/bin/daphne"
0089 else
0090     echo "Warning: Virtual environment not found at $SWF_TESTBED_DIR/.venv"
0091     echo "Continuing with system python..."
0092     PYTHON_CMD="python"
0093     DAPHNE_CMD="daphne"
0094 fi
0095 
0096 # Kill existing Django servers if running
0097 echo "Stopping existing Django servers..."
0098 
0099 # Murder Django processes with extreme prejudice
0100 kill_django_processes() {
0101     local pattern="$1"
0102     local pids=$(pgrep -u "$USER" -f "$pattern" 2>/dev/null | tr '\n' ' ' | xargs)
0103     if [ -n "$pids" ]; then
0104         echo "Killing $pattern processes: $pids"
0105         kill -9 $pids 2>/dev/null || true
0106     fi
0107 }
0108 
0109 kill_django_processes "manage.py runserver"
0110 kill_django_processes "daphne.*swf_monitor_project"
0111 sleep 1
0112 
0113 # Ensure 8443 is completely free before starting Daphne
0114 kill_port 8443
0115 
0116 # Check if SSL certificate exists, create self-signed if not
0117 SSL_CERT="../ssl_cert.pem"
0118 SSL_KEY="../ssl_key.pem"
0119 
0120 if [[ ! -f "$SSL_CERT" || ! -f "$SSL_KEY" ]]; then
0121     echo "Creating self-signed SSL certificate for development..."
0122     openssl req -x509 -newkey rsa:4096 -keyout "$SSL_KEY" -out "$SSL_CERT" -sha256 -days 365 -nodes \
0123         -subj "/C=US/ST=NY/L=Upton/O=BNL/OU=NPPS/CN=localhost"
0124     echo "SSL certificate created: $SSL_CERT"
0125     echo "SSL private key created: $SSL_KEY"
0126 fi
0127 
0128 # Start HTTP server on port 8002 (for REST logging) IN BACKGROUND
0129 echo "Starting HTTP server on port 8002 for REST logging (background)..."
0130 $PYTHON_CMD manage.py runserver 0.0.0.0:8002 &
0131 HTTP_PID=$!
0132 
0133 # Wait a moment for HTTP server to start
0134 sleep 2
0135 
0136 echo "Django servers starting:"
0137 echo "  HTTP (REST logging):     http://localhost:8002 (background)"
0138 echo "  HTTPS (authenticated):   https://localhost:8443 (foreground with auto-reload)"
0139 echo ""
0140 echo "Process ID: HTTP=$HTTP_PID"
0141 echo "Press Ctrl+C to stop both servers"
0142 
0143 # Start HTTPS server on port 8443 IN FOREGROUND (for auto-reload)
0144 echo "Starting HTTPS server on port 8443 for authenticated APIs (foreground with auto-reload)..."
0145 $DAPHNE_CMD -e ssl:8443:privateKey="$SSL_KEY":certKey="$SSL_CERT":interface=0.0.0.0 swf_monitor_project.asgi:application &
0146 HTTPS_PID=$!
0147 
0148 # Set up trap to kill both servers when interrupted
0149 trap 'echo -e "\nStopping servers..."; kill -9 $HTTP_PID $HTTPS_PID 2>/dev/null; echo "Django servers stopped."; exit' INT
0150 
0151 # Wait for the foreground HTTPS process
0152 wait $HTTPS_PID