chiark / gitweb /
47142ee296beecf4eb93c57e7cf92687dc11f01a
[dgit.git] / infra / dgit-mirror-rsync
1 #!/bin/bash
2 #
3 # Mirror script for use as a dgit-repos-server mirror hook
4 #
5 # In addition to updated-hook (invoked by dgit-repos-server),
6 # this script also supports the following ACTIONs:
7 #   MIRROR-HOOK-SCRIPT ... setup [...]            create queue dir etc.
8 #   MIRROR-HOOK-SCRIPT ... backlog [...]          do all packages which need it
9 #   MIRROR-HOOK-SCRIPT ... all [...]              do all packages
10 #   MIRROR-HOOK-SCRIPT ... mirror PACKAGE [...]   do just that, longer timeout
11 #
12 # DISTRO-DIR must contain a file `mirror-settings' which is a bash
13 # script fragment assigning the following variables:
14 #   remoterepos         for rsync, in form user@host:/dir
15 # and optionally
16 #   hooktimeout         default 30 [sec]
17 #   rsynctimeout        default 900 [sec]
18 #   rsyncssh            default 'ssh -o batchmode=yes'
19 #   rsync               array, default (rsync -rltH --safe-links --delete)
20 #   repos               default DISTRO-DIR/repos
21 # (optional settings are all set before mirror-settings is included,
22 # so you can modify them with += or some such)
23
24 set -e
25 set -o pipefail
26 shopt -s nullglob
27
28 case "$DGIT_DRS_DEBUG" in
29 ''|0|1)         ;;
30 *)              set -x  ;;
31 esac
32
33 fail () {
34         echo >&2 "dgit-mirror-rsync: $*"; exit 127
35 }
36
37 if [ $# -lt 2 ]; then fail "too few arguments"; fi
38
39 self=$0
40
41 case "$self" in
42 /*)                             ;;
43 */*)    self="$PWD/$self"       ;;
44 *)                              ;;
45 esac
46
47 distrodir=$1;   shift
48 action=$1;      shift
49 package=$1
50
51 repos=$distrodir/repos
52
53 rsync=(rsync -rltH --safe-links --delete)
54 hooktimeout=30
55 rsynctimeout=900
56 rsyncssh='ssh -o batchmode=yes'
57
58 . $distrodir/mirror-settings
59
60 # contents of $queue
61 # $queue/$package.n     - mirror needed
62 # $queue/$package.a     - being attempted, or attempt failed
63 # $queue/$package.lock  - lock (with-lock-ex)
64 # $queue/$package.err   - stderr from failed (or current) run
65 # $queue/$package.log   - stderr from last successful run
66
67 cd $repos
68 queue=_mirror-queue
69
70 case "$remoterepos" in
71 *:/*|/*)        ;;
72 '')             fail "remoterepos config not set" ;;
73 *)              fail "remoterepos config does not match *:/* or /*" ;;
74 esac
75
76 actually () {
77         "${rsync[@]}" \
78                 --timeout=$rsynctimeout                         \
79                 -e "$rsyncssh"                                  \
80                 "$repos/$package.git"/.                         \
81                 "$remoterepos/$package.git"
82 }
83
84 reinvoke () {
85         newaction="$1"; shift
86
87         exec                                                    \
88         "$@"                                                    \
89         "$self" "$distrodir" "reinvoke$newaction" "$package"
90 }
91
92 check-package-mirrorable () {
93         local repo=$repos/$package.git
94         local mode=$(stat -c%a "$repo")
95         case $mode in
96         *5)     return  0       ;;
97         *0)     return  1       ;;
98         *)      echo >&2 "unexpected mode $mode for $repo"; return 1    ;;
99         esac
100 }
101
102 lock-and-process () {
103         check-package-mirrorable || return 0
104         reinvoke -locked with-lock-ex -w "$queue/$package.lock"
105 }
106
107 attempt () {
108         exec 3>&2 >"$queue/$package.err" 2>&1
109         if actually; then
110                 rm -f "$queue/$package.a"
111                 mv -f "$queue/$package.err" "$queue/$package.log"
112                 rm "$queue/$package.lock"
113         else
114                 cat >&3 "$queue/$package.err"
115                 exit 127
116         fi
117 }
118
119 lock-and-process-baseof-f () {
120         package=${f##*/}
121         package=${package%.*}
122         lock-and-process
123 }
124
125 case "$action" in
126
127 updated-hook)
128         check-package-mirrorable || exit 0
129         touch "$queue/$package.n"
130         reinvoke -timed timeout --foreground $hooktimeout
131         ;;
132
133 reinvoke-timed)
134         (lock-and-process) >/dev/null 2>&1
135         ;;
136
137 mirror)
138         lock-and-process
139         ;;
140
141 reinvoke-locked)
142         touch "$queue/$package.a"
143         rm -f "$queue/$package.n"
144         attempt
145         ;;
146
147 backlog)
148         for f in $queue/*.[na]; do
149                 (lock-and-process-baseof-f ||:)
150         done
151         ;;
152
153 all)
154         for f in [a-z0-9]*.git; do
155                 (lock-and-process-baseof-f)
156         done
157         ;;
158
159 setup)
160         test -d "$queue" || mkdir "$queue"
161         ;;
162
163 *)
164         fail "bad action $action"
165         ;;
166
167 esac