update_remotes_json_cronjob.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. # this script is intended to be run from a crontab owned by the
  3. # www-data user on ArduPilot's autotest server
  4. # CBS_REMOTES_RELOAD_TOKEN can be supplied as an environment
  5. # variable in the crontab line, for example:
  6. # 0 * * * * CBS_REMOTES_RELOAD_TOKEN=8d64ed06945 /home/custom/beta/CustomBuild/scripts/fetch_release_cronjob.sh
  7. # or can be read from base/secrets/reload_token (the token from the file gets the preference)
  8. CUSTOM_HOME=/home/custom
  9. TOPDIR=$CUSTOM_HOME/beta
  10. LOGDIR=$TOPDIR/cron
  11. BASEDIR=$TOPDIR/base
  12. # Maximum number of log files to retain
  13. MAX_LOG_FILES=50
  14. # Get the current timestamp
  15. TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
  16. # Function to clean up old log files
  17. cleanup_old_logs() {
  18. # Find and sort log files by modification time, oldest first
  19. LOG_FILES=($(ls -1t ${LOGDIR}/fetch_releases_*.log 2>/dev/null))
  20. # Count the number of log files
  21. NUM_LOG_FILES=${#LOG_FILES[@]}
  22. # If the number of log files is greater than the maximum allowed
  23. if [ $NUM_LOG_FILES -gt $MAX_LOG_FILES ]; then
  24. # Loop through and delete the oldest files
  25. for ((i = $MAX_LOG_FILES ; i < $NUM_LOG_FILES ; i++ )); do
  26. rm -f "${LOG_FILES[$i]}"
  27. done
  28. fi
  29. }
  30. # Method to reload remotes on custom build server app
  31. reload_remotes_on_app() {
  32. local token_file="$BASEDIR/secrets/reload_token"
  33. local token
  34. # Check if the token file exists and is readable
  35. if [[ -f "$token_file" && -r "$token_file" ]]; then
  36. # Read the token from the file
  37. token=$(cat "$token_file")
  38. else
  39. echo "Token can't be retrieved from the file."
  40. # Try to get the token from the environment variable
  41. token=$CBS_REMOTES_RELOAD_TOKEN
  42. fi
  43. # Check if token is still empty
  44. if [[ -z "$token" ]]; then
  45. echo "Error: Token could not be retrieved."
  46. return 1
  47. fi
  48. # Send the curl request
  49. curl -X POST https://custom-beta.ardupilot.org/refresh_remotes \
  50. -H "Content-Type: application/json" \
  51. -d "{\"token\": \"$token\"}"
  52. return 0
  53. }
  54. # Call the cleanup function before executing the main script
  55. cleanup_old_logs
  56. # Run fetch_releases.py to add official releases from AP
  57. python3 $TOPDIR/CustomBuild/scripts/fetch_releases.py \
  58. --basedir $BASEDIR \
  59. >> $LOGDIR/fetch_releases_${TIMESTAMP}.log 2>&1
  60. # Run fetch_whitelisted_tags.py to add tags from whitelisted repos
  61. python3 $TOPDIR/CustomBuild/scripts/fetch_whitelisted_tags.py \
  62. --basedir $BASEDIR \
  63. >> $LOGDIR/fetch_whitelisted_tags_${TIMESTAMP}.log 2>&1
  64. # Call the reload_remotes_on_app function to refresh remotes.json on app
  65. reload_remotes_on_app