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

118 lines
2.9 KiB
Plaintext
Raw Normal View History

2021-02-09 21:27:12 +01:00
#!/usr/bin/with-contenv bash
# 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
# Plugins installer
function taskInstallPlugins() {
export WP_PLUGINS_PATH
CONCURRENCY_LIMIT="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}"
echo "> Automated WordPress Plugins 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"
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
}
# Main function
main() {
PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}"
WP_CONTENT_PATH="/var/www/html/wp-content"
WP_PLUGINS_PATH="${WP_CONTENT_PATH}/plugins"
echo "> Automated WordPress Resources Installer"
export WP_CONTENT_PATH
WP_PLUGINS_INSTALL_CONCURRENCY="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}"
export PLUGIN_LIST WP_PLUGINS_PATH WP_PLUGINS_INSTALL_CONCURRENCY
taskInstallPlugins "${@}" &
sleep 1
echo "> Waiting for all tasks to complete"
wait
return 0
}
2022-07-26 21:38:26 +02:00
main "${@}"
exit $?