#!/bin/bash # Trasabilitate Application - Quick Deployment Script # This script handles the complete deployment process set -e # Exit on any error echo "🚀 Trasabilitate Application - Quick Deployment" echo "==============================================" # 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}📋 Step $1: $2${NC}" echo "----------------------------------------" } print_success() { echo -e "${GREEN}✅ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } # Check if running as root if [[ $EUID -eq 0 ]]; then print_error "This script should not be run as root for security reasons" exit 1 fi print_step 1 "Checking Prerequisites" # Check if we're in the right directory if [[ ! -f "run.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_success "Running from correct directory" # Check if MariaDB is running if ! systemctl is-active --quiet mariadb; then print_warning "MariaDB is not running. Attempting to start..." if sudo systemctl start mariadb; then print_success "MariaDB started successfully" else print_error "Failed to start MariaDB. Please start it manually:" print_error "sudo systemctl start mariadb" exit 1 fi else print_success "MariaDB is running" fi # Check if virtual environment exists if [[ ! -d "../recticel" ]]; then print_error "Virtual environment 'recticel' not found" print_error "Please create it first:" print_error "python3 -m venv ../recticel" exit 1 fi print_success "Virtual environment found" print_step 2 "Setting up Database and User" # Create database and user print_warning "You may be prompted for the MySQL root password" if sudo mysql -e "CREATE DATABASE IF NOT EXISTS trasabilitate; CREATE USER IF NOT EXISTS 'trasabilitate'@'localhost' IDENTIFIED BY 'Initial01!'; GRANT ALL PRIVILEGES ON trasabilitate.* TO 'trasabilitate'@'localhost'; FLUSH PRIVILEGES;" 2>/dev/null; then print_success "Database 'trasabilitate' and user created successfully" else print_error "Failed to create database or user. Please check MySQL root access" exit 1 fi print_step 3 "Activating Virtual Environment and Installing Dependencies" # Activate virtual environment and install dependencies source ../recticel/bin/activate if pip install -r requirements.txt > /dev/null 2>&1; then print_success "Python dependencies installed/verified" else print_error "Failed to install Python dependencies" exit 1 fi print_step 4 "Running Complete Database Setup" # Run the comprehensive database setup script if python3 app/db_create_scripts/setup_complete_database.py; then print_success "Database setup completed successfully" else print_error "Database setup failed" exit 1 fi print_step 5 "Testing Application Startup" # Test if the application can start (run for 3 seconds then kill) print_warning "Testing application startup (will stop after 3 seconds)..." timeout 3s python3 run.py > /dev/null 2>&1 || true print_success "Application startup test completed" echo "" echo "==============================================" echo -e "${GREEN}🎉 DEPLOYMENT COMPLETED SUCCESSFULLY!${NC}" echo "==============================================" echo "" echo "📋 Deployment Summary:" echo " • MariaDB database and user configured" echo " • All database tables and triggers created" echo " • Permissions system initialized" echo " • Default superadmin user ready" echo "" echo "🚀 To start the application:" echo " cd /srv/quality_recticel/py_app" echo " source ../recticel/bin/activate" echo " python3 run.py" 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 -e "${YELLOW}⚠️ Remember to change the default password after first login!${NC}" echo ""