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 #
0008 # SWF Monitor Apache Deployment Setup Script
0009 # 
0010 # USAGE: sudo ./setup-apache-deployment.sh
0011 # 
0012 # See docs/PRODUCTION_DEPLOYMENT.md for complete documentation
0013 
0014 set -e  # Exit on any error
0015 
0016 DEPLOY_ROOT="/opt/swf-monitor"
0017 APACHE_CONF_DIR="/etc/httpd/conf.d"
0018 REPO_URL="https://github.com/BNLNPPS/swf-monitor.git"
0019 CURRENT_USER="wenauseic"
0020 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
0021 
0022 echo "=== SWF Monitor Apache Deployment Setup ==="
0023 echo "This will create production deployment infrastructure"
0024 echo "Press Enter to continue or Ctrl+C to cancel"
0025 read
0026 
0027 # Check if running as root
0028 if [[ $EUID -ne 0 ]]; then
0029    echo "This script must be run as root (with sudo)" 
0030    exit 1
0031 fi
0032 
0033 # Verify required files exist
0034 REQUIRED_FILES=("deploy-swf-monitor.sh" "apache-swf-monitor.conf" "production.env")
0035 for file in "${REQUIRED_FILES[@]}"; do
0036     if [ ! -f "$SCRIPT_DIR/$file" ]; then
0037         echo "ERROR: Required file $file not found in $SCRIPT_DIR"
0038         exit 1
0039     fi
0040 done
0041 
0042 echo "Creating deployment directory structure..."
0043 mkdir -p "$DEPLOY_ROOT"/{releases,shared/{logs,static,uploads},config/{apache,env},bin}
0044 
0045 echo "Setting ownership and permissions..."
0046 chown -R "$CURRENT_USER:eic" "$DEPLOY_ROOT"
0047 chmod -R 755 "$DEPLOY_ROOT"
0048 
0049 echo "Installing deployment automation script..."
0050 cp "$SCRIPT_DIR/deploy-swf-monitor.sh" "$DEPLOY_ROOT/bin/deploy-swf-monitor.sh"
0051 chmod +x "$DEPLOY_ROOT/bin/deploy-swf-monitor.sh"
0052 
0053 # Apache configuration will be installed after mod_wsgi setup
0054 
0055 
0056 echo "Creating production environment configuration..."
0057 if [ ! -f "$DEPLOY_ROOT/config/env/production.env" ]; then
0058     cp "$SCRIPT_DIR/production.env" "$DEPLOY_ROOT/config/env/production.env"
0059     chmod 600 "$DEPLOY_ROOT/config/env/production.env"
0060     chown "$CURRENT_USER:eic" "$DEPLOY_ROOT/config/env/production.env"
0061     echo "Created production.env template - please review and update values"
0062 else
0063     echo "Production environment file already exists, skipping..."
0064 fi
0065 
0066 echo "Installing required Apache modules and dependencies..."
0067 
0068 # Install Apache development headers (required for mod_wsgi compilation)
0069 echo "Installing Apache development headers..."
0070 yum install -y httpd-devel || {
0071     echo "Failed to install httpd-devel. This is required for mod_wsgi compilation."
0072     exit 1
0073 }
0074 
0075 # Install Python 3.11 mod_wsgi in the project's virtual environment
0076 echo "Installing Python 3.11 mod_wsgi in project virtual environment..."
0077 VENV_PATH="/eic/u/wenauseic/github/swf-testbed/.venv"
0078 if [ ! -d "$VENV_PATH" ]; then
0079     echo "ERROR: Project virtual environment not found at $VENV_PATH"
0080     echo "Please ensure swf-testbed is set up with: cd /eic/u/wenauseic/github/swf-testbed && source install.sh"
0081     exit 1
0082 fi
0083 
0084 # Activate venv and install mod_wsgi
0085 cd /eic/u/wenauseic/github/swf-testbed
0086 source .venv/bin/activate
0087 source ~/.env || true  # Load environment but don't fail if missing
0088 
0089 pip install mod_wsgi || {
0090     echo "Failed to install mod_wsgi in Python 3.11 virtual environment"
0091     exit 1
0092 }
0093 
0094 # Generate mod_wsgi LoadModule config into /etc/httpd/conf.modules.d/ so it
0095 # loads BEFORE conf.d/ content. Only LoadModule + WSGIPythonHome here; the
0096 # vhost/daemon config (WSGIDaemonProcess, locations, ProxyPass, etc.) lives
0097 # in the repo canonical at apache-swf-monitor.conf and is cp'd below.
0098 echo "Generating Python mod_wsgi LoadModule config..."
0099 MODWSGI_CONFIG=$(mod_wsgi-express module-config)
0100 echo "$MODWSGI_CONFIG"
0101 MODWSGI_MODULE=$(echo "$MODWSGI_CONFIG" | grep "LoadModule" | cut -d'"' -f2)
0102 MODWSGI_PYTHON_HOME=$(echo "$MODWSGI_CONFIG" | grep "WSGIPythonHome" | cut -d'"' -f2)
0103 if [ -z "$MODWSGI_MODULE" ] || [ -z "$MODWSGI_PYTHON_HOME" ]; then
0104     echo "ERROR: Failed to extract mod_wsgi configuration"
0105     exit 1
0106 fi
0107 
0108 # Disable system mod_wsgi to avoid conflicts with our venv-built one
0109 echo "Disabling system mod_wsgi to avoid Python version conflicts..."
0110 SYSTEM_MODWSGI="/etc/httpd/conf.modules.d/10-wsgi-python3.conf"
0111 if [ -f "$SYSTEM_MODWSGI" ]; then
0112     mv "$SYSTEM_MODWSGI" "${SYSTEM_MODWSGI}.disabled"
0113     echo "Disabled system mod_wsgi at $SYSTEM_MODWSGI"
0114 fi
0115 
0116 # LoadModule file — loaded from conf.modules.d/ before conf.d/ content.
0117 echo "Installing LoadModule config to /etc/httpd/conf.modules.d/20-swf-monitor-wsgi.conf..."
0118 cat > /etc/httpd/conf.modules.d/20-swf-monitor-wsgi.conf << EOF
0119 # Python mod_wsgi module for swf-monitor (generated by setup-apache-deployment.sh)
0120 LoadModule wsgi_module $MODWSGI_MODULE
0121 WSGIPythonHome $MODWSGI_PYTHON_HOME
0122 EOF
0123 
0124 # Vhost/daemon config — install from repo canonical (not regenerated here).
0125 # This is the source-of-truth flow: edit apache-swf-monitor.conf → deploy picks it up.
0126 echo "Installing Apache vhost config from repo: $SCRIPT_DIR/apache-swf-monitor.conf ..."
0127 if [ ! -f "$SCRIPT_DIR/apache-swf-monitor.conf" ]; then
0128     echo "ERROR: $SCRIPT_DIR/apache-swf-monitor.conf not found — cannot install vhost config"
0129     exit 1
0130 fi
0131 install -o root -g root -m 644 "$SCRIPT_DIR/apache-swf-monitor.conf" "$APACHE_CONF_DIR/swf-monitor.conf"
0132 
0133 # Check if required modules are enabled (warn only, don't try to fix)
0134 if ! httpd -M 2>/dev/null | grep -q rewrite_module; then
0135     echo "WARNING: mod_rewrite not found. HTTPS redirects may not work."
0136     echo "Please ensure mod_rewrite is enabled by your system administrator."
0137 fi
0138 
0139 if ! httpd -M 2>/dev/null | grep -q ssl_module; then
0140     echo "WARNING: mod_ssl not found. HTTPS will not work."
0141     echo "Please ensure SSL is properly configured by your system administrator."
0142 fi
0143 
0144 if ! httpd -M 2>/dev/null | grep -q headers_module; then
0145     echo "WARNING: mod_headers not found. Security headers will not work."
0146     echo "Please ensure mod_headers is enabled by your system administrator."
0147 fi
0148 
0149 echo "Testing Apache configuration..."
0150 httpd -t || {
0151     echo "Apache configuration test failed. Please check the configuration."
0152     exit 1
0153 }
0154 
0155 echo "Restarting Apache..."
0156 systemctl restart httpd
0157 systemctl enable httpd
0158 
0159 echo ""
0160 echo "=== Setup Complete! ==="
0161 echo ""
0162 echo "Deployment structure created at: $DEPLOY_ROOT"
0163 echo "Apache configuration: $APACHE_CONF_DIR/swf-monitor.conf (with Python 3.11 mod_wsgi)"
0164 echo "Deployment script: $DEPLOY_ROOT/bin/deploy-swf-monitor.sh"
0165 echo "mod_wsgi module: $MODWSGI_MODULE"
0166 echo "Python home: $MODWSGI_PYTHON_HOME"
0167 echo ""
0168 echo "Next steps:"
0169 echo "1. Deploy your first release:"
0170 echo "   sudo /opt/swf-monitor/bin/deploy-swf-monitor.sh branch infra/baseline-v18"
0171 echo ""
0172 echo "2. Test the deployment:"
0173 echo "   curl http://localhost/swf-monitor/"
0174 echo ""
0175 echo "Your development environment remains unchanged at:"
0176 echo "   /eic/u/wenauseic/github/swf-monitor/"