staticsync: let's assume that IPv6 is not worse than IPv4
[mirror/dsa-puppet.git] / modules / staticsync / files / static-update-component
1 #!/bin/bash
2
3 # Copyright (c) 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 usage() {
25   echo >&2 "Usage: $0 <component>"
26   exit 1
27 }
28
29 componentlist=/etc/static-components.conf
30 . /etc/staticsync.conf
31 if ! [ -n "$staticuser" ]; then
32   echo >&2 "staticuser not configured!"
33   exit 1
34 fi
35
36 if [ "$#" = 1 ]; then
37   component="$1"
38 else
39   usage
40 fi
41
42
43 if [ "${component%/*}" != "$component" ] ; then
44   echo >&2 "$0: Invalid component: $component";
45   exit 1
46 fi
47
48 thishost=$(hostname -f)
49 masterhost="$(awk -v component="$component" '!/^ *(#|$)/ && $2 == component {print $1; exit}' "$componentlist")"
50 srchost="$(awk -v component="$component" '!/^ *(#|$)/ && $2 == component {print $3; exit}' "$componentlist")"
51 srcdir="$(awk -v component="$component" '!/^ *(#|$)/ && $2 == component {print $4; exit}' "$componentlist")"
52 inextralist="$(
53                awk -v component="$component" -v host="$thishost" '
54                  !/^ *(#|$)/ && $2 == component {
55                    split($5,extra,",")
56                    for (i in extra) {
57                      if (host == extra[i]) {
58                        printf "%s:%s\n", $3, $4
59                        exit
60                      }
61                    }
62                    exit
63                  }' "$componentlist"
64               )"
65 if [ -z "$srchost" ] || [ -z "$srcdir" ]; then
66   echo >&2 "$0: Invalid component: $component (not found in $componentlist)";
67   exit 1
68 fi
69
70 if ! [ "$srchost" = "$thishost" ] && [ -z "$inextralist" ]; then
71   echo >&2 "Component $component is sourced from $srchost, and this host is neither that nor in the extra allowed list."
72   exit 1
73 fi
74
75 if [ "$srchost" = "$thishost" ] && ! [ -d "$srcdir" ]; then
76   echo >&2 "Component source directory $srcdir does not exist or is not a directory, or is not accessible."
77   exit 1
78 fi
79
80 if [ "`id -nu`" != "$staticuser" ]; then
81   sudo -u "$staticuser" static-update-component "$@"
82 else
83   ssh -t -t -o ServerAliveInterval=300 -o PreferredAuthentications=publickey "$masterhost" static-master-update-component "$component"
84 fi
85
86 # vim:set et:
87 # vim:set ts=2:
88 # vim:set shiftwidth=2:
89 # vim:set syn=sh: