Files
wordpress/rootfs/etc/cont-init.d/20-install-resources

138 lines
3.7 KiB
Plaintext
Raw Normal View History

2021-02-09 21:27:12 +01:00
#!/usr/bin/with-contenv bash
2022-07-26 21:38:26 +02:00
# Register exit handler
trap scriptExitHandler EXIT
function scriptExitHandler() {
2023-03-17 19:43:44 +01:00
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
2023-03-17 19:43:44 +01:00
if [ "${LAST_EXIT_CODE}" = "0" ]; then
echo "> Script finished successfully"
exit "${LAST_EXIT_CODE}"
fi
2022-07-26 21:38:26 +02:00
2023-03-17 19:43:44 +01:00
echo "> Script finished with an error"
exit "${LAST_EXIT_CODE}"
2022-07-26 21:38:26 +02:00
}
# Install plugin
installPlugin() {
ARCHIVE="$(wp-plugin download "${@}" 2>/dev/null)"
if [ -z "${ARCHIVE}" ]; then
return 1
2023-03-17 19:43:44 +01:00
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
}
2021-02-09 21:27:12 +01:00
2022-07-26 21:38:26 +02:00
# Main function
function main() {
2023-03-17 19:43:44 +01:00
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"
2023-03-17 19:43:44 +01:00
# 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="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}"
echo "> Automated WordPress Resources Installer"
2023-03-17 19:43:44 +01:00
if [ -z "${PLUGIN_LIST}" ]; then
echo "> No plugins defined. Skipping installation."
return 0
fi
2021-02-09 21:27:12 +01:00
2023-03-17 19:43:44 +01:00
echo "> About to install defined plugins"
for PLUGIN_EXPR in ${PLUGIN_LIST}; do
# Split plugin name and version
2023-03-17 19:43:44 +01:00
IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}"
2021-02-09 21:27:12 +01:00
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."
2023-03-17 19:43:44 +01:00
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}" &
2023-03-17 19:43:44 +01:00
fi
2021-02-15 01:17:40 +01:00
2023-03-17 21:14:26 +01:00
# 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
2023-03-17 19:43:44 +01:00
done
echo "> Waiting for all tasks to finish..."
2023-03-17 19:43:44 +01:00
wait
2023-03-17 19:43:44 +01:00
# 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
2023-03-17 19:43:44 +01:00
fi
return 0
2023-03-17 19:43:44 +01:00
fi
echo "> All plugins installed successfully"
return 0
2022-07-26 21:38:26 +02:00
}
2022-07-26 21:38:26 +02:00
main "${@}"
exit $?