Use local variables

This commit is contained in:
Aleksandar Puharic 2024-05-17 01:52:44 +02:00
parent 4cb7286fc4
commit e6110e37f2
Signed by: xZero707
GPG Key ID: 3CC53DCAA9C237BB
2 changed files with 55 additions and 42 deletions

View File

@ -1,9 +1,11 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Install plugin
installPlugin() {
ARCHIVE="$(wp-plugin download "${@}" 2>/dev/null)"
if [ -z "${ARCHIVE}" ]; then
local pluginArchive
pluginArchive="$(wp-plugin download "${@}" 2>/dev/null)"
if [ -z "${pluginArchive}" ]; then
return 1
fi
@ -12,27 +14,30 @@ installPlugin() {
}
checkInstalled() {
FAILED_COUNT=0
local failedCount=0
local pluginSlug
local pluginExpr
local plugin
for PLUGIN_EXPR in ${PLUGIN_LIST}; do
IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}"
for pluginExpr in ${PLUGIN_LIST}; do
IFS=':' read -ra plugin <<<"${pluginExpr}"
PLUGIN_SLUG="${PLUGIN[0]}"
pluginSlug="${plugin[0]}"
if wp-plugin check "${PLUGIN_SLUG}"; then
echo "Plugin '${PLUGIN_SLUG}' installed"
if wp-plugin check "${pluginSlug}"; then
echo "Plugin '${pluginSlug}' installed"
continue
fi
((FAILED_COUNT = FAILED_COUNT + 1))
echo "Warning: Plugin '${PLUGIN_SLUG}' failed to install"
((failedCount = failedCount + 1))
echo "Warning: Plugin '${pluginSlug}' failed to install"
done
if [ "${FAILED_COUNT}" = "0" ]; then
if [ "${failedCount}" = "0" ]; then
return 0
fi
echo "Total of ${FAILED_COUNT} plugins failed to install"
echo "Total of ${failedCount} plugins failed to install"
return 1
}
@ -40,7 +45,11 @@ checkInstalled() {
function taskInstallPlugins() {
export WP_PLUGINS_PATH
CONCURRENCY_LIMIT="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}"
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
@ -49,30 +58,30 @@ function taskInstallPlugins() {
fi
echo "About to install defined plugins"
for PLUGIN_EXPR in ${PLUGIN_LIST}; do
for pluginExpr in ${PLUGIN_LIST}; do
# Split plugin name and version
IFS=':' read -ra PLUGIN <<<"${PLUGIN_EXPR}"
IFS=':' read -ra plugin <<<"${pluginExpr}"
PLUGIN_SLUG="${PLUGIN[0]}"
PLUGIN_VERSION="${PLUGIN[1]:-}"
pluginSlug="${plugin[0]}"
pluginVersion="${plugin[1]:-}"
if wp-plugin check "${PLUGIN_SLUG}"; then
echo "Plugin '${PLUGIN_SLUG}' already installed and will be skipped."
if wp-plugin check "${pluginSlug}"; then
echo "Plugin '${pluginSlug}' 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}" &
if [ -n "${pluginVersion}" ]; then
echo "Installing plugin '${pluginSlug}' version '${pluginVersion}'"
installPlugin "${pluginSlug}" "${pluginVersion}" &
else
echo "Installing plugin '${PLUGIN_SLUG}'"
installPlugin "${PLUGIN_SLUG}" &
echo "Installing plugin '${pluginSlug}'"
installPlugin "${pluginSlug}" &
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..."
while [ "$(jobs | wc -l)" -ge "${concurrencyLimit}" ]; do
echo " Waiting for batch of ${concurrencyLimit} plugins to install..."
wait
done
done
@ -104,7 +113,6 @@ main() {
export WP_CONTENT_PATH
WP_PLUGINS_INSTALL_CONCURRENCY="${WP_PLUGINS_INSTALL_CONCURRENCY:-5}"
export PLUGIN_LIST WP_PLUGINS_PATH WP_PLUGINS_INSTALL_CONCURRENCY
taskInstallPlugins "${@}" &
@ -115,4 +123,4 @@ main() {
return 0
}
main "${@}"
main "${@}"

View File

@ -1,4 +1,6 @@
#!/command/with-contenv bash
# shellcheck shell=bash
# Designed to replace original, overcomplicated entrypoint script from official wordpress docker repository
# Why not use already available tools instead?!
@ -6,14 +8,14 @@
trap scriptExitHandler EXIT
function scriptExitHandler() {
LAST_EXIT_CODE=$?
if [ "${LAST_EXIT_CODE}" = "0" ]; then
local lastExitCode=$?
if [ "${lastExitCode}" = "0" ]; then
echo "Script finished successfully"
exit "${LAST_EXIT_CODE}"
exit "${lastExitCode}"
fi
echo "Script finished with an error"
exit "${LAST_EXIT_CODE}"
exit "${lastExitCode}"
}
function reportUnhealthy() {
@ -25,35 +27,38 @@ function main() {
exec > >(while read line; do echo "[init-verify-wordpress] ${line}"; done) 2>&1
local wpCurrentVersion
local wpInstalledVersion
# Removes trailing zero if found
# This is required due to inconsistencies between WodPress docker image versioning and wp-cli core download
# If patch version is 0, it is not considered by wp-cli.
WP_VERSION=$(echo "${WP_VERSION:?}" | sed --expression='s/.0$//g')
wpCurrentVersion=$(echo "${WP_VERSION:?}" | sed --expression='s/.0$//g')
echo "Verifying 'WordPress ${WP_VERSION}' installation..."
WP_INSTALLED_VERSION="$(wp core version)"
echo "Verifying 'WordPress ${wpCurrentVersion}' installation..."
wpInstalledVersion="$(wp core version)"
set -e
rm -f "/tmp/.wp-unhealthy"
if [ -z "${WP_INSTALLED_VERSION}" ]; then
if [ -z "${wpInstalledVersion}" ]; then
echo "Error: WordPress installation does not seem to be present or valid. Continuing anyway..."
reportUnhealthy "WP_NOT_PRESENT"
return 0
elif [ "${WP_INSTALLED_VERSION}" != "${WP_VERSION}" ]; then
elif [ "${wpInstalledVersion}" != "${wpCurrentVersion}" ]; then
echo "WARNING! WordPress version mismatch"
echo " Expected version: ${WP_VERSION}"
echo " Detected version: ${WP_INSTALLED_VERSION}"
echo " Expected version: ${wpCurrentVersion}"
echo " Detected version: ${wpInstalledVersion}"
echo "Seems like WordPress installation got updated outside image scope"
echo " - This is dangerous as changes will not persist when container is recreated which might lead to inconsistencies between installation and the database."
echo " - You should assume that recreating the container will render the website inoperable."
echo " - Please make sure that you're running image: nlss/wordpress:${WP_VERSION}"
echo " - Please make sure that you're running image: nlss/wordpress:${wpCurrentVersion}"
reportUnhealthy "WP_VERSION_MISMATCH"
return 0
else
echo "Identified 'WordPress ${WP_VERSION}'"
fi
echo "Identified 'WordPress ${wpCurrentVersion}'"
}
main "${@}"