aboutsummaryrefslogtreecommitdiff
path: root/net/keepalived/files/usr/bin/keepalived-rsync-inotify
blob: 226f928a9f1aa8748c37b7ca003d40d937edb630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/sh

# shellcheck shell=ash

# shellcheck source=/dev/null
. /lib/functions/keepalived/common.sh

if [ $# -lt 3 ]; then
	echo "$0 <vrrp_instance> <peer> <rsync_dir>"
	exit 1
fi

VRRP_INSTANCE=$1
PEER=$2
RSYNC_DIR=$3

INOTIFY_ACTIONS="create,delete,modify,move,moved_to,moved_from"
INOTIFY_PID=""
TMP_DIR=/tmp/keepalived
FIFO_FILE="$TMP_DIR"/inotifywait-$PEER.fifo

daemonize_inotifywait() {
	/usr/bin/inotifywait -q -r --exclude '/\..+' -o "$FIFO_FILE" -m "$RSYNC_DIR" -e ${INOTIFY_ACTIONS} 2> /dev/null &
	INOTIFY_PID="$!"
}

main() {
	local inotify_action inotify_dir inotify_file
	local source_file target_file

	[ ! -d "$TMP_DIR" ] && mkdir "$TMP_DIR"
	mkfifo "${FIFO_FILE}" || exit 1

	daemonize_inotifywait

	while read -r inotify_dir inotify_action inotify_file; do
		source_file="${inotify_dir}${inotify_file}"
		target_file=$(echo "${inotify_dir}" | sed -e "s:${RSYNC_DIR}::g")"${inotify_file}"

		log_debug "received $target_file ($inotify_action) in $source_file"

		ACTION=NOTIFY_SYNC TYPE=peer NAME=$PEER INSTANCE=$VRRP_INSTANCE \
			RSYNC_SOURCE="${source_file}" RSYNC_TARGET="${target_file}" \
			/sbin/hotplug-call keepalived
	done < "$FIFO_FILE"
}

TRAP() {
	[ -n "$INOTIFY_PID" ] && kill "$INOTIFY_PID"
	[ -e "$FIFO_FILE" ] && rm -f "$FIFO_FILE"
}

trap TRAP TERM INT
main "$@"