Refactor plugin installer

- Fixes issue where installation is dependent of existence of working database connection

- Overall improvements

- Move out some logic to wp-plugin script

- Use curl to download plugins, instead of wp cli
This commit is contained in:
Aleksandar Puharic 2023-03-21 01:40:07 +01:00
parent c26a0afd1f
commit 8df891a400
Signed by: xZero707
GPG Key ID: 3CC53DCAA9C237BB
2 changed files with 158 additions and 38 deletions

View File

@ -14,18 +14,40 @@ function scriptExitHandler() {
exit "${LAST_EXIT_CODE}" exit "${LAST_EXIT_CODE}"
} }
# Check if plugin installed. This is very basic check that doesn't involve database # Install plugin
function isPluginInstalled() { installPlugin() {
if [ -d "${PLUGIN_PATH}" ] || [ -f "${PLUGIN_PATH}.php" ]; then ARCHIVE="$(wp-plugin download "${@}" 2>/dev/null)"
return 0 if [ -z "${ARCHIVE}" ]; then
fi
return 1 return 1
fi
wp-plugin unpack "${@}"
return $?
} }
# Install plugin checkInstalled() {
function installPlugin() { FAILED_COUNT=0
wp plugin install "${@}" >/dev/null 2>&1
sleep 0.5 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
} }
# Main function # Main function
@ -33,8 +55,11 @@ function main() {
PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}" PLUGIN_LIST="${WORDPRESS_PLUGIN_LIST:-}"
PLUGIN_STRICT_INSTALL="${WORDPRESS_PLUGIN_INSTALL_STRICT:-false}" PLUGIN_STRICT_INSTALL="${WORDPRESS_PLUGIN_INSTALL_STRICT:-false}"
WP_CONTENT_PATH="/var/www/html/wp-content" WP_CONTENT_PATH="/var/www/html/wp-content"
WP_PLUGINS_PATH="${WP_CONTENT_PATH}/plugins"
CONCURRENCY_LIMIT="${CONCURRENCY_LIMIT:-4}" CONCURRENCY_LIMIT="${CONCURRENCY_LIMIT:-4}"
export WP_PLUGINS_PATH
echo "> Automated WordPress Plugin Installer" echo "> Automated WordPress Plugin Installer"
if [ -z "${PLUGIN_LIST}" ]; then if [ -z "${PLUGIN_LIST}" ]; then
echo "> No plugins defined. Skipping installation." echo "> No plugins defined. Skipping installation."
@ -43,24 +68,26 @@ function main() {
echo "> About to install defined plugins" echo "> About to install defined plugins"
for PLUGIN_EXPR in ${PLUGIN_LIST}; do for PLUGIN_EXPR in ${PLUGIN_LIST}; do
# Split plugin name and version
IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}" IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}"
PLUGIN_PATH="${WP_CONTENT_PATH}/plugins/${PLUGIN[0]}" PLUGIN_SLUG="${PLUGIN[0]}"
PLUGIN_VERSION="${PLUGIN[1]:-}"
if isPluginInstalled; then if wp-plugin check "${PLUGIN_SLUG}"; then
echo "> Plugin '${PLUGIN[0]}' already installed and will be skipped." echo "> Plugin '${PLUGIN_SLUG}' already installed and will be skipped."
continue continue
fi fi
WP_PLUGIN_INSTALL_ARGS="${PLUGIN[0]}" if [ -n "${PLUGIN_VERSION}" ]; then
echo "> Installing plugin '${PLUGIN_SLUG}' version '${PLUGIN_VERSION}'"
if [ -n "${PLUGIN[1]}" ]; then installPlugin "${PLUGIN_SLUG}" "${PLUGIN_VERSION}" &
WP_PLUGIN_INSTALL_ARGS="${WP_PLUGIN_INSTALL_ARGS} --version=${PLUGIN[1]}" else
echo "> Installing plugin '${PLUGIN_SLUG}'"
installPlugin "${PLUGIN_SLUG}" &
fi fi
echo "> Installing plugin '${PLUGIN[0]}' version '${PLUGIN[1]}'"
installPlugin "${WP_PLUGIN_INSTALL_ARGS}" &
# Run maximum of X plugin installs in parallel # Run maximum of X plugin installs in parallel
while [ "$(jobs | wc -l)" -ge "${CONCURRENCY_LIMIT}" ]; do while [ "$(jobs | wc -l)" -ge "${CONCURRENCY_LIMIT}" ]; do
echo " Waiting for batch of ${CONCURRENCY_LIMIT} plugins to install..." echo " Waiting for batch of ${CONCURRENCY_LIMIT} plugins to install..."
@ -68,35 +95,25 @@ function main() {
done done
done done
echo "> Waiting for all tasks to finish..." echo "> Waiting for all tasks to finish..."
wait wait
# Plugins are installed concurrently, so we need to verify if installed, separately # Plugins are installed concurrently, so we need to verify if installed, separately
echo "> About to verify install of defined plugins" echo "> About to verify install of defined plugins"
FAILED_COUNT=0 if ! checkInstalled; then
echo "> Some plugins failed to install"
for PLUGIN_EXPR in ${PLUGIN_LIST}; do if [ "${PLUGIN_STRICT_INSTALL}" = "true" ]; then
IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}"
PLUGIN_PATH="${WP_CONTENT_PATH}/plugins/${PLUGIN[0]}"
if isPluginInstalled; then
echo "> Plugin '${PLUGIN[0]}' installed"
continue
fi
((FAILED_COUNT = FAILED_COUNT + 1))
echo "> Warning: Plugin '${PLUGIN[0]}' failed to install"
done
echo "> Total of ${FAILED_COUNT} plugins failed to install"
if [ "${PLUGIN_STRICT_INSTALL}" = "true" ] && [ ${FAILED_COUNT} != "0" ]; then
echo "> WORDPRESS_PLUGIN_INSTALL_STRICT is set to true. Terminating with non-zero exit code" echo "> WORDPRESS_PLUGIN_INSTALL_STRICT is set to true. Terminating with non-zero exit code"
return 1 return 1
fi fi
return 0
fi
echo "> All plugins installed successfully"
return 0
} }
main "${@}" main "${@}"

103
rootfs/usr/local/bin/wp-plugin Executable file
View File

@ -0,0 +1,103 @@
#!/usr/bin/env bash
# Download plugin (curl)
# $1 - plugin slug
# $2 - plugin version (optional)
# Returns 0 on success, X on failure
download() {
PLUGIN_SLUG="${1:?download: PLUGIN_SLUG is required}"
PLUGIN_VERSION="${2:-}"
if [ -n "${PLUGIN_VERSION}" ]; then
PLUGIN_FILENAME="${PLUGIN_SLUG}.${PLUGIN_VERSION}.zip"
else
PLUGIN_FILENAME="${PLUGIN_SLUG}.zip"
fi
curl --fail -gsO "https://downloads.wordpress.org/plugin/${PLUGIN_FILENAME}" || return $?
echo "${PLUGIN_FILENAME}"
return 0
}
# Unpack plugin (unzip)
# $1 - plugin slug
# $2 - plugin version (optional)
# $3 - target directory (optional)
# Returns 0 on success, 1 on failure
unpack() {
TARGET_PLUGINS_DIR="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}"
PLUGIN_SLUG="${1:?unpack: PLUGIN_SLUG is required}"
PLUGIN_VERSION="${2:-}"
if [ -n "${PLUGIN_VERSION}" ]; then
PLUGIN_FILENAME="${PLUGIN_SLUG}.${PLUGIN_VERSION}.zip"
else
PLUGIN_FILENAME="${PLUGIN_SLUG}.zip"
fi
if [ -f "${PLUGIN_FILENAME}" ]; then
unzip -qq -d "${TARGET_PLUGINS_DIR}" "${PLUGIN_FILENAME}" || return $?
rm "${PLUGIN_FILENAME}" -f
return 0
fi
return 1
}
# Check if plugin is installed
# $1 - plugin slug
# Returns 0 if plugin is installed, 1 otherwise
check() {
TARGET_PLUGINS_DIR="${TARGET_PLUGINS_DIR:?check: TARGET_PLUGINS_DIR is required}"
PLUGIN_SLUG="${1:?check: PLUGIN_SLUG is required}"
PLUGIN_PATH="${TARGET_PLUGINS_DIR}/${PLUGIN_SLUG}"
# Check if plugin directory exists
if [ ! -d "${PLUGIN_PATH}" ]; then
return 1
fi
# Check if plugin file exists - if yes, plugin is probably installed successfully
if [ -f "${PLUGIN_PATH}/${PLUGIN_SLUG}.php" ]; then
return 0
fi
# Check if plugin directory is empty - if not, plugin is probably installed successfully
if [ "$(ls -A "${PLUGIN_PATH}")" ]; then
return 0
fi
# If we got here, then plugin is not installed
return 1
}
# Main function
main() {
COMMAND="${1:?COMMAND is required}"
TARGET_PLUGINS_DIR="${WP_PLUGINS_PATH:?WP_PLUGINS_PATH is required}"
export TARGET_PLUGINS_DIR
# Execute command by calling function with the same name
case "${COMMAND}" in
download)
download "${@:2}"
return $?
;;
unpack)
unpack "${@:2}"
return $?
;;
check)
check "${@:2}"
return $?
;;
*)
echo "Error: Unknown command '${COMMAND}'"
return 1
;;
esac
}
main "${@}"
exit $?