Forráskód Böngészése

scripts: add script to be run from cronjob to update releases json

Peter Barker 1 éve
szülő
commit
f0d137b945
1 módosított fájl, 53 hozzáadás és 0 törlés
  1. 53 0
      scripts/fetch_releases.py

+ 53 - 0
scripts/fetch_releases.py

@@ -0,0 +1,53 @@
+#!/bin/bash
+
+# this script is intended to be run from a crontab owned by the
+# www-data user on ArduPilot's autotest server
+
+# CBS_REMOTES_RELOAD_TOKEN can be supplied as an environment
+# variable in the crontab line, for example:
+
+# 0 * * * * CBS_REMOTES_RELOAD_TOKEN=8d64ed06945 /home/custom/beta/CustomBuild/scripts/fetch_release_cronjob.sh
+
+# or can be read from base/secrets/reload_token (the token from the file gets the preference)
+
+CUSTOM_HOME=/home/custom
+TOPDIR=$CUSTOM_HOME/beta
+LOGDIR=$TOPDIR/cron
+BASEDIR=$TOPDIR/base
+
+# Maximum number of log files to retain
+MAX_LOG_FILES=50
+
+# Get the current timestamp
+TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
+
+# Log file name
+LOG_FILE="${LOGDIR}/fetch_releases_${TIMESTAMP}.log"
+
+# Function to clean up old log files
+cleanup_old_logs() {
+  # Find and sort log files by modification time, oldest first
+  LOG_FILES=($(ls -1t ${LOGDIR}/fetch_releases_*.log 2>/dev/null))
+
+  # Count the number of log files
+  NUM_LOG_FILES=${#LOG_FILES[@]}
+
+  # If the number of log files is greater than the maximum allowed
+  if [ $NUM_LOG_FILES -gt $MAX_LOG_FILES ]; then
+
+    # Loop through and delete the oldest files
+    for ((i = $MAX_LOG_FILES ; i < $NUM_LOG_FILES ; i++ )); do
+      rm -f "${LOG_FILES[$i]}"
+    done
+
+  fi
+}
+
+# Call the cleanup function before executing the main script
+cleanup_old_logs
+
+# Call the main python script
+python3 $TOPDIR/CustomBuild/scripts/fetch_releases.py \
+        --appurl https://custom-beta.ardupilot.org/refresh_remotes \
+        --basedir $BASEDIR \
+        >> $LOG_FILE 2>&1