link_morgue.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. # No way I try to deal with a crippled sh just for POSIX foo.
  3. # Copyright (C) 2011 Joerg Jaspert <joerg@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as
  7. # published by the Free Software Foundation; version 2.
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. # Homer: Are you saying you're never going to eat any animal again? What
  18. # about bacon?
  19. # Lisa: No.
  20. # Homer: Ham?
  21. # Lisa: No.
  22. # Homer: Pork chops?
  23. # Lisa: Dad, those all come from the same animal.
  24. # Homer: Heh heh heh. Ooh, yeah, right, Lisa. A wonderful, magical animal.
  25. # Let files inside morgue be symlinks to the snapshot farm
  26. # exit on errors
  27. set -e
  28. # make sure to only use defined variables
  29. set -u
  30. # ERR traps should be inherited from functions too. (And command
  31. # substitutions and subshells and whatnot, but for us the functions is
  32. # the important part here)
  33. set -E
  34. # Make sure we start out with a sane umask setting
  35. umask 022
  36. # And use one locale, no matter what the caller has set
  37. export LANG=C
  38. export LC_ALL=C
  39. # log something (basically echo it together with a timestamp)
  40. # Set $PROGRAM to a string to have it added to the output.
  41. function log () {
  42. local prefix=${PROGRAM:-$0}
  43. echo "$(date +"%b %d %H:%M:%S") $(hostname -s) ${prefix}[$$]: $@"
  44. }
  45. case "$(hostname)" in
  46. fasolo)
  47. SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
  48. archive=ftp-master
  49. ;;
  50. seger)
  51. SCRIPTVARS=/srv/security-master.debian.org/dak/config/debian-security/vars
  52. archive=security-master
  53. ;;
  54. *)
  55. echo "Unknown host $(hostname)" >&2
  56. exit 1
  57. ;;
  58. esac
  59. export SCRIPTVARS
  60. . $SCRIPTVARS
  61. function byebye_lock() {
  62. rm -f $lockdir/link_morgue
  63. }
  64. lockfile -l 3600 $lockdir/link_morgue
  65. trap byebye_lock ERR EXIT TERM HUP INT QUIT
  66. PROCESSDIR="${base}/morgue"
  67. FARMBASE="/srv/snapshot.debian.org/farm"
  68. FARMURL="https://snapshot.debian.org/file/"
  69. PROGRAM="link_morgue"
  70. DBHOST="lw08.debian.org"
  71. HASHFILE="${dbdir}/hashes"
  72. NOW=$(date -Is)
  73. # We have to prepare our file with list of hashes. We get it directly
  74. # from the snapshot db. Thats a costly operation taking some 15 or so
  75. # minutes, but still better than the rate limiting we run into when
  76. # using the web api.
  77. #
  78. # The preparehashes is an otion the ssh forced command on the remote
  79. # host uses to generate a nice file with hashes, one per line. It does
  80. # so by running something like "psql service=snapshot-guest -c "select
  81. # hash from file" > somefile", then packs the file. To not stress the
  82. # db host too much with that query, it only refreshes the file if its
  83. # older than 24 hours.
  84. out=""
  85. out=$(ssh ${DBHOST} preparehashes)
  86. # And now we get us the file here, so we can easily lookup hashes.
  87. # (the rsync uses the same ssh key and runs into the forced command.
  88. # That just knows to send the file for rsync instead of preparing it.)
  89. cd "${dbdir}"
  90. rsync ${DBHOST}:/srv/ftp-master.debian.org/home/hashes.gz ${HASHFILE}.gz
  91. cd "${PROCESSDIR}"
  92. log "Processing ${PROCESSDIR}"
  93. ${scriptsdir}/link_morgue \
  94. --known-hashes ${HASHFILE}.gz \
  95. --farmdir "${FARMBASE}" \
  96. --morguedir "${PROCESSDIR}"
  97. log "Processing ${PROCESSDIR} done"
  98. # And now, maybe, transfer stuff over to stabile...
  99. if [ "$(hostname -s)" != "stabile" ]; then
  100. cd "${PROCESSDIR}"
  101. LISTFILE=$(mktemp -p ${TMPDIR} )
  102. # We only transfer symlinks or files changed more than 14 days ago
  103. # (assuming we won't ever find anything on snapshot for them)
  104. find . \( -type l -o \( -type f -ctime +14 \) \) -print0 >${LISTFILE}
  105. # morgue-sync has to be setup in ~/.ssh/config and the authorized_keys
  106. # on the other side should contain (one line, no #)
  107. # command="rsync --server -lHogDtpRe.Lsf --remove-source-files . /srv/morgue.debian.org/sync/ftp-master",
  108. # restrict,from="ftp-master.debian.org" ssh-rsa...
  109. rsync -aHq -e "ssh -o Batchmode=yes -o ConnectTimeout=30 -o SetupTimeout=30 " --remove-source-files --from0 --files-from=${LISTFILE} $base/morgue/ morgue-sync:/srv/morgue.debian.org/sync/$archive
  110. # And remove empty subdirs. To remove entire hierarchies we probably should run this
  111. # in a loop, but why bother? They'll be gone in a few days then, so meh.
  112. find "${PROCESSDIR}" -type d -empty -print0 | xargs --no-run-if-empty -0 rmdir
  113. log "Moving old files out of morgue done"
  114. fi