Refactor to new S6 format

This commit is contained in:
Aleksandar Puharic 2024-05-17 01:37:37 +02:00
parent 63832d68b4
commit 4cb7286fc4
Signed by: xZero707
GPG Key ID: 3CC53DCAA9C237BB
9 changed files with 39 additions and 32 deletions

View File

@ -20,19 +20,19 @@ checkInstalled() {
PLUGIN_SLUG="${PLUGIN[0]}" PLUGIN_SLUG="${PLUGIN[0]}"
if wp-plugin check "${PLUGIN_SLUG}"; then if wp-plugin check "${PLUGIN_SLUG}"; then
echo "> Plugin '${PLUGIN_SLUG}' installed" echo "Plugin '${PLUGIN_SLUG}' installed"
continue continue
fi fi
((FAILED_COUNT = FAILED_COUNT + 1)) ((FAILED_COUNT = FAILED_COUNT + 1))
echo "> Warning: Plugin '${PLUGIN_SLUG}' failed to install" echo "Warning: Plugin '${PLUGIN_SLUG}' failed to install"
done done
if [ "${FAILED_COUNT}" = "0" ]; then if [ "${FAILED_COUNT}" = "0" ]; then
return 0 return 0
fi fi
echo "> Total of ${FAILED_COUNT} plugins failed to install" echo "Total of ${FAILED_COUNT} plugins failed to install"
return 1 return 1
} }
@ -42,13 +42,13 @@ function taskInstallPlugins() {
CONCURRENCY_LIMIT="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}" CONCURRENCY_LIMIT="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}"
echo "> Automated WordPress Plugins Installer" echo "Automated WordPress Plugins Installer"
if [ -z "${PLUGIN_LIST}" ]; then if [ -z "${PLUGIN_LIST}" ]; then
echo "> No plugins defined. Skipping installation." echo "No plugins defined. Skipping installation."
return 0 return 0
fi fi
echo "> About to install defined plugins" echo "About to install defined plugins"
for PLUGIN_EXPR in ${PLUGIN_LIST}; do for PLUGIN_EXPR in ${PLUGIN_LIST}; do
# Split plugin name and version # Split plugin name and version
@ -58,15 +58,15 @@ function taskInstallPlugins() {
PLUGIN_VERSION="${PLUGIN[1]:-}" PLUGIN_VERSION="${PLUGIN[1]:-}"
if wp-plugin check "${PLUGIN_SLUG}"; then if wp-plugin check "${PLUGIN_SLUG}"; then
echo "> Plugin '${PLUGIN_SLUG}' already installed and will be skipped." echo "Plugin '${PLUGIN_SLUG}' already installed and will be skipped."
continue continue
fi fi
if [ -n "${PLUGIN_VERSION}" ]; then if [ -n "${PLUGIN_VERSION}" ]; then
echo "> Installing plugin '${PLUGIN_SLUG}' version '${PLUGIN_VERSION}'" echo "Installing plugin '${PLUGIN_SLUG}' version '${PLUGIN_VERSION}'"
installPlugin "${PLUGIN_SLUG}" "${PLUGIN_VERSION}" & installPlugin "${PLUGIN_SLUG}" "${PLUGIN_VERSION}" &
else else
echo "> Installing plugin '${PLUGIN_SLUG}'" echo "Installing plugin '${PLUGIN_SLUG}'"
installPlugin "${PLUGIN_SLUG}" & installPlugin "${PLUGIN_SLUG}" &
fi fi
@ -77,28 +77,30 @@ function taskInstallPlugins() {
done done
done done
echo "> Waiting for all tasks to finish..." echo "Waiting for all tasks to finish..."
wait wait
# Plugins are installed concurrently, so we need to verify if installed, separately # Plugins are installed concurrently, so we need to verify if installed, separately
echo "> About to verify install of defined plugins" echo "About to verify install of defined plugins"
if ! checkInstalled; then if ! checkInstalled; then
echo "> Some plugins failed to install" echo "Some plugins failed to install"
return 0 return 0
fi fi
echo "> All plugins installed successfully" echo "All plugins installed successfully"
return 0 return 0
} }
# Main function # init-install-resources main
main() { main() {
exec > >(while read line; do echo "[init-install-resources] ${line}"; done) 2>&1
PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}" PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}"
WP_CONTENT_PATH="/var/www/html/wp-content" WP_CONTENT_PATH="/var/www/html/wp-content"
WP_PLUGINS_PATH="${WP_CONTENT_PATH}/plugins" WP_PLUGINS_PATH="${WP_CONTENT_PATH}/plugins"
echo "> Automated WordPress Resources Installer" echo "Automated WordPress Resources Installer"
export WP_CONTENT_PATH export WP_CONTENT_PATH
@ -108,10 +110,9 @@ main() {
taskInstallPlugins "${@}" & taskInstallPlugins "${@}" &
sleep 1 sleep 1
echo "> Waiting for all tasks to complete" echo "Waiting for all tasks to complete"
wait wait
return 0 return 0
} }
main "${@}" main "${@}"
exit $?

View File

@ -0,0 +1 @@
oneshot

View File

@ -0,0 +1 @@
/etc/s6-overlay/s6-rc.d/init-install-resources/run

View File

@ -8,11 +8,11 @@ trap scriptExitHandler EXIT
function scriptExitHandler() { function scriptExitHandler() {
LAST_EXIT_CODE=$? LAST_EXIT_CODE=$?
if [ "${LAST_EXIT_CODE}" = "0" ]; then if [ "${LAST_EXIT_CODE}" = "0" ]; then
echo "> Script finished successfully" echo "Script finished successfully"
exit "${LAST_EXIT_CODE}" exit "${LAST_EXIT_CODE}"
fi fi
echo "> Script finished with an error" echo "Script finished with an error"
exit "${LAST_EXIT_CODE}" exit "${LAST_EXIT_CODE}"
} }
@ -20,38 +20,40 @@ function reportUnhealthy() {
echo "${1:?REASON is required}" >"/tmp/.wp-unhealthy" echo "${1:?REASON is required}" >"/tmp/.wp-unhealthy"
} }
# Main function # init-verify-wordpress main
function main() { function main() {
exec > >(while read line; do echo "[init-verify-wordpress] ${line}"; done) 2>&1
# Removes trailing zero if found # Removes trailing zero if found
# This is required due to inconsistencies between WodPress docker image versioning and wp-cli core download # This is required due to inconsistencies between WodPress docker image versioning and wp-cli core download
# If patch version is 0, it is not considered by wp-cli. # If patch version is 0, it is not considered by wp-cli.
WP_VERSION=$(echo "${WP_VERSION:?}" | sed --expression='s/.0$//g') WP_VERSION=$(echo "${WP_VERSION:?}" | sed --expression='s/.0$//g')
echo "> Verifying 'WordPress ${WP_VERSION}' installation..." echo "Verifying 'WordPress ${WP_VERSION}' installation..."
WP_INSTALLED_VERSION="$(wp core version)" WP_INSTALLED_VERSION="$(wp core version)"
set -e set -e
rm -f "/tmp/.wp-unhealthy" rm -f "/tmp/.wp-unhealthy"
if [ -z "${WP_INSTALLED_VERSION}" ]; then if [ -z "${WP_INSTALLED_VERSION}" ]; then
echo "> ERROR! WordPress installation does not seem to be present or valid. Continuing anyway..." echo "Error: WordPress installation does not seem to be present or valid. Continuing anyway..."
reportUnhealthy "WP_NOT_PRESENT" reportUnhealthy "WP_NOT_PRESENT"
return 0 return 0
elif [ "${WP_INSTALLED_VERSION}" != "${WP_VERSION}" ]; then elif [ "${WP_INSTALLED_VERSION}" != "${WP_VERSION}" ]; then
echo "> WARNING! WordPress version mismatch" echo "WARNING! WordPress version mismatch"
echo " Expected version: ${WP_VERSION}" echo " Expected version: ${WP_VERSION}"
echo " Detected version: ${WP_INSTALLED_VERSION}" echo " Detected version: ${WP_INSTALLED_VERSION}"
echo "> Seems like WordPress installation got updated outside image scope" echo "Seems like WordPress installation got updated outside image scope"
echo " - This is dangerous as changes will not persist when container is recreated which might lead to inconsistencies between installation and the database." 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 " - 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}" echo " - Please make sure that you're running image: nlss/wordpress:${WP_VERSION}"
reportUnhealthy "WP_VERSION_MISMATCH" reportUnhealthy "WP_VERSION_MISMATCH"
return 0 return 0
else else
echo "> Identified 'WordPress ${WP_VERSION}'" echo "Identified 'WordPress ${WP_VERSION}'"
fi fi
} }
main "${@}" main "${@}"
exit $?

View File

@ -0,0 +1 @@
oneshot

View File

@ -0,0 +1 @@
/etc/s6-overlay/s6-rc.d/init-verify-wordpress/run