From 653074dfe48ff14a627c840b080f851398aace10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Mik=C3=B3?= Date: Fri, 18 Oct 2024 16:15:11 +0200 Subject: [PATCH] chore: update check-deps and install scripts for magento and wordpress --- .../www-data/.local/bin/check-dependencies.sh | 139 +++++-- .../home/www-data/.local/bin/install.sh | 380 +++++++++++++++--- .../www-data/.local/bin/check-dependencies.sh | 51 ++- .../home/www-data/.local/bin/install.sh | 168 +++++--- .../bin/{start-wordpress.sh => start.sh} | 0 .../usr/local/bin/check-dependencies.sh | 139 +++++-- .../context/rootfs/usr/local/bin/install.sh | 380 +++++++++++++++--- .../usr/local/bin/check-dependencies.sh | 51 ++- .../context/rootfs/usr/local/bin/install.sh | 168 +++++--- 9 files changed, 1144 insertions(+), 332 deletions(-) rename images/php-fpm-rootless/wordpress-web/context/rootfs/home/www-data/.local/bin/{start-wordpress.sh => start.sh} (100%) diff --git a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/check-dependencies.sh b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/check-dependencies.sh index b405aba5..e301099e 100644 --- a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/check-dependencies.sh +++ b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/check-dependencies.sh @@ -1,58 +1,109 @@ #!/usr/bin/env bash -set -e - -echo "Checking database connection..." -if mysql -h"${MAGENTO_DATABASE_HOST:-db}" -P"${MAGENTO_DATABASE_PORT:-3306}" -u"${MAGENTO_DATABASE_USER:-magento}" -p"${MAGENTO_DATABASE_PASSWORD:-magento}" -e "CREATE DATABASE IF NOT EXISTS ${MAGENTO_DATABASE_NAME:-magento}; "; then - echo "Database connection ready." -else - echo "Database connection failed." - exit 1 +[ "${DEBUG:-false}" = "true" ] && set -x +set -eE -o pipefail -o errtrace +shopt -s extdebug + +if [ "${DEBUG:-false}" != "true" ]; then + QUIET="--quiet" fi -if [ "${MAGENTO_ELASTICSEARCH_ENABLED:-false}" = "true" ]; then - echo "Checking Elasticsearch connection..." - if curl --connect-timeout 10 -fsSL -X GET "http://${MAGENTO_ELASTICSEARCH_HOST:-opensearch}:${MAGENTO_ELASTICSEARCH_PORT:-9200}/_cat/health?pretty" &>/dev/null; then - echo "Elasticsearch connection ready." +log() { + [ "${SILENT:-false}" != "true" ] && printf "%s INFO: %s\n" "$(date --iso-8601=seconds)" "$*" +} + +error() { + exitcode=$? + # color red + printf "\033[1;31m%s ERROR: %s\033[0m\n" "$(date --iso-8601=seconds)" "$*" + + if [ "${exitcode}" -eq 0 ]; then exit 1; fi + exit "${exitcode}" +} + +trapinfo() { + # shellcheck disable=SC2145 + error "Command failed: $BASH_COMMAND STATUS=$? LINENO=${@:1:$(($# - 1))}" +} + +check_database() { + log "Checking database connection..." + if mysql -h"${MAGENTO_DATABASE_HOST:-db}" -P"${MAGENTO_DATABASE_PORT:-3306}" -u"${MAGENTO_DATABASE_USER:-magento}" -p"${MAGENTO_DATABASE_PASSWORD:-magento}" -e "CREATE DATABASE IF NOT EXISTS ${MAGENTO_DATABASE_NAME:-magento}; "; then + log "Database connection ready." else - echo "Elasticsearch connection failed." - exit 1 + error "Database connection failed." fi -fi +} -if [ "${MAGENTO_REDIS_ENABLED:-false}" = "true" ]; then - echo "Checking Redis connection..." - AUTH_COMMAND="" - if [ -n "${MAGENTO_REDIS_PASSWORD:-}" ]; then - AUTH_COMMAND="AUTH ${MAGENTO_REDIS_PASSWORD:-redis}\r\n" +check_elasticsearch() { + if [ "${MAGENTO_ELASTICSEARCH_ENABLED:-false}" = "true" ]; then + log "Checking Elasticsearch connection..." + if curl --connect-timeout 10 -fsSL -X GET "http://${MAGENTO_ELASTICSEARCH_HOST:-opensearch}:${MAGENTO_ELASTICSEARCH_PORT:-9200}/_cat/health?pretty" &>/dev/null; then + log "Elasticsearch connection ready." + else + error "Elasticsearch connection failed." + fi fi +} - if printf "%bPING\r\n" "${AUTH_COMMAND}" | nc -N -v "${MAGENTO_REDIS_HOST:-redis}" "${MAGENTO_REDIS_PORT:-6379}" | grep "PONG"; then - echo "Redis connection ready." - else - echo "Redis connection failed." - exit 1 +check_opensearch() { + if [ "${MAGENTO_OPENSEARCH_ENABLED:-false}" = "true" ]; then + log "Checking Opensearch connection..." + if curl --connect-timeout 10 -fsSL -X GET "http://${MAGENTO_OPENSEARCH_HOST:-opensearch}:${MAGENTO_OPENSEARCH_PORT:-9200}/_cat/health?pretty" &>/dev/null; then + log "Opensearch connection ready." + else + error "Opensearch connection failed." + fi fi -fi +} -if [ "${MAGENTO_VARNISH_ENABLED:-false}" = "true" ]; then - echo "Checking Varnish connection..." - if nc -v -z "${MAGENTO_VARNISH_HOST:-varnish}" "${MAGENTO_VARNISH_PORT:-80}"; then - echo "Varnish connection ready." - else - echo "Varnish connection failed." - exit 1 +check_redis() { + if [ "${MAGENTO_REDIS_ENABLED:-false}" = "true" ]; then + log "Checking Redis connection..." + AUTH_COMMAND="" + if [ -n "${MAGENTO_REDIS_PASSWORD:-}" ]; then + AUTH_COMMAND="AUTH ${MAGENTO_REDIS_PASSWORD:-redis}\r\n" + fi + + if printf "%bPING\r\n" "${AUTH_COMMAND}" | nc -N -v "${MAGENTO_REDIS_HOST:-redis}" "${MAGENTO_REDIS_PORT:-6379}" | grep "PONG"; then + log "Redis connection ready." + else + error "Redis connection failed." + fi fi -fi +} -if [ "${MAGENTO_RABBITMQ_ENABLED:-false}" = "true" ]; then - echo "Checking RabbitMQ connection..." - if nc -v -z "${MAGENTO_AMQP_HOST:-rabbitmq}" "${MAGENTO_AMQP_PORT:-5672}"; then - echo "RabbitMQ connection ready." - else - echo "RabbitMQ connection failed." - exit 1 +check_rabbitmq() { + if [ "${MAGENTO_RABBITMQ_ENABLED:-false}" = "true" ]; then + log "Checking RabbitMQ connection..." + if nc -v -z "${MAGENTO_AMQP_HOST:-rabbitmq}" "${MAGENTO_AMQP_PORT:-5672}"; then + log "RabbitMQ connection ready." + else + error "RabbitMQ connection failed." + fi fi -fi +} + +check_varnish() { + if [ "${MAGENTO_VARNISH_ENABLED:-false}" = "true" ]; then + log "Checking Varnish connection..." + if nc -v -z "${MAGENTO_VARNISH_HOST:-varnish}" "${MAGENTO_VARNISH_PORT:-80}"; then + log "Varnish connection ready." + else + error "Varnish connection failed." + fi + fi +} + +main() { + trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR + + check_database + check_elasticsearch + check_opensearch + check_redis + check_rabbitmq + check_varnish + log "All connections are ready." +} -echo "All connections are ready." -exit 0 +main diff --git a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/install.sh b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/install.sh index 510df9cd..f9f33687 100644 --- a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/install.sh +++ b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/install.sh @@ -1,32 +1,100 @@ #!/usr/bin/env bash -set -e -function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } +[ "${DEBUG:-false}" = "true" ] && set -x +set -eE -o pipefail -o errtrace +shopt -s extdebug -if [ -n "${COMMAND_BEFORE_INSTALL-}" ]; then eval "${COMMAND_BEFORE_INSTALL-}"; fi +log() { + [ "${SILENT:-false}" != "true" ] && printf "%s INFO: %s\n" "$(date --iso-8601=seconds)" "$*" +} -if [ "${MAGENTO_SKIP_BOOTSTRAP:-false}" = "true" ]; then - if [ -n "${COMMAND_AFTER_INSTALL-}" ]; then eval "${COMMAND_AFTER_INSTALL-}"; fi - exit -fi +error() { + exitcode=$? + # color red + printf "\033[1;31m%s ERROR: %s\033[0m\n" "$(date --iso-8601=seconds)" "$*" -if [ "${MAGENTO_SKIP_INSTALL:-false}" != "true" ]; then - MAGENTO_VERSION=${MAGENTO_VERSION:-'2.4.4'} - MAGENTO_HOST=${MAGENTO_HOST:-'magento.test'} - MAGENTO_BASE_URL=${MAGENTO_BASE_URL:-"http://$MAGENTO_HOST"} - MAGENTO_BASE_URL_SECURE=${MAGENTO_BASE_URL_SECURE:-"https://$MAGENTO_HOST"} + if [ "${exitcode}" -eq 0 ]; then exit 1; fi + exit "${exitcode}" +} - ARGS=() +trapinfo() { + # shellcheck disable=SC2145 + error "Command failed: $BASH_COMMAND STATUS=$? LINENO=${@:1:$(($# - 1))}" +} + +version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } + +command_before_install() { + if [ -n "${COMMAND_BEFORE_INSTALL-}" ]; then + log "Executing custom command before installation" + eval "${COMMAND_BEFORE_INSTALL-}" + fi +} + +command_after_install() { + if [ -n "${COMMAND_AFTER_INSTALL-}" ]; then + log "Executing custom command after installation" + eval "${COMMAND_AFTER_INSTALL-}" + fi +} + +MAGENTO_COMMAND="${MAGENTO_COMMAND:-php -derror_reporting=E_ALL bin/magento} --no-ansi --no-interaction" +MAGERUN_COMMAND="${MAGERUN_COMMAND:-php -derror_reporting=E_ALL $(command -v mr)} --no-ansi --no-interaction" + +magento_bootstrap_check() { + if [ "${MAGENTO_SKIP_BOOTSTRAP:-false}" = "true" ]; then + log "Skipping Magento bootstrap" + command_after_install + exit + fi +} + +magento_is_installed() { + if [ -f app/etc/env.php ]; then + return + fi + + false +} + +magento_args_install_only() { ARGS+=( "--base-url=${MAGENTO_BASE_URL}" "--base-url-secure=${MAGENTO_BASE_URL_SECURE}" + ) + if [ "${MAGENTO_ENABLE_HTTPS:-true}" = "true" ]; then + ARGS+=( + "--use-secure=1" + ) + fi + if [ "${MAGENTO_ENABLE_ADMIN_HTTPS:-true}" = "true" ]; then + ARGS+=( + "--use-secure-admin=1" + ) + fi + if [ "${MAGENTO_USE_REWRITES:-true}" = "true" ]; then + ARGS+=( + "--use-rewrites=1" + ) + fi +} + +magento_args_defaults() { + ARGS+=( "--key=${MAGENTO_KEY:-12345678901234567890123456789012}" "--backend-frontname=${MAGENTO_ADMIN_URL_PREFIX:-admin}" + ) +} + +magento_args_db() { + ARGS+=( "--db-host=${MAGENTO_DATABASE_HOST:-db}" "--db-name=${MAGENTO_DATABASE_NAME:-magento}" "--db-user=${MAGENTO_DATABASE_USER:-magento}" "--db-password=${MAGENTO_DATABASE_PASSWORD:-magento}" ) +} +magento_args_redis() { # Configure Redis if [ "${MAGENTO_REDIS_ENABLED:-true}" = "true" ]; then MAGENTO_REDIS_HOST=${MAGENTO_REDIS_HOST:-redis} @@ -79,14 +147,18 @@ if [ "${MAGENTO_SKIP_INSTALL:-false}" != "true" ]; then "--session-save=files" ) fi +} +magento_args_varnish() { # Configure Varnish if [ "${MAGENTO_VARNISH_ENABLED:-true}" = "true" ]; then ARGS+=( "--http-cache-hosts=${MAGENTO_VARNISH_HOST:-varnish}:${MAGENTO_VARNISH_PORT:-80}" ) fi +} +magento_args_rabbitmq() { # Configure RabbitMQ if [ "${MAGENTO_RABBITMQ_ENABLED:-true}" = "true" ]; then ARGS+=( @@ -103,91 +175,269 @@ if [ "${MAGENTO_SKIP_INSTALL:-false}" != "true" ]; then ) fi fi +} - # Configure Elasticsearch +magento_args_search() { if [ "${MAGENTO_ELASTICSEARCH_ENABLED:-true}" = "true" ]; then if version_gt "${MAGENTO_VERSION}" "2.3.99"; then - ARGS+=( - "--search-engine=${MAGENTO_SEARCH_ENGINE:-elasticsearch7}" - "--elasticsearch-host=${MAGENTO_ELASTICSEARCH_HOST:-opensearch}" - "--elasticsearch-port=${MAGENTO_ELASTICSEARCH_PORT:-9200}" - "--elasticsearch-index-prefix=${MAGENTO_ELASTICSEARCH_INDEX_PREFIX:-magento2}" - "--elasticsearch-enable-auth=${MAGENTO_ELASTICSEARCH_ENABLE_AUTH:-0}" - "--elasticsearch-timeout=${MAGENTO_ELASTICSEARCH_TIMEOUT:-15}" - ) + if [ "${MAGENTO_SEARCH_ENGINE:-opensearch}" = "elasticsearch7" ]; then + ARGS+=( + "--search-engine=${MAGENTO_SEARCH_ENGINE:-elasticsearch7}" + "--elasticsearch-host=${MAGENTO_ELASTICSEARCH_HOST:-elasticsearch}" + "--elasticsearch-port=${MAGENTO_ELASTICSEARCH_PORT:-9200}" + "--elasticsearch-index-prefix=${MAGENTO_ELASTICSEARCH_INDEX_PREFIX:-magento2}" + "--elasticsearch-enable-auth=${MAGENTO_ELASTICSEARCH_ENABLE_AUTH:-0}" + "--elasticsearch-timeout=${MAGENTO_ELASTICSEARCH_TIMEOUT:-15}" + ) + elif [ "${MAGENTO_SEARCH_ENGINE:-opensearch}" = "opensearch" ]; then + ARGS+=( + "--search-engine=${MAGENTO_SEARCH_ENGINE:-opensearch}" + "--opensearch-host=${MAGENTO_OPENSEARCH_HOST:-opensearch}" + "--opensearch-port=${MAGENTO_OPENSEARCH_PORT:-9200}" + "--opensearch-index-prefix=${MAGENTO_OPENSEARCH_INDEX_PREFIX:-magento2}" + "--opensearch-enable-auth=${MAGENTO_OPENSEARCH_ENABLE_AUTH:-0}" + "--opensearch-timeout=${MAGENTO_OPENSEARCH_TIMEOUT:-15}" + ) + fi fi fi +} +magento_args_sample_data() { if [ "${MAGENTO_DEPLOY_SAMPLE_DATA:-false}" = "true" ]; then ARGS+=( "--use-sample-data" ) fi +} +magento_args_extra() { if [ -n "${MAGENTO_EXTRA_INSTALL_ARGS:-}" ]; then ARGS+=( "${MAGENTO_EXTRA_INSTALL_ARGS}" ) fi +} + +magento_setup_install() { + if [ "${MAGENTO_SKIP_INSTALL:-false}" != "true" ]; then + log "Installing Magento ${MAGENTO_VERSION:-'2.4.4'}" + + MAGENTO_VERSION=${MAGENTO_VERSION:-'2.4.4'} + MAGENTO_HOST=${MAGENTO_HOST:-'magento.test'} + MAGENTO_BASE_URL=${MAGENTO_BASE_URL:-"http://$MAGENTO_HOST"} + MAGENTO_BASE_URL_SECURE=${MAGENTO_BASE_URL_SECURE:-"https://$MAGENTO_HOST"} + + ARGS=() + + magento_args_install_only + magento_args_defaults + magento_args_db + magento_args_redis + magento_args_varnish + magento_args_rabbitmq + magento_args_search + magento_args_sample_data + magento_args_extra + + # shellcheck disable=SC2068 + ${MAGENTO_COMMAND} setup:install ${ARGS[@]} + + magento_configure_search + fi +} + +magento_configure() { + log "Configuring Magento" + INSTALL_DATE=$(${MAGERUN_COMMAND} config:env:show install.date 2>/dev/null || true) + CACHE_GRAPHQL_ID_SALT=$(${MAGERUN_COMMAND} config:env:show cache.graphql.id_salt 2>/dev/null || true) - bin/magento setup:install --no-interaction ${ARGS[@]} + rm -f app/etc/env.php + MAGENTO_VERSION=${MAGENTO_VERSION:-'2.4.4'} + + ARGS=() + magento_args_defaults + magento_args_db + magento_args_redis + magento_args_varnish + magento_args_rabbitmq + + # shellcheck disable=SC2068 + ${MAGENTO_COMMAND} setup:config:set ${ARGS[@]} + + if [ -z "${INSTALL_DATE:+x}" ]; then INSTALL_DATE=$(date -R); fi + ${MAGERUN_COMMAND} config:env:set install.date "${INSTALL_DATE}" + + if [ -n "${CACHE_GRAPHQL_ID_SALT}" ]; then ${MAGERUN_COMMAND} config:env:set cache.graphql.id_salt "${CACHE_GRAPHQL_ID_SALT}"; fi + + magento_configure_search +} + +magento_configure_search() { + if [ "${MAGENTO_ELASTICSEARCH_ENABLED:-true}" = "true" ]; then + if version_gt "${MAGENTO_VERSION}" "2.3.99"; then + if [ "${MAGENTO_SEARCH_ENGINE:-opensearch}" = "elasticsearch7" ]; then + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/engine" "${MAGENTO_SEARCH_ENGINE:-opensearch}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_server_hostname" "${MAGENTO_ELASTICSEARCH_HOST:-opensearch}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_server_port" "${MAGENTO_ELASTICSEARCH_PORT:-9200}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_index_prefix" "${MAGENTO_ELASTICSEARCH_INDEX_PREFIX:-magento2}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_enable_auth" "${MAGENTO_ELASTICSEARCH_ENABLE_AUTH:-0}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_server_timeout" "${MAGENTO_ELASTICSEARCH_TIMEOUT:-15}" + elif [ "${MAGENTO_SEARCH_ENGINE:-opensearch}" = "opensearch" ]; then + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/engine" "${MAGENTO_SEARCH_ENGINE:-opensearch}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_server_hostname" "${MAGENTO_OPENSEARCH_HOST:-opensearch}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_server_port" "${MAGENTO_OPENSEARCH_PORT:-9200}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_index_prefix" "${MAGENTO_OPENSEARCH_INDEX_PREFIX:-magento2}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_enable_auth" "${MAGENTO_OPENSEARCH_ENABLE_AUTH:-0}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_server_timeout" "${MAGENTO_OPENSEARCH_TIMEOUT:-15}" + fi + fi + fi +} + +magento_setup_di_compile() { if [ "${MAGENTO_DI_COMPILE:-false}" = "true" ] || [ "${MAGENTO_DI_COMPILE_ON_DEMAND:-false}" = "true" ]; then - bin/magento setup:di:compile --no-interaction --ansi + log "Compiling Magento dependencies" + ${MAGENTO_COMMAND} setup:di:compile fi +} +magento_setup_static_content_deploy() { if [ "${MAGENTO_STATIC_CONTENT_DEPLOY:-false}" = "true" ] || [ "${MAGENTO_SCD_ON_DEMAND:-false}" = "true" ]; then + log "Deploying static content" if [ "${MAGENTO_STATIC_CONTENT_DEPLOY_FORCE}" = "true" ]; then - bin/magento setup:static-content:deploy --no-interaction --jobs=$(nproc) -fv ${MAGENTO_LANGUAGES:-} + ${MAGENTO_COMMAND} setup:static-content:deploy --jobs=$(nproc) -fv ${MAGENTO_LANGUAGES:-} else - bin/magento setup:static-content:deploy --no-interaction --jobs=$(nproc) -v ${MAGENTO_LANGUAGES:-} + ${MAGENTO_COMMAND} setup:static-content:deploy --jobs=$(nproc) -v ${MAGENTO_LANGUAGES:-} fi fi -fi +} -if [ "${MAGENTO_MODE:-default}" != "default" ]; then - bin/magento deploy:mode:set --no-interaction "${MAGENTO_MODE}" -fi +magento_cache_enable() { + log "Enabling cache" + ${MAGENTO_COMMAND} cache:enable +} -if [ "${MAGENTO_ENABLE_HTTPS:-true}" = "true" ]; then - bin/magento config:set --no-interaction "web/secure/use_in_frontend" 1 -fi +magento_reindex() { + if [ "${MAGENTO_SKIP_REINDEX:-true}" != "true" ]; then + ${MAGENTO_COMMAND} indexer:reindex + fi +} -if [ "${MAGENTO_ENABLE_ADMIN_HTTPS:-true}" = "true" ]; then - bin/magento config:set --no-interaction "web/secure/use_in_adminhtml" 1 -fi +magento_upgrade_required() { + log "Checking if Magento upgrade is required" + if ${MAGENTO_COMMAND} setup:db:status; then + false + fi +} -if [ "${MAGENTO_USE_REWRITES:-true}" = "true" ]; then - bin/magento config:set --no-interaction "web/seo/use_rewrites" 1 -fi +magento_maintenance_disable() { + log "Disabling maintenance mode" + ${MAGENTO_COMMAND} maintenance:disable +} -if mr admin:user:list --no-interaction --format=csv | tail -n +2 | awk -F',' '{print $2}' | grep "^${MAGENTO_USERNAME:-admin}$" >/dev/null; then - mr admin:user:change-password --no-interaction ${MAGENTO_USERNAME:-admin} ${MAGENTO_PASSWORD:-ASDqwe123} -else - ARGS=() - ARGS+=( - "--admin-firstname=${MAGENTO_FIRST_NAME:-admin}" - "--admin-lastname=${MAGENTO_LAST_NAME:-admin}" - "--admin-email=${MAGENTO_EMAIL:-admin@example.com}" - "--admin-user=${MAGENTO_USERNAME:-admin}" - "--admin-password=${MAGENTO_PASSWORD:-ASDqwe123}" - ) - mr admin:user:delete --force --no-interaction "${MAGENTO_USERNAME:-admin}" || true - mr admin:user:delete --force --no-interaction "${MAGENTO_EMAIL:-admin@example.com}" || true - mr admin:user:create --no-interaction "${ARGS[@]}" -fi +magento_maintenance_enable() { + log "Enabling maintenance mode" + ${MAGENTO_COMMAND} maintenance:enable +} + +magento_setup_upgrade() { + if [ "${MAGENTO_SKIP_UPGRADE:-false}" != "true" ]; then + if magento_upgrade_required; then + log "Running Magento setup:upgrade" + magento_maintenance_enable + ${MAGENTO_COMMAND} setup:upgrade --keep-generated + magento_maintenance_disable + fi + + log "Magento upgrade is not required" + fi +} + +magento_deploy_mode_set() { + if [ "${MAGENTO_MODE:-default}" != "default" ]; then + log "Setting Magento deploy mode to ${MAGENTO_MODE:-default}" + ${MAGENTO_COMMAND} deploy:mode:set "${MAGENTO_MODE:-default}" + fi +} + +magento_secure_frontend() { + if [ "${MAGENTO_ENABLE_HTTPS:-true}" = "true" ]; then + log "Enabling HTTPS for frontend" + ${MAGENTO_COMMAND} config:set "web/secure/use_in_frontend" 1 + fi +} + +magento_secure_backend() { + if [ "${MAGENTO_ENABLE_ADMIN_HTTPS:-true}" = "true" ]; then + log "Enabling HTTPS for admin" + ${MAGENTO_COMMAND} config:set "web/secure/use_in_adminhtml" 1 + fi +} + +magento_use_rewrites() { + if [ "${MAGENTO_USE_REWRITES:-true}" = "true" ]; then + log "Enabling rewrites" + ${MAGENTO_COMMAND} config:set "web/seo/use_rewrites" 1 + fi +} + +magento_admin_user() { + if ${MAGERUN_COMMAND} admin:user:list --format=csv | tail -n +2 | awk -F',' '{print $2}' | grep "^${MAGENTO_USERNAME:-admin}$" >/dev/null; then + log "Admin user already exists, updating password" + ${MAGERUN_COMMAND} admin:user:change-password ${MAGENTO_USERNAME:-admin} ${MAGENTO_PASSWORD:-ASDqwe123} + else + log "Creating admin user" + ARGS=() + ARGS+=( + "--admin-firstname=${MAGENTO_FIRST_NAME:-admin}" + "--admin-lastname=${MAGENTO_LAST_NAME:-admin}" + "--admin-email=${MAGENTO_EMAIL:-admin@example.com}" + "--admin-user=${MAGENTO_USERNAME:-admin}" + "--admin-password=${MAGENTO_PASSWORD:-ASDqwe123}" + ) + ${MAGERUN_COMMAND} admin:user:delete --force "${MAGENTO_USERNAME:-admin}" || true + ${MAGERUN_COMMAND} admin:user:delete --force "${MAGENTO_EMAIL:-admin@example.com}" || true + ${MAGERUN_COMMAND} admin:user:create "${ARGS[@]}" + fi +} + +magento_deploy_sample_data() { + if [ "${MAGENTO_DEPLOY_SAMPLE_DATA:-false}" = "true" ]; then + log "Deploying sample data" + ${MAGENTO_COMMAND} sampledata:deploy + ${MAGENTO_COMMAND} setup:upgrade --keep-generated + fi +} -if [ "${MAGENTO_DEPLOY_SAMPLE_DATA:-false}" = "true" ]; then - bin/magento sampledata:deploy --no-interaction - bin/magento setup:upgrade --no-interaction --keep-generated -fi +main() { + trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR + + command_before_install + + magento_bootstrap_check + + if magento_is_installed; then + magento_configure + magento_setup_upgrade + else + magento_setup_install + fi -if [ "${MAGENTO_DEPLOY_STATIC_CONTENT:-false}" = "true" ]; then - bin/magento setup:static-content:deploy --no-interaction --jobs="$(nproc)" -fv "${MAGENTO_LANGUAGES:-}" -fi + magento_setup_di_compile + magento_setup_static_content_deploy + magento_deploy_mode_set + magento_secure_frontend + magento_secure_backend + magento_use_rewrites + magento_admin_user + magento_deploy_sample_data + magento_setup_static_content_deploy + magento_cache_enable + magento_reindex -if [ "${MAGENTO_SKIP_REINDEX:-true}" != "true" ]; then - bin/magento indexer:reindex --no-interaction -fi + command_after_install +} -if [ -n "${COMMAND_AFTER_INSTALL-}" ]; then eval "${COMMAND_AFTER_INSTALL-}"; fi +main diff --git a/images/php-fpm-rootless/wordpress-web/context/rootfs/home/www-data/.local/bin/check-dependencies.sh b/images/php-fpm-rootless/wordpress-web/context/rootfs/home/www-data/.local/bin/check-dependencies.sh index 1cbd20aa..f903059e 100644 --- a/images/php-fpm-rootless/wordpress-web/context/rootfs/home/www-data/.local/bin/check-dependencies.sh +++ b/images/php-fpm-rootless/wordpress-web/context/rootfs/home/www-data/.local/bin/check-dependencies.sh @@ -1,13 +1,44 @@ #!/usr/bin/env bash -set -e - -echo "Checking database connection..." -if mysql -h"${WORDPRESS_DATABASE_HOST:-db}" -P"${WORDPRESS_DATABASE_PORT:-3306}" -u"${WORDPRESS_DATABASE_NAME:-wordpress}" -p"${WORDPRESS_DATABASE_PASSWORD:-wordpress}" -e "CREATE DATABASE IF NOT EXISTS ${WORDPRESS_DATABASE_NAME:-wordpress}; "; then - echo "Database connection ready." -else - echo "Database connection failed." - exit 1 +[ "${DEBUG:-false}" = "true" ] && set -x +set -eE -o pipefail -o errtrace +shopt -s extdebug + +if [ "${DEBUG:-false}" != "true" ]; then + QUIET="--quiet" fi -echo "All connections are ready." -exit 0 +log() { + [ "${SILENT:-false}" != "true" ] && printf "%s INFO: %s\n" "$(date --iso-8601=seconds)" "$*" +} + +error() { + exitcode=$? + # color red + printf "\033[1;31m%s ERROR: %s\033[0m\n" "$(date --iso-8601=seconds)" "$*" + + if [ "${exitcode}" -eq 0 ]; then exit 1; fi + exit "${exitcode}" +} + +trapinfo() { + # shellcheck disable=SC2145 + error "Command failed: $BASH_COMMAND STATUS=$? LINENO=${@:1:$(($# - 1))}" +} + +check_database() { + log "Checking database connection..." + if mysql -h"${WORDPRESS_DATABASE_HOST:-db}" -P"${WORDPRESS_DATABASE_PORT:-3306}" -u"${WORDPRESS_DATABASE_NAME:-wordpress}" -p"${WORDPRESS_DATABASE_PASSWORD:-wordpress}" -e "CREATE DATABASE IF NOT EXISTS ${WORDPRESS_DATABASE_NAME:-wordpress}; "; then + log "Database connection ready." + else + error "Database connection failed." + fi +} + +main() { + trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR + + check_database + log "All connections are ready." +} + +main diff --git a/images/php-fpm-rootless/wordpress-web/context/rootfs/home/www-data/.local/bin/install.sh b/images/php-fpm-rootless/wordpress-web/context/rootfs/home/www-data/.local/bin/install.sh index db70f586..7a762554 100644 --- a/images/php-fpm-rootless/wordpress-web/context/rootfs/home/www-data/.local/bin/install.sh +++ b/images/php-fpm-rootless/wordpress-web/context/rootfs/home/www-data/.local/bin/install.sh @@ -1,57 +1,131 @@ #!/usr/bin/env bash -set -e - -if [ -n "${COMMAND_BEFORE_INSTALL-}" ]; then eval "${COMMAND_BEFORE_INSTALL-}"; fi - -if [ "${WORDPRESS_SKIP_BOOTSTRAP:-false}" = "true" ]; then - if [ -n "${COMMAND_AFTER_INSTALL-}" ]; then eval "${COMMAND_AFTER_INSTALL-}"; fi - exit -fi - -if [ "${WORDPRESS_CONFIG:-true}" = "true" ]; then - ARGS=() - ARGS+=( - "--force" - "--dbhost=${WORDPRESS_DATABASE_HOST:-db}" - "--dbname=${WORDPRESS_DATABASE_NAME:-wordpress}" - "--dbuser=${WORDPRESS_DATABASE_USER:-wordpress}" - "--dbpass=${WORDPRESS_DATABASE_PASSWORD:-wordpress}" - "--dbprefix=${WORDPRESS_DATABASE_PREFIX:-wp_}" - "--dbcharset=${WORDPRESS_DATABASE_CHARSET:-utf8}" - "--dbcollate=${WORDPRESS_DATABASE_COLLATE:-}" - "--locale=${WORDPRESS_LOCALE:-}" - ) - - wp core config "${ARGS[@]}" --extra-php </dev/null; then - echo "Elasticsearch connection ready." +log() { + [ "${SILENT:-false}" != "true" ] && printf "%s INFO: %s\n" "$(date --iso-8601=seconds)" "$*" +} + +error() { + exitcode=$? + # color red + printf "\033[1;31m%s ERROR: %s\033[0m\n" "$(date --iso-8601=seconds)" "$*" + + if [ "${exitcode}" -eq 0 ]; then exit 1; fi + exit "${exitcode}" +} + +trapinfo() { + # shellcheck disable=SC2145 + error "Command failed: $BASH_COMMAND STATUS=$? LINENO=${@:1:$(($# - 1))}" +} + +check_database() { + log "Checking database connection..." + if mysql -h"${MAGENTO_DATABASE_HOST:-db}" -P"${MAGENTO_DATABASE_PORT:-3306}" -u"${MAGENTO_DATABASE_USER:-magento}" -p"${MAGENTO_DATABASE_PASSWORD:-magento}" -e "CREATE DATABASE IF NOT EXISTS ${MAGENTO_DATABASE_NAME:-magento}; "; then + log "Database connection ready." else - echo "Elasticsearch connection failed." - exit 1 + error "Database connection failed." fi -fi +} -if [ "${MAGENTO_REDIS_ENABLED:-false}" = "true" ]; then - echo "Checking Redis connection..." - AUTH_COMMAND="" - if [ -n "${MAGENTO_REDIS_PASSWORD:-}" ]; then - AUTH_COMMAND="AUTH ${MAGENTO_REDIS_PASSWORD:-redis}\r\n" +check_elasticsearch() { + if [ "${MAGENTO_ELASTICSEARCH_ENABLED:-false}" = "true" ]; then + log "Checking Elasticsearch connection..." + if curl --connect-timeout 10 -fsSL -X GET "http://${MAGENTO_ELASTICSEARCH_HOST:-opensearch}:${MAGENTO_ELASTICSEARCH_PORT:-9200}/_cat/health?pretty" &>/dev/null; then + log "Elasticsearch connection ready." + else + error "Elasticsearch connection failed." + fi fi +} - if printf "%bPING\r\n" "${AUTH_COMMAND}" | nc -N -v "${MAGENTO_REDIS_HOST:-redis}" "${MAGENTO_REDIS_PORT:-6379}" | grep "PONG"; then - echo "Redis connection ready." - else - echo "Redis connection failed." - exit 1 +check_opensearch() { + if [ "${MAGENTO_OPENSEARCH_ENABLED:-false}" = "true" ]; then + log "Checking Opensearch connection..." + if curl --connect-timeout 10 -fsSL -X GET "http://${MAGENTO_OPENSEARCH_HOST:-opensearch}:${MAGENTO_OPENSEARCH_PORT:-9200}/_cat/health?pretty" &>/dev/null; then + log "Opensearch connection ready." + else + error "Opensearch connection failed." + fi fi -fi +} -if [ "${MAGENTO_VARNISH_ENABLED:-false}" = "true" ]; then - echo "Checking Varnish connection..." - if nc -v -z "${MAGENTO_VARNISH_HOST:-varnish}" "${MAGENTO_VARNISH_PORT:-80}"; then - echo "Varnish connection ready." - else - echo "Varnish connection failed." - exit 1 +check_redis() { + if [ "${MAGENTO_REDIS_ENABLED:-false}" = "true" ]; then + log "Checking Redis connection..." + AUTH_COMMAND="" + if [ -n "${MAGENTO_REDIS_PASSWORD:-}" ]; then + AUTH_COMMAND="AUTH ${MAGENTO_REDIS_PASSWORD:-redis}\r\n" + fi + + if printf "%bPING\r\n" "${AUTH_COMMAND}" | nc -N -v "${MAGENTO_REDIS_HOST:-redis}" "${MAGENTO_REDIS_PORT:-6379}" | grep "PONG"; then + log "Redis connection ready." + else + error "Redis connection failed." + fi fi -fi +} -if [ "${MAGENTO_RABBITMQ_ENABLED:-false}" = "true" ]; then - echo "Checking RabbitMQ connection..." - if nc -v -z "${MAGENTO_AMQP_HOST:-rabbitmq}" "${MAGENTO_AMQP_PORT:-5672}"; then - echo "RabbitMQ connection ready." - else - echo "RabbitMQ connection failed." - exit 1 +check_rabbitmq() { + if [ "${MAGENTO_RABBITMQ_ENABLED:-false}" = "true" ]; then + log "Checking RabbitMQ connection..." + if nc -v -z "${MAGENTO_AMQP_HOST:-rabbitmq}" "${MAGENTO_AMQP_PORT:-5672}"; then + log "RabbitMQ connection ready." + else + error "RabbitMQ connection failed." + fi fi -fi +} + +check_varnish() { + if [ "${MAGENTO_VARNISH_ENABLED:-false}" = "true" ]; then + log "Checking Varnish connection..." + if nc -v -z "${MAGENTO_VARNISH_HOST:-varnish}" "${MAGENTO_VARNISH_PORT:-80}"; then + log "Varnish connection ready." + else + error "Varnish connection failed." + fi + fi +} + +main() { + trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR + + check_database + check_elasticsearch + check_opensearch + check_redis + check_rabbitmq + check_varnish + log "All connections are ready." +} -echo "All connections are ready." -exit 0 +main diff --git a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/install.sh b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/install.sh index 510df9cd..f9f33687 100644 --- a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/install.sh +++ b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/install.sh @@ -1,32 +1,100 @@ #!/usr/bin/env bash -set -e -function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } +[ "${DEBUG:-false}" = "true" ] && set -x +set -eE -o pipefail -o errtrace +shopt -s extdebug -if [ -n "${COMMAND_BEFORE_INSTALL-}" ]; then eval "${COMMAND_BEFORE_INSTALL-}"; fi +log() { + [ "${SILENT:-false}" != "true" ] && printf "%s INFO: %s\n" "$(date --iso-8601=seconds)" "$*" +} -if [ "${MAGENTO_SKIP_BOOTSTRAP:-false}" = "true" ]; then - if [ -n "${COMMAND_AFTER_INSTALL-}" ]; then eval "${COMMAND_AFTER_INSTALL-}"; fi - exit -fi +error() { + exitcode=$? + # color red + printf "\033[1;31m%s ERROR: %s\033[0m\n" "$(date --iso-8601=seconds)" "$*" -if [ "${MAGENTO_SKIP_INSTALL:-false}" != "true" ]; then - MAGENTO_VERSION=${MAGENTO_VERSION:-'2.4.4'} - MAGENTO_HOST=${MAGENTO_HOST:-'magento.test'} - MAGENTO_BASE_URL=${MAGENTO_BASE_URL:-"http://$MAGENTO_HOST"} - MAGENTO_BASE_URL_SECURE=${MAGENTO_BASE_URL_SECURE:-"https://$MAGENTO_HOST"} + if [ "${exitcode}" -eq 0 ]; then exit 1; fi + exit "${exitcode}" +} - ARGS=() +trapinfo() { + # shellcheck disable=SC2145 + error "Command failed: $BASH_COMMAND STATUS=$? LINENO=${@:1:$(($# - 1))}" +} + +version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } + +command_before_install() { + if [ -n "${COMMAND_BEFORE_INSTALL-}" ]; then + log "Executing custom command before installation" + eval "${COMMAND_BEFORE_INSTALL-}" + fi +} + +command_after_install() { + if [ -n "${COMMAND_AFTER_INSTALL-}" ]; then + log "Executing custom command after installation" + eval "${COMMAND_AFTER_INSTALL-}" + fi +} + +MAGENTO_COMMAND="${MAGENTO_COMMAND:-php -derror_reporting=E_ALL bin/magento} --no-ansi --no-interaction" +MAGERUN_COMMAND="${MAGERUN_COMMAND:-php -derror_reporting=E_ALL $(command -v mr)} --no-ansi --no-interaction" + +magento_bootstrap_check() { + if [ "${MAGENTO_SKIP_BOOTSTRAP:-false}" = "true" ]; then + log "Skipping Magento bootstrap" + command_after_install + exit + fi +} + +magento_is_installed() { + if [ -f app/etc/env.php ]; then + return + fi + + false +} + +magento_args_install_only() { ARGS+=( "--base-url=${MAGENTO_BASE_URL}" "--base-url-secure=${MAGENTO_BASE_URL_SECURE}" + ) + if [ "${MAGENTO_ENABLE_HTTPS:-true}" = "true" ]; then + ARGS+=( + "--use-secure=1" + ) + fi + if [ "${MAGENTO_ENABLE_ADMIN_HTTPS:-true}" = "true" ]; then + ARGS+=( + "--use-secure-admin=1" + ) + fi + if [ "${MAGENTO_USE_REWRITES:-true}" = "true" ]; then + ARGS+=( + "--use-rewrites=1" + ) + fi +} + +magento_args_defaults() { + ARGS+=( "--key=${MAGENTO_KEY:-12345678901234567890123456789012}" "--backend-frontname=${MAGENTO_ADMIN_URL_PREFIX:-admin}" + ) +} + +magento_args_db() { + ARGS+=( "--db-host=${MAGENTO_DATABASE_HOST:-db}" "--db-name=${MAGENTO_DATABASE_NAME:-magento}" "--db-user=${MAGENTO_DATABASE_USER:-magento}" "--db-password=${MAGENTO_DATABASE_PASSWORD:-magento}" ) +} +magento_args_redis() { # Configure Redis if [ "${MAGENTO_REDIS_ENABLED:-true}" = "true" ]; then MAGENTO_REDIS_HOST=${MAGENTO_REDIS_HOST:-redis} @@ -79,14 +147,18 @@ if [ "${MAGENTO_SKIP_INSTALL:-false}" != "true" ]; then "--session-save=files" ) fi +} +magento_args_varnish() { # Configure Varnish if [ "${MAGENTO_VARNISH_ENABLED:-true}" = "true" ]; then ARGS+=( "--http-cache-hosts=${MAGENTO_VARNISH_HOST:-varnish}:${MAGENTO_VARNISH_PORT:-80}" ) fi +} +magento_args_rabbitmq() { # Configure RabbitMQ if [ "${MAGENTO_RABBITMQ_ENABLED:-true}" = "true" ]; then ARGS+=( @@ -103,91 +175,269 @@ if [ "${MAGENTO_SKIP_INSTALL:-false}" != "true" ]; then ) fi fi +} - # Configure Elasticsearch +magento_args_search() { if [ "${MAGENTO_ELASTICSEARCH_ENABLED:-true}" = "true" ]; then if version_gt "${MAGENTO_VERSION}" "2.3.99"; then - ARGS+=( - "--search-engine=${MAGENTO_SEARCH_ENGINE:-elasticsearch7}" - "--elasticsearch-host=${MAGENTO_ELASTICSEARCH_HOST:-opensearch}" - "--elasticsearch-port=${MAGENTO_ELASTICSEARCH_PORT:-9200}" - "--elasticsearch-index-prefix=${MAGENTO_ELASTICSEARCH_INDEX_PREFIX:-magento2}" - "--elasticsearch-enable-auth=${MAGENTO_ELASTICSEARCH_ENABLE_AUTH:-0}" - "--elasticsearch-timeout=${MAGENTO_ELASTICSEARCH_TIMEOUT:-15}" - ) + if [ "${MAGENTO_SEARCH_ENGINE:-opensearch}" = "elasticsearch7" ]; then + ARGS+=( + "--search-engine=${MAGENTO_SEARCH_ENGINE:-elasticsearch7}" + "--elasticsearch-host=${MAGENTO_ELASTICSEARCH_HOST:-elasticsearch}" + "--elasticsearch-port=${MAGENTO_ELASTICSEARCH_PORT:-9200}" + "--elasticsearch-index-prefix=${MAGENTO_ELASTICSEARCH_INDEX_PREFIX:-magento2}" + "--elasticsearch-enable-auth=${MAGENTO_ELASTICSEARCH_ENABLE_AUTH:-0}" + "--elasticsearch-timeout=${MAGENTO_ELASTICSEARCH_TIMEOUT:-15}" + ) + elif [ "${MAGENTO_SEARCH_ENGINE:-opensearch}" = "opensearch" ]; then + ARGS+=( + "--search-engine=${MAGENTO_SEARCH_ENGINE:-opensearch}" + "--opensearch-host=${MAGENTO_OPENSEARCH_HOST:-opensearch}" + "--opensearch-port=${MAGENTO_OPENSEARCH_PORT:-9200}" + "--opensearch-index-prefix=${MAGENTO_OPENSEARCH_INDEX_PREFIX:-magento2}" + "--opensearch-enable-auth=${MAGENTO_OPENSEARCH_ENABLE_AUTH:-0}" + "--opensearch-timeout=${MAGENTO_OPENSEARCH_TIMEOUT:-15}" + ) + fi fi fi +} +magento_args_sample_data() { if [ "${MAGENTO_DEPLOY_SAMPLE_DATA:-false}" = "true" ]; then ARGS+=( "--use-sample-data" ) fi +} +magento_args_extra() { if [ -n "${MAGENTO_EXTRA_INSTALL_ARGS:-}" ]; then ARGS+=( "${MAGENTO_EXTRA_INSTALL_ARGS}" ) fi +} + +magento_setup_install() { + if [ "${MAGENTO_SKIP_INSTALL:-false}" != "true" ]; then + log "Installing Magento ${MAGENTO_VERSION:-'2.4.4'}" + + MAGENTO_VERSION=${MAGENTO_VERSION:-'2.4.4'} + MAGENTO_HOST=${MAGENTO_HOST:-'magento.test'} + MAGENTO_BASE_URL=${MAGENTO_BASE_URL:-"http://$MAGENTO_HOST"} + MAGENTO_BASE_URL_SECURE=${MAGENTO_BASE_URL_SECURE:-"https://$MAGENTO_HOST"} + + ARGS=() + + magento_args_install_only + magento_args_defaults + magento_args_db + magento_args_redis + magento_args_varnish + magento_args_rabbitmq + magento_args_search + magento_args_sample_data + magento_args_extra + + # shellcheck disable=SC2068 + ${MAGENTO_COMMAND} setup:install ${ARGS[@]} + + magento_configure_search + fi +} + +magento_configure() { + log "Configuring Magento" + INSTALL_DATE=$(${MAGERUN_COMMAND} config:env:show install.date 2>/dev/null || true) + CACHE_GRAPHQL_ID_SALT=$(${MAGERUN_COMMAND} config:env:show cache.graphql.id_salt 2>/dev/null || true) - bin/magento setup:install --no-interaction ${ARGS[@]} + rm -f app/etc/env.php + MAGENTO_VERSION=${MAGENTO_VERSION:-'2.4.4'} + + ARGS=() + magento_args_defaults + magento_args_db + magento_args_redis + magento_args_varnish + magento_args_rabbitmq + + # shellcheck disable=SC2068 + ${MAGENTO_COMMAND} setup:config:set ${ARGS[@]} + + if [ -z "${INSTALL_DATE:+x}" ]; then INSTALL_DATE=$(date -R); fi + ${MAGERUN_COMMAND} config:env:set install.date "${INSTALL_DATE}" + + if [ -n "${CACHE_GRAPHQL_ID_SALT}" ]; then ${MAGERUN_COMMAND} config:env:set cache.graphql.id_salt "${CACHE_GRAPHQL_ID_SALT}"; fi + + magento_configure_search +} + +magento_configure_search() { + if [ "${MAGENTO_ELASTICSEARCH_ENABLED:-true}" = "true" ]; then + if version_gt "${MAGENTO_VERSION}" "2.3.99"; then + if [ "${MAGENTO_SEARCH_ENGINE:-opensearch}" = "elasticsearch7" ]; then + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/engine" "${MAGENTO_SEARCH_ENGINE:-opensearch}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_server_hostname" "${MAGENTO_ELASTICSEARCH_HOST:-opensearch}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_server_port" "${MAGENTO_ELASTICSEARCH_PORT:-9200}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_index_prefix" "${MAGENTO_ELASTICSEARCH_INDEX_PREFIX:-magento2}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_enable_auth" "${MAGENTO_ELASTICSEARCH_ENABLE_AUTH:-0}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/elasticsearch7_server_timeout" "${MAGENTO_ELASTICSEARCH_TIMEOUT:-15}" + elif [ "${MAGENTO_SEARCH_ENGINE:-opensearch}" = "opensearch" ]; then + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/engine" "${MAGENTO_SEARCH_ENGINE:-opensearch}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_server_hostname" "${MAGENTO_OPENSEARCH_HOST:-opensearch}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_server_port" "${MAGENTO_OPENSEARCH_PORT:-9200}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_index_prefix" "${MAGENTO_OPENSEARCH_INDEX_PREFIX:-magento2}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_enable_auth" "${MAGENTO_OPENSEARCH_ENABLE_AUTH:-0}" + ${MAGENTO_COMMAND} config:set --lock-env "catalog/search/opensearch_server_timeout" "${MAGENTO_OPENSEARCH_TIMEOUT:-15}" + fi + fi + fi +} + +magento_setup_di_compile() { if [ "${MAGENTO_DI_COMPILE:-false}" = "true" ] || [ "${MAGENTO_DI_COMPILE_ON_DEMAND:-false}" = "true" ]; then - bin/magento setup:di:compile --no-interaction --ansi + log "Compiling Magento dependencies" + ${MAGENTO_COMMAND} setup:di:compile fi +} +magento_setup_static_content_deploy() { if [ "${MAGENTO_STATIC_CONTENT_DEPLOY:-false}" = "true" ] || [ "${MAGENTO_SCD_ON_DEMAND:-false}" = "true" ]; then + log "Deploying static content" if [ "${MAGENTO_STATIC_CONTENT_DEPLOY_FORCE}" = "true" ]; then - bin/magento setup:static-content:deploy --no-interaction --jobs=$(nproc) -fv ${MAGENTO_LANGUAGES:-} + ${MAGENTO_COMMAND} setup:static-content:deploy --jobs=$(nproc) -fv ${MAGENTO_LANGUAGES:-} else - bin/magento setup:static-content:deploy --no-interaction --jobs=$(nproc) -v ${MAGENTO_LANGUAGES:-} + ${MAGENTO_COMMAND} setup:static-content:deploy --jobs=$(nproc) -v ${MAGENTO_LANGUAGES:-} fi fi -fi +} -if [ "${MAGENTO_MODE:-default}" != "default" ]; then - bin/magento deploy:mode:set --no-interaction "${MAGENTO_MODE}" -fi +magento_cache_enable() { + log "Enabling cache" + ${MAGENTO_COMMAND} cache:enable +} -if [ "${MAGENTO_ENABLE_HTTPS:-true}" = "true" ]; then - bin/magento config:set --no-interaction "web/secure/use_in_frontend" 1 -fi +magento_reindex() { + if [ "${MAGENTO_SKIP_REINDEX:-true}" != "true" ]; then + ${MAGENTO_COMMAND} indexer:reindex + fi +} -if [ "${MAGENTO_ENABLE_ADMIN_HTTPS:-true}" = "true" ]; then - bin/magento config:set --no-interaction "web/secure/use_in_adminhtml" 1 -fi +magento_upgrade_required() { + log "Checking if Magento upgrade is required" + if ${MAGENTO_COMMAND} setup:db:status; then + false + fi +} -if [ "${MAGENTO_USE_REWRITES:-true}" = "true" ]; then - bin/magento config:set --no-interaction "web/seo/use_rewrites" 1 -fi +magento_maintenance_disable() { + log "Disabling maintenance mode" + ${MAGENTO_COMMAND} maintenance:disable +} -if mr admin:user:list --no-interaction --format=csv | tail -n +2 | awk -F',' '{print $2}' | grep "^${MAGENTO_USERNAME:-admin}$" >/dev/null; then - mr admin:user:change-password --no-interaction ${MAGENTO_USERNAME:-admin} ${MAGENTO_PASSWORD:-ASDqwe123} -else - ARGS=() - ARGS+=( - "--admin-firstname=${MAGENTO_FIRST_NAME:-admin}" - "--admin-lastname=${MAGENTO_LAST_NAME:-admin}" - "--admin-email=${MAGENTO_EMAIL:-admin@example.com}" - "--admin-user=${MAGENTO_USERNAME:-admin}" - "--admin-password=${MAGENTO_PASSWORD:-ASDqwe123}" - ) - mr admin:user:delete --force --no-interaction "${MAGENTO_USERNAME:-admin}" || true - mr admin:user:delete --force --no-interaction "${MAGENTO_EMAIL:-admin@example.com}" || true - mr admin:user:create --no-interaction "${ARGS[@]}" -fi +magento_maintenance_enable() { + log "Enabling maintenance mode" + ${MAGENTO_COMMAND} maintenance:enable +} + +magento_setup_upgrade() { + if [ "${MAGENTO_SKIP_UPGRADE:-false}" != "true" ]; then + if magento_upgrade_required; then + log "Running Magento setup:upgrade" + magento_maintenance_enable + ${MAGENTO_COMMAND} setup:upgrade --keep-generated + magento_maintenance_disable + fi + + log "Magento upgrade is not required" + fi +} + +magento_deploy_mode_set() { + if [ "${MAGENTO_MODE:-default}" != "default" ]; then + log "Setting Magento deploy mode to ${MAGENTO_MODE:-default}" + ${MAGENTO_COMMAND} deploy:mode:set "${MAGENTO_MODE:-default}" + fi +} + +magento_secure_frontend() { + if [ "${MAGENTO_ENABLE_HTTPS:-true}" = "true" ]; then + log "Enabling HTTPS for frontend" + ${MAGENTO_COMMAND} config:set "web/secure/use_in_frontend" 1 + fi +} + +magento_secure_backend() { + if [ "${MAGENTO_ENABLE_ADMIN_HTTPS:-true}" = "true" ]; then + log "Enabling HTTPS for admin" + ${MAGENTO_COMMAND} config:set "web/secure/use_in_adminhtml" 1 + fi +} + +magento_use_rewrites() { + if [ "${MAGENTO_USE_REWRITES:-true}" = "true" ]; then + log "Enabling rewrites" + ${MAGENTO_COMMAND} config:set "web/seo/use_rewrites" 1 + fi +} + +magento_admin_user() { + if ${MAGERUN_COMMAND} admin:user:list --format=csv | tail -n +2 | awk -F',' '{print $2}' | grep "^${MAGENTO_USERNAME:-admin}$" >/dev/null; then + log "Admin user already exists, updating password" + ${MAGERUN_COMMAND} admin:user:change-password ${MAGENTO_USERNAME:-admin} ${MAGENTO_PASSWORD:-ASDqwe123} + else + log "Creating admin user" + ARGS=() + ARGS+=( + "--admin-firstname=${MAGENTO_FIRST_NAME:-admin}" + "--admin-lastname=${MAGENTO_LAST_NAME:-admin}" + "--admin-email=${MAGENTO_EMAIL:-admin@example.com}" + "--admin-user=${MAGENTO_USERNAME:-admin}" + "--admin-password=${MAGENTO_PASSWORD:-ASDqwe123}" + ) + ${MAGERUN_COMMAND} admin:user:delete --force "${MAGENTO_USERNAME:-admin}" || true + ${MAGERUN_COMMAND} admin:user:delete --force "${MAGENTO_EMAIL:-admin@example.com}" || true + ${MAGERUN_COMMAND} admin:user:create "${ARGS[@]}" + fi +} + +magento_deploy_sample_data() { + if [ "${MAGENTO_DEPLOY_SAMPLE_DATA:-false}" = "true" ]; then + log "Deploying sample data" + ${MAGENTO_COMMAND} sampledata:deploy + ${MAGENTO_COMMAND} setup:upgrade --keep-generated + fi +} -if [ "${MAGENTO_DEPLOY_SAMPLE_DATA:-false}" = "true" ]; then - bin/magento sampledata:deploy --no-interaction - bin/magento setup:upgrade --no-interaction --keep-generated -fi +main() { + trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR + + command_before_install + + magento_bootstrap_check + + if magento_is_installed; then + magento_configure + magento_setup_upgrade + else + magento_setup_install + fi -if [ "${MAGENTO_DEPLOY_STATIC_CONTENT:-false}" = "true" ]; then - bin/magento setup:static-content:deploy --no-interaction --jobs="$(nproc)" -fv "${MAGENTO_LANGUAGES:-}" -fi + magento_setup_di_compile + magento_setup_static_content_deploy + magento_deploy_mode_set + magento_secure_frontend + magento_secure_backend + magento_use_rewrites + magento_admin_user + magento_deploy_sample_data + magento_setup_static_content_deploy + magento_cache_enable + magento_reindex -if [ "${MAGENTO_SKIP_REINDEX:-true}" != "true" ]; then - bin/magento indexer:reindex --no-interaction -fi + command_after_install +} -if [ -n "${COMMAND_AFTER_INSTALL-}" ]; then eval "${COMMAND_AFTER_INSTALL-}"; fi +main diff --git a/images/php-fpm/wordpress-web/context/rootfs/usr/local/bin/check-dependencies.sh b/images/php-fpm/wordpress-web/context/rootfs/usr/local/bin/check-dependencies.sh index 1cbd20aa..f903059e 100644 --- a/images/php-fpm/wordpress-web/context/rootfs/usr/local/bin/check-dependencies.sh +++ b/images/php-fpm/wordpress-web/context/rootfs/usr/local/bin/check-dependencies.sh @@ -1,13 +1,44 @@ #!/usr/bin/env bash -set -e - -echo "Checking database connection..." -if mysql -h"${WORDPRESS_DATABASE_HOST:-db}" -P"${WORDPRESS_DATABASE_PORT:-3306}" -u"${WORDPRESS_DATABASE_NAME:-wordpress}" -p"${WORDPRESS_DATABASE_PASSWORD:-wordpress}" -e "CREATE DATABASE IF NOT EXISTS ${WORDPRESS_DATABASE_NAME:-wordpress}; "; then - echo "Database connection ready." -else - echo "Database connection failed." - exit 1 +[ "${DEBUG:-false}" = "true" ] && set -x +set -eE -o pipefail -o errtrace +shopt -s extdebug + +if [ "${DEBUG:-false}" != "true" ]; then + QUIET="--quiet" fi -echo "All connections are ready." -exit 0 +log() { + [ "${SILENT:-false}" != "true" ] && printf "%s INFO: %s\n" "$(date --iso-8601=seconds)" "$*" +} + +error() { + exitcode=$? + # color red + printf "\033[1;31m%s ERROR: %s\033[0m\n" "$(date --iso-8601=seconds)" "$*" + + if [ "${exitcode}" -eq 0 ]; then exit 1; fi + exit "${exitcode}" +} + +trapinfo() { + # shellcheck disable=SC2145 + error "Command failed: $BASH_COMMAND STATUS=$? LINENO=${@:1:$(($# - 1))}" +} + +check_database() { + log "Checking database connection..." + if mysql -h"${WORDPRESS_DATABASE_HOST:-db}" -P"${WORDPRESS_DATABASE_PORT:-3306}" -u"${WORDPRESS_DATABASE_NAME:-wordpress}" -p"${WORDPRESS_DATABASE_PASSWORD:-wordpress}" -e "CREATE DATABASE IF NOT EXISTS ${WORDPRESS_DATABASE_NAME:-wordpress}; "; then + log "Database connection ready." + else + error "Database connection failed." + fi +} + +main() { + trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR + + check_database + log "All connections are ready." +} + +main diff --git a/images/php-fpm/wordpress-web/context/rootfs/usr/local/bin/install.sh b/images/php-fpm/wordpress-web/context/rootfs/usr/local/bin/install.sh index db70f586..7a762554 100644 --- a/images/php-fpm/wordpress-web/context/rootfs/usr/local/bin/install.sh +++ b/images/php-fpm/wordpress-web/context/rootfs/usr/local/bin/install.sh @@ -1,57 +1,131 @@ #!/usr/bin/env bash -set -e - -if [ -n "${COMMAND_BEFORE_INSTALL-}" ]; then eval "${COMMAND_BEFORE_INSTALL-}"; fi - -if [ "${WORDPRESS_SKIP_BOOTSTRAP:-false}" = "true" ]; then - if [ -n "${COMMAND_AFTER_INSTALL-}" ]; then eval "${COMMAND_AFTER_INSTALL-}"; fi - exit -fi - -if [ "${WORDPRESS_CONFIG:-true}" = "true" ]; then - ARGS=() - ARGS+=( - "--force" - "--dbhost=${WORDPRESS_DATABASE_HOST:-db}" - "--dbname=${WORDPRESS_DATABASE_NAME:-wordpress}" - "--dbuser=${WORDPRESS_DATABASE_USER:-wordpress}" - "--dbpass=${WORDPRESS_DATABASE_PASSWORD:-wordpress}" - "--dbprefix=${WORDPRESS_DATABASE_PREFIX:-wp_}" - "--dbcharset=${WORDPRESS_DATABASE_CHARSET:-utf8}" - "--dbcollate=${WORDPRESS_DATABASE_COLLATE:-}" - "--locale=${WORDPRESS_LOCALE:-}" - ) - - wp core config "${ARGS[@]}" --extra-php <