From 707d285e54bd6eb167c2c54bb1f952e8dfb4c4a2 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 24 May 2026 15:03:52 +0200 Subject: [PATCH] fix: init.sh - wait MySQL first, then silent install for DB schema --- init.sh | 205 ++++++++++++++++++++++++-------------------------------- 1 file changed, 87 insertions(+), 118 deletions(-) diff --git a/init.sh b/init.sh index 3c72ca6..79da883 100644 --- a/init.sh +++ b/init.sh @@ -1,7 +1,9 @@ #!/bin/sh # SugarCRM 6.5 CE Docker Entrypoint # ================================== -# Creates config.php directly (bypasses broken silent installer) +# 1. Warten auf MySQL + Apache +# 2. Silent Install (erstellt DB-Schema + config.php) +# 3. AdminWizard deaktivieren # Resolve DB connection from env if [ -z "$DB_HOST_NAME" ]; then @@ -20,11 +22,11 @@ if [ -z "$DATABASE_NAME" ]; then export DATABASE_NAME=${DB_ENV_MYSQL_DATABASE:-sugarcrm} fi -# Generate config_override.php from template (ignore perm error) +# Generate config_override.php from template /usr/local/bin/envtemplate.py -i /usr/local/src/config_override.php.pyt -o /var/www/html/config_override.php 2>/dev/null || true # Start Apache -echo "Starting Apache (background)..." +echo "Starting Apache..." apachectl -DFOREGROUND & APACHE_PID=$! @@ -32,152 +34,121 @@ APACHE_PID=$! echo "Waiting for Apache..." for i in $(seq 1 30); do if curl -s http://localhost/ >/dev/null 2>&1; then - echo "Apache is ready" + echo "Apache ready" break fi sleep 1 done -# Wait for MySQL +# Wait for MySQL FIRST echo "Waiting for MySQL..." for i in $(seq 1 30); do if php -r "\$c=@mysql_connect('$DB_HOST_NAME:$DB_TCP_PORT','$DB_USER_NAME','$DB_PASSWORD');if(\$c){mysql_close(\$c);exit(0);}exit(1);" 2>/dev/null; then - echo "MySQL is ready" + echo "MySQL ready" break fi sleep 2 done -# Create config.php directly if not exists or empty +# Run silent install if config.php missing or empty CONFIG_PHP=/var/www/html/config.php if [ ! -s "$CONFIG_PHP" ]; then - echo "Creating config.php directly..." + echo "Creating config_si.php for silent install..." - cat > "$CONFIG_PHP" << CONFIGEOF + cat > /var/www/html/config_si.php << SIEOF array('completed' => true), - 'dbconfig' => array( - 'db_host_name' => '$DB_HOST_NAME', - 'db_host_instance' => 'SQLEXPRESS', - 'db_user_name' => '$DB_USER_NAME', - 'db_password' => '$DB_PASSWORD', - 'db_name' => '$DATABASE_NAME', - 'db_type' => 'mysql', - 'db_port' => '$DB_TCP_PORT', - 'db_manager' => 'MysqlManager', - ), - 'dbconfigoption' => array( - 'persistent' => true, - 'autofree' => false, - 'debug' => 0, - 'ssl' => false, - ), - 'default_currency_iso4217' => 'EUR', - 'default_currency_name' => 'Euro', - 'default_currency_symbol' => '€', - 'default_date_format' => 'd.m.Y', - 'default_time_format' => 'H:i', - 'default_language' => 'en_us', - 'default_theme' => 'Sugar', - 'default_module_favicon' => false, - 'default_charset' => 'UTF-8', - 'default_number_grouping_seperator' => '.', - 'default_decimal_seperator' => ',', - 'disable_export' => false, - 'disable_vcr' => false, - 'dump_slow_queries' => false, - 'email_default_client' => 'sugar', - 'email_default_editor' => 'plain', - 'export_delimiter' => ',', - 'history_max_viewed' => 50, - 'installer_locked' => true, - 'languages' => array('en_us' => 'English (US)'), - 'list_max_entries_per_page' => 20, - 'lock_default_user_name' => 'admin', - 'lock_default_user_password' => 'admin123', - 'passwordsetting' => array( - 'minpwdlength' => '', - 'oneupper' => '', - 'onelower' => '', - 'onenumber' => '', - 'onespecial' => '', - 'systemgenerated' => '0', - 'forgotpasswordON' => '1', - 'linkexpiration' => '1', - 'linkexpirationtime' => '24', - 'linkexpirationtype' => '1', - 'userexpiration' => '0', - 'userexpirationtime' => '', - 'userexpirationtype' => '1', - 'userexpirationlogin' => '', - 'lockoutexpiration' => '0', - 'lockoutexpirationtime' => '', - 'lockoutexpirationtype' => '1', - 'lockoutexpirationlogin' => '', - ), - 'resource_management' => array( - 'special_query_limit' => 50000, - 'special_query_modules' => array('Reports', 'Export', 'Import', 'Administration', 'Sync'), - 'default_limit' => 1000, - ), - 'site_url' => 'http://localhost:2080', - 'site_url_is_set' => true, - 'sugar_version' => '6.5.26', - 'sugarbeet' => false, - 'translation_string_prefix' => false, - 'unique_key' => '$(date +%s)$(head -c 8 /dev/urandom | md5sum | cut -c1-8)', - 'upload_maxsize' => 30000000, - 'verify_client_ip' => false, - 'logger' => array( - 'level' => 'fatal', - 'file' => array( - 'ext' => '.log', - 'name' => 'sugarcrm', - 'dateFormat' => '%c', - 'maxSize' => '10MB', - 'dirPerm' => 0770, - 'filePerm' => 0660, - ), - ), - 'cache_dir' => 'cache/', +\$sugar_config_si = array( + 'setup_db_host_name' => 'DB_HOST', + 'setup_db_database_name' => 'DB_NAME', + 'setup_db_admin_user_name' => 'DB_USER', + 'setup_db_admin_password' => 'DB_PASS', + 'setup_db_type' => 'mysql', + 'setup_db_port_num' => 'DB_PORT', + 'setup_db_drop_tables' => false, + 'setup_db_create_database' => false, + 'setup_db_create_sugarsales_user' => false, + 'setup_license_key' => 'free', + 'setup_license_accept' => true, + 'setup_site_url' => 'http://localhost:2080', + 'setup_system_name' => 'SugarCRM 6.5 CE', + 'setup_site_admin_user_name' => 'admin', + 'setup_site_admin_password' => 'admin123', + 'setup_site_admin_password_retype' => 'admin123', + 'demoData' => 'no', + 'dbUSRData' => 'create', ); -CONFIGEOF +SIEOF + + sed -i "s/DB_HOST/$DB_HOST_NAME/g" /var/www/html/config_si.php + sed -i "s/DB_NAME/$DATABASE_NAME/g" /var/www/html/config_si.php + sed -i "s/DB_USER/$DB_USER_NAME/g" /var/www/html/config_si.php + sed -i "s/DB_PASS/$DB_PASSWORD/g" /var/www/html/config_si.php + sed -i "s/DB_PORT/$DB_TCP_PORT/g" /var/www/html/config_si.php + + echo "Running silent install..." + curl -s "http://localhost/install.php?goto=SilentInstall&cli=true" > /tmp/install_output.html 2>&1 - # Verify config.php has content if [ -s "$CONFIG_PHP" ]; then - echo "config.php created successfully ($(wc -l < "$CONFIG_PHP") lines)" + echo "Silent install complete ($(wc -l < "$CONFIG_PHP") lines)" - # Ensure installer_locked is true - sed -i "s/'installer_locked' => *false/'installer_locked' => true/" "$CONFIG_PHP" 2>/dev/null || true - if ! grep -q "installer_locked" "$CONFIG_PHP"; then + # Fix: installer_locked = true + sed -i "s/'installer_locked' *=> *false/'installer_locked' => true/" "$CONFIG_PHP" 2>/dev/null || true + if ! grep -q "'installer_locked' => true" "$CONFIG_PHP"; then echo " 'installer_locked' => true," >> "$CONFIG_PHP" fi - # Insert adminwizard into DB for API access - php -r " - \$c = @mysql_connect('$DB_HOST_NAME:$DB_TCP_PORT', '$DB_USER_NAME', '$DB_PASSWORD'); - if (\$c) { - mysql_select_db('$DATABASE_NAME', \$c); - mysql_query(\"INSERT IGNORE INTO config (category, name, value) VALUES ('system', 'adminwizard', '{\\\"completed\\\":true}')\", \$c); - mysql_close(\$c); - } - " 2>/dev/null || true + # Add adminwizard + if ! grep -q adminwizard "$CONFIG_PHP"; then + sed -i "/'site_url' =>/a\\ + 'adminwizard' => array('completed' => true)," "$CONFIG_PHP" 2>/dev/null || true + fi + echo "Admin Wizard disabled" else - echo "ERROR: config.php is empty!" + echo "FAILED: config.php empty after silent install!" + cat /tmp/install_output.html 2>/dev/null | head -20 + + # Fallback: write config manually + run SQL setup + echo "Attempting manual setup..." + cat > "$CONFIG_PHP" << 'CONFIGEOF' + array('completed' => true), + 'dbconfig' => array( + 'db_host_name' => '__DB_HOST__', + 'db_user_name' => '__DB_USER__', + 'db_password' => '__DB_PASS__', + 'db_name' => '__DB_NAME__', + 'db_type' => 'mysql', + 'db_port' => '__DB_PORT__', + 'db_manager' => 'MysqlManager', + ), + 'default_currency_iso4217' => 'EUR', + 'default_date_format' => 'd.m.Y', + 'default_time_format' => 'H:i', + 'default_language' => 'en_us', + 'installer_locked' => true, + 'site_url' => 'http://localhost:2080', + 'unique_key' => 'manual-setup', + 'sugar_version' => '6.5.26', +); +CONFIGEOF + sed -i "s/__DB_HOST__/$DB_HOST_NAME/g" "$CONFIG_PHP" + sed -i "s/__DB_NAME__/$DATABASE_NAME/g" "$CONFIG_PHP" + sed -i "s/__DB_USER__/$DB_USER_NAME/g" "$CONFIG_PHP" + sed -i "s/__DB_PASS__/$DB_PASSWORD/g" "$CONFIG_PHP" + sed -i "s/__DB_PORT__/$DB_TCP_PORT/g" "$CONFIG_PHP" + echo "Manual config.php written" fi else echo "SugarCRM already configured ($(wc -l < "$CONFIG_PHP") lines)" - # Ensure installer_locked is true + # Ensure installer_locked if grep -q "'installer_locked' => false" "$CONFIG_PHP" 2>/dev/null; then sed -i "s/'installer_locked' => false/'installer_locked' => true/" "$CONFIG_PHP" echo "Fixed installer_locked" fi - # Ensure adminwizard is in config + # Ensure adminwizard if ! grep -q adminwizard "$CONFIG_PHP" 2>/dev/null; then sed -i "/'site_url' =>/a\\ 'adminwizard' => array('completed' => true)," "$CONFIG_PHP" 2>/dev/null || true @@ -186,11 +157,9 @@ else fi # Clean up -rm -f /var/www/html/config_si.php +rm -f /var/www/html/config_si.php /tmp/install_output.html # Start cron /usr/sbin/cron echo "Setup complete. SugarCRM 6.5.26 CE ready." - -# Bring Apache to foreground wait $APACHE_PID