wordpress/rootfs/etc/s6-overlay/s6-rc.d/init-install-resources/run

118 lines
3.0 KiB
Plaintext
Executable File

#!/command/with-contenv bash
# 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
}
# Plugins installer
function taskInstallPlugins() {
export WP_PLUGINS_PATH
CONCURRENCY_LIMIT="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}"
echo "Automated WordPress Plugins 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"
return 0
fi
echo "All plugins installed successfully"
return 0
}
# init-install-resources main
main() {
exec > >(while read line; do echo "[init-install-resources] ${line}"; done) 2>&1
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
}
main "${@}"