]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/roles/files/static-mirroring/staticsync-ssh-wrap
Also add the merged files/templates
[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="/home/staticsync/static-master"
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 args="--server --sender -vlHtrze.iLsf --safe-links ."
78
79         for component in $(awk -v this_host="$(hostname -f)" '$1 == this_host {print $2}' $COMPONENTLIST); do
80                 if [ "$*" = "$args $component/-new-/" ] || [ "$*" = "$args ./$component/-new-/" ] ; then
81                         local path="$BASEDIR/master/$component-current-push"
82                         info "serving $remote_host with $path"
83                         exec rsync $args "$path/."
84                         croak "Exec failed"
85                 elif [ "$*" = "$args $component/-live-/" ] || [ "$*" = "$args ./$component/-live-/" ] ; then
86                         local path="$BASEDIR/master/$component-current-live"
87                         info "host $remote_host wants $path, acquiring lock"
88                         exec 200< "$path"
89                         if ! flock -s -w 0 200; then
90                         echo >&2 "Cannot acquire shared lock on $path - this should mean an update is already underway anyway."
91                         exit 1
92                         fi
93                         exec rsync $args "$path/."
94                         croak "Exec failed"
95                 fi
96         done
97 }
98
99 do_rsync_on_source() {
100         local remote_host="$1"
101         shift
102
103         local allowed_rsyncs
104         allowed_rsyncs=()
105
106         if [ -e "$COMPONENTLIST" ]; then
107                 for path in $(awk -v host="$(hostname -f)" '$3 == host {print $4}' $COMPONENTLIST); do
108                         allowed_rsyncs+=("--server --sender -lHtrze.iLsf --safe-links . $path/.")
109                 done
110         fi
111         for cmd_idx in ${!allowed_rsyncs[*]}; do
112                 allowed="${allowed_rsyncs[$cmd_idx]}"
113                 if [ "$*" = "$allowed" ]; then
114                         info "Running for host $remote_host: rsync $*"
115                         exec rsync "$@"
116                         croak "Exec failed"
117                 fi
118         done
119 }
120
121 do_rsync() {
122         do_rsync_on_master "$@"
123         do_rsync_on_source "$@"
124
125         info "NOT allowed for $remote_host: rsync $*"
126         echo >&2 "This rsync command ($@) not allowed."
127         exit 1
128 }
129
130 do_update_component() {
131         local remote_host="$1"; shift
132
133         one_more_arg "$@"
134         component="$1"
135         shift
136
137         hit="$(
138                 awk -v this_host="$(hostname -f)" -v component="$component" -v host="$remote_host" '
139                         $1 == this_host && $2 == component {
140                                 if ($3 == host) {
141                                         print $4
142                                         exit
143                                 }
144                                 split($5,extra,",")
145                                 for (i in extra) {
146                                         if (host == extra[i]) {
147                                                 printf "%s:%s\n", $3, $4
148                                                 exit
149                                         }
150                                 }
151                                 exit
152                         }' "$COMPONENTLIST"
153                 )"
154         if [ -n "$hit" ]; then
155                 exec static-master-update-component "$component"
156                 echo >&2 "Exec failed"
157                 croak "exec failed"
158         else
159                 info "Not whitelisted: $remote_host update $component"
160                 echo >&2 "Not whitelisted: $remote_host update $component"
161                 exit 1
162         fi
163 }
164
165
166 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
167         usage
168         exit 0
169 fi
170
171 one_more_arg "$@"
172 remote_host="$1"
173 shift
174
175
176 # check/parse remote command line
177 if [ -z "${SSH_ORIGINAL_COMMAND:-}" ] ; then
178         croak "Did not find SSH_ORIGINAL_COMMAND"
179 fi
180 set "dummy" ${SSH_ORIGINAL_COMMAND}
181 shift
182
183 info "host $remote_host called with $*"
184
185 one_more_arg "$@"
186 action="$1"
187 shift
188
189 case "$action" in
190         # on a static mirror, update a component from its master
191         mirror)
192                 do_mirror "$remote_host" "$@"
193                 ;;
194         # on a static source, allow fetching from the master,
195         # on a master, allow fetching from a component's mirrors
196         rsync)
197                 do_rsync "$remote_host" "$@"
198                 ;;
199         # on a master, initiate an update of a component
200         static-master-update-component)
201                 do_update_component "$remote_host" "$@"
202                 ;;
203         *)
204                 croak "Invalid operation '$action'"
205                 ;;
206 esac