2026-11-23 06:03:23 -08:00
|
|
|
#!/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.
|
|
|
|
|
|
2026-11-23 06:03:24 -08:00
|
|
|
## 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 "$@"; }
|
|
|
|
|
|
2026-11-23 06:03:23 -08:00
|
|
|
dev_log() { echo "dev-mode: $*"; }
|
|
|
|
|
|
2026-11-23 06:03:24 -08:00
|
|
|
## 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
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 06:03:24 -08:00
|
|
|
dev_truthy() {
|
|
|
|
|
case "${1,,}" in 1|true|yes|on) return 0 ;; *) return 1 ;; esac
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 06:03:23 -08:00
|
|
|
## 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
|
|
|
|
|
}
|
2026-11-23 06:03:24 -08:00
|
|
|
|
2026-11-23 06:03:24 -08:00
|
|
|
## php.ini overrides for development. Written to the scan dir (wins over
|
|
|
|
|
## /etc/php.ini, sorts before zz-php2shell-mitigation.ini which sets other keys).
|
|
|
|
|
## Must run BEFORE php-fpm starts.
|
|
|
|
|
dev_write_php_ini() {
|
|
|
|
|
cat > /etc/php.d/99-dev.ini <<'EOF'
|
|
|
|
|
; written by dev-mode.sh (environment=DEV only)
|
|
|
|
|
; (error_reporting stays at prod-php.ini's E_ALL & ~E_DEPRECATED & ~E_STRICT: with
|
|
|
|
|
; deprecations on, wp-cli and PHP 8.5's own startup notices flood every CLI call)
|
|
|
|
|
display_errors = On
|
|
|
|
|
; edit a PHP file, reload the page: no 60 s opcache staleness
|
|
|
|
|
opcache.revalidate_freq = 0
|
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
## Install php-process (posix, pcntl, ...) if missing. Best effort: not fatal.
|
|
|
|
|
dev_install_posix() {
|
|
|
|
|
php -m 2>/dev/null | grep -qix posix && return 0
|
|
|
|
|
dev_log "installing php-process (posix extension)"
|
|
|
|
|
dev_dnf php-process >/dev/null 2>&1 \
|
|
|
|
|
|| echo "dev-mode: WARNING: could not install php-process (posix extension unavailable)" >&2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
## XDEBUG=1 -> install + configure Xdebug 3 for the image's PHP. Must run before
|
|
|
|
|
## php-fpm starts. Client settings (all overridable):
|
|
|
|
|
## XDEBUG_MODE debug (default; e.g. debug,develop,coverage)
|
2026-11-23 07:24:44 -08:00
|
|
|
## XDEBUG_START trigger (default; only requests/CLI runs carrying the XDEBUG_TRIGGER
|
|
|
|
|
## cookie, GET/POST parameter or env var) | yes (connect on every request) | no | default
|
2026-11-23 06:03:24 -08:00
|
|
|
## XDEBUG_CLIENT_HOST host.docker.internal (default)
|
|
|
|
|
## XDEBUG_CLIENT_PORT 9003 (default)
|
|
|
|
|
dev_setup_xdebug() {
|
|
|
|
|
dev_truthy "${XDEBUG:-}" || return 0
|
|
|
|
|
if ! php -m 2>/dev/null | grep -qix xdebug; then
|
|
|
|
|
dev_log "XDEBUG requested: installing php-pecl-xdebug3 for PHP $(php -r 'echo PHP_VERSION;')"
|
|
|
|
|
if ! dev_dnf php-pecl-xdebug3 >/dev/null 2>&1 && ! dev_dnf php-pecl-xdebug >/dev/null 2>&1; then
|
|
|
|
|
dev_fail "XDEBUG=1 but no Xdebug package could be installed for this PHP"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
local mode start host port
|
|
|
|
|
mode=${XDEBUG_MODE:-debug}
|
2026-11-23 07:24:44 -08:00
|
|
|
start=${XDEBUG_START:-trigger}
|
2026-11-23 06:03:24 -08:00
|
|
|
host=${XDEBUG_CLIENT_HOST:-host.docker.internal}
|
|
|
|
|
port=${XDEBUG_CLIENT_PORT:-9003}
|
|
|
|
|
# The values land in an ini file: refuse anything that is not plain.
|
|
|
|
|
if [[ ! $mode =~ ^[a-z,]+$ || ! $start =~ ^(yes|no|trigger|default)$ || \
|
|
|
|
|
! $host =~ ^[A-Za-z0-9._:-]+$ || ! $port =~ ^[0-9]{1,5}$ ]]; then
|
|
|
|
|
dev_fail "invalid XDEBUG_MODE/XDEBUG_START/XDEBUG_CLIENT_HOST/XDEBUG_CLIENT_PORT value"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
cat > /etc/php.d/98-xdebug-dev.ini <<EOF
|
|
|
|
|
; written by dev-mode.sh (XDEBUG=1, environment=DEV only)
|
|
|
|
|
xdebug.mode = ${mode}
|
|
|
|
|
xdebug.start_with_request = ${start}
|
|
|
|
|
xdebug.client_host = ${host}
|
|
|
|
|
xdebug.client_port = ${port}
|
|
|
|
|
xdebug.discover_client_host = 0
|
|
|
|
|
EOF
|
|
|
|
|
if php -m 2>/dev/null | grep -qix xdebug; then
|
|
|
|
|
dev_log "Xdebug loaded (mode=$mode start=$start client=$host:$port)"
|
|
|
|
|
else
|
|
|
|
|
dev_fail "XDEBUG=1: Xdebug installed but php does not load it"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 06:03:24 -08:00
|
|
|
## 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 &
|
2026-11-23 06:13:06 -08:00
|
|
|
for _ in $(seq 1 60); do
|
2026-11-23 06:03:24 -08:00
|
|
|
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/urandom | head -c 13 ; echo '')
|
|
|
|
|
mysql_password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 18 ; echo '')
|
|
|
|
|
mysql_db=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 6 ; echo '')
|
|
|
|
|
if ! { "$mysql_cli" -e "CREATE DATABASE devdb_${mysql_db};" \
|
|
|
|
|
&& "$mysql_cli" -e "CREATE USER '${mysql_user}'@'localhost' IDENTIFIED BY '${mysql_password}';" \
|
|
|
|
|
&& "$mysql_cli" -e "GRANT ALL PRIVILEGES ON *.* TO '${mysql_user}'@'localhost' WITH GRANT OPTION;" \
|
|
|
|
|
&& "$mysql_cli" -e "FLUSH PRIVILEGES;"; }; then
|
|
|
|
|
dev_fail "could not create the dev database/user"
|
|
|
|
|
return 0
|
|
|
|
|
fi
|
|
|
|
|
# Create user crontab with MySQL backup job. Written AS THE CUSTOMER
|
|
|
|
|
# (as_user tee), never by root through a customer path.
|
|
|
|
|
drop_symlink "/home/$u/crontab"
|
|
|
|
|
printf '%s\n' "# User crontab for $u" "*/15 * * * * /scripts/mysql-backup.sh $u devdb_${mysql_db}" \
|
|
|
|
|
| as_user tee "/home/$u/crontab" >/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"
|
|
|
|
|
}
|