update_remotes_json_cronjob_main.sh 2.7 KB

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