Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving some code into the new files functions.sh and run-maintenance-scripts.sh #410

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions _sources/scripts/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# read variables from LocalSettings.php
get_mediawiki_variable() {
php /getMediawikiSettings.php --variable="$1" --format="${2:-string}"
}

get_mediawiki_db_var() {
case $1 in
"wgDBtype")
I="type"
;;
"wgDBserver")
I="host"
;;
"wgDBname")
I="dbname"
;;
"wgDBuser")
I="user"
;;
"wgDBpassword")
I="password"
;;
*)
echo "Unexpected variable name passed to the get_mediawiki_db_var() function: $1"
return
esac
VALUE=$(php /getMediawikiSettings.php --variable=wgDBservers --variableArrayIndex="[0,\"$I\"]" --format=string)
if [ -z "$VALUE" ]; then
VALUE=$(get_mediawiki_variable "$1")
fi
echo "$VALUE"
}

isTrue() {
case $1 in
"True" | "TRUE" | "true" | 1)
return 0
;;
*)
return 1
;;
esac
}

get_hostname_with_port() {
port=$(echo "$1" | grep ":" | cut -d":" -f2)
echo "$1:${port:-$2}"
}

make_dir_writable() {
find "$@" '(' -type f -o -type d ')' \
-not '(' '(' -user "$WWW_USER" -perm -u=w ')' -o \
'(' -group "$WWW_GROUP" -perm -g=w ')' -o \
'(' -perm -o=w ')' \
')' \
-exec chgrp "$WWW_GROUP" {} \; -exec chmod g=rwX {} \;
}
142 changes: 4 additions & 138 deletions _sources/scripts/run-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,7 @@

set -x

WG_CIRRUS_SEARCH_SERVER=$(get_mediawiki_cirrus_search_server)

isTrue() {
case $1 in
"True" | "TRUE" | "true" | 1)
return 0
;;
*)
return 1
;;
esac
}

isFalse() {
case $1 in
"True" | "TRUE" | "true" | 1)
return 1
;;
*)
return 0
;;
esac
}

dir_is_writable() {
# Use -L to get information about the target of a symlink,
# not the link itself, as pointed out in the comments
INFO=( $(stat -L -c "0%a %G %U" "$1") )
PERM=${INFO[0]}
GROUP=${INFO[1]}
OWNER=${INFO[2]}

if (( ($PERM & 0002) != 0 )); then
# Everyone has write access
return 0
elif (( ($PERM & 0020) != 0 )); then
# Some group has write access.
# Is user in that group?
if [[ $GROUP == $WWW_GROUP ]]; then
return 0
fi
elif (( ($PERM & 0200) != 0 )); then
# The owner has write access.
# Does the user own the file?
[[ $WWW_USER == $OWNER ]] && return 0
fi

return 1
}
. /functions.sh

# Symlink all extensions and skins (both bundled and user)
/create-symlinks.sh
Expand Down Expand Up @@ -94,88 +46,6 @@ else
chmod -R g=rwX $APACHE_LOG_DIR
fi

run_maintenance_scripts() {
# Iterate through all the .sh files in /maintenance-scripts/ directory
for maintenance_script in $(find /maintenance-scripts/ -maxdepth 1 -mindepth 1 -type f -name "*.sh"); do
script_name=$(basename "$maintenance_script")

# If the script's name starts with "mw_", run it with the run_mw_script function
if [[ "$script_name" == mw* ]]; then
run_mw_script "$script_name" &
else
# If the script's name doesn't start with "mw"
echo "Running $script_name with user $WWW_USER..."
nice -n 20 runuser -c "/maintenance-scripts/$script_name" -s /bin/bash "$WWW_USER" &
fi
done
}

# Naming convention:
# Scripts with names starting with "mw_" have corresponding enable variables.
# The enable variable is formed by converting the script's name to uppercase and replacing the first underscore with "_ENABLE_".
# For example, the enable variable for "mw_sitemap_generator.sh" would be "MW_ENABLE_SITEMAP_GENERATOR".

run_mw_script() {
sleep 3

# Process the script name and create the corresponding enable variable
local script_name="$1"
script_name_no_ext="${script_name%.*}"
script_name_upper=$(basename "$script_name_no_ext" | tr '[:lower:]' '[:upper:]')
local MW_ENABLE_VAR="${script_name_upper/_/_ENABLE_}"

if isTrue "${!MW_ENABLE_VAR}"; then
echo "Running $script_name with user $WWW_USER..."
nice -n 20 runuser -c "/maintenance-scripts/$script_name" -s /bin/bash "$WWW_USER"
else
echo >&2 "$script_name is disabled."
fi
}


waitdatabase() {
if isFalse "$USE_EXTERNAL_DB"; then
/wait-for-it.sh -t 60 db:3306
fi
}

waitelastic() {
if [ -n "$es_started" ]; then
return 0; # already started
fi

echo >&2 'Waiting for elasticsearch to start'
/wait-for-it.sh -t 60 "$WG_CIRRUS_SEARCH_SERVER"

for i in {300..0}; do
result=0
output=$(wget --timeout=1 -q -O - "http://$WG_CIRRUS_SEARCH_SERVER/_cat/health") || result=$?
if [[ "$result" = 0 && $(echo "$output"|awk '{ print $4 }') = "green" ]]; then
break
fi
if [ "$result" = 0 ]; then
echo >&2 "Waiting for elasticsearch health status changed from [$(echo "$output"|awk '{ print $4 }')] to [green]..."
else
echo >&2 'Waiting for elasticsearch to start...'
fi
sleep 1
done
if [ "$i" = 0 ]; then
echo >&2 'Elasticsearch is not ready for use'
echo "$output"
return 1
fi
echo >&2 'Elasticsearch started successfully'
es_started="1"
return 0
}

run_autoupdate () {
echo "Running auto-update..."
runuser -c "php maintenance/update.php --quick" -s /bin/bash "$WWW_USER"
echo "Auto-update completed"
}

config_subdir_wikis() {
echo "Configuring subdirectory wikis..."
/config-subdir-wikis.sh
Expand All @@ -202,12 +72,6 @@ check_mount_points () {
fi
}

# Wait db
waitdatabase

# Pause setup until ElasticSearch starts running
waitelastic

# Check for `user-` prefixed mounts and bow out if not found
check_mount_points

Expand All @@ -227,7 +91,9 @@ fi

echo "Starting services..."

run_maintenance_scripts &
# Run maintenance scripts in background.
touch "$WWW_ROOT/.maintenance"
/run-maintenance-scripts.sh &

echo "Checking permissions of $MW_VOLUME/sitemap..."
if dir_is_writable "$MW_VOLUME/sitemap"; then
Expand Down
Loading
Loading