]> git.decadent.org.uk Git - dak.git/blob - scripts/debian/link_morgue.sh
* scripts/debian/link_morgue.sh: New script, converting morgue/ into a
[dak.git] / scripts / debian / link_morgue.sh
1 #!/bin/bash
2
3 # No way I try to deal with a crippled sh just for POSIX foo.
4
5 # Copyright (C) 2011 Joerg Jaspert <joerg@debian.org>
6 #
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.
10 #
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.
15 #
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.
19
20 # Homer: Are you saying you're never going to eat any animal again? What
21 #        about bacon?
22 # Lisa: No.
23 # Homer: Ham?
24 # Lisa: No.
25 # Homer: Pork chops?
26 # Lisa: Dad, those all come from the same animal.
27 # Homer: Heh heh heh. Ooh, yeah, right, Lisa. A wonderful, magical animal.
28
29 # Let files inside morgue be symlinks to the snapshot farm
30
31 # exit on errors
32 set -e
33 # make sure to only use defined variables
34 set -u
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)
38 set -E
39
40 # Make sure we start out with a sane umask setting
41 umask 022
42
43 # And use one locale, no matter what the caller has set
44 export LANG=C
45 export LC_ALL=C
46
47 # log something (basically echo it together with a timestamp)
48 # Set $PROGRAM to a string to have it added to the output.
49 function log () {
50         local prefix=${PROGRAM:-$0}
51         echo "$(date +"%b %d %H:%M:%S") $(hostname -s) ${prefix}[$$]: $@"
52 }
53
54 export SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
55 . $SCRIPTVARS
56
57 function byebye_lock() {
58     rm -f $lockdir/link_morgue
59 }
60
61 lockfile -l 3600 $lockdir/link_morgue
62 trap byebye_lock ERR EXIT TERM HUP INT QUIT
63
64 PROCESSDIR="${base}/morgue"
65 FARMBASE="/srv/snapshot.debian.org/farm"
66 FARMURL="http://snapshot.debian.org/file/"
67 PROGRAM="link_morgue"
68
69 cd "${PROCESSDIR}"
70 log "Processing ${PROCESSDIR}"
71 find ${PROCESSDIR} -type f |
72 while read mfile; do
73     # Get the files sha1sum
74     mshasum=$(sha1sum ${mfile})
75     mshasum=${mshasum%% *}
76
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
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}"
91         fi
92     else
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}"
99         fi
100     fi
101 done # for mfile in...
102
103 # And now, maybe, transfer stuff over to stabile...
104 if [ "$(hostname -s)" != "stabile" ]; then
105     cd "${PROCESSDIR}"
106     LISTFILE=$(mktemp -p ${TMPDIR} )
107
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}
111
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
117
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
121 fi