wordpress/rootfs/etc/cont-init.d/20-install-plugins

138 lines
3.7 KiB
Plaintext
Executable File

#!/usr/bin/with-contenv bash
# Register exit handler
trap scriptExitHandler EXIT
function scriptExitHandler() {
LAST_EXIT_CODE=$?
# START_PROCESS is set only if script has passed the lock check
# This should ensure that the lock is lifted in any case
if [ -n "${WP_PLUGINS_PATH}" ] && [ -n "${START_PROCESS}" ]; then
rm "${WP_PLUGINS_PATH}/.installer-lock" -f
fi
if [ "${LAST_EXIT_CODE}" = "0" ]; then
echo "> Script finished successfully"
exit "${LAST_EXIT_CODE}"
fi
echo "> Script finished with an error"
exit "${LAST_EXIT_CODE}"
}
# Install plugin
installPlugin() {
ARCHIVE="$(wp-plugin download "${@}" 2>/dev/null)"
if [ -z "${ARCHIVE}" ]; then
return 1
fi
wp-plugin unpack "${@}"
return $?
}
checkInstalled() {
FAILED_COUNT=0
for PLUGIN_EXPR in ${PLUGIN_LIST}; do
IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}"
PLUGIN_SLUG="${PLUGIN[0]}"
if wp-plugin check "${PLUGIN_SLUG}"; then
echo "> Plugin '${PLUGIN_SLUG}' installed"
continue
fi
((FAILED_COUNT = FAILED_COUNT + 1))
echo "> Warning: Plugin '${PLUGIN_SLUG}' failed to install"
done
if [ "${FAILED_COUNT}" = "0" ]; then
return 0
fi
echo "> Total of ${FAILED_COUNT} plugins failed to install"
return 1
}
# Main function
function main() {
PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}"
PLUGIN_STRICT_INSTALL="${WORDPRESS_PLUGIN_INSTALL_STRICT:-false}"
WP_CONTENT_PATH="/var/www/html/wp-content"
WP_PLUGINS_PATH="${WP_CONTENT_PATH}/plugins"
# Process locking to prevent multiple instances of this script from running at the same time
if [ -f "${WP_PLUGINS_PATH}/.installer-lock" ]; then
echo "> Installer is locked by another process. Skipping installation."
return 0
fi
START_PROCESS="$(date +%s)"
export WP_PLUGINS_PATH START_PROCESS
touch "${WP_PLUGINS_PATH}/.installer-lock"
CONCURRENCY_LIMIT="${CONCURRENCY_LIMIT:-4}"
echo "> Automated WordPress Plugin Installer"
if [ -z "${PLUGIN_LIST}" ]; then
echo "> No plugins defined. Skipping installation."
return 0
fi
echo "> About to install defined plugins"
for PLUGIN_EXPR in ${PLUGIN_LIST}; do
# Split plugin name and version
IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}"
PLUGIN_SLUG="${PLUGIN[0]}"
PLUGIN_VERSION="${PLUGIN[1]:-}"
if wp-plugin check "${PLUGIN_SLUG}"; then
echo "> Plugin '${PLUGIN_SLUG}' already installed and will be skipped."
continue
fi
if [ -n "${PLUGIN_VERSION}" ]; then
echo "> Installing plugin '${PLUGIN_SLUG}' version '${PLUGIN_VERSION}'"
installPlugin "${PLUGIN_SLUG}" "${PLUGIN_VERSION}" &
else
echo "> Installing plugin '${PLUGIN_SLUG}'"
installPlugin "${PLUGIN_SLUG}" &
fi
# Run maximum of X plugin installs in parallel
while [ "$(jobs | wc -l)" -ge "${CONCURRENCY_LIMIT}" ]; do
echo " Waiting for batch of ${CONCURRENCY_LIMIT} plugins to install..."
wait
done
done
echo "> Waiting for all tasks to finish..."
wait
# Plugins are installed concurrently, so we need to verify if installed, separately
echo "> About to verify install of defined plugins"
if ! checkInstalled; then
echo "> Some plugins failed to install"
if [ "${PLUGIN_STRICT_INSTALL}" = "true" ]; then
echo "> WORDPRESS_PLUGIN_INSTALL_STRICT is set to true. Terminating with non-zero exit code"
return 1
fi
return 0
fi
echo "> All plugins installed successfully"
return 0
}
main "${@}"
exit $?