da162159ac51d4935c03228012b0cbb3f1120524
[mirror/dsa-puppet.git] / modules / roles / files / static-mirroring / static-master-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 "                      rsync <stuff>"
35         echo "                      static-master-update-component <component>"
36 }
37
38 one_more_arg() {
39         if [ "$#" -lt 1 ]; then
40                 usage >&2
41                 exit 1
42         fi
43 }
44
45 info() {
46         logger -p daemon.info -t "$MYLOGNAME" "$1"
47 }
48
49 croak() {
50         logger -s -p daemon.warn -t "$MYLOGNAME" "$1"
51         exit 1
52 }
53
54 lock() {
55   local fd="$1"; shift
56   local path="$1"; shift
57   local exclusive="$1"; shift
58
59   eval "exec $fd< '$path'"
60
61   if [ "$exclusive" -gt 0 ]; then
62     locktype="-e"
63   else
64     locktype="-s"
65   fi
66
67   if ! flock "$locktype" "$fd"; then
68     echo >&2 "$0: Cannot acquire lock on $base (flock $locktype failed) - Very bad, we should have waited!"
69     exit 1
70   fi
71 }
72
73 do_rsync() {
74         local remote_host="$1"; shift
75         local args="--server --sender -vlHtrze.iLsf --safe-links ."
76
77         for component in $(awk -v this_host="$(hostname -f)" '$1 == this_host {print $2}' $COMPONENTLIST); do
78           if [ "$*" = "$args $component/-new-/" ] || [ "$*" = "$args ./$component/-new-/" ] ; then
79                   local path="$BASEDIR/$component-current-push"
80                   info "serving $remote_host with $path"
81                   rsync $args "$path/."
82                   return
83           elif [ "$*" = "$args . $component/-live-/" ] || [ "$*" = "$args . ./$component/-live-/" ] ; then
84                   local path="$BASEDIR/$component-current-live"
85                   info "host $remote_host wants $path, acquiring lock"
86                   lock 200 "$path" 0
87                   rsync $args "$path/."
88                   return
89           fi
90         done
91
92         info "NOT allowed for $remote_host: rsync $*"
93         echo >&2 "This rsync command ($@) not allowed."
94         exit 1
95 }
96
97 do_update_component() {
98         local remote_host="$1"; shift
99
100         one_more_arg "$@"
101         component="$1"
102         shift
103
104         hit="$(
105                 awk -v this_host="$(hostname -f)" -v component="$component" -v host="$remote_host" '
106                   $1 == this_host && $2 == component {
107                           if ($3 == host) {
108                                   print $4
109                                   exit
110                           }
111                           split($5,extra,",")
112                           for (i in extra) {
113                                   if (host == extra[i]) {
114                                           printf "%s:%s\n", $3, $4
115                                           exit
116                                   }
117                           }
118                           exit
119                   }' "$COMPONENTLIST"
120                 )"
121         if [ -n "$hit" ]; then
122                 exec static-master-update-component "$component"
123                 echo >&2 "Exec failed"
124                 croak "exec failed"
125         else
126                 info "Not whitelisted: $remote_host update $component"
127                 echo >&2 "Not whitelisted: $remote_host update $component"
128                 exit 1
129         fi
130 }
131
132
133 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
134         usage
135         exit 0
136 fi
137
138 one_more_arg "$@"
139 remote_host="$1"
140 shift
141
142
143 # check/parse remote command line
144 if [ -z "${SSH_ORIGINAL_COMMAND:-}" ] ; then
145         croak "Did not find SSH_ORIGINAL_COMMAND"
146 fi
147 set "dummy" ${SSH_ORIGINAL_COMMAND}
148 shift
149
150 info "host $remote_host called with $*"
151
152 one_more_arg "$@"
153 action="$1"
154 shift
155
156 case "$action" in
157         rsync)
158                 do_rsync "$remote_host" "$@"
159                 ;;
160         static-master-update-component)
161                 do_update_component "$remote_host" "$@"
162                 ;;
163         *)
164                 croak "Invalid operation '$action'"
165                 ;;
166 esac