]> git.decadent.org.uk Git - dak.git/blob - scripts/debian/link_morgue.sh
dont die if you cant touch a file
[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     if [[ -f ${mfile}.nosnapshot ]]; then
89         # We know this file does not exist on snapshot, don't check again
90         continue
91     fi
92
93     # Get the files sha1sum
94     mshasum=$(sha1sum ${mfile})
95     mshasum=${mshasum%% *}
96
97     # And now get the "levels" of the farm
98     if [[ ${mshasum} =~ ([0-9a-z][0-9a-z])([0-9a-z][0-9a-z]).* ]]; then
99         LVL1=${BASH_REMATCH[1]}
100         LVL2=${BASH_REMATCH[2]}
101     else
102         log "Ups, unknown error in regex for ${mfile} (${mshasum})"
103         continue
104     fi
105
106     # See if we have a target
107     if [ "$(hostname -s)" = "stabile" ]; then
108         # If we run on the snapshot host directly just look locally
109         if [ -f "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" ]; then
110             ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
111         fi
112     else
113         # If we run wherever, use curl and the http interface
114         if curl --fail --silent --max-time 120 --head ${FARMURL}/${mshasum} >/dev/null; then
115             # Yes, lets symlink it
116             # Yay for tons of dangling symlinks, but when this is done a rsync
117             # will run and transfer the whole shitload of links over to the morgue host.
118             ln -sf "${FARMBASE}/${LVL1}/${LVL2}/${mshasum}" "${mfile}"
119         else
120             touch "${mfile}.nosnapshot" || true
121         fi
122     fi
123 done # for mfile in...
124
125 # And now, maybe, transfer stuff over to stabile...
126 if [ "$(hostname -s)" != "stabile" ]; then
127     cd "${PROCESSDIR}"
128     LISTFILE=$(mktemp -p ${TMPDIR} )
129
130     # We only transfer symlinks or files changed more than 14 days ago
131     # (assuming we won't ever find anything on snapshot for them)
132     find . \( -type l -o \( -type f -ctime 14 \) \) -print0 >${LISTFILE}
133
134     # morgue-sync has to be setup in ~/.ssh/config and the authorized_keys
135     # on the other side should contain (one line, no #)
136 # command="rsync --server -lHogDtpRe.Lsf --remove-source-files . /srv/morgue.debian.org/sync/ftp-master",
137 # no-port-forwarding,no-X11-forwarding,no-agent-forwarding,from="ftp-master.debian.org" ssh-rsa...
138     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
139
140     # And remove empty subdirs. To remove entire hierarchies we probably should run this
141     # in a loop, but why bother? They'll be gone in a few days then, so meh.
142     find "${PROCESSDIR}" -type d -empty -print0 | xargs --no-run-if-empty -0 rmdir
143 fi