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