From f83dde78f90fe6bd2df44a72d25b995b1ea4bbac Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 29 Apr 2026 13:26:34 +0800 Subject: [PATCH] refactor: automate build.sh to clone modules, configure DB and init permissions - Clone all reference and business modules to pkgs/ - Interactive DB config (host, port, user, password) - Auto-create database, import schema, encrypt password in config.json - Auto-run permission initialization - Generate start.sh/stop.sh --- build.sh | 308 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 214 insertions(+), 94 deletions(-) diff --git a/build.sh b/build.sh index a351908..1cd13c9 100755 --- a/build.sh +++ b/build.sh @@ -1,150 +1,261 @@ #!/bin/bash -# Integrated CRM Application Build Script -# Uses local modules from ~/repos instead of cloning from git +# 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)" -REPOS_DIR="$HOME/repos" +PKGS_DIR="$APP_DIR/pkgs" +GIT_BASE="git@git.opencomputing.cn:yumoqing" -echo "Building Integrated CRM Application at: $APP_DIR" -echo "Using local modules from: $REPOS_DIR" +echo "============================================" +echo " Integrated CRM Application Builder" +echo "============================================" +echo "" +# ============================================================ # Step 1: Create required directories -mkdir -p "$APP_DIR/pkgs" +# ============================================================ +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 - echo "Creating Python virtual environment..." python3 -m venv "$APP_DIR/py3" fi source "$APP_DIR/py3/bin/activate" +pip install --upgrade pip --quiet +# ============================================================ # Step 3: Install core dependencies -echo "Installing core dependencies..." -pip install --quiet git+https://git.opencomputing.cn/yumoqing/apppublic -pip install --quiet git+https://git.opencomputing.cn/yumoqing/sqlor -pip install --quiet git+https://git.opencomputing.cn/yumoqing/ahserver -pip install --quiet xls2ddl +# ============================================================ +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 xls2ddl --quiet +pip install pymysql --quiet +pip install cryptography --quiet -# Step 4: Install local modules from ~/repos -echo "Installing local modules..." +# ============================================================ +# Step 4: Clone all modules to pkgs/ +# ============================================================ +echo "[4/10] Cloning modules to pkgs/..." -MODULES="apppublic sqlor ahserver appbase rbac customer_management opportunity_management contract_management financial_management workflow_approval unified_dashboard bricks" +REFERENCE_MODULES="apppublic sqlor ahserver appbase rbac" +BUSINESS_MODULES="customer_management opportunity_management contract_management financial_management workflow_approval unified_dashboard" -for module in $MODULES; do - MOD_DIR="$REPOS_DIR/$module" - if [ -d "$MOD_DIR" ]; then - echo " Installing $module from $MOD_DIR..." - # Create symlink in pkgs for consistency - ln -sf "$MOD_DIR" "$APP_DIR/pkgs/$module" - pip install -e "$MOD_DIR" --quiet 2>/dev/null || echo " (pip install failed for $module, may be a non-package module)" +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 " WARNING: Module $module not found at $MOD_DIR" + 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 -# Step 5: Generate database DDL for all modules with models/ -echo "Generating database DDL..." +cd "$APP_DIR" + +# ============================================================ +# Step 5: Generate database DDL and CRUD UI +# ============================================================ +echo "[5/10] Generating database DDL..." -# Collect all DDL into a single file > "$APP_DIR/integrated_crm_app_schema.sql" -for module in appbase rbac customer_management opportunity_management contract_management financial_management workflow_approval unified_dashboard; do - MOD_DIR="$APP_DIR/pkgs/$module" +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 - echo " Processing models for $module..." cd "$MOD_DIR/models" - - # Check for .xlsx files first if ls *.xlsx >/dev/null 2>&1; then xls2ddl mysql . > "$MOD_DIR/mysql.ddl.sql" 2>/dev/null || true - # Check for .json files elif ls *.json >/dev/null 2>&1; then json2ddl mysql . > "$MOD_DIR/mysql.ddl.sql" 2>/dev/null || true fi - - # Append to combined schema if DDL was generated if [ -f "$MOD_DIR/mysql.ddl.sql" ] && [ -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 " No DDL generated for $module (using existing mysql.ddl.sql if available)" - if [ -f "$MOD_DIR/mysql.ddl.sql" ] && [ -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" - fi fi fi done +cd "$APP_DIR" -echo "Combined schema written to: $APP_DIR/integrated_crm_app_schema.sql" -wc -l "$APP_DIR/integrated_crm_app_schema.sql" - -# Step 6: Generate CRUD UI for all modules with json/ -echo "Generating CRUD UI..." - -for module in appbase rbac customer_management opportunity_management contract_management financial_management workflow_approval unified_dashboard; do - MOD_DIR="$APP_DIR/pkgs/$module" +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" - # Get list of json files (excluding potential subdirectories) for jsonfile in *.json; do if [ -f "$jsonfile" ]; then - xls2ui -m ../models -o ../wwwroot "$module" "$jsonfile" 2>/dev/null || echo " Warning: xls2ui failed for $jsonfile" + xls2ui -m ../models -o ../wwwroot "$module" "$jsonfile" 2>/dev/null || true fi done fi done +cd "$APP_DIR" -# Step 7: Create symbolic links for wwwroot -echo "Creating wwwroot symbolic links..." +# ============================================================ +# Step 6: Create wwwroot symbolic links +# ============================================================ +echo "[6/10] Creating wwwroot symbolic links..." -mkdir -p "$APP_DIR/wwwroot" - -for module in appbase rbac customer_management opportunity_management contract_management financial_management workflow_approval unified_dashboard; do - MOD_DIR="$APP_DIR/pkgs/$module" +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 -sf "$MOD_DIR/wwwroot" "$APP_DIR/wwwroot/$module" - echo " Linked $module/wwwroot" + ln -sfn "$MOD_DIR/wwwroot" "$APP_DIR/wwwroot/$module" + echo " Linked wwwroot/$module" fi done -# Step 8: Setup Bricks framework -echo "Setting up Bricks framework..." - -BRICKS_DIR="$REPOS_DIR/bricks" -if [ -d "$BRICKS_DIR" ]; then - if [ -f "$BRICKS_DIR/build.sh" ]; then - echo " Building bricks framework..." - cd "$BRICKS_DIR" && ./build.sh - fi - if [ -d "$BRICKS_DIR/dist" ]; then - ln -sf "$BRICKS_DIR/dist" "$APP_DIR/wwwroot/bricks" - echo " Linked bricks/dist to wwwroot/bricks" - else - echo " WARNING: bricks/dist not found" - fi -else - echo " WARNING: bricks module not found at $BRICKS_DIR" +# 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 9: Create main app wwwroot links -echo "Setting up main app wwwroot..." +# ============================================================ +# Step 7: Interactive database configuration +# ============================================================ +echo "" +echo "[7/10] Database Configuration" +echo "--------------------------------------------" -# Link main app wwwroot files -if [ -d "$APP_DIR/wwwroot_main" ]; then - cp -r "$APP_DIR/wwwroot_main"/* "$APP_DIR/wwwroot/" 2>/dev/null || true -fi +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 @@ -152,8 +263,10 @@ APP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$APP_DIR" source "$APP_DIR/py3/bin/activate" export PYTHONPATH="$APP_DIR:$PYTHONPATH" -echo "Starting Integrated CRM Application on port 8080..." -python app/integrated_crm_app.py --port 8080 --root wwwroot/ +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' @@ -164,18 +277,25 @@ STOPSCRIPT chmod +x "$APP_DIR/start.sh" "$APP_DIR/stop.sh" +# ============================================================ +# Done +# ============================================================ echo "" -echo "==========================================" -echo "Build completed successfully!" -echo "==========================================" +echo "============================================" +echo " Build completed successfully!" +echo "============================================" echo "" echo "Next steps:" -echo "1. Apply database schema:" -echo " mysql -u hermes -phermes123 < $APP_DIR/integrated_crm_app_schema.sql" +echo " 1. Start the application:" +echo " ./start.sh [port] (default: 8080)" echo "" -echo "2. Start the application:" -echo " ./start.sh" +echo " 2. Register the first admin user:" +echo " http://localhost:8080/user/register.ui" echo "" -echo "3. Access the application:" -echo " http://localhost:8080/main/login.ui" +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 ""