#!/command/with-contenv bash # shellcheck shell=bash # Install plugin installPlugin() { local pluginArchive pluginArchive="$(wp-plugin download "${@}" 2>/dev/null)" if [ -z "${pluginArchive}" ]; then return 1 fi wp-plugin unpack "${@}" return $? } checkInstalled() { local failedCount=0 local pluginSlug local pluginExpr local plugin for pluginExpr in ${PLUGIN_LIST}; do IFS=':' read -ra plugin <<<"${pluginExpr}" pluginSlug="${plugin[0]}" if wp-plugin check "${pluginSlug}"; then echo "Plugin '${pluginSlug}' installed" continue fi ((failedCount = failedCount + 1)) echo "Warning: Plugin '${pluginSlug}' failed to install" done if [ "${failedCount}" = "0" ]; then return 0 fi echo "Total of ${failedCount} plugins failed to install" return 1 } # Plugins installer taskInstallPlugins() { export WP_PLUGINS_PATH local concurrencyLimit="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}" local pluginSlug local pluginVersion local pluginExpr local plugin echo "Automated WordPress Plugins Installer" if [ -z "${PLUGIN_LIST}" ]; then echo "No plugins defined. Skipping installation." return 0 fi local jobs=() echo "About to install defined plugins" for pluginExpr in ${PLUGIN_LIST}; do # Split plugin name and version IFS=':' read -ra plugin <<<"${pluginExpr}" pluginSlug="${plugin[0]}" pluginVersion="${plugin[1]:-}" if wp-plugin check "${pluginSlug}"; then echo "Plugin '${pluginSlug}' already installed and will be skipped." continue fi if [ -n "${pluginVersion}" ]; then echo "Installing plugin '${pluginSlug}' version '${pluginVersion}'" installPlugin "${pluginSlug}" "${pluginVersion}" & jobs+=($!) else echo "Installing plugin '${pluginSlug}'" installPlugin "${pluginSlug}" & jobs+=($!) fi # Run maximum of X plugin installs in parallel while [ "$(jobs | wc -l)" -ge "${concurrencyLimit}" ]; do echo " Waiting for batch of ${concurrencyLimit} plugins to install..." wait done done echo "Waiting for all tasks to finish..." for pid in "${jobs[@]}"; do wait "$pid" done # 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 -r 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" local jobs=() export WP_CONTENT_PATH WP_PLUGINS_INSTALL_CONCURRENCY="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}" export PLUGIN_LIST WP_PLUGINS_PATH WP_PLUGINS_INSTALL_CONCURRENCY taskInstallPlugins "${@}" & jobs+=($!) echo "Waiting for all tasks to complete" # Wait for all tasks but do not wait for the background task for pid in "${jobs[@]}"; do wait "$pid" done return 0 } main "${@}"