Skip to content

Commit

Permalink
feat: add support for running hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
janosmiko committed Nov 23, 2024
1 parent 5bd00e2 commit 9ae1baf
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,30 @@ PHP_ARGS="-derror_reporting=${PHP_ERROR_REPORTING:-E_ALL} --memory_limit=${PHP_M
_magento_command="bin/magento"
MAGENTO_COMMAND="${MAGENTO_COMMAND:-php ${PHP_ARGS} ${_magento_command} --no-ansi --no-interaction}"
readonly MAGENTO_COMMAND
unset _magento_command

_magerun_command="n98-magerun2"
if command -v mr 2>/dev/null; then
_magerun_command="$(command -v mr 2>/dev/null)"
fi
MAGERUN_COMMAND="${MAGERUN_COMMAND:-php ${PHP_ARGS} ${_magerun_command} --no-ansi --no-interaction}"
readonly MAGERUN_COMMAND
unset _magerun_command

_composer_command="composer"
if command -v composer 2>/dev/null; then
_composer_command="$(command -v composer 2>/dev/null)"
fi
COMPOSER_COMMAND="${COMPOSER_COMMAND:-php ${PHP_ARGS} ${_composer_command} --no-ansi --no-interaction}"
readonly COMPOSER_COMMAND
unset _composer_command

_n_command="n"
if command -v n 2>/dev/null; then
_n_command="$(command -v n 2>/dev/null)"
fi
N_COMMAND="${N_COMMAND:-${_n_command}}"

check_requirements() {
check_command "mr"
check_command "composer"
check_command "n"
}
unset _n_command

magento() {
${MAGENTO_COMMAND} "$@"
Expand All @@ -62,6 +60,12 @@ n() {
${N_COMMAND} "$@"
}

check_requirements() {
check_command "mr"
check_command "composer"
check_command "n"
}

command_before_build() {
if [[ -z "${COMMAND_BEFORE_BUILD:-}" ]]; then
return 0
Expand Down Expand Up @@ -210,6 +214,8 @@ dump_build_version() {
}

main() {
run_hooks "pre-build"

check_requirements

command_before_build
Expand All @@ -229,6 +235,8 @@ main() {
dump_build_version

command_after_build

run_hooks "post-build"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,54 @@ function test_composer_self_update() {
unset COMPOSER_VERSION
}

function test_composer_configure() {
# Default
local APP_PATH="./test-data"
mock composer echo
spy composer
composer_configure
assert_directory_exists "./test-data/var/composer_home"
assert_have_been_called_times 4 composer

# Test if only MAGENTO_PUBLIC_KEY is set
local MAGENTO_PUBLIC_KEY="public"
spy composer
composer_configure
assert_have_been_called_times 4 composer

# Test if only MAGENTO_PRIVATE_KEY is set
local MAGENTO_PUBLIC_KEY=""
local MAGENTO_PRIVATE_KEY="private"
spy composer
composer_configure
assert_have_been_called_times 4 composer

local MAGENTO_PUBLIC_KEY="public"
local MAGENTO_PRIVATE_KEY="private"
spy composer
composer_configure
assert_have_been_called_times 5 composer

local GITHUB_USER="user"
local GITHUB_TOKEN="token"
spy composer
composer_configure
assert_have_been_called_times 6 composer

local BITBUCKET_PUBLIC_KEY="public"
local BITBUCKET_PRIVATE_KEY="private"
spy composer
composer_configure
assert_have_been_called_times 7 composer

local GITLAB_TOKEN="token"
spy composer
composer_configure
assert_have_been_called_times 8 composer

rm -fr "./test-data"
}

function test_composer_install() {
spy composer
composer_install
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,19 @@ 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')"
assert_true "$(version_gt '2.4' '2.3.99')"
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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,23 @@ PHP_ARGS="-derror_reporting=${PHP_ERROR_REPORTING:-E_ALL} --memory_limit=${PHP_M
_magento_command="bin/magento"
MAGENTO_COMMAND="${MAGENTO_COMMAND:-php ${PHP_ARGS} ${_magento_command} --no-ansi --no-interaction}"
readonly MAGENTO_COMMAND
unset _magento_command

_magerun_command="n98-magerun2"
if command -v mr 2>/dev/null; then
_magerun_command="$(command -v mr 2>/dev/null)"
fi
MAGERUN_COMMAND="${MAGERUN_COMMAND:-php ${PHP_ARGS} ${_magerun_command} --no-ansi --no-interaction}"
readonly MAGERUN_COMMAND
unset _magerun_command

_composer_command="composer"
if command -v composer 2>/dev/null; then
_composer_command="$(command -v composer 2>/dev/null)"
fi
COMPOSER_COMMAND="${COMPOSER_COMMAND:-php ${PHP_ARGS} ${_composer_command} --no-ansi --no-interaction}"
readonly COMPOSER_COMMAND

check_requirements() {
check_command "composer"
check_command "mr"
}
unset _composer_command

magento() {
${MAGENTO_COMMAND} "$@"
Expand All @@ -55,6 +53,11 @@ composer() {
${COMPOSER_COMMAND} "$@"
}

check_requirements() {
check_command "composer"
check_command "mr"
}

command_before_install() {
if [[ -z "${COMMAND_BEFORE_INSTALL:-}" ]]; then
return 0
Expand Down Expand Up @@ -594,8 +597,6 @@ magento_publish_config() {
}

main() {
check_requirements

LOCKFILE="$(shared_config_path)/.deploy.lock"
readonly LOCKFILE

Expand All @@ -604,6 +605,10 @@ main() {

lock_acquire "${LOCKFILE}"

run_hooks "pre-install"

check_requirements

conditional_sleep
command_before_install
bootstrap_check
Expand All @@ -630,6 +635,8 @@ main() {
magento_publish_config

command_after_install

run_hooks "post-install"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,27 +708,29 @@ function test_bootstrap_check() {
function test_composer_configure() {
# Default
local APP_PATH="./test-data"
mock composer echo
spy composer
composer_configure
assert_directory_exists "./test-data/var/composer_home"

# Test if only MAGENTO_PUBLIC_KEY is set
local MAGENTO_PUBLIC_KEY="public"
spy composer
composer_configure
assert_have_been_called_times 0 composer

# Test if only MAGENTO_PRIVATE_KEY is set
local MAGENTO_PUBLIC_KEY=""
local MAGENTO_PRIVATE_KEY="private"
spy composer
composer_configure
assert_have_been_called_times 0 composer

local MAGENTO_PUBLIC_KEY="public"
local MAGENTO_PRIVATE_KEY="private"
spy composer
composer_configure
assert_have_been_called composer
# unset MAGENTO_PUBLIC_KEY
# unset MAGENTO_PRIVATE_KEY
assert_have_been_called_times 1 composer

local GITHUB_USER="user"
local GITHUB_TOKEN="token"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ main() {
trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR

create_symlink

run_hooks "post-start"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ main() {
trap 'trapinfo $LINENO ${BASH_LINENO[*]}' ERR

lock_deploy

run_hooks "pre-stop"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
20 changes: 14 additions & 6 deletions images/php-fpm/magento2-web/context/rootfs/usr/local/bin/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,30 @@ PHP_ARGS="-derror_reporting=${PHP_ERROR_REPORTING:-E_ALL} --memory_limit=${PHP_M
_magento_command="bin/magento"
MAGENTO_COMMAND="${MAGENTO_COMMAND:-php ${PHP_ARGS} ${_magento_command} --no-ansi --no-interaction}"
readonly MAGENTO_COMMAND
unset _magento_command

_magerun_command="n98-magerun2"
if command -v mr 2>/dev/null; then
_magerun_command="$(command -v mr 2>/dev/null)"
fi
MAGERUN_COMMAND="${MAGERUN_COMMAND:-php ${PHP_ARGS} ${_magerun_command} --no-ansi --no-interaction}"
readonly MAGERUN_COMMAND
unset _magerun_command

_composer_command="composer"
if command -v composer 2>/dev/null; then
_composer_command="$(command -v composer 2>/dev/null)"
fi
COMPOSER_COMMAND="${COMPOSER_COMMAND:-php ${PHP_ARGS} ${_composer_command} --no-ansi --no-interaction}"
readonly COMPOSER_COMMAND
unset _composer_command

_n_command="n"
if command -v n 2>/dev/null; then
_n_command="$(command -v n 2>/dev/null)"
fi
N_COMMAND="${N_COMMAND:-${_n_command}}"

check_requirements() {
check_command "mr"
check_command "composer"
check_command "n"
}
unset _n_command

magento() {
${MAGENTO_COMMAND} "$@"
Expand All @@ -62,6 +60,12 @@ n() {
${N_COMMAND} "$@"
}

check_requirements() {
check_command "mr"
check_command "composer"
check_command "n"
}

command_before_build() {
if [[ -z "${COMMAND_BEFORE_BUILD:-}" ]]; then
return 0
Expand Down Expand Up @@ -210,6 +214,8 @@ dump_build_version() {
}

main() {
run_hooks "pre-build"

check_requirements

command_before_build
Expand All @@ -229,6 +235,8 @@ main() {
dump_build_version

command_after_build

run_hooks "post-build"
}

(return 0 2>/dev/null) && sourced=1
Expand Down
Loading

0 comments on commit 9ae1baf

Please sign in to comment.