#!/usr/bin/env bash ## dev-mode.sh -- helpers sourced by entrypoint.sh and entrypoint-fpm.sh. Every ## function is DEV ONLY (environment=DEV, i.e. local development): PROD never ## calls any of them, so a production container's behaviour and rendered config ## do not depend on this file. (Sourcing it only defines functions.) ## ## Package set (AlmaLinux 10 base, verified 2026-11-23): ## * The image already carries AppStream's `mariadb` 10.11 client (a ## `microdnf install MariaDB-server MariaDB-client` from mariadb.repo, the ## old recipe, dies in depsolve: MariaDB-client "obsoletes mariadb" and ## mariadb-client-utils needs it). `dnf --allowerasing` swaps the AppStream ## client for mariadb.org's 11.4 client+server cleanly; if that repo is ## unreachable the AppStream `mariadb-server` (client already present) is ## the fallback. ## * memcached: AppStream. ## * posix (php-process) and Xdebug (php-pecl-xdebug3): Remi, from the same ## module stream (php:remi-X.Y) the image's PHP was installed from, so the ## extension always matches the PHP build. ## dnf install that tolerates an unreachable repo (mariadb.org down must not ## stop memcached/posix/the AlmaLinux mariadb-server fallback from installing). dev_dnf() { dnf install -y --setopt=skip_if_unavailable=True "$@"; } dev_log() { echo "dev-mode: $*"; } ## Failures are collected and reported at the end by dev_report_failures, which ## exits non-zero: a DEV container that cannot give you the database/cache/ ## debugger it promised must not look healthy (the HEALTHCHECK only probes ## Apache, so it would stay green). DEV_FAILURES=() dev_fail() { DEV_FAILURES+=("$1"); echo "dev-mode: ERROR: $1" >&2; } dev_report_failures() { [ "${#DEV_FAILURES[@]}" -eq 0 ] && return 0 { echo "################################################################" echo "# FATAL: DEV mode could not set up what it promised:" for f in "${DEV_FAILURES[@]}"; do echo "# - $f"; done echo "# (environment=DEV installs MariaDB, memcached and optional PHP" echo "# extensions from the network at start; check connectivity/DNS.)" echo "# Exiting non-zero instead of reporting a healthy container." echo "################################################################" } >&2 exit 1 } ## PHP version in the image, as "83" for 8.3. The tag selects the version; the ## PHPVER env var does NOT (README). Warn when they disagree so a stale ## -e PHPVER=84 on a cac:php85 container is not mistaken for PHP 8.4. ## $1 = the PHPVER the caller passed in (before entrypoint defaulting; "" = unset). dev_check_phpver() { local requested="${1//./}" actual actual=$(php -r 'echo PHP_MAJOR_VERSION . PHP_MINOR_VERSION;' 2>/dev/null) || return 0 [ -n "$requested" ] || return 0 if [ "$requested" != "$actual" ]; then echo "################################################################" >&2 echo "# WARNING: PHPVER=$1 was requested but this image runs PHP ${actual:0:1}.${actual:1}." >&2 echo "# The PHP version is fixed by the IMAGE TAG, not by PHPVER." >&2 echo "# Use cac:php$requested (local-dev.sh -a $requested picks it for you)." >&2 echo "################################################################" >&2 fi } ## Install MariaDB + memcached if absent, start both, create the dev database. ## $1 = customer user. Uses safe-paths.sh helpers (as_user, drop_symlink, ## user_mkdir): the caller has already sourced it. dev_install_db_and_cache() { local u=$1 before=${#DEV_FAILURES[@]} if ! command -v mysqld >/dev/null 2>&1; then dev_log "installing MariaDB" if ! dev_dnf --allowerasing MariaDB-server MariaDB-client >/tmp/dev-mariadb-install.log 2>&1; then dev_log "mariadb.org packages failed (tail of log below); trying AlmaLinux mariadb-server" tail -n 8 /tmp/dev-mariadb-install.log >&2 dev_dnf --disablerepo=mariadb mariadb-server >/tmp/dev-mariadb-install2.log 2>&1 \ || { tail -n 8 /tmp/dev-mariadb-install2.log >&2; dev_fail "MariaDB server could not be installed"; } fi fi if ! command -v memcached >/dev/null 2>&1; then dev_log "installing memcached" dev_dnf memcached >/tmp/dev-memcached-install.log 2>&1 \ || { tail -n 8 /tmp/dev-memcached-install.log >&2; dev_fail "memcached could not be installed"; } fi [ "${#DEV_FAILURES[@]}" -eq "$before" ] || return 0 sed -r -i 's/session.save_path="memcache:11211/session.save_path="localhost:11211/' /etc/php.ini # /var/lib/mysql is a docker volume: make sure the mysql user (uid differs by # package source) owns it, and initialise it if the package did not. if [ "$(stat -c %U /var/lib/mysql 2>/dev/null)" != "mysql" ]; then chown -R mysql:mysql /var/lib/mysql; fi if [ ! -d /var/lib/mysql/mysql ]; then dev_log "initialising empty MariaDB data directory" mariadb-install-db --user=mysql --datadir=/var/lib/mysql >/tmp/dev-mariadb-initdb.log 2>&1 \ || { tail -n 8 /tmp/dev-mariadb-initdb.log >&2; dev_fail "mariadb-install-db failed"; return 0; } fi # `mysql` is a deprecated alias in MariaDB 11 and prints a warning per call. local mysql_cli; mysql_cli=$(command -v mariadb || command -v mysql) nohup mysqld --user=mysql >/var/log/mysqld-dev.log 2>&1 & local i for i in $(seq 1 60); do mysqladmin ping --silent >/dev/null 2>&1 && break sleep 1 done if ! mysqladmin ping --silent >/dev/null 2>&1; then tail -n 15 /var/log/mysqld-dev.log >&2 dev_fail "MariaDB did not start within 60 s (log: /var/log/mysqld-dev.log)" return 0 fi dev_log "MariaDB is up" if [ ! -f "/home/$u/mysql_creds" ]; then local mysql_user mysql_password mysql_db mysql_user=$(tr -dc A-Za-z0-9 /dev/null printf '%s\n' "MySQL User: $mysql_user" "MySQL Password: $mysql_password" "MySQL Database: devdb_${mysql_db}" \ | as_user tee "/home/$u/mysql_creds" fi /usr/bin/memcached -d -u "$u" || dev_fail "memcached failed to start" }