3 # No way I try to deal with a crippled sh just for POSIX foo.
5 # Copyright (C) 2011 Joerg Jaspert <joerg@debian.org>
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; version 2.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 # Homer: Are you saying you're never going to eat any animal again? What
26 # Lisa: Dad, those all come from the same animal.
27 # Homer: Heh heh heh. Ooh, yeah, right, Lisa. A wonderful, magical animal.
29 # Let files inside morgue be symlinks to the snapshot farm
33 # make sure to only use defined variables
35 # ERR traps should be inherited from functions too. (And command
36 # substitutions and subshells and whatnot, but for us the functions is
37 # the important part here)
40 # Make sure we start out with a sane umask setting
43 # And use one locale, no matter what the caller has set
47 # log something (basically echo it together with a timestamp)
48 # Set $PROGRAM to a string to have it added to the output.
50 local prefix=${PROGRAM:-$0}
51 echo "$(date +"%b %d %H:%M:%S") $(hostname -s) ${prefix}[$$]: $@"
54 export SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
57 function byebye_lock() {
58 rm -f $lockdir/link_morgue
61 lockfile -l 3600 $lockdir/link_morgue
62 trap byebye_lock ERR EXIT TERM HUP INT QUIT
64 PROCESSDIR="${base}/morgue"
65 FARMBASE="/srv/snapshot.debian.org/farm"
66 FARMURL="http://snapshot.debian.org/file/"
70 log "Processing ${PROCESSDIR}"
71 find ${PROCESSDIR} -type f |
73 # Get the files sha1sum
74 mshasum=$(sha1sum ${mfile})
75 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]}
82 log "Ups, unknown error in regex for ${mfile} (${mshasum})"
86 # See if we have a target
87 if [ "$(hostname -s)" = "stabile" ]; then
88 # If we run on the snapshot host directly just look locally
89 if [ -f "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" ]; then
90 ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
93 # If we run wherever, use curl and the http interface
94 if curl --fail --silent --max-time 120 --head ${FARMURL}/${mshasum} >/dev/null; then
95 # Yes, lets symlink it
96 # Yay for tons of dangling symlinks, but when this is done a rsync
97 # will run and transfer the whole shitload of links over to the morgue host.
98 ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
101 done # for mfile in...
103 # And now, maybe, transfer stuff over to stabile...
104 if [ "$(hostname -s)" != "stabile" ]; then
106 LISTFILE=$(mktemp -p ${TMPDIR} )
108 # We only transfer symlinks or files changed more than 14 days ago
109 # (assuming we won't ever find anything on snapshot for them)
110 find . \( -type l -o \( -type f -ctime 14 \) \) -print0 >${LISTFILE}
112 # morgue-sync has to be setup in ~/.ssh/config and the authorized_keys
113 # on the other side should contain (one line, no #)
114 # command="rsync --server -lHogDtpRe.Lsf --remove-source-files . /srv/morgue.debian.org/sync/ftp-master",
115 # no-port-forwarding,no-X11-forwarding,no-agent-forwarding,from="ftp-master.debian.org" ssh-rsa...
116 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/ftp-master
118 # And remove empty subdirs. To remove entire hierarchies we probably should run this
119 # in a loop, but why bother? They'll be gone in a few days then, so meh.
120 find "${PROCESSDIR}" -type d -empty -print0 | xargs --no-run-if-empty -0 rmdir