link_morgue.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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="http://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. if [[ ${out} == UPDATED ]]; then
  90. cd "${dbdir}"
  91. rsync ${DBHOST}:/srv/ftp-master.debian.org/home/hashes.gz ${HASHFILE}.gz
  92. fi
  93. cd "${PROCESSDIR}"
  94. log "Processing ${PROCESSDIR}"
  95. ${scriptsdir}/link_morgue \
  96. --known-hashes ${HASHFILE}.gz \
  97. --farmdir "${FARMBASE}" \
  98. --morguedir "${PROCESSDIR}"
  99. # And now, maybe, transfer stuff over to stabile...
  100. if [ "$(hostname -s)" != "stabile" ]; then
  101. cd "${PROCESSDIR}"
  102. LISTFILE=$(mktemp -p ${TMPDIR} )
  103. # We only transfer symlinks or files changed more than 14 days ago
  104. # (assuming we won't ever find anything on snapshot for them)
  105. find . \( -type l -o \( -type f -ctime 14 \) \) -print0 >${LISTFILE}
  106. # morgue-sync has to be setup in ~/.ssh/config and the authorized_keys
  107. # on the other side should contain (one line, no #)
  108. # command="rsync --server -lHogDtpRe.Lsf --remove-source-files . /srv/morgue.debian.org/sync/ftp-master",
  109. # no-port-forwarding,no-X11-forwarding,no-agent-forwarding,from="ftp-master.debian.org" ssh-rsa...
  110. 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
  111. # And remove empty subdirs. To remove entire hierarchies we probably should run this
  112. # in a loop, but why bother? They'll be gone in a few days then, so meh.
  113. find "${PROCESSDIR}" -type d -empty -print0 | xargs --no-run-if-empty -0 rmdir
  114. fi