3 # initiate a staged mirror update from sync-source for a component.
5 # if we have a serial file and we got a serial on the command line, only sync if the serial is different
7 # Copyright (c) 2012 Peter Palfrader
9 # Permission is hereby granted, free of charge, to any person obtaining
10 # a copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to
14 # permit persons to whom the Software is furnished to do so, subject to
15 # the following conditions:
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 NAME="$(basename "$0")"
35 echo "Usage: $0 [--one-stage] <componentdir> <sync-source> [<serial>]"
38 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then usage; exit 0; fi
60 COMPONENTDIR=${1:-}; shift
61 SYNC_SOURCE=${1:-}; shift
62 SYNC_SERIAL=${1:-}; shift || true
63 if [ -z "$COMPONENTDIR" ]; then usage >&2; exit 1; fi
64 if [ -z "$SYNC_SOURCE" ]; then usage >&2; exit 1; fi
65 COMPONENT="$(basename "${COMPONENTDIR}")"
68 RSYNC_BASE_OPTIONS="-rtvz --delete --links --hard-links --safe-links"
69 RSYNC_SSH_OPTIONS="ssh -o AddressFamily=inet -o BatchMode=yes"
72 LOGFILE="$LOGDIR/$NAME-run-${COMPONENT}.log"
79 CNF_FILE="$HOME/etc/$NAME.conf"
80 ! [ -e "$CNF_FILE" ] || . "$CNF_FILE"
82 SOURCE="${SYNC_SOURCE}/"
83 COMPONENTDIR="${COMPONENTDIR}/"
85 ###############################################
87 # point stdout and stderr to the logfile if it's not a tty.
88 # save stdout to fd5 for communications with the master
93 # move current stdout to fd5 and reopen to logfile
97 # duplicate stdout to fd5
106 echo "[$(date)][$NAME][$$] $1"
110 mkdir -p "$COMPONENTDIR"
111 exec 200< "$COMPONENTDIR"
112 if ! flock -e 200; then
113 log "Cannot acquire lock."
114 echo >&5 "[MSM] LOCK-ERROR"
120 ###############################################
127 if [ -e "${COMPONENTDIR}${ACTIVE}" ] && [ "$(readlink "${COMPONENTDIR}${ACTIVE}")" = "$ALPHA" ] ; then
130 elif [ -e "${COMPONENTDIR}${ACTIVE}" ] && [ "$(readlink "${COMPONENTDIR}${ACTIVE}")" != "$BRAVO" ] ; then
131 echo >&5 "Invalid state of ${COMPONENTDIR}${ACTIVE}."
137 log "active is $active; staging is $staging"
139 rsync_source="${SOURCE}"
140 rsync_curactive="${COMPONENTDIR}${active}/"
141 rsync_target="${COMPONENTDIR}${staging}/"
143 if [ -e "$rsync_curactive/.serial" ] && [ -n "$SYNC_SERIAL" ] && [ "$(cat $rsync_curactive/.serial)" = "$SYNC_SERIAL" ]; then
144 log "active is already at serial $SYNC_SERIAL. No action required."
145 echo >&5 "[MSM] ALREADY-CURRENT"
149 echo >&5 "[MSM] STAGE1-START"
150 log "Running $RSYNC $RSYNC_BASE_OPTIONS -e $RSYNC_SSH_OPTIONS --link-dest $rsync_curactive $rsync_source $rsync_target"
151 $RSYNC $RSYNC_BASE_OPTIONS -e "$RSYNC_SSH_OPTIONS" --link-dest "$rsync_curactive" "$rsync_source" "$rsync_target"
153 echo >&5 "[MSM] STAGE1-DONE"
154 if [ "$one_stage" -gt 0 ]; then
162 ln --symbolic --force --no-target-directory "$staging" "${COMPONENTDIR}$ACTIVE"
163 rm -rf "$rsync_curactive"
164 echo >&5 "[MSM] STAGE2-DONE"
168 echo >&5 "[MSM] STAGE2-ABORT"
172 echo >&5 "[MSM] STAGE2-UNKNOWN-ACTION $action"
173 log "stage2 unknown action $action"