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