#!/bin/bash # Production Startup Script for Trasabilitate Application # This script starts the application using Gunicorn WSGI server set -e # Exit on any error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color print_step() { echo -e "\n${BLUE}📋 $1${NC}" echo "----------------------------------------" } print_success() { echo -e "${GREEN}✅ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } echo -e "${BLUE}🚀 Trasabilitate Application - Production Startup${NC}" echo "==============================================" # Check if we're in the right directory if [[ ! -f "wsgi.py" ]]; then print_error "Please run this script from the py_app directory" print_error "Expected location: /srv/quality_recticel/py_app" exit 1 fi print_step "Checking Prerequisites" # Check if virtual environment exists if [[ ! -d "../recticel" ]]; then print_error "Virtual environment 'recticel' not found" print_error "Please create it first or run './quick_deploy.sh'" exit 1 fi print_success "Virtual environment found" # Activate virtual environment print_step "Activating Virtual Environment" source ../recticel/bin/activate print_success "Virtual environment activated" # Check if Gunicorn is installed if ! command -v gunicorn &> /dev/null; then print_error "Gunicorn not found. Installing..." pip install gunicorn fi print_success "Gunicorn is available" # Check database connection print_step "Testing Database Connection" if python3 -c " import mariadb try: conn = mariadb.connect(user='trasabilitate', password='Initial01!', host='localhost', database='trasabilitate') conn.close() print('Database connection successful') except Exception as e: print(f'Database connection failed: {e}') exit(1) " > /dev/null 2>&1; then print_success "Database connection verified" else print_error "Database connection failed. Please run database setup first:" print_error "python3 app/db_create_scripts/setup_complete_database.py" exit 1 fi # Create PID file directory print_step "Setting up Runtime Environment" mkdir -p ../run print_success "Runtime directory created" # Check if already running PID_FILE="../run/trasabilitate.pid" if [[ -f "$PID_FILE" ]]; then PID=$(cat "$PID_FILE") if ps -p "$PID" > /dev/null 2>&1; then print_warning "Application is already running (PID: $PID)" echo "To stop the application, run:" echo "kill $PID" echo "rm $PID_FILE" exit 1 else print_warning "Stale PID file found, removing..." rm -f "$PID_FILE" fi fi # Start Gunicorn print_step "Starting Production Server" echo "Starting Gunicorn WSGI server..." echo "Configuration: gunicorn.conf.py" echo "Workers: $(python3 -c 'import multiprocessing; print(multiprocessing.cpu_count() * 2 + 1)')" echo "Binding to: 0.0.0.0:8781" echo "" # Start Gunicorn with configuration file gunicorn --config gunicorn.conf.py \ --pid "$PID_FILE" \ --daemon \ wsgi:application # Wait a moment for startup sleep 2 # Check if the process started successfully if [[ -f "$PID_FILE" ]]; then PID=$(cat "$PID_FILE") if ps -p "$PID" > /dev/null 2>&1; then print_success "Application started successfully!" echo "" echo "==============================================" echo -e "${GREEN}🎉 PRODUCTION SERVER RUNNING${NC}" echo "==============================================" echo "" echo "📋 Server Information:" echo " • Process ID: $PID" echo " • Configuration: gunicorn.conf.py" echo " • Access Log: /srv/quality_recticel/logs/access.log" echo " • Error Log: /srv/quality_recticel/logs/error.log" echo "" echo "🌐 Application URLs:" echo " • Local: http://127.0.0.1:8781" echo " • Network: http://$(hostname -I | awk '{print $1}'):8781" echo "" echo "👤 Default Login:" echo " • Username: superadmin" echo " • Password: superadmin123" echo "" echo "🔧 Management Commands:" echo " • Stop server: kill $PID && rm $PID_FILE" echo " • View logs: tail -f /srv/quality_recticel/logs/error.log" echo " • Monitor access: tail -f /srv/quality_recticel/logs/access.log" echo " • Server status: ps -p $PID" echo "" print_warning "Server is running in daemon mode (background)" else print_error "Failed to start application. Check logs:" print_error "tail /srv/quality_recticel/logs/error.log" exit 1 fi else print_error "Failed to create PID file. Check permissions and logs." exit 1 fi