summaryrefslogtreecommitdiff
blob: b4a6857b905ea4c036dd4b0771a1bb036a41a6f9 (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
#!/bin/bash -x

# On container start, run an rsync to get a good copy of the tree.
# Then execute rsyncd; we will start serving once the sync completes.
# Then keep syncing in the background every 30m.

function sync() {
	OPTS=(
	 --quiet
	 --recursive
	 --links
	 --perms
	 --times
	 --delete
	 --timeout=300
	 --checksum
	)
	SRC="${2}"
	DST="${1}"
  
	echo "Started update at" $(date) >> $0.log 2>&1
	logger -t rsync "re-rsyncing the gentoo-portage tree"
	/usr/bin/rsync ${OPTS[@]} "${SRC}" "${DST}" >> $0.log 2>&1
	echo "End: "$(date) >> $0.log 2>&1
	return 0
}

tmp=$(mktemp -d -p "${DEST_DIR}" XXXXXX)
sync "${tmp}" "${SOURCE_MIRROR}" # this is synchronous.

# We serve out of ${DEST_DIR}/serving
ln -s "${tmp}" "serving"

# Then launch rsyncd; it will detach into the background and serve from serving.
rsync --daemon --config="/opt/rsync/rsyncd.conf"

while true
do
	sleep "${WAIT_TIME}"
	tmp=$(mktemp -d -p "${DEST_DIR}" XXXXXX)
	# If we fail to sync, just try again.
	if ! sync "${tmp}" "${SOURCE_MIRROR}"; then
		rm -rf "${tmp}"
		continue
	fi
	# Atomically rename
	ln -sf "${tmp}" "staging" && \
		mv -fT "${DEST_DIR}/staging" "${DEST_DIR}/serving"
done