]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/roles/files/static-mirroring/staticsync-ssh-wrap
Add timeline.debian.net to the static CDN
[dsa-puppet.git] / modules / roles / files / static-mirroring / staticsync-ssh-wrap
1 #!/bin/bash
2
3 # Copyright (c) 2009, 2010, 2012 Peter Palfrader
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 set -e
25 set -u
26
27 MYLOGNAME="`basename "$0"`[$$]"
28 BASEDIR="/srv/static.debian.org"
29 COMPONENTLIST=/etc/static-components.conf
30
31 usage() {
32         echo "local Usage: $0 <host>"
33         echo "via ssh orig command:"
34         echo "                      mirror <component> <serial>"
35         echo "                      rsync <stuff>"
36         echo "                      static-master-update-component <component>"
37 }
38
39 one_more_arg() {
40         if [ "$#" -lt 1 ]; then
41                 usage >&2
42                 exit 1
43         fi
44 }
45
46 info() {
47         logger -p daemon.info -t "$MYLOGNAME" "$1"
48 }
49
50 croak() {
51         logger -s -p daemon.warn -t "$MYLOGNAME" "$1"
52         exit 1
53 }
54
55 do_mirror() {
56         local remote_host="$1"; shift
57         one_more_arg "$@"
58         local component="$1"; shift
59         one_more_arg "$@"
60         local serial="$1"; shift
61
62         masterhost="$(awk -v component="$component" '!/^ *(#|$)/ && $2 == component {print $1; exit}' "$COMPONENTLIST")"
63         if [ -z "$masterhost" ]; then
64                 croak "Did not find master for component $component."
65         elif [ "$masterhost" != "$remote_host" ]; then
66                 croak "$remote_host is not master for $component."
67         else
68                 info "Host $remote_host triggered a mirror run for $component, serial $serial"
69                 exec /usr/local/bin/static-mirror-run "$BASEDIR/mirrors/$component" "$remote_host:$component/-new-" "$serial"
70                 echo >&2 "Exec failed"
71                 croak "exec failed"
72         fi
73 }
74
75 do_rsync_on_master() {
76         local remote_host="$1"; shift
77         local allowed_rsyncs
78         allowed_rsyncs=()
79         allowed_rsyncs+=("--server --sender -vlHtrze.iLsf --safe-links .") # wheezy
80         allowed_rsyncs+=("--server --sender -vlHtrze.iLsfx --safe-links .") # jessie
81
82         for cmd_idx in ${!allowed_rsyncs[*]}; do
83                 args="${allowed_rsyncs[$cmd_idx]}"
84                 for component in $(awk -v this_host="$(hostname -f)" '!/^ *(#|$)/ && $1 == this_host {print $2}' $COMPONENTLIST); do
85                         if [ "$*" = "$args $component/-new-/" ] || [ "$*" = "$args ./$component/-new-/" ] ; then
86                                 local path="$BASEDIR/master/$component-current-push"
87                                 info "serving $remote_host with $path"
88                                 exec rsync $args "$path/."
89                                 croak "Exec failed"
90                         elif [ "$*" = "$args $component/-live-/" ] || [ "$*" = "$args ./$component/-live-/" ] ; then
91                                 local path="$BASEDIR/master/$component-current-live"
92                                 info "host $remote_host wants $path, acquiring lock"
93                                 exec 200< "$path"
94                                 if ! flock -s -w 0 200; then
95                                 echo >&2 "Cannot acquire shared lock on $path - this should mean an update is already underway anyway."
96                                 exit 1
97                                 fi
98                                 exec rsync $args "$path/."
99                                 croak "Exec failed"
100                         fi
101                 done
102         done
103 }
104
105 do_rsync_on_source() {
106         local remote_host="$1"
107         shift
108
109         local allowed_rsyncs
110         allowed_rsyncs=()
111
112         if [ -e "$COMPONENTLIST" ]; then
113                 for path in $(awk -v host="$(hostname -f)" '!/^ *(#|$)/ && $3 == host {print $4}' $COMPONENTLIST); do
114                         allowed_rsyncs+=("--server --sender -lHtrze.iLsf --safe-links . $path/.") # wheezy
115                         allowed_rsyncs+=("--server --sender -lHtrze.iLsfx --safe-links . $path/.") # jessie
116                 done
117         fi
118         for cmd_idx in ${!allowed_rsyncs[*]}; do
119                 allowed="${allowed_rsyncs[$cmd_idx]}"
120                 if [ "$*" = "$allowed" ]; then
121                         info "Running for host $remote_host: rsync $*"
122                         exec rsync "$@"
123                         croak "Exec failed"
124                 fi
125         done
126 }
127
128 do_rsync() {
129         do_rsync_on_master "$@"
130         do_rsync_on_source "$@"
131
132         info "NOT allowed for $remote_host: rsync $*"
133         echo >&2 "This rsync command ($@) not allowed."
134         exit 1
135 }
136
137 do_update_component() {
138         local remote_host="$1"; shift
139
140         one_more_arg "$@"
141         component="$1"
142         shift
143
144         hit="$(
145                 awk -v this_host="$(hostname -f)" -v component="$component" -v host="$remote_host" '
146                         !/^ *(#|$)/ && $1 == this_host && $2 == component {
147                                 if ($3 == host) {
148                                         print $4
149                                         exit
150                                 }
151                                 split($5,extra,",")
152                                 for (i in extra) {
153                                         if (host == extra[i]) {
154                                                 printf "%s:%s\n", $3, $4
155                                                 exit
156                                         }
157                                 }
158                                 exit
159                         }' "$COMPONENTLIST"
160                 )"
161         if [ -n "$hit" ]; then
162                 exec static-master-update-component "$component"
163                 echo >&2 "Exec failed"
164                 croak "exec failed"
165         else
166                 info "Not whitelisted: $remote_host update $component"
167                 echo >&2 "Not whitelisted: $remote_host update $component"
168                 exit 1
169         fi
170 }
171
172
173 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
174         usage
175         exit 0
176 fi
177
178 one_more_arg "$@"
179 remote_host="$1"
180 shift
181
182
183 # check/parse remote command line
184 if [ -z "${SSH_ORIGINAL_COMMAND:-}" ] ; then
185         croak "Did not find SSH_ORIGINAL_COMMAND"
186 fi
187 set "dummy" ${SSH_ORIGINAL_COMMAND}
188 shift
189
190 info "host $remote_host called with $*"
191
192 one_more_arg "$@"
193 action="$1"
194 shift
195
196 case "$action" in
197         # on a static mirror, update a component from its master
198         mirror)
199                 do_mirror "$remote_host" "$@"
200                 ;;
201         # on a static source, allow fetching from the master,
202         # on a master, allow fetching from a component's mirrors
203         rsync)
204                 do_rsync "$remote_host" "$@"
205                 ;;
206         # on a master, initiate an update of a component
207         static-master-update-component)
208                 do_update_component "$remote_host" "$@"
209                 ;;
210         *)
211                 croak "Invalid operation '$action'"
212                 ;;
213 esac