link_morgue_with_snapshot_farm.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. PROCESSDIR="/srv/morgue.debian.org"
  46. FARMBASE="/srv/snapshot.debian.org/farm"
  47. cd "${PROCESSDIR}"
  48. log "Processing ${PROCESSDIR}"
  49. find ${PROCESSDIR} -type f |
  50. while read mfile; do
  51. # Get the files sha1sum
  52. mshasum=$(sha1sum ${mfile})
  53. mshasum=${mshasum%% *}
  54. # And now get the "levels" of the farm
  55. if [[ ${mshasum} =~ ([0-9a-z][0-9a-z])([0-9a-z][0-9a-z]).* ]]; then
  56. LVL1=${BASH_REMATCH[1]}
  57. LVL2=${BASH_REMATCH[2]}
  58. else
  59. log "Ups, unknown error in regex for ${mfile} (${mshasum})"
  60. continue
  61. fi
  62. # See if we have a target
  63. if [ -f "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" ]; then
  64. # Yes, lets symlink it
  65. log "Symlinking ${mfile} to ${FARMBASE}/${LVL1}/${LVL2}/${mshasum}"
  66. ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
  67. else
  68. # No, just tell
  69. log "No symlink target for ${mfile}"
  70. fi
  71. done # mfile read mfile