3 # Copyright (c) 2009, 2010, 2012 Peter Palfrader
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:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
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.
27 MYLOGNAME="`basename "$0"`[$$]"
28 BASEDIR="/srv/static.debian.org"
29 COMPONENTLIST=/etc/static-components.conf
32 echo "local Usage: $0 <host>"
33 echo "via ssh orig command:"
34 echo " mirror <component> <serial>"
36 echo " static-master-update-component <component>"
40 if [ "$#" -lt 1 ]; then
47 logger -p daemon.info -t "$MYLOGNAME" "$1"
51 logger -s -p daemon.warn -t "$MYLOGNAME" "$1"
56 local remote_host="$1"; shift
58 local component="$1"; shift
60 local serial="$1"; shift
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."
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"
75 do_rsync_on_master() {
76 local remote_host="$1"; shift
79 allowed_rsyncs+=("--server --sender -vlHtrze.iLsf --safe-links .") # wheezy
80 allowed_rsyncs+=("--server --sender -vlHtrze.iLsfx --safe-links .") # jessie
81 allowed_rsyncs+=("--server --sender -vlHtrze.iLsfxC --safe-links .") # stretch
83 for cmd_idx in ${!allowed_rsyncs[*]}; do
84 args="${allowed_rsyncs[$cmd_idx]}"
85 for component in $(awk -v this_host="$(hostname -f)" '!/^ *(#|$)/ && $1 == this_host {print $2}' $COMPONENTLIST); do
86 if [ "$*" = "$args $component/-new-/" ] || [ "$*" = "$args ./$component/-new-/" ] ; then
87 local path="$BASEDIR/master/$component-current-push"
88 info "serving $remote_host with $path"
89 exec rsync $args "$path/."
91 elif [ "$*" = "$args $component/-live-/" ] || [ "$*" = "$args ./$component/-live-/" ] ; then
92 local path="$BASEDIR/master/$component-current-live"
93 info "host $remote_host wants $path, acquiring lock"
95 if ! flock -s -w 0 200; then
96 echo >&2 "Cannot acquire shared lock on $path - this should mean an update is already underway anyway."
99 exec rsync $args "$path/."
106 do_rsync_on_source() {
107 local remote_host="$1"
113 if [ -e "$COMPONENTLIST" ]; then
114 for path in $(awk -v host="$(hostname -f)" '!/^ *(#|$)/ && $3 == host {print $4}' $COMPONENTLIST); do
115 allowed_rsyncs+=("--server --sender -lHtrze.iLsf --safe-links . $path/.") # wheezy
116 allowed_rsyncs+=("--server --sender -lHtrze.iLsfx --safe-links . $path/.") # jessie
117 allowed_rsyncs+=("--server --sender -lHtrze.iLsfxC --safe-links . $path/.") # stretch
120 for cmd_idx in ${!allowed_rsyncs[*]}; do
121 allowed="${allowed_rsyncs[$cmd_idx]}"
122 if [ "$*" = "$allowed" ]; then
123 info "Running for host $remote_host: rsync $*"
131 do_rsync_on_master "$@"
132 do_rsync_on_source "$@"
134 info "NOT allowed for $remote_host: rsync $*"
135 echo >&2 "This rsync command ($@) not allowed."
139 do_update_component() {
140 local remote_host="$1"; shift
147 awk -v this_host="$(hostname -f)" -v component="$component" -v host="$remote_host" '
148 !/^ *(#|$)/ && $1 == this_host && $2 == component {
155 if (host == extra[i]) {
156 printf "%s:%s\n", $3, $4
163 if [ -n "$hit" ]; then
164 exec static-master-update-component "$component"
165 echo >&2 "Exec failed"
168 info "Not whitelisted: $remote_host update $component"
169 echo >&2 "Not whitelisted: $remote_host update $component"
175 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
185 # check/parse remote command line
186 if [ -z "${SSH_ORIGINAL_COMMAND:-}" ] ; then
187 croak "Did not find SSH_ORIGINAL_COMMAND"
189 set "dummy" ${SSH_ORIGINAL_COMMAND}
192 info "host $remote_host called with $*"
199 # on a static mirror, update a component from its master
201 do_mirror "$remote_host" "$@"
203 # on a static source, allow fetching from the master,
204 # on a master, allow fetching from a component's mirrors
206 do_rsync "$remote_host" "$@"
208 # on a master, initiate an update of a component
209 static-master-update-component)
210 do_update_component "$remote_host" "$@"
213 croak "Invalid operation '$action'"