Add healthcheck script and mechanism to make unhealthy if WP install integrity is compromised

This commit is contained in:
Aleksandar Puharic 2023-01-24 01:42:53 +01:00
parent fedfaeafc4
commit 64722d5b6c
Signed by: xZero707
GPG Key ID: 3CC53DCAA9C237BB
2 changed files with 29 additions and 0 deletions

View File

@ -16,6 +16,10 @@ function scriptExitHandler() {
exit "${LAST_EXIT_CODE}"
}
function reportUnhealthy() {
echo "${1:?REASON is required}" > "/tmp/.wp-unhealthy"
}
# Main function
function main() {
# Removes trailing zero if found
@ -28,8 +32,11 @@ function main() {
set -e
rm -f "/tmp/.wp-unhealthy"
if [ -z "${WP_INSTALLED_VERSION}" ]; then
echo "> ERROR! WordPress installation does not seem to be present or valid. Continuing anyway..."
reportUnhealthy "WP_NOT_PRESENT"
return 0
elif [ "${WP_INSTALLED_VERSION}" != "${WP_VERSION}" ]; then
echo "> WARNING! WordPress version mismatch"
@ -39,6 +46,7 @@ function main() {
echo " - This is dangerous as changes will not persist when container is recreated which might lead to inconsistencies between installation and the database."
echo " - You should assume that recreating the container will render the website inoperable."
echo " - Please make sure that you're running image: nlss/wordpress:${WP_VERSION}"
reportUnhealthy "WP_VERSION_MISMATCH"
return 0
else
echo "> Identified 'WordPress ${WP_VERSION}'"

View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
function main() {
FULL_CHECK="${1:-false}"
if [ -f "/tmp/.wp-unhealthy" ]; then
echo "Error: WordPress health is compromised: $(cat "/tmp/.wp-unhealthy")"
return 1
fi
# If FULL_CHECK is not requested, site functionality check with curl is skipped
if [ "${FULL_CHECK}" = "false" ]; then
return 0
fi
curl -sSf --output "/dev/null" "http://localhost/"
return $?
}
main "${@}"
exit $?