first commit
This commit is contained in:
95
docker/entrypoint.sh
Normal file
95
docker/entrypoint.sh
Normal file
@@ -0,0 +1,95 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
# MBIP Pusat Data — Container Entrypoint
|
||||
# Dijalankan setiap kali container bermula sebelum PHP-FPM boot
|
||||
# ─────────────────────────────────────────────────────────────
|
||||
|
||||
DB_HOST="${DB_HOST:-host.docker.internal}"
|
||||
DB_PORT="${DB_PORT:-3306}"
|
||||
DB_USER="${DB_USERNAME:-root}"
|
||||
DB_PASS="${DB_PASSWORD}"
|
||||
TIMEOUT=60
|
||||
|
||||
echo "[entrypoint] Memulakan MBIP Pusat Data — PHP $(php -r 'echo PHP_VERSION;')"
|
||||
echo "[entrypoint] MySQL luaran: ${DB_HOST}:${DB_PORT}"
|
||||
|
||||
# ── 1. Tunggu MySQL luaran sedia ─────────────────────────────
|
||||
echo "[entrypoint] Menunggu MySQL..."
|
||||
elapsed=0
|
||||
until mysqladmin ping -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASS}" \
|
||||
--connect-timeout=3 --silent 2>/dev/null; do
|
||||
if [ "$elapsed" -ge "$TIMEOUT" ]; then
|
||||
echo "[entrypoint] RALAT: MySQL di ${DB_HOST}:${DB_PORT} tidak bertindak balas dalam ${TIMEOUT}s. Dibatalkan."
|
||||
exit 1
|
||||
fi
|
||||
echo "[entrypoint] MySQL belum sedia — cuba lagi dalam 3s... (${elapsed}s berlalu)"
|
||||
sleep 3
|
||||
elapsed=$((elapsed + 3))
|
||||
done
|
||||
echo "[entrypoint] MySQL sedia."
|
||||
|
||||
# ── 2. Pasang composer dependencies jika vendor tiada ────────
|
||||
if [ ! -f /var/www/html/vendor/autoload.php ]; then
|
||||
echo "[entrypoint] vendor/ tidak dijumpai — menjalankan composer install..."
|
||||
composer install --no-dev --no-interaction --no-progress --optimize-autoloader --no-scripts \
|
||||
--working-dir=/var/www/html
|
||||
fi
|
||||
|
||||
# ── 3. Pastikan direktori storage wujud ──────────────────────
|
||||
mkdir -p \
|
||||
/var/www/html/storage/app/public \
|
||||
/var/www/html/storage/app/private/reports \
|
||||
/var/www/html/storage/framework/cache/data \
|
||||
/var/www/html/storage/framework/sessions \
|
||||
/var/www/html/storage/framework/testing \
|
||||
/var/www/html/storage/framework/views \
|
||||
/var/www/html/storage/logs \
|
||||
/var/log/php-fpm \
|
||||
/var/log/supervisor
|
||||
|
||||
chown -R www-data:www-data /var/www/html/storage
|
||||
chmod -R 775 /var/www/html/storage
|
||||
|
||||
# ── 4. Bersihkan bootstrap cache lapuk ───────────────────────
|
||||
rm -f /var/www/html/bootstrap/cache/*.php
|
||||
mkdir -p /var/www/html/bootstrap/cache
|
||||
chown -R www-data:www-data /var/www/html/bootstrap/cache
|
||||
|
||||
# ── 5. Jana APP_KEY jika belum ditetapkan ────────────────────
|
||||
if [ -z "${APP_KEY}" ]; then
|
||||
echo "[entrypoint] APP_KEY belum ditetapkan — menjana..."
|
||||
php artisan key:generate --force
|
||||
fi
|
||||
|
||||
# ── 6. Cipta storage symlink ─────────────────────────────────
|
||||
if [ ! -L /var/www/html/public/storage ]; then
|
||||
echo "[entrypoint] Mencipta storage symlink..."
|
||||
php artisan storage:link --force
|
||||
fi
|
||||
|
||||
# ── 7. Jalankan migrasi pangkalan data ───────────────────────
|
||||
echo "[entrypoint] Menjalankan migrasi..."
|
||||
php artisan migrate --force --no-interaction
|
||||
|
||||
# ── 8. Semai data jika kosong (kali pertama sahaja) ──────────
|
||||
ROLE_COUNT=$(php artisan tinker --execute="echo \Spatie\Permission\Models\Role::count();" 2>/dev/null | tail -1)
|
||||
if [ "${ROLE_COUNT}" = "0" ] || [ -z "${ROLE_COUNT}" ]; then
|
||||
echo "[entrypoint] Pangkalan data kosong — menjalankan seeder..."
|
||||
php artisan db:seed --force --no-interaction
|
||||
fi
|
||||
|
||||
# ── 9. Bina semula cache ─────────────────────────────────────
|
||||
echo "[entrypoint] Membina semula cache..."
|
||||
su -s /bin/sh www-data -c "php artisan package:discover --ansi"
|
||||
su -s /bin/sh www-data -c "php artisan optimize:clear"
|
||||
su -s /bin/sh www-data -c "php artisan config:cache"
|
||||
su -s /bin/sh www-data -c "php artisan route:cache"
|
||||
su -s /bin/sh www-data -c "php artisan view:cache"
|
||||
su -s /bin/sh www-data -c "php artisan event:cache"
|
||||
|
||||
echo "[entrypoint] Persediaan selesai. Memulakan PHP-FPM..."
|
||||
|
||||
# ── 10. Mulakan PHP-FPM + queue worker melalui Supervisor ────
|
||||
exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
|
||||
Reference in New Issue
Block a user