]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/roles/files/static-mirroring/static-master-ssh-wrap
9c543bf0f1ec12cc9e6ac3bf7fc517462d4da290
[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
76         local args="--server --sender -vlHtrze.iLsf --safe-links ."
77         if [ "$*" = "$args -new-/" ] || [ "$*" = "$args ./-new-/" ] ; then
78                 local path="$BASEDIR/current-push"
79                 info "serving $remote_host with $path"
80                 rsync $args "$path/."
81         elif [ "$*" = "$args . -live-/" ] || [ "$*" = "$args . ./-live-/" ] ; then
82                 local path="$BASEDIR/current-live"
83                 info "host $remote_host wants $path, acquiring lock"
84                 lock 200 "$path" 0
85                 rsync $args "$path/."
86         else
87                 info "NOT allowed for $remote_host: rsync $*"
88                 echo >&2 "This rsync command ($@) not allowed."
89                 exit 1
90         fi
91 }
92
93 do_update_component() {
94         local remote_host="$1"; shift
95
96         one_more_arg "$@"
97         component="$1"
98         shift
99
100         hit="$(awk -v component="$component" -v host="$remote_host" '$1 == component && $2 == host {print $3; exit}' "$COMPONENTLIST")"
101         if [ -n "$hit" ]; then
102                 exec static-master-update-component "$component"
103                 echo >&2 "Exec failed"
104                 croak "exec failed"
105         else
106                 info "Not whitelisted: $remote_host update $component"
107                 echo >&2 "Not whitelisted: $remote_host update $component"
108                 exit 1
109         fi
110 }
111
112
113 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
114         usage
115         exit 0
116 fi
117
118 one_more_arg "$@"
119 remote_host="$1"
120 shift
121
122
123 # check/parse remote command line
124 if [ -z "${SSH_ORIGINAL_COMMAND:-}" ] ; then
125         croak "Did not find SSH_ORIGINAL_COMMAND"
126 fi
127 set "dummy" ${SSH_ORIGINAL_COMMAND}
128 shift
129
130 info "host $remote_host called with $*"
131
132 one_more_arg "$@"
133 action="$1"
134 shift
135
136 case "$action" in
137         rsync)
138                 do_rsync "$remote_host" "$@"
139                 ;;
140         static-master-update-component)
141                 do_update_component "$remote_host" "$@"
142                 ;;
143         *)
144                 croak "Invalid operation '$action'"
145                 ;;
146 esac