staticsync: let's assume that IPv6 is not worse than IPv4
[mirror/dsa-puppet.git] / modules / staticsync / files / static-mirror-run
1 #!/bin/bash
2
3 # initiate a staged mirror update from sync-source for a component.
4 #
5 # if we have a serial file and we got a serial on the command line, only sync if the serial is different
6
7 # Copyright (c) 2012 Peter Palfrader
8 #
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:
16 #
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
19 #
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.
27
28
29 set -e
30 set -u
31
32 NAME="$(basename "$0")"
33
34 usage() {
35         echo "Usage: $0 [--one-stage] <componentdir> <sync-source> [<serial>]"
36 }
37
38 if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then usage; exit 0; fi
39
40 one_stage=0
41 while :; do
42         case "${1:-}" in
43                 --)
44                         shift
45                         break;
46                         ;;
47                 --one-stage)
48                         shift
49                         one_stage=1
50                         ;;
51                 -*)     usage >&2
52                         exit 1
53                         ;;
54                 *)
55                         break
56                         ;;
57         esac
58 done
59
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}")"
66
67 RSYNC="rsync"
68 RSYNC_BASE_OPTIONS="-rtvz --delete --links --hard-links --safe-links"
69 RSYNC_SSH_OPTIONS="ssh -o BatchMode=yes"
70
71 LOGDIR="$HOME/logs"
72 LOGFILE="$LOGDIR/$NAME-run-${COMPONENT}.log"
73
74
75 ALPHA="tree-a"
76 BRAVO="tree-b"
77 ACTIVE="cur"
78
79 CNF_FILE="$HOME/etc/$NAME.conf"
80 ! [ -e "$CNF_FILE" ] || . "$CNF_FILE"
81
82 SOURCE="${SYNC_SOURCE}/"
83 COMPONENTDIR="${COMPONENTDIR}/"
84
85 ###############################################
86
87 # point stdout and stderr to the logfile if it's not a tty.
88 # save stdout to fd5 for communications with the master
89
90 log_setup() {
91         mkdir -p "$LOGDIR"
92         if ! [ -t 1 ]; then
93                 # move current stdout to fd5 and reopen to logfile
94                 exec 5>&1-
95                 exec 1>> "$LOGFILE"
96         else
97                 # duplicate stdout to fd5
98                 exec 5>&1
99         fi
100         if ! [ -t 2 ]; then
101                 exec 2>> "$LOGFILE"
102         fi
103 }
104
105 log() {
106         echo "[$(date)][$NAME][$$] $1"
107 }
108
109 lock() {
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"
115                 exit 1
116         fi
117         log "Got the lock."
118 }
119
120 ###############################################
121
122
123 log_setup
124 log "called with $*"
125 lock
126
127 if [ -e "${COMPONENTDIR}${ACTIVE}" ] && [ "$(readlink "${COMPONENTDIR}${ACTIVE}")" = "$ALPHA" ] ; then
128         staging="$BRAVO"
129         active="$ALPHA"
130 elif [ -e "${COMPONENTDIR}${ACTIVE}" ] && [ "$(readlink "${COMPONENTDIR}${ACTIVE}")" != "$BRAVO" ] ; then
131         echo >&5 "Invalid state of ${COMPONENTDIR}${ACTIVE}."
132         exit 1
133 else
134         staging="$ALPHA"
135         active="$BRAVO"
136 fi
137 log "active is $active; staging is $staging"
138
139 rsync_source="${SOURCE}"
140 rsync_curactive="${COMPONENTDIR}${active}/"
141 rsync_target="${COMPONENTDIR}${staging}/"
142
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"
146         exit 0
147 fi
148
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"
152 log "rsync done."
153 echo >&5 "[MSM] STAGE1-DONE"
154 if [ "$one_stage" -gt 0 ]; then
155         action="go"
156 else
157         read action
158 fi
159
160 case "$action" in
161         go)
162                 ln --symbolic --force --no-target-directory "$staging" "${COMPONENTDIR}$ACTIVE"
163                 rm -rf "$rsync_curactive"
164                 echo >&5 "[MSM] STAGE2-DONE"
165                 log "stage2 done"
166                 ;;
167         abort)
168                 echo >&5 "[MSM] STAGE2-ABORT"
169                 log "stage2 abort"
170                 ;;
171         *)
172                 echo >&5 "[MSM] STAGE2-UNKNOWN-ACTION $action"
173                 log "stage2 unknown action $action"
174                 exit 1
175                 ;;
176 esac
177
178 savelog "$LOGFILE"