update_remotes_json_cronjob.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 \
  20. ${LOGDIR}/fetch_whitelisted_tags_*.log \
  21. 2>/dev/null))
  22. # Count the number of log files
  23. NUM_LOG_FILES=${#LOG_FILES[@]}
  24. # If the number of log files is greater than the maximum allowed
  25. if [ $NUM_LOG_FILES -gt $MAX_LOG_FILES ]; then
  26. # Loop through and delete the oldest files
  27. for ((i = $MAX_LOG_FILES ; i < $NUM_LOG_FILES ; i++ )); do
  28. rm -f "${LOG_FILES[$i]}"
  29. done
  30. fi
  31. }
  32. # Method to reload remotes on custom build server app
  33. reload_remotes_on_app() {
  34. local token_file="$BASEDIR/secrets/reload_token"
  35. local token
  36. # Check if the token file exists and is readable
  37. if [[ -f "$token_file" && -r "$token_file" ]]; then
  38. # Read the token from the file
  39. token=$(cat "$token_file")
  40. else
  41. echo "Token can't be retrieved from the file."
  42. # Try to get the token from the environment variable
  43. token=$CBS_REMOTES_RELOAD_TOKEN
  44. fi
  45. # Check if token is still empty
  46. if [[ -z "$token" ]]; then
  47. echo "Error: Token could not be retrieved."
  48. return 1
  49. fi
  50. # Send the curl request
  51. curl -X POST https://custom-beta.ardupilot.org/refresh_remotes \
  52. -H "Content-Type: application/json" \
  53. -d "{\"token\": \"$token\"}"
  54. return 0
  55. }
  56. # Call the cleanup function before executing the main script
  57. cleanup_old_logs
  58. # Run fetch_releases.py to add official releases from AP
  59. python3 $TOPDIR/CustomBuild/scripts/fetch_releases.py \
  60. --basedir $BASEDIR \
  61. >> $LOGDIR/fetch_releases_${TIMESTAMP}.log 2>&1
  62. # Run fetch_whitelisted_tags.py to add tags from whitelisted repos
  63. python3 $TOPDIR/CustomBuild/scripts/fetch_whitelisted_tags.py \
  64. --basedir $BASEDIR \
  65. >> $LOGDIR/fetch_whitelisted_tags_${TIMESTAMP}.log 2>&1
  66. # Call the reload_remotes_on_app function to refresh remotes.json on app
  67. reload_remotes_on_app