move things from modules/roles/static* to modules/static*
[mirror/dsa-puppet.git] / modules / staticsync / files / static-master-update-component
diff --git a/modules/staticsync/files/static-master-update-component b/modules/staticsync/files/static-master-update-component
new file mode 100755 (executable)
index 0000000..4ab5143
--- /dev/null
@@ -0,0 +1,156 @@
+#!/bin/bash
+
+# Updates one component (i.e. subdirectory) in static-master/master
+
+# acquires a shared lock on the base directory (so that we know no updates are
+# outgoing, as those acquire an exclusive one).  Also acquired an exclusive lock
+# on the component directory in question.
+#
+# The config file is a list of component source-directory pairs.
+
+# Copyright (c) 2012 Peter Palfrader
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be
+# included in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+componentlist=/etc/static-components.conf
+. /etc/staticsync.conf
+if ! [ -n "$masterbase" ]; then
+  echo >&2 "masterbase not configured!"
+  exit 1
+fi
+
+set -e
+set -u
+
+if [ "`id -u`" != "`stat -c %u "$masterbase"`" ]; then
+  echo >&2 "You are probably running this as the wrong user."
+  exit 1
+fi
+
+lock() {
+  local fd="$1"; shift
+  local path="$1"; shift
+  local exclusive="$1"; shift
+
+  eval "exec $fd< '$path'"
+
+  if [ "$exclusive" -gt 0 ]; then
+    locktype="-e"
+  else
+    locktype="-s"
+  fi
+
+  if ! flock "$locktype" "$fd"; then
+    echo >&2 "$0: Cannot acquire lock on $path (flock $locktype failed) - Very bad, we should have waited!"
+    exit 1
+  fi
+}
+
+unlock() {
+  local fd="$1"; shift
+
+  if ! flock -o "$fd"; then
+    echo >&2 "$0: Cannot release lock on fd $fd - This should not have happened!"
+    exit 1
+  fi
+  eval "exec $fd<&-"
+}
+
+if [ "$#" != 1 ]; then
+  echo >&2 "Usage: $0 <component>"
+  exit 1
+fi
+
+component="$1"
+
+if [ "${component%/*}" != "$component" ] ; then
+  echo >&2 "$0: Invalid component: $component";
+  exit 1
+fi
+
+srchost="$(awk -v this_host="$(hostname -f)" -v component="$component" '!/^ *(#|$)/ && $1 == this_host && $2 == component {print $3; exit}' "$componentlist")"
+srcdir="$(awk -v this_host="$(hostname -f)" -v component="$component" '!/^ *(#|$)/ && $1 == this_host && $2 == component {print $4; exit}' "$componentlist")"
+if [ -z "$srchost" ] || [ -z "$srcdir" ]; then
+  echo >&2 "$0: Invalid component: $component (not found in $componentlist)";
+  exit 1
+fi
+
+tgtlock="$masterbase/$component.lock"
+if ! [ -e "$tgtlock" ]; then
+  touch "$tgtlock"
+fi
+echo "$0: Acquiring lock on $tgtlock..."
+lock 203 "$tgtlock" 1
+
+tgt="$masterbase/$component"
+if ! [ -d "$tgt" ]; then
+  echo "$0: Creating $tgt for $component";
+  mkdir "$tgt"
+fi
+
+if [ "$srchost" = "`hostname -f`" ]; then
+  src="$srcdir"
+else
+  src="$srchost:$srcdir"
+fi
+
+#echo "$0: Acquiring lock on $tgt..."
+#lock 201 "$tgt" 1
+
+tmpdir_new="$(mktemp -d --tmpdir="$masterbase" "${component}-updating.incoming-XXXXXX")"
+tmpdir_old="$(mktemp -d --tmpdir="$masterbase" "${component}-updating.removing-XXXXXX")"
+trap "rm -rf '$tmpdir_new' '$tmpdir_old'" EXIT
+chmod 0755 "$tmpdir_new"
+
+#echo "$0: Acquiring lock on $tmpdir_new..."
+#lock 202 "$tmpdir_new" 1
+echo "$0: Got them."
+
+echo "$0: Updating master copy of $component..."
+rsync --delete \
+  -trz \
+  --links --hard-links --safe-links \
+  --link-dest="$tgt" \
+  --exclude='/.serial' \
+  "$src/." "$tmpdir_new/."
+echo "$0: Done.  Committing."
+
+mv --no-target-directory "$tgt" "$tmpdir_old/old"
+if ! mv --no-target-directory "$tmpdir_new" "$tgt"; then
+  echo >&2 "$0: WARNING: could not move $tmpdir_new to $tgt.  Trying to recover"
+  rm -rf "$tgt"
+  mv --no-target-directory "$tmpdir_old/old" "$tgt"
+  echo >&2 "$0: Rolled back to old tree, maybe even successfully."
+  exit 1
+fi
+
+rm -rf "$tmpdir_new" "$tmpdir_old"
+trap - EXIT
+
+date '+%s' > "$tgt/.serial"
+#unlock 201
+#unlock 202
+unlock 203
+echo "$0: Triggering mirror runs..."
+exec static-master-run "$component"
+
+# vim:set et:
+# vim:set ts=2:
+# vim:set shiftwidth=2: