1
0
Fork 0

Refactor maintenance.sh, cron

This commit is contained in:
Félix Piédallu 2024-09-13 20:28:40 +02:00 committed by Félix Piédallu
parent 674eb31347
commit db090b056b
2 changed files with 73 additions and 113 deletions

6
cron
View file

@ -6,9 +6,3 @@
# Every friday at 6 PM UTC # Every friday at 6 PM UTC
0 17 * * 5 root /bin/bash __BASEDIR__/maintenance.sh update_app_levels 0 17 * * 5 root /bin/bash __BASEDIR__/maintenance.sh update_app_levels
# Every 2 hours
0 */2 * * * root /bin/bash __BASEDIR__/maintenance.sh fetch_main_dashboard
# Every day at 2AM
0 2 * * * root /bin/bash __BASEDIR__/maintenance.sh fetch_level_history

180
maintenance.sh Normal file → Executable file
View file

@ -1,135 +1,101 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -Eeuo pipefail
workdir=$(realpath $(dirname "$0")) SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd $workdir
function update_venv() update_venv() {
{
if [ -d "venv" ]; then if [ -d "venv" ]; then
venv/bin/pip install -r requirements.txt >/dev/null venv/bin/pip install -r requirements.txt >/dev/null
fi fi
} }
function git_pull_and_update_cron_and_restart_services_if_needed() update_apps_repo() {
{ if [ -d ".apps" ]; then
if ! git pull &>/dev/null; then git -C .apps pull
sendxmpppy "[apps repo] Couldn't pull, maybe local changes are present?" else
exit 1 git clone https://github.com/YunoHost/apps.git .apps
fi fi
.cache/tools/app_caches.py -l .cache/apps/ -c .apps_cache -d -j20
# Cron
cat cron | sed "s@__BASEDIR__@$workdir@g" > /etc/cron.d/app_list
# App store
chown -R appstore store
pushd store >/dev/null
modified_after_service_start="$(find *.py translations/ templates/ assets/ -newermt "$(systemctl show --property=ActiveEnterTimestamp appstore | cut -d= -f2 | cut -d' ' -f2-3)")"
if [ -n "$modified_after_service_start" ]
then
update_venv
pushd assets >/dev/null
./tailwindcss-linux-x64 --input tailwind-local.css --output tailwind.css --minify
popd >/dev/null
systemctl restart appstore
sleep 3
fi
popd >/dev/null
systemctl --quiet is-active appstore || sendxmpppy "[appstore] Uhoh, failed to (re)start the appstore service?"
# App generator
chown -R appgenerator tools/app_generator
pushd tools/app_generator >/dev/null
modified_after_service_start="$(find *.py translations/ templates/ static/ -newermt "$(systemctl show --property=ActiveEnterTimestamp appgenerator | cut -d= -f2 | cut -d' ' -f2-3)")"
if [ -n "$modified_after_service_start" ]
then
update_venv
pushd assets >/dev/null
./tailwindcss-linux-x64 --input tailwind-local.css --output tailwind.css --minify
popd >/dev/null
systemctl restart appgenerator
sleep 3
fi
popd >/dev/null
systemctl --quiet is-active appgenerator || sendxmpppy "[appgenerator] Uhoh, failed to (re)start the appgenerator service?"
# Autoreadme
pushd tools/readme_generator >/dev/null
modified_after_service_start="$(find *.py translations/ templates/ -newermt "$(systemctl show --property=ActiveEnterTimestamp webhooks | cut -d= -f2 | cut -d' ' -f2-3)")"
if [ -n "$modified_after_service_start" ]
then
update_venv
systemctl restart webhooks
sleep 3
fi
popd >/dev/null
# Autoreadme
pushd tools/webhooks >/dev/null
modified_after_service_start="$(find *.py -newermt "$(systemctl show --property=ActiveEnterTimestamp webhooks | cut -d= -f2 | cut -d' ' -f2-3)")"
if [ -n "$modified_after_service_start" ]
then
update_venv
systemctl restart webhooks
sleep 3
fi
popd >/dev/null
pushd tools/autoupdate_app_sources >/dev/null
update_venv
popd >/dev/null
systemctl --quiet is-active webhooks || sendxmpppy "[autoreadme] Uhoh, failed to (re)start the autoreadme service?"
} }
function update_app_cache() update_apps_cache() {
{
./tools/app_caches.py -d -l . -c .apps_cache -j20 ./tools/app_caches.py -d -l . -c .apps_cache -j20
} }
function rebuild_catalog() git_pull_and_restart_services() {
{ commit="$(git rev-parse HEAD)"
log=$workdir/app_list_auto_update.log
date >> $log if ! git pull &>/dev/null; then
git_pull_and_update_cron_and_restart_services_if_needed sendxmpppy "[apps-tools] Couldn't pull, maybe local changes are present?"
update_app_cache exit 1
./tools/list_builder.py -l . &>> $log || sendxmpppy "[listbuilder] Rebuilding the application list failed miserably" fi
if [[ "$(git rev-parse HEAD)" == "$commit" ]]; then
return
fi
# Cron
cat cron | sed "s@__BASEDIR__@$SCRIPT_DIR@g" > /etc/cron.d/apps_tools
pushd app_generator > /dev/null
update_venv
pushd assets >/dev/null
./tailwindcss-linux-x64 --input tailwind-local.css --output tailwind.css --minify
popd >/dev/null
popd > /dev/null
systemctl restart appgenerator
sleep 3
systemctl --quiet is-active appgenerator || sendxmpppy "[appgenerator] Uhoh, failed to (re)start the appgenerator service?"
update_venv
systemctl restart webhooks
sleep 3
systemctl --quiet is-active webhooks || sendxmpppy "[autoreadme] Uhoh, failed to (re)start the autoreadme service?"
} }
function autoupdate_app_sources() rebuild_catalog_error_msg="[list_builder] Rebuilding the application list failed miserably!"
{ rebuild_catalog() {
log=$workdir/app_sources_auto_update.log date
date >> $log update_app_cache
git_pull_and_update_cron_and_restart_services_if_needed ./tools/list_builder.py -l .
}
autoupdate_app_sources_error_msg="[autoupdate_app_sources] App sources auto-update failed miserably!"
autoupdate_app_sources() {
date
update_app_cache update_app_cache
tools/autoupdate_app_sources/venv/bin/python3 tools/autoupdate_app_sources/autoupdate_app_sources.py \ tools/autoupdate_app_sources/venv/bin/python3 tools/autoupdate_app_sources/autoupdate_app_sources.py \
-l . --latest-commit-weekly --edit --commit --pr --paste -j1 \ -l . --latest-commit-weekly --edit --commit --pr --paste -j1
&> $log || sendxmpppy "[appsourcesautoupdate] App sources auto-update failed miserably"
} }
function update_app_levels() update_app_levels_error_msg="[update_app_levels] Updating apps level failed miserably!"
{ update_app_levels() {
date
update_app_cache update_app_cache
pushd tools/update_app_levels >/dev/null python3 update_app_levels.py -l .
python3 update_app_levels.py -l .
popd >/dev/null
} }
function fetch_main_dashboard() safe_run() {
{ logfile="$SCRIPT_DIR/$1.log"
pushd store >/dev/null error_msg_var="${1}_error_msg"
venv/bin/python3 fetch_main_dashboard.py 2>&1 | grep -v 'Following Github server redirection' if ! "$@" &> "$logfile"; then
popd >/dev/null sendxmpppy "${!error_msg_var}"
fi
} }
main() {
cd "$SCRIPT_DIR"
function fetch_level_history() # Update self, then re-exec to prevent an issue with modified bash scripts
{ if [[ -z "${APPS_TOOLS_UPDATED:-}" ]]; then
pushd store >/dev/null git_pull_and_restart_services
venv/bin/python3 fetch_level_history.py APPS_TOOLS_UPDATED=1 exec "$0" "$@"
popd >/dev/null fi
safe_run "$@"
} }
$1 main "$@"