diff --git a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/build.sh b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/build.sh index 48e96fd..6bf1a82 100755 --- a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/build.sh +++ b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/build.sh @@ -210,6 +210,8 @@ dump_build_version() { } main() { + run_hooks "pre-build" + check_requirements command_before_build @@ -229,6 +231,8 @@ main() { dump_build_version command_after_build + + run_hooks "post-build" } (return 0 2>/dev/null) && sourced=1 diff --git a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/functions.sh b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/functions.sh index ebf5c04..569d357 100755 --- a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/functions.sh +++ b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/functions.sh @@ -134,3 +134,14 @@ check_command() { error "Error: $1 is required but not installed." fi } + +run_hooks() { + local hook="${1:-}" + if [[ -n "${hook}" ]] && [[ -d "$(app_path)/hooks/${hook}.d" ]]; then + for file in "$(app_path)"/hooks/"${hook}.d"/*.sh; do + log "Running ${file} for ${hook}" + # shellcheck disable=SC1090 + source "${file}" + done + fi +} diff --git a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/functions_test.sh b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/functions_test.sh index c3a09dc..0b1a72b 100644 --- a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/functions_test.sh +++ b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/functions_test.sh @@ -74,7 +74,7 @@ function test_app_path() { assert_equals "/app" "$(app_path)" } -function version_gt() { +function test_version_gt() { assert_true "$(version_gt '2.4.4' '2.3.99')" assert_false "$(version_gt '2.3.99' '2.4.4')" assert_false "$(version_gt '2.4.4' '2.4.4')" @@ -82,3 +82,11 @@ function version_gt() { assert_false "$(version_gt '2.3.99' '2.4')" assert_false "$(version_gt '2.4' '2.4')" } + +function test_run_hooks() { + local APP_PATH="./test-data/app" + mkdir -p "${APP_PATH}/hooks/test.d" + printf "#!/bin/bash\necho 'test-123'" >"${APP_PATH}/hooks/test.d/01-test.sh" + assert_contains "test-123" "$(run_hooks 'test')" + rm -fr "./test-data" +} 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 862de77..bfcbe02 100755 --- 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 @@ -594,8 +594,6 @@ magento_publish_config() { } main() { - check_requirements - LOCKFILE="$(shared_config_path)/.deploy.lock" readonly LOCKFILE @@ -604,6 +602,10 @@ main() { lock_acquire "${LOCKFILE}" + run_hooks "pre-install" + + check_requirements + conditional_sleep command_before_install bootstrap_check @@ -630,6 +632,8 @@ main() { magento_publish_config command_after_install + + run_hooks "post-install" } (return 0 2>/dev/null) && sourced=1 diff --git a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/post-start.sh b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/post-start.sh index 9a3f027..c44ce80 100755 --- a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/post-start.sh +++ b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/post-start.sh @@ -21,6 +21,8 @@ fi create_symlink() { mkdir -p "$(app_path)/app/etc" ln -sf "$(shared_config_path)/app/etc/env.php" "$(app_path)/app/etc/env.php" + + run_hooks "post-start" } main() { diff --git a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/pre-stop.sh b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/pre-stop.sh index 3a51d9f..3f4f810 100755 --- a/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/pre-stop.sh +++ b/images/php-fpm-rootless/magento2-web/context/rootfs/home/www-data/.local/bin/pre-stop.sh @@ -26,6 +26,8 @@ main() { trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR lock_deploy + + run_hooks "pre-stop" } (return 0 2>/dev/null) && sourced=1 diff --git a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/build.sh b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/build.sh index 48e96fd..6bf1a82 100755 --- a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/build.sh +++ b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/build.sh @@ -210,6 +210,8 @@ dump_build_version() { } main() { + run_hooks "pre-build" + check_requirements command_before_build @@ -229,6 +231,8 @@ main() { dump_build_version command_after_build + + run_hooks "post-build" } (return 0 2>/dev/null) && sourced=1 diff --git a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/functions.sh b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/functions.sh index ebf5c04..569d357 100755 --- a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/functions.sh +++ b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/functions.sh @@ -134,3 +134,14 @@ check_command() { error "Error: $1 is required but not installed." fi } + +run_hooks() { + local hook="${1:-}" + if [[ -n "${hook}" ]] && [[ -d "$(app_path)/hooks/${hook}.d" ]]; then + for file in "$(app_path)"/hooks/"${hook}.d"/*.sh; do + log "Running ${file} for ${hook}" + # shellcheck disable=SC1090 + source "${file}" + done + fi +} diff --git a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/functions_test.sh b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/functions_test.sh index c3a09dc..0b1a72b 100644 --- a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/functions_test.sh +++ b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/functions_test.sh @@ -74,7 +74,7 @@ function test_app_path() { assert_equals "/app" "$(app_path)" } -function version_gt() { +function test_version_gt() { assert_true "$(version_gt '2.4.4' '2.3.99')" assert_false "$(version_gt '2.3.99' '2.4.4')" assert_false "$(version_gt '2.4.4' '2.4.4')" @@ -82,3 +82,11 @@ function version_gt() { assert_false "$(version_gt '2.3.99' '2.4')" assert_false "$(version_gt '2.4' '2.4')" } + +function test_run_hooks() { + local APP_PATH="./test-data/app" + mkdir -p "${APP_PATH}/hooks/test.d" + printf "#!/bin/bash\necho 'test-123'" >"${APP_PATH}/hooks/test.d/01-test.sh" + assert_contains "test-123" "$(run_hooks 'test')" + rm -fr "./test-data" +} 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 862de77..bfcbe02 100755 --- 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 @@ -594,8 +594,6 @@ magento_publish_config() { } main() { - check_requirements - LOCKFILE="$(shared_config_path)/.deploy.lock" readonly LOCKFILE @@ -604,6 +602,10 @@ main() { lock_acquire "${LOCKFILE}" + run_hooks "pre-install" + + check_requirements + conditional_sleep command_before_install bootstrap_check @@ -630,6 +632,8 @@ main() { magento_publish_config command_after_install + + run_hooks "post-install" } (return 0 2>/dev/null) && sourced=1 diff --git a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/post-start.sh b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/post-start.sh index 9a3f027..c44ce80 100755 --- a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/post-start.sh +++ b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/post-start.sh @@ -21,6 +21,8 @@ fi create_symlink() { mkdir -p "$(app_path)/app/etc" ln -sf "$(shared_config_path)/app/etc/env.php" "$(app_path)/app/etc/env.php" + + run_hooks "post-start" } main() { diff --git a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/pre-stop.sh b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/pre-stop.sh index 3a51d9f..3f4f810 100755 --- a/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/pre-stop.sh +++ b/images/php-fpm/magento2-web/context/rootfs/usr/local/bin/pre-stop.sh @@ -26,6 +26,8 @@ main() { trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR lock_deploy + + run_hooks "pre-stop" } (return 0 2>/dev/null) && sourced=1