#!/bin/bash # Integrated CRM Application - Automated Build & Deploy Script # Clones all dependencies, configures database, initializes tables and permissions set -e APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PKGS_DIR="$APP_DIR/pkgs" GIT_BASE="git@git.opencomputing.cn:yumoqing" echo "============================================" echo " Integrated CRM Application Builder" echo "============================================" echo "" # ============================================================ # Step 1: Create required directories # ============================================================ echo "[1/10] Creating directories..." mkdir -p "$PKGS_DIR" mkdir -p "$APP_DIR/logs" mkdir -p "$APP_DIR/files" mkdir -p "$APP_DIR/wwwroot" mkdir -p "$APP_DIR/conf" # ============================================================ # Step 2: Setup Python virtual environment # ============================================================ echo "[2/10] Setting up Python virtual environment..." if [ ! -d "$APP_DIR/py3" ]; then python3 -m venv "$APP_DIR/py3" fi source "$APP_DIR/py3/bin/activate" pip install --upgrade pip --quiet # ============================================================ # Step 3: Install core dependencies # ============================================================ echo "[3/10] Installing core dependencies..." pip install git+https://git.opencomputing.cn/yumoqing/apppublic --quiet pip install git+https://git.opencomputing.cn/yumoqing/sqlor --quiet pip install git+https://git.opencomputing.cn/yumoqing/ahserver --quiet pip install git+https://git.opencomputing.cn/yumoqing/bricks-for-python --quiet pip install xls2ddl --quiet pip install pymysql --quiet pip install cryptography --quiet # ============================================================ # Step 4: Clone all modules to pkgs/ # ============================================================ echo "[4/10] Cloning modules to pkgs/..." REFERENCE_MODULES="apppublic sqlor ahserver appbase rbac" BUSINESS_MODULES="customer_management opportunity_management contract_management financial_management workflow_approval unified_dashboard" for module in $REFERENCE_MODULES $BUSINESS_MODULES; do MOD_DIR="$PKGS_DIR/$module" if [ -d "$MOD_DIR/.git" ]; then echo " Pulling latest $module..." cd "$MOD_DIR" && git pull --quiet 2>/dev/null else echo " Cloning $module..." git clone --quiet "$GIT_BASE/$module.git" "$MOD_DIR" fi # Install module pip install -e "$MOD_DIR" --quiet 2>/dev/null || true done cd "$APP_DIR" # ============================================================ # Step 5: Generate database DDL and CRUD UI # ============================================================ echo "[5/10] Generating database DDL..." > "$APP_DIR/integrated_crm_app_schema.sql" for module in rbac customer_management opportunity_management contract_management financial_management workflow_approval unified_dashboard; do MOD_DIR="$PKGS_DIR/$module" if [ -d "$MOD_DIR/models" ]; then cd "$MOD_DIR/models" # Generate DDL to a temp file, filter out exception/error lines TEMP_DDL=$(mktemp) if ls *.xlsx >/dev/null 2>&1; then xls2ddl mysql . > "$TEMP_DDL" 2>/dev/null || true elif ls *.json >/dev/null 2>&1; then json2ddl mysql . > "$TEMP_DDL" 2>/dev/null || true fi # Filter: only keep lines that look like valid SQL (CREATE TABLE, CREATE INDEX, comments, blank lines, semicolons) if [ -f "$TEMP_DDL" ] && [ -s "$TEMP_DDL" ]; then grep -v "^Exception:" "$TEMP_DDL" > "$MOD_DIR/mysql.ddl.sql" 2>/dev/null || true if [ -s "$MOD_DIR/mysql.ddl.sql" ]; then echo "-- Module: $module" >> "$APP_DIR/integrated_crm_app_schema.sql" cat "$MOD_DIR/mysql.ddl.sql" >> "$APP_DIR/integrated_crm_app_schema.sql" echo "" >> "$APP_DIR/integrated_crm_app_schema.sql" echo " Generated DDL for $module" else echo " WARNING: No valid DDL generated for $module" fi fi rm -f "$TEMP_DDL" fi done cd "$APP_DIR" echo "[5/10] Generating CRUD UI..." for module in rbac customer_management opportunity_management contract_management financial_management workflow_approval unified_dashboard; do MOD_DIR="$PKGS_DIR/$module" if [ -d "$MOD_DIR/json" ] && [ -d "$MOD_DIR/models" ]; then echo " Generating UI for $module..." cd "$MOD_DIR/json" for jsonfile in *.json; do if [ -f "$jsonfile" ]; then xls2ui -m ../models -o ../wwwroot "$module" "$jsonfile" 2>/dev/null || true fi done fi done cd "$APP_DIR" # ============================================================ # Step 6: Create wwwroot symbolic links # ============================================================ echo "[6/10] Creating wwwroot symbolic links..." for module in rbac customer_management opportunity_management contract_management financial_management workflow_approval unified_dashboard; do MOD_DIR="$PKGS_DIR/$module" if [ -d "$MOD_DIR/wwwroot" ]; then ln -sfn "$MOD_DIR/wwwroot" "$APP_DIR/wwwroot/$module" echo " Linked wwwroot/$module" fi done # Link bricks framework if [ -d "$PKGS_DIR/bricks/dist" ]; then ln -sfn "$PKGS_DIR/bricks/dist" "$APP_DIR/wwwroot/bricks" elif [ -d "$PKGS_DIR/appbase/wwwroot/bricks" ]; then ln -sfn "$PKGS_DIR/appbase/wwwroot/bricks" "$APP_DIR/wwwroot/bricks" fi # ============================================================ # Step 7: Interactive database configuration # ============================================================ echo "" echo "[7/10] Database Configuration" echo "--------------------------------------------" read -p " MySQL host [localhost]: " DB_HOST DB_HOST=${DB_HOST:-localhost} read -p " MySQL port [3306]: " DB_PORT DB_PORT=${DB_PORT:-3306} read -p " MySQL admin user (needs CREATE DATABASE permission) [root]: " DB_ADMIN_USER DB_ADMIN_USER=${DB_ADMIN_USER:-root} read -sp " MySQL admin password: " DB_ADMIN_PASS echo "" read -p " CRM database name [crm_db]: " DB_NAME DB_NAME=${DB_NAME:-crm_db} read -p " CRM database user [hermes]: " DB_USER DB_USER=${DB_USER:-hermes} read -sp " CRM database password: " DB_PASS echo "" # ============================================================ # Step 8: Create database, import schema, configure app # ============================================================ echo "[8/10] Initializing database..." # Create database and user via MySQL admin mysql -h "$DB_HOST" -P "$DB_PORT" -u "$DB_ADMIN_USER" -p"$DB_ADMIN_PASS" < "$APP_DIR/start.sh" << 'STARTSCRIPT' #!/bin/bash APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$APP_DIR" source "$APP_DIR/py3/bin/activate" export PYTHONPATH="$APP_DIR:$PYTHONPATH" PORT="${1:-8080}" echo "Starting Integrated CRM Application on port $PORT..." echo "Access: http://localhost:$PORT/main/login.ui" python app/integrated_crm_app.py --port "$PORT" --root wwwroot/ STARTSCRIPT cat > "$APP_DIR/stop.sh" << 'STOPSCRIPT' #!/bin/bash pkill -f "integrated_crm_app.py" 2>/dev/null || true echo "Application stopped." STOPSCRIPT chmod +x "$APP_DIR/start.sh" "$APP_DIR/stop.sh" # ============================================================ # Done # ============================================================ echo "" echo "============================================" echo " Build completed successfully!" echo "============================================" echo "" echo "Next steps:" echo " 1. Start the application:" echo " ./start.sh [port] (default: 8080)" echo "" echo " 2. Register the first admin user:" echo " http://localhost:8080/user/register.ui" echo "" echo " 3. Assign admin_superuser role to your user:" echo " mysql -u $DB_USER -p $DB_NAME" echo " INSERT INTO userroles (userid, roleid) VALUES ('', 'admin_superuser');" echo "" echo " 4. Access the application:" echo " http://localhost:8080/main/login.ui" echo ""