link_morgue.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. franck)
  47. SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
  48. archive=ftp-master
  49. ;;
  50. chopin)
  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. cd "${PROCESSDIR}"
  71. log "Processing ${PROCESSDIR}"
  72. find ${PROCESSDIR} -type f |
  73. while read mfile; do
  74. # Get the files sha1sum
  75. mshasum=$(sha1sum ${mfile})
  76. mshasum=${mshasum%% *}
  77. # And now get the "levels" of the farm
  78. if [[ ${mshasum} =~ ([0-9a-z][0-9a-z])([0-9a-z][0-9a-z]).* ]]; then
  79. LVL1=${BASH_REMATCH[1]}
  80. LVL2=${BASH_REMATCH[2]}
  81. else
  82. log "Ups, unknown error in regex for ${mfile} (${mshasum})"
  83. continue
  84. fi
  85. # See if we have a target
  86. if [ "$(hostname -s)" = "stabile" ]; then
  87. # If we run on the snapshot host directly just look locally
  88. if [ -f "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" ]; then
  89. ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
  90. fi
  91. else
  92. # If we run wherever, use curl and the http interface
  93. if curl --fail --silent --max-time 120 --head ${FARMURL}/${mshasum} >/dev/null; then
  94. # Yes, lets symlink it
  95. # Yay for tons of dangling symlinks, but when this is done a rsync
  96. # will run and transfer the whole shitload of links over to the morgue host.
  97. ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
  98. fi
  99. fi
  100. done # for mfile in...
  101. # And now, maybe, transfer stuff over to stabile...
  102. if [ "$(hostname -s)" != "stabile" ]; then
  103. cd "${PROCESSDIR}"
  104. LISTFILE=$(mktemp -p ${TMPDIR} )
  105. # We only transfer symlinks or files changed more than 14 days ago
  106. # (assuming we won't ever find anything on snapshot for them)
  107. find . \( -type l -o \( -type f -ctime 14 \) \) -print0 >${LISTFILE}
  108. # morgue-sync has to be setup in ~/.ssh/config and the authorized_keys
  109. # on the other side should contain (one line, no #)
  110. # command="rsync --server -lHogDtpRe.Lsf --remove-source-files . /srv/morgue.debian.org/sync/ftp-master",
  111. # no-port-forwarding,no-X11-forwarding,no-agent-forwarding,from="ftp-master.debian.org" ssh-rsa...
  112. 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
  113. # And remove empty subdirs. To remove entire hierarchies we probably should run this
  114. # in a loop, but why bother? They'll be gone in a few days then, so meh.
  115. find "${PROCESSDIR}" -type d -empty -print0 | xargs --no-run-if-empty -0 rmdir
  116. fi