Mess with rsync options
[mirror/dsa-puppet.git] / modules / roles / files / static-mirroring / 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 base=/home/staticsync/static-master/master
34
35 set -e
36 set -u
37
38
39 lock() {
40   local fd="$1"; shift
41   local path="$1"; shift
42   local exclusive="$1"; shift
43
44   eval "exec $fd< '$path'"
45
46   if [ "$exclusive" -gt 0 ]; then
47     locktype="-e"
48   else
49     locktype="-s"
50   fi
51
52   if ! flock "$locktype" "$fd"; then
53     echo >&2 "$0: Cannot acquire lock on $base (flock $locktype failed) - Very bad, we should have waited!"
54     exit 1
55   fi
56 }
57
58 unlock() {
59   local fd="$1"; shift
60
61   if ! flock -o "$fd"; then
62     echo >&2 "$0: Cannot release lock on fd $fd - This should not have happened!"
63     exit 1
64   fi
65   eval "exec $fd<&-"
66 }
67
68 if [ "$#" != 1 ]; then
69   echo >&2 "Usage: $0 <component>"
70   exit 1
71 fi
72
73 component="$1"
74
75 if [ "${component%/*}" != "$component" ] ; then
76   echo >&2 "$0: Invalid component: $component";
77   exit 1
78 fi
79
80 srchost="$(awk -v component="$component" '$1 == component {print $2; exit}' "$componentlist")"
81 srcdir="$(awk -v component="$component" '$1 == component {print $3; exit}' "$componentlist")"
82 if [ -z "$srchost" ] || [ -z "$srcdir" ]; then
83   echo >&2 "$0: Invalid component: $component (not found in $componentlist)";
84   exit 1
85 fi
86 tgt="$base/$component"
87 if ! [ -d "$tgt" ]; then
88   echo "$0: Creating $tgt for $component";
89   mkdir "$tgt"
90 fi
91
92 if [ "$srchost" = "`hostname -f`" ]; then
93   src="$srcdir"
94 else
95   src="$srchost:$srcdir"
96 fi
97
98 echo "$0: Acquiring locks..."
99 lock 200 "$base" 0
100 lock 201 "$tgt" 1
101
102 tmpdir_new="$(mktemp -d --tmpdir="$base" "${component}.new-XXXXXX")"
103 tmpdir_old="$(mktemp -d --tmpdir="$base" "${component}.old-XXXXXX")"
104 trap "rm -rf '$tmpdir_new' '$tmpdir_old'" EXIT
105 chmod 0755 "$tmpdir_new"
106
107 lock 202 "$tmpdir_new" 1
108 echo "$0: Got them."
109
110 echo "$0: Updating master copy of $component..."
111 rsync --delete \
112   -trz \
113   --links --safe-links \
114   --link-dest="$tgt" \
115   "$src/." "$tmpdir_new/."
116 echo "$0: Done.  Committing."
117
118 mv "$tgt" "$tmpdir_old/old"
119 if ! mv "$tmpdir_new" "$tgt"; then
120   echo >&2 "$0: WARNING: could not move $tmpdir_new to $tgt.  Trying to recover"
121   rm -rf "$tgt"
122   mv "$tmpdir_old/old" "$tgt"
123   echo >&2 "$0: Rolled back to old tree maybe successfully."
124   exit 1
125 fi
126
127 rm -rf "$tmpdir_new" "$tmpdir_old"
128 trap - EXIT
129
130 date '+%s' > "$base/.serial"
131 unlock 201
132 unlock 200
133 echo "$0: Triggering mirror runs..."
134 exec static-master-run
135
136 # vim:set et:
137 # vim:set ts=2:
138 # vim:set shiftwidth=2: