1- #! /bin/sh
1+ #! /bin/bash
22
33echo " === Sentinel Kit Backend Entrypoint ==="
44echo " Environment: $APP_ENV "
55
66chown -R www-data:www-data /var/www/html
77chown -R www-data:www-data /detection-rules
88
9+ su -s /bin/bash www-data << 'EOF '
910setup_symfony() {
1011 MARKER_FILE="/var/www/html/.initial_setup_done"
1112 rm -rf /var/www/html/var/cache
1213 rm -rf /var/www/html/public/bundles
1314 rm -rf /detection-rules/elastalert/*
1415
15- # Vérifier si les dépendances Composer sont installées
1616 if [ ! -d "/var/www/html/vendor" ] || [ ! -f "/var/www/html/vendor/autoload.php" ]; then
1717 echo "Installing Composer dependencies..."
1818 composer install --no-scripts
@@ -21,17 +21,76 @@ setup_symfony() {
2121 echo "Composer dependencies already installed."
2222 fi
2323
24+ echo "Waiting for database to be ready..."
25+ max_attempts=30
26+ attempt=1
27+ while [ $attempt -le $max_attempts ]; do
28+ if php /var/www/html/bin/console doctrine:database:create --if-not-exists >/dev/null 2>&1; then
29+ echo "Database is ready!"
30+ break
31+ fi
32+ echo "Database not ready yet (attempt $attempt/$max_attempts), waiting 2 seconds..."
33+ sleep 2
34+ attempt=$((attempt + 1))
35+ done
36+
37+ if [ $attempt -gt $max_attempts ]; then
38+ echo "ERROR: Database is not ready after $max_attempts attempts"
39+ exit 1
40+ fi
41+
2442 if [ ! -f "$MARKER_FILE" ]; then
2543 echo "Running initial setup..."
26- sleep 10
44+
45+ echo "Creating database if not exists..."
46+ php /var/www/html/bin/console doctrine:database:create --if-not-exists
47+
48+ echo "Dropping existing schema..."
49+ php /var/www/html/bin/console doctrine:schema:drop --force --full-database || true
50+
51+ echo "Cleaning old migrations..."
2752 rm -rf /var/www/html/migrations/*.php
28- php /var/www/html/bin/console doctrine:schema:drop --force --full-database
29- php /var/www/html/bin/console make:migration -n
30- php /var/www/html/bin/console doctrine:migrations:migrate -n
31- php /var/www/html/bin/console lexik:jwt:generate-keypair
53+
54+ echo "Creating new migration..."
55+ if ! php /var/www/html/bin/console make:migration -n; then
56+ echo "ERROR: Failed to create migration"
57+ exit 1
58+ fi
59+
60+ if [ -z "$(ls -A /var/www/html/migrations/)" ]; then
61+ echo "ERROR: No migration file was created"
62+ exit 1
63+ fi
64+
65+ echo "Applying migrations..."
66+ if ! php /var/www/html/bin/console doctrine:migrations:migrate -n; then
67+ echo "ERROR: Failed to apply migrations"
68+ exit 1
69+ fi
70+
71+ echo "Verifying database schema..."
72+ if ! php /var/www/html/bin/console doctrine:schema:validate; then
73+ echo "WARNING: Database schema validation failed"
74+ fi
75+
76+ echo "Generating JWT keypair..."
77+ if ! php /var/www/html/bin/console lexik:jwt:generate-keypair; then
78+ echo "ERROR: Failed to generate JWT keypair"
79+ exit 1
80+ fi
81+
3282 php /var/www/html/bin/console lexik:jwt:check-config
83+
3384 touch "$MARKER_FILE"
34- echo " Initial setup completed."
85+ echo "Initial setup completed successfully."
86+ else
87+ echo "Initial setup already completed (marker file exists)."
88+
89+ echo "Checking if database migrations are up to date..."
90+ if ! php /var/www/html/bin/console doctrine:migrations:up-to-date; then
91+ echo "Database migrations are not up to date, running migrations..."
92+ php /var/www/html/bin/console doctrine:migrations:migrate -n
93+ fi
3594 fi
3695
3796 if [ "$APP_ENV" = "prod" ]; then
@@ -44,8 +103,6 @@ setup_symfony() {
44103 fi
45104}
46105
47- su -s /bin/sh www-data << 'EOF '
48- $(declare -f setup_symfony)
49106setup_symfony
50107EOF
51108
0 commit comments