chiark / gitweb /
Mirroring: Mention in changelog
[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 self=$0
34 distrodir=$1;   shift
35 action=$1;      shift
36 package=$1
37
38 repos=$distrodir/repos
39
40 rsync=(rsync -rltH --safe-links --delete)
41 hooktimeout=30
42 rsynctimeout=900
43 rsyncssh='ssh -o batchmode=yes'
44
45 . $distrodir/mirror-settings
46
47 # contents of $queue
48 # $queue/$package.n     - mirror needed
49 # $queue/$package.a     - being attempted, or attempt failed
50 # $queue/$package.lock  - lock (with-lock-ex)
51 # $queue/$package.err   - stderr from failed (or current) run
52 # $queue/$package.log   - stderr from last successful run
53
54 cd $repos
55 queue=_mirror-queue
56
57 fail () {
58         echo >&2 "dgit-mirror-rsync: $*"; exit 127
59 }
60
61 case "$remoterepos" in
62 *:/*|/*)        ;;
63 '')             fail "remoterepos config not set" ;;
64 *)              fail "remoterepos config does not match *:/* or /*" ;;
65 esac
66
67 actually () {
68         "${rsync[@]}" \
69                 --timeout=$rsynctimeout                         \
70                 -e "$rsyncssh"                                  \
71                 "$repos/$package.git"/.                         \
72                 "$remoterepos/$package.git"
73 }
74
75 reinvoke () {
76         newaction="$1"; shift
77
78         exec                                                    \
79         "$@"                                                    \
80         "$0"    "$distrodir" "reinvoke$newaction" "$package"
81 }
82
83 check-package-mirrorable () {
84         local repo=$repos/$package.git
85         local mode=$(stat -c%a "$repo")
86         case $mode in
87         *5)     return  0       ;;
88         *0)     return  1       ;;
89         *)      echo >&2 "unexpected mode $mode for $repo"; return 1    ;;
90         esac
91 }
92
93 lock-and-process () {
94         check-package-mirrorable || return 0
95         reinvoke -locked with-lock-ex -w "$queue/$package.lock"
96 }
97
98 attempt () {
99         exec >"$queue/$package.err" 2>&1
100         if actually; then
101                 rm "$queue/$package.a"
102                 mv -f "$queue/$package.err" "$queue/$package.log"
103                 rm "$queue/$package.lock"
104         else
105                 cat >&2 "$queue/$package.err"
106                 exit 127
107         fi
108 }
109
110 lock-and-process-baseof-f () {
111         package=${f##*/}
112         package=${package%.*}
113         lock-and-process
114 }
115
116 case "$action" in
117
118 updated-hook)
119         check-package-mirrorable || exit 0
120         touch "$queue/$package.n"
121         reinvoke -timed timeout --foreground $hooktimeout
122         ;;
123
124 reinvoke-timed)
125         (lock-and-process) >/dev/null 2>&1
126         ;;
127
128 mirror)
129         lock-and-process
130         ;;
131
132 reinvoke-locked)
133         touch "$queue/$package.a"
134         rm -f "$queue/$package.n"
135         attempt
136         ;;
137
138 backlog)
139         for f in $queue/*.[na]; do
140                 lock-and-process-baseof-f
141         done
142         ;;
143
144 all)
145         for f in [a-z0-9]*.git; do
146                 lock-and-process-baseof-f
147         done
148         ;;
149
150 setup)
151         test -d "$queue" || mkdir "$queue"
152         ;;
153
154 *)
155         fail "bad action $action"
156         ;;
157
158 esac