move things from modules/roles/static* to modules/static*
[mirror/dsa-puppet.git] / modules / staticsync / files / static-master-update-component
1 #!/bin/bash
2
3 # Updates one component (i.e. subdirectory) in static-master/master
4
5 # acquires a shared lock on the base directory (so that we know no updates are
6 # outgoing, as those acquire an exclusive one).  Also acquired an exclusive lock
7 # on the component directory in question.
8 #
9 # The config file is a list of component source-directory pairs.
10
11 # Copyright (c) 2012 Peter Palfrader
12 #
13 # Permission is hereby granted, free of charge, to any person obtaining
14 # a copy of this software and associated documentation files (the
15 # "Software"), to deal in the Software without restriction, including
16 # without limitation the rights to use, copy, modify, merge, publish,
17 # distribute, sublicense, and/or sell copies of the Software, and to
18 # permit persons to whom the Software is furnished to do so, subject to
19 # the following conditions:
20 #
21 # The above copyright notice and this permission notice shall be
22 # included in all copies or substantial portions of the Software.
23 #
24 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
32 componentlist=/etc/static-components.conf
33 . /etc/staticsync.conf
34 if ! [ -n "$masterbase" ]; then
35   echo >&2 "masterbase not configured!"
36   exit 1
37 fi
38
39 set -e
40 set -u
41
42 if [ "`id -u`" != "`stat -c %u "$masterbase"`" ]; then
43   echo >&2 "You are probably running this as the wrong user."
44   exit 1
45 fi
46
47 lock() {
48   local fd="$1"; shift
49   local path="$1"; shift
50   local exclusive="$1"; shift
51
52   eval "exec $fd< '$path'"
53
54   if [ "$exclusive" -gt 0 ]; then
55     locktype="-e"
56   else
57     locktype="-s"
58   fi
59
60   if ! flock "$locktype" "$fd"; then
61     echo >&2 "$0: Cannot acquire lock on $path (flock $locktype failed) - Very bad, we should have waited!"
62     exit 1
63   fi
64 }
65
66 unlock() {
67   local fd="$1"; shift
68
69   if ! flock -o "$fd"; then
70     echo >&2 "$0: Cannot release lock on fd $fd - This should not have happened!"
71     exit 1
72   fi
73   eval "exec $fd<&-"
74 }
75
76 if [ "$#" != 1 ]; then
77   echo >&2 "Usage: $0 <component>"
78   exit 1
79 fi
80
81 component="$1"
82
83 if [ "${component%/*}" != "$component" ] ; then
84   echo >&2 "$0: Invalid component: $component";
85   exit 1
86 fi
87
88 srchost="$(awk -v this_host="$(hostname -f)" -v component="$component" '!/^ *(#|$)/ && $1 == this_host && $2 == component {print $3; exit}' "$componentlist")"
89 srcdir="$(awk -v this_host="$(hostname -f)" -v component="$component" '!/^ *(#|$)/ && $1 == this_host && $2 == component {print $4; exit}' "$componentlist")"
90 if [ -z "$srchost" ] || [ -z "$srcdir" ]; then
91   echo >&2 "$0: Invalid component: $component (not found in $componentlist)";
92   exit 1
93 fi
94
95 tgtlock="$masterbase/$component.lock"
96 if ! [ -e "$tgtlock" ]; then
97   touch "$tgtlock"
98 fi
99 echo "$0: Acquiring lock on $tgtlock..."
100 lock 203 "$tgtlock" 1
101
102 tgt="$masterbase/$component"
103 if ! [ -d "$tgt" ]; then
104   echo "$0: Creating $tgt for $component";
105   mkdir "$tgt"
106 fi
107
108 if [ "$srchost" = "`hostname -f`" ]; then
109   src="$srcdir"
110 else
111   src="$srchost:$srcdir"
112 fi
113
114 #echo "$0: Acquiring lock on $tgt..."
115 #lock 201 "$tgt" 1
116
117 tmpdir_new="$(mktemp -d --tmpdir="$masterbase" "${component}-updating.incoming-XXXXXX")"
118 tmpdir_old="$(mktemp -d --tmpdir="$masterbase" "${component}-updating.removing-XXXXXX")"
119 trap "rm -rf '$tmpdir_new' '$tmpdir_old'" EXIT
120 chmod 0755 "$tmpdir_new"
121
122 #echo "$0: Acquiring lock on $tmpdir_new..."
123 #lock 202 "$tmpdir_new" 1
124 echo "$0: Got them."
125
126 echo "$0: Updating master copy of $component..."
127 rsync --delete \
128   -trz \
129   --links --hard-links --safe-links \
130   --link-dest="$tgt" \
131   --exclude='/.serial' \
132   "$src/." "$tmpdir_new/."
133 echo "$0: Done.  Committing."
134
135 mv --no-target-directory "$tgt" "$tmpdir_old/old"
136 if ! mv --no-target-directory "$tmpdir_new" "$tgt"; then
137   echo >&2 "$0: WARNING: could not move $tmpdir_new to $tgt.  Trying to recover"
138   rm -rf "$tgt"
139   mv --no-target-directory "$tmpdir_old/old" "$tgt"
140   echo >&2 "$0: Rolled back to old tree, maybe even successfully."
141   exit 1
142 fi
143
144 rm -rf "$tmpdir_new" "$tmpdir_old"
145 trap - EXIT
146
147 date '+%s' > "$tgt/.serial"
148 #unlock 201
149 #unlock 202
150 unlock 203
151 echo "$0: Triggering mirror runs..."
152 exec static-master-run "$component"
153
154 # vim:set et:
155 # vim:set ts=2:
156 # vim:set shiftwidth=2: