summaryrefslogtreecommitdiffstats
path: root/tools/sfsnapshot-upload
blob: d8ebcc540b899e2ae171001fed7384cd88242e2b (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
# sfsnapshot-upload - Snapshot upload script using sfsnapshotgit
# Original author: Thomas Guyot-Sionnest <tguyot@gmail.com>
#
# This script uses sfsnapshotgit to update the snapshot is needed and upload
# it to SourceForge. The branches to create snapshot from can be given as an
# argument, otherwise the default is master.

# Handle command errors (-e) and coder sleep deprivation issues (-u)
set -eu
trap 'echo "An error occurred in sfsnapshot-upload at line $LINENO"; exit 1' EXIT

# This can be used to override the default in sfsnapshotgit:
export SFSNAP_REPO=~/staging/nagiosplugins
export SFSNAP_ORIGIN=origin
export SFSNAP_DEST=~/staging/snapshot

## Some stuff that shouldn't change often...
# The file we'll use to create the snapshot
sfsnapshot=~/bin/sfsnapshotgit

# Retention time for snapshots (in minutes), 0 for no retention.
CLEAN_TIME=1440

# Where to place the generated files
OUT_SERVER="tonvoon@frs.sourceforge.net"
OUT_PATH="/home/groups/n/na/nagiosplug/htdocs/snapshot"

# Links to always point to the master branch for backwards-compatibility
COMPATLINKS="HEAD trunk-`date -u +%Y%m%d%H%M`"
# And compatibility links to always delete except the last one
COMPATCLEANUP="trunk-*"

# If one or more argument is given, this is the branches to create the snapshots from
if [ $# -eq 0 ]
then
	HEADS='master'
else
	HEADS=$@
fi

# If we don't keep old snapshots we can clean up all links now
if [ $CLEAN_TIME -eq 0 ]
then
	find $SFSNAP_DEST -type l -name '*.gz' -delete
fi

for head in $HEADS ; do
	# This runs the actual snapshot code. It creates new snapshots if needed and always
	# return the current snapshot file (even if it wasn't modified).
	file=$($sfsnapshot $head)
	# Create main head link
	ln -sf $file $SFSNAP_DEST/nagios-plugins-$head.tar.gz

	# Keep links by branch name too if we keep old snapshots, so we can keep tracks of them
	if [ $CLEAN_TIME -gt 0 -a ! -e "$SFSNAP_DEST/nagios-plugins-$head-${file#nagios-plugins-}" ]
	then
		ln -s $file $SFSNAP_DEST/nagios-plugins-$head-${file#nagios-plugins-}
	fi

	# Cleanup and re-create backward-compatibility links
	if [ "$head" == "master" ]
	then
		for cclean in $COMPATCLEANUP
		do
			find $SFSNAP_DEST -type l -name "nagios-plugins-$cclean.tar.gz" -delete
		done
		for compat in $COMPATLINKS
		do
			ln -sf $file $SFSNAP_DEST/nagios-plugins-$compat.tar.gz
		done
	fi
done

cd $SFSNAP_DEST

# Clean up links older than $CLEAN_TIME if needed
if [ $CLEAN_TIME -gt 0 ]
then
	find . -type l -name '*.gz' -mmin +$CLEAN_TIME -delete
fi

# Now clean up files that we don't need
# 1. loop over actual snapshots
for dest in `find . -type f -name '*.gz' |xargs -n 1 basename`
do
	# 2. Loop over the list of linked-to files
	for current in `find . -type l -name '*.gz' |xargs -n 1 readlink | sort | uniq`
	do
		if [ "$current" == "$dest" ]
		then
			# File is being linked to - don't delete (continue first loop)
			continue 2
		fi
	done
	# No link to this file, we can drop it
	rm -f $dest
done

# Create MD5 sum
cat <<-END_README > README
	This is the latest snapshot of nagiosplug, consisting of the following
	head(s):
	        $HEADS

	The nagios-plugins-<head>.tar.gz link will always point to the latest
	corresponding snapshot (nagios-plugins-<git-describe>.tar.gz).

	For backward-compatibility, the nagios-plugins-HEAD.tar.gz and
	nagios-plugins-trunk-<ts>.tar.gz point to their corresponding "master"
	head.

	The tarballs will only be updated when a change has been made. The
	MD5SUM file is updated every time the snapshot script runs.

	The MD5SUMs are:
	END_README
md5sum *.gz | tee -a README > MD5SUM

# Sync the files
[ -n "$OUT_SERVER" ] && OUT_SERVER="$OUT_SERVER:"
rsync -a --exclude=.htaccess --exclude=HEADER.html --delete "$SFSNAP_DEST/" "$OUT_SERVER$OUT_PATH"

trap - EXIT