sfsnapshot-upload 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. # sfsnapshot-upload - Snapshot upload script using sfsnapshotgit
  3. # Original author: Thomas Guyot-Sionnest <tguyot@gmail.com>
  4. #
  5. # This script uses sfsnapshotgit to update the snapshot is needed and upload
  6. # it to SourceForge. The branches to create snapshot from can be given as an
  7. # argument, otherwise the default is master.
  8. # Handle command errors (-e) and coder sleep deprivation issues (-u)
  9. set -eu
  10. trap 'echo "An error occurred in sfsnapshot-upload at line $LINENO"; exit 1' EXIT
  11. # This can be used to override the default in sfsnapshotgit:
  12. export SFSNAP_REPO=~/staging/nagiosplugins
  13. export SFSNAP_ORIGIN=origin
  14. export SFSNAP_DEST=~/staging/snapshot
  15. ## Some stuff that shouldn't change often...
  16. # The file we'll use to create the snapshot
  17. sfsnapshot=~/bin/sfsnapshotgit
  18. # Retention time for snapshots (in minutes), 0 for no retention.
  19. CLEAN_TIME=1440
  20. # Where to place the generated files
  21. OUT_SERVER="tonvoon@frs.sourceforge.net"
  22. OUT_PATH="/home/groups/n/na/nagiosplug/htdocs/snapshot"
  23. # Links to always point to the master branch for backwards-compatibility
  24. COMPATLINKS="HEAD trunk-`date -u +%Y%m%d%H%M`"
  25. # And compatibility links to always delete except the last one
  26. COMPATCLEANUP="trunk-*"
  27. # If one or more argument is given, this is the branches to create the snapshots from
  28. if [ $# -eq 0 ]
  29. then
  30. HEADS='master'
  31. else
  32. HEADS=$@
  33. fi
  34. # If we don't keep old snapshots we can clean up all links now
  35. if [ $CLEAN_TIME -eq 0 ]
  36. then
  37. find $SFSNAP_DEST -type l -name '*.gz' -delete
  38. fi
  39. for head in $HEADS ; do
  40. # This runs the actual snapshot code. It creates new snapshots if needed and always
  41. # return the current snapshot file (even if it wasn't modified).
  42. file=$($sfsnapshot $head)
  43. # Create main head link
  44. ln -sf $file $SFSNAP_DEST/nagios-plugins-$head.tar.gz
  45. # Keep links by branch name too if we keep old snapshots, so we can keep tracks of them
  46. if [ $CLEAN_TIME -gt 0 -a ! -e "$SFSNAP_DEST/nagios-plugins-$head-${file#nagios-plugins-}" ]
  47. then
  48. ln -s $file $SFSNAP_DEST/nagios-plugins-$head-${file#nagios-plugins-}
  49. fi
  50. # Cleanup and re-create backward-compatibility links
  51. if [ "$head" == "master" ]
  52. then
  53. for cclean in $COMPATCLEANUP
  54. do
  55. find $SFSNAP_DEST -type l -name "nagios-plugins-$cclean.tar.gz" -delete
  56. done
  57. for compat in $COMPATLINKS
  58. do
  59. ln -sf $file $SFSNAP_DEST/nagios-plugins-$compat.tar.gz
  60. done
  61. fi
  62. done
  63. cd $SFSNAP_DEST
  64. # Clean up links older than $CLEAN_TIME if needed
  65. if [ $CLEAN_TIME -gt 0 ]
  66. then
  67. find . -type l -name '*.gz' -mmin +$CLEAN_TIME -delete
  68. fi
  69. # Now clean up files that we don't need
  70. # 1. loop over actual snapshots
  71. for dest in `find . -type f -name '*.gz' |xargs -n 1 basename`
  72. do
  73. # 2. Loop over the list of linked-to files
  74. for current in `find . -type l -name '*.gz' |xargs -n 1 readlink | sort | uniq`
  75. do
  76. if [ "$current" == "$dest" ]
  77. then
  78. # File is being linked to - don't delete (continue first loop)
  79. continue 2
  80. fi
  81. done
  82. # No link to this file, we can drop it
  83. rm -f $dest
  84. done
  85. # Create MD5 sum
  86. cat <<-END_README > README
  87. This is the latest snapshot of nagiosplug, consisting of the following
  88. head(s):
  89. $HEADS
  90. The nagios-plugins-<head>.tar.gz link will always point to the latest
  91. corresponding snapshot (nagios-plugins-<git-describe>.tar.gz).
  92. For backward-compatibility, the nagios-plugins-HEAD.tar.gz and
  93. nagios-plugins-trunk-<ts>.tar.gz point to their corresponding "master"
  94. head.
  95. The tarballs will only be updated when a change has been made. The
  96. MD5SUM file is updated every time the snapshot script runs.
  97. The MD5SUMs are:
  98. END_README
  99. md5sum *.gz | tee -a README > MD5SUM
  100. # Sync the files
  101. [ -n "$OUT_SERVER" ] && OUT_SERVER="$OUT_SERVER:"
  102. rsync -a --exclude=.htaccess --exclude=HEADER.html --delete "$SFSNAP_DEST/" "$OUT_SERVER$OUT_PATH"
  103. trap - EXIT