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}[$$]: $@"
56 SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
60 SCRIPTVARS=/srv/security-master.debian.org/dak/config/debian-security/vars
61 archive=security-master
64 echo "Unknown host $(hostname)" >&2
72 function byebye_lock() {
73 rm -f $lockdir/link_morgue
76 lockfile -l 3600 $lockdir/link_morgue
77 trap byebye_lock ERR EXIT TERM HUP INT QUIT
79 PROCESSDIR="${base}/morgue"
80 FARMBASE="/srv/snapshot.debian.org/farm"
81 FARMURL="http://snapshot.debian.org/file/"
85 log "Processing ${PROCESSDIR}"
86 find ${PROCESSDIR} -type f |
88 # Get the files sha1sum
89 mshasum=$(sha1sum ${mfile})
90 mshasum=${mshasum%% *}
92 # And now get the "levels" of the farm
93 if [[ ${mshasum} =~ ([0-9a-z][0-9a-z])([0-9a-z][0-9a-z]).* ]]; then
94 LVL1=${BASH_REMATCH[1]}
95 LVL2=${BASH_REMATCH[2]}
97 log "Ups, unknown error in regex for ${mfile} (${mshasum})"
101 # See if we have a target
102 if [ "$(hostname -s)" = "stabile" ]; then
103 # If we run on the snapshot host directly just look locally
104 if [ -f "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" ]; then
105 ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
108 # If we run wherever, use curl and the http interface
109 if curl --fail --silent --max-time 120 --head ${FARMURL}/${mshasum} >/dev/null; then
110 # Yes, lets symlink it
111 # Yay for tons of dangling symlinks, but when this is done a rsync
112 # will run and transfer the whole shitload of links over to the morgue host.
113 ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
116 done # for mfile in...
118 # And now, maybe, transfer stuff over to stabile...
119 if [ "$(hostname -s)" != "stabile" ]; then
121 LISTFILE=$(mktemp -p ${TMPDIR} )
123 # We only transfer symlinks or files changed more than 14 days ago
124 # (assuming we won't ever find anything on snapshot for them)
125 find . \( -type l -o \( -type f -ctime 14 \) \) -print0 >${LISTFILE}
127 # morgue-sync has to be setup in ~/.ssh/config and the authorized_keys
128 # on the other side should contain (one line, no #)
129 # command="rsync --server -lHogDtpRe.Lsf --remove-source-files . /srv/morgue.debian.org/sync/ftp-master",
130 # no-port-forwarding,no-X11-forwarding,no-agent-forwarding,from="ftp-master.debian.org" ssh-rsa...
131 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
133 # And remove empty subdirs. To remove entire hierarchies we probably should run this
134 # in a loop, but why bother? They'll be gone in a few days then, so meh.
135 find "${PROCESSDIR}" -type d -empty -print0 | xargs --no-run-if-empty -0 rmdir