]> git.decadent.org.uk Git - dak.git/blob - scripts/debian/link_morgue.sh
83d4aeb213345f5e1fb66fa45becef7e87738209
[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 case "$(hostname)" in
55     franck)
56         SCRIPTVARS=/srv/ftp-master.debian.org/dak/config/debian/vars
57         archive=ftp-master
58         ;;
59     seger)
60         SCRIPTVARS=/srv/security-master.debian.org/dak/config/debian-security/vars
61         archive=security-master
62         ;;
63     *)
64         echo "Unknown host $(hostname)" >&2
65         exit 1
66         ;;
67 esac
68
69 export SCRIPTVARS
70 . $SCRIPTVARS
71
72 function byebye_lock() {
73     rm -f $lockdir/link_morgue
74 }
75
76 lockfile -l 3600 $lockdir/link_morgue
77 trap byebye_lock ERR EXIT TERM HUP INT QUIT
78
79 PROCESSDIR="${base}/morgue"
80 FARMBASE="/srv/snapshot.debian.org/farm"
81 FARMURL="http://snapshot.debian.org/file/"
82 PROGRAM="link_morgue"
83
84 cd "${PROCESSDIR}"
85 log "Processing ${PROCESSDIR}"
86 find ${PROCESSDIR} -type f |
87 while read mfile; do
88     # Get the files sha1sum
89     mshasum=$(sha1sum ${mfile})
90     mshasum=${mshasum%% *}
91
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]}
96     else
97         log "Ups, unknown error in regex for ${mfile} (${mshasum})"
98         continue
99     fi
100
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}"
106         fi
107     else
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}"
114         fi
115     fi
116 done # for mfile in...
117
118 # And now, maybe, transfer stuff over to stabile...
119 if [ "$(hostname -s)" != "stabile" ]; then
120     cd "${PROCESSDIR}"
121     LISTFILE=$(mktemp -p ${TMPDIR} )
122
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}
126
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
132
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
136 fi