]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/roles/files/static-mirroring/static-mirror-run
Always remove acpi packages from jessie hosts
[dsa-puppet.git] / modules / roles / files / static-mirroring / 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
66 RSYNC="rsync"
67 RSYNC_BASE_OPTIONS="-rtvz --delete --links --hard-links --safe-links"
68 RSYNC_SSH_OPTIONS="ssh -o AddressFamily=inet -o BatchMode=yes"
69
70 LOGDIR="$HOME/logs"
71 LOGFILE="$LOGDIR/$NAME-run.log"
72
73
74 ALPHA="tree-a"
75 BRAVO="tree-b"
76 ACTIVE="cur"
77
78 CNF_FILE="$HOME/etc/$NAME.conf"
79 ! [ -e "$CNF_FILE" ] || . "$CNF_FILE"
80
81 SOURCE="${SYNC_SOURCE}/"
82 COMPONENTDIR="${COMPONENTDIR}/"
83
84 ###############################################
85
86 # point stdout and stderr to the logfile if it's not a tty.
87 # save stdout to fd5 for communications with the master
88
89 log_setup() {
90         mkdir -p "$LOGDIR"
91         if ! [ -t 1 ]; then
92                 # move current stdout to fd5 and reopen to logfile
93                 exec 5>&1-
94                 exec 1>> "$LOGFILE"
95         else
96                 # duplicate stdout to fd5
97                 exec 5>&1
98         fi
99         if ! [ -t 2 ]; then
100                 exec 2>> "$LOGFILE"
101         fi
102 }
103
104 log() {
105         echo "[$(date)][$NAME][$$] $1"
106 }
107
108 lock() {
109         mkdir -p "$COMPONENTDIR"
110         exec 200< "$COMPONENTDIR"
111         if ! flock -e 200; then
112                 log "Cannot acquire lock."
113                 echo >&5 "[MSM] LOCK-ERROR"
114                 exit 1
115         fi
116         log "Got the lock."
117 }
118
119 ###############################################
120
121
122 log_setup
123 log "called with $*"
124 lock
125
126 if [ -e "${COMPONENTDIR}${ACTIVE}" ] && [ "$(readlink "${COMPONENTDIR}${ACTIVE}")" = "$ALPHA" ] ; then
127         staging="$BRAVO"
128         active="$ALPHA"
129 elif [ -e "${COMPONENTDIR}${ACTIVE}" ] && [ "$(readlink "${COMPONENTDIR}${ACTIVE}")" != "$BRAVO" ] ; then
130         echo >&5 "Invalid state of ${COMPONENTDIR}${ACTIVE}."
131         exit 1
132 else
133         staging="$ALPHA"
134         active="$BRAVO"
135 fi
136 log "active is $active; staging is $staging"
137
138 rsync_source="${SOURCE}"
139 rsync_curactive="${COMPONENTDIR}${active}/"
140 rsync_target="${COMPONENTDIR}${staging}/"
141
142 if [ -e "$rsync_curactive/.serial" ] && [ -n "$SYNC_SERIAL" ] && [ "$(cat $rsync_curactive/.serial)" = "$SYNC_SERIAL" ]; then
143         log "active is already at serial $SYNC_SERIAL.  No action required."
144         echo >&5 "[MSM] ALREADY-CURRENT"
145         exit 0
146 fi
147
148 echo >&5 "[MSM] STAGE1-START"
149 log "Running $RSYNC $RSYNC_BASE_OPTIONS -e $RSYNC_SSH_OPTIONS --link-dest $rsync_curactive $rsync_source $rsync_target"
150 $RSYNC $RSYNC_BASE_OPTIONS -e "$RSYNC_SSH_OPTIONS" --link-dest "$rsync_curactive" "$rsync_source" "$rsync_target"
151 log "rsync done."
152 echo >&5 "[MSM] STAGE1-DONE"
153 if [ "$one_stage" -gt 0 ]; then
154         action="go"
155 else
156         read action
157 fi
158
159 case "$action" in
160         go)
161                 ln --symbolic --force --no-target-directory "$staging" "${COMPONENTDIR}$ACTIVE"
162                 rm -rf "$rsync_curactive"
163                 echo >&5 "[MSM] STAGE2-DONE"
164                 log "stage2 done"
165                 ;;
166         abort)
167                 echo >&5 "[MSM] STAGE2-ABORT"
168                 log "stage2 abort"
169                 ;;
170         *)
171                 echo >&5 "[MSM] STAGE2-UNKNOWN-ACTION $action"
172                 log "stage2 unknown action $action"
173                 exit 1
174                 ;;
175 esac
176
177 savelog "$LOGFILE"