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

96 lines
2.4 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() {
LAST_EXIT_CODE=$?
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}"
}
# Check if plugin installed. This is very basic check that doesn't involve database
function isPluginInstalled() {
if [ -d "${PLUGIN_PATH}" ] || [ -f "${PLUGIN_PATH}.php" ]; then
return 0
fi
return 1
}
# Install plugin
function installPlugin() {
wp plugin install "${@}" >/dev/null 2>&1
sleep 0.5
}
2021-02-09 21:27:12 +01:00
2022-07-26 21:38:26 +02:00
# Main function
function main() {
PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}"
PLUGIN_STRICT_INSTALL="${WORDPRESS_PLUGIN_INSTALL_STRICT:-false}"
2023-01-23 19:50:30 +01:00
WP_CONTENT_PATH="/var/www/html/wp-content"
2021-02-09 21:27:12 +01:00
2022-07-26 21:38:26 +02:00
echo "> Automated WordPress Plugin Installer"
if [ -z "${PLUGIN_LIST}" ]; then
echo "> No plugins defined. Skipping installation."
return 0
fi
2021-02-09 21:27:12 +01:00
2022-07-26 21:38:26 +02:00
echo "> About to install defined plugins"
for PLUGIN_EXPR in ${PLUGIN_LIST}; do
IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}"
2021-02-09 21:27:12 +01:00
2022-07-26 21:38:26 +02:00
PLUGIN_PATH="${WP_CONTENT_PATH}/plugins/${PLUGIN[0]}"
2022-07-26 21:38:26 +02:00
if isPluginInstalled; then
echo "> Plugin '${PLUGIN[0]}' already installed and will be skipped."
continue
fi
2022-07-26 21:38:26 +02:00
WP_PLUGIN_INSTALL_ARGS="${PLUGIN[0]}"
2021-02-15 01:17:40 +01:00
2022-07-26 21:38:26 +02:00
if [ -n "${PLUGIN[1]}" ]; then
WP_PLUGIN_INSTALL_ARGS="${WP_PLUGIN_INSTALL_ARGS} --version=${PLUGIN[1]}"
fi
2021-02-15 01:17:40 +01:00
2022-07-26 21:38:26 +02:00
echo "> Installing plugin '${PLUGIN[0]}' version '${PLUGIN[1]}'"
installPlugin "${WP_PLUGIN_INSTALL_ARGS}" &
done
2022-07-26 21:38:26 +02:00
echo "> Waiting for all plugins to install..."
wait
2022-07-26 21:38:26 +02:00
# Plugins are installed concurrently, so we need to verify if installed, separately
echo "> About to verify install of defined plugins"
2022-07-26 21:38:26 +02:00
FAILED_COUNT=0
2022-07-26 21:38:26 +02:00
for PLUGIN_EXPR in ${PLUGIN_LIST}; do
IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}"
2022-07-26 21:38:26 +02:00
PLUGIN_PATH="${WP_CONTENT_PATH}/plugins/${PLUGIN[0]}"
2022-07-26 21:38:26 +02:00
if isPluginInstalled; then
echo "> Plugin '${PLUGIN[0]}' installed"
continue
fi
2022-07-26 21:38:26 +02:00
((FAILED_COUNT = FAILED_COUNT + 1))
echo "> Warning: Plugin '${PLUGIN[0]}' failed to install"
done
2022-07-26 21:38:26 +02:00
echo "> Total of ${FAILED_COUNT} plugins failed to install"
2022-07-26 21:38:26 +02:00
if [ "${PLUGIN_STRICT_INSTALL}" = "true" ] && [ ${FAILED_COUNT} != "0" ]; then
echo "> WORDPRESS_PLUGIN_INSTALL_STRICT is set to true. Terminating with non-zero exit code"
return 1
fi
}
2022-07-26 21:38:26 +02:00
main "${@}"
exit $?