2024-05-17 00:46:42 +02:00
|
|
|
#!/command/with-contenv bash
|
2024-05-17 01:52:44 +02:00
|
|
|
# shellcheck shell=bash
|
2022-01-22 01:05:52 +01:00
|
|
|
|
2023-03-21 01:40:07 +01:00
|
|
|
# Install plugin
|
|
|
|
installPlugin() {
|
2024-05-17 01:52:44 +02:00
|
|
|
local pluginArchive
|
|
|
|
pluginArchive="$(wp-plugin download "${@}" 2>/dev/null)"
|
|
|
|
if [ -z "${pluginArchive}" ]; then
|
2023-03-21 01:40:07 +01:00
|
|
|
return 1
|
2023-03-17 19:43:44 +01:00
|
|
|
fi
|
2023-03-21 01:40:07 +01:00
|
|
|
|
|
|
|
wp-plugin unpack "${@}"
|
|
|
|
return $?
|
2022-01-22 01:05:52 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 01:40:07 +01:00
|
|
|
checkInstalled() {
|
2024-05-17 01:52:44 +02:00
|
|
|
local failedCount=0
|
|
|
|
local pluginSlug
|
|
|
|
local pluginExpr
|
|
|
|
local plugin
|
2023-03-21 01:40:07 +01:00
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
for pluginExpr in ${PLUGIN_LIST}; do
|
|
|
|
IFS=':' read -ra plugin <<<"${pluginExpr}"
|
2023-03-21 01:40:07 +01:00
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
pluginSlug="${plugin[0]}"
|
2023-03-21 01:40:07 +01:00
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
if wp-plugin check "${pluginSlug}"; then
|
|
|
|
echo "Plugin '${pluginSlug}' installed"
|
2023-03-21 01:40:07 +01:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
((failedCount = failedCount + 1))
|
|
|
|
echo "Warning: Plugin '${pluginSlug}' failed to install"
|
2023-03-21 01:40:07 +01:00
|
|
|
done
|
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
if [ "${failedCount}" = "0" ]; then
|
2023-03-21 01:40:07 +01:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
echo "Total of ${failedCount} plugins failed to install"
|
2023-03-21 01:40:07 +01:00
|
|
|
return 1
|
2022-01-22 01:05:52 +01:00
|
|
|
}
|
2021-02-09 21:27:12 +01:00
|
|
|
|
2023-03-22 03:24:27 +01:00
|
|
|
# Plugins installer
|
2024-05-17 07:25:10 +02:00
|
|
|
taskInstallPlugins() {
|
2023-03-22 03:24:27 +01:00
|
|
|
export WP_PLUGINS_PATH
|
2023-03-21 08:06:47 +01:00
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
local concurrencyLimit="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}"
|
|
|
|
local pluginSlug
|
|
|
|
local pluginVersion
|
|
|
|
local pluginExpr
|
|
|
|
local plugin
|
2023-03-21 01:40:07 +01:00
|
|
|
|
2024-05-17 01:37:37 +02:00
|
|
|
echo "Automated WordPress Plugins Installer"
|
2023-03-17 19:43:44 +01:00
|
|
|
if [ -z "${PLUGIN_LIST}" ]; then
|
2024-05-17 01:37:37 +02:00
|
|
|
echo "No plugins defined. Skipping installation."
|
2023-03-17 19:43:44 +01:00
|
|
|
return 0
|
|
|
|
fi
|
2021-02-09 21:27:12 +01:00
|
|
|
|
2024-05-17 07:25:10 +02:00
|
|
|
local jobs=()
|
|
|
|
|
2024-05-17 01:37:37 +02:00
|
|
|
echo "About to install defined plugins"
|
2024-05-17 01:52:44 +02:00
|
|
|
for pluginExpr in ${PLUGIN_LIST}; do
|
2023-03-21 01:40:07 +01:00
|
|
|
|
|
|
|
# Split plugin name and version
|
2024-05-17 01:52:44 +02:00
|
|
|
IFS=':' read -ra plugin <<<"${pluginExpr}"
|
2021-02-09 21:27:12 +01:00
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
pluginSlug="${plugin[0]}"
|
|
|
|
pluginVersion="${plugin[1]:-}"
|
2021-02-15 01:14:42 +01:00
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
if wp-plugin check "${pluginSlug}"; then
|
|
|
|
echo "Plugin '${pluginSlug}' already installed and will be skipped."
|
2023-03-17 19:43:44 +01:00
|
|
|
continue
|
|
|
|
fi
|
2021-02-15 01:14:42 +01:00
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
if [ -n "${pluginVersion}" ]; then
|
|
|
|
echo "Installing plugin '${pluginSlug}' version '${pluginVersion}'"
|
|
|
|
installPlugin "${pluginSlug}" "${pluginVersion}" &
|
2024-05-17 07:25:10 +02:00
|
|
|
jobs+=($!)
|
2023-03-21 01:40:07 +01:00
|
|
|
else
|
2024-05-17 01:52:44 +02:00
|
|
|
echo "Installing plugin '${pluginSlug}'"
|
|
|
|
installPlugin "${pluginSlug}" &
|
2024-05-17 07:25:10 +02:00
|
|
|
jobs+=($!)
|
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
|
2024-05-17 01:52:44 +02:00
|
|
|
while [ "$(jobs | wc -l)" -ge "${concurrencyLimit}" ]; do
|
|
|
|
echo " Waiting for batch of ${concurrencyLimit} plugins to install..."
|
2023-03-17 21:00:06 +01:00
|
|
|
wait
|
|
|
|
done
|
2023-03-17 19:43:44 +01:00
|
|
|
done
|
2022-01-22 01:05:52 +01:00
|
|
|
|
2024-05-17 01:37:37 +02:00
|
|
|
echo "Waiting for all tasks to finish..."
|
2024-05-17 07:25:10 +02:00
|
|
|
|
2024-05-19 00:23:58 +02:00
|
|
|
for pid in "${jobs[@]}"; do
|
|
|
|
wait "$pid"
|
2024-05-17 07:25:10 +02:00
|
|
|
done
|
2022-01-22 01:05:52 +01:00
|
|
|
|
2023-03-17 19:43:44 +01:00
|
|
|
# Plugins are installed concurrently, so we need to verify if installed, separately
|
2024-05-17 01:37:37 +02:00
|
|
|
echo "About to verify install of defined plugins"
|
2022-01-22 01:05:52 +01:00
|
|
|
|
2023-03-21 01:40:07 +01:00
|
|
|
if ! checkInstalled; then
|
2024-05-17 01:37:37 +02:00
|
|
|
echo "Some plugins failed to install"
|
2023-03-21 01:40:07 +01:00
|
|
|
return 0
|
2023-03-17 19:43:44 +01:00
|
|
|
fi
|
2023-03-21 01:40:07 +01:00
|
|
|
|
2024-05-17 01:37:37 +02:00
|
|
|
echo "All plugins installed successfully"
|
2023-03-21 01:40:07 +01:00
|
|
|
return 0
|
2022-07-26 21:38:26 +02:00
|
|
|
}
|
2022-01-22 01:05:52 +01:00
|
|
|
|
2024-05-17 01:37:37 +02:00
|
|
|
# init-install-resources main
|
2023-03-22 03:24:27 +01:00
|
|
|
main() {
|
2024-05-17 01:54:52 +02:00
|
|
|
exec > >(while read -r line; do echo "[init-install-resources] ${line}"; done) 2>&1
|
2024-05-17 01:37:37 +02:00
|
|
|
|
2023-03-22 03:24:27 +01:00
|
|
|
PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}"
|
|
|
|
WP_CONTENT_PATH="/var/www/html/wp-content"
|
|
|
|
WP_PLUGINS_PATH="${WP_CONTENT_PATH}/plugins"
|
|
|
|
|
2024-05-17 01:37:37 +02:00
|
|
|
echo "Automated WordPress Resources Installer"
|
2023-03-22 03:24:27 +01:00
|
|
|
|
2024-05-17 07:25:10 +02:00
|
|
|
local jobs=()
|
|
|
|
|
2023-03-22 03:24:27 +01:00
|
|
|
export WP_CONTENT_PATH
|
|
|
|
|
|
|
|
WP_PLUGINS_INSTALL_CONCURRENCY="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}"
|
|
|
|
export PLUGIN_LIST WP_PLUGINS_PATH WP_PLUGINS_INSTALL_CONCURRENCY
|
|
|
|
taskInstallPlugins "${@}" &
|
2024-05-17 07:25:10 +02:00
|
|
|
jobs+=($!)
|
2023-03-22 03:24:27 +01:00
|
|
|
|
2024-05-17 01:37:37 +02:00
|
|
|
echo "Waiting for all tasks to complete"
|
2024-05-17 07:25:10 +02:00
|
|
|
|
|
|
|
# Wait for all tasks but do not wait for the background task
|
2024-05-19 00:23:58 +02:00
|
|
|
for pid in "${jobs[@]}"; do
|
|
|
|
wait "$pid"
|
2024-05-17 07:25:10 +02:00
|
|
|
done
|
|
|
|
|
2023-03-22 03:24:27 +01:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2024-05-17 01:52:44 +02:00
|
|
|
main "${@}"
|