summaryrefslogtreecommitdiff
path: root/examples/cron/snapshots.sh
blob: 10e0a4395b63c3622ab9d7df2ecae8f8de498d9b (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/sh

# Static variables
if [ -n "${1}" ]
then
	PACKAGES="${@}"
else
	PACKAGES="live-helper live-initramfs live-initscripts live-webhelper"
fi

DEBEMAIL="debian-live-devel@lists.alioth.debian.org"
EMAIL="debian-live-devel@lists.alioth.debian.org"
DEBFULLNAME="Debian Live Autobuilder"
NAME="Debian Live Autobuilder"
KEY="FDB8D39A"

export DEBEMAIL EMAIL DEBFULLNAME NAME KEY

TEMPDIR="$(mktemp -d -t debian-live.XXXXXXXX)"
SERVER="/mnt/daniel1/srv/debian-unofficial/live/debian"
LOGFILE="${SERVER}/build.log"

DATE_START="$(date -R)"

# Checking lock file
if [ -f "${SERVER}"/Archive-Update-in-Progress ]
then
	echo "E: locked."
	exit 1
fi

# Creating server directory
if [ ! -d "${SERVER}" ]
then
	mkdir -p "${SERVER}"
fi

# Creating lock trap
trap "test -f ${SERVER}/Archive-Update-in-Progress && rm -f ${SERVER}/Archive-Update-in-Progress; exit 0" 0 1 2 3 9 15

# Creating lock file
echo "${DATE_START}" > "${SERVER}"/Archive-Update-in-Progress

echo "$(date +%b\ %d\ %H:%M:%S) ${HOSTNAME} live-snapshots: begin build." >> "${LOGFILE}"

# Processing packages
for PACKAGE in ${PACKAGES}
do
	# Cleaning build directory
	if [ -d "${TEMPDIR}" ]
	then
		rm -rf "${TEMPDIR}"
	fi

	# Creating build directory
	mkdir -p "${TEMPDIR}"

	# Getting sources
	cd "${TEMPDIR}"
	git clone git://git.debian.org/git/users/daniel/${PACKAGE}.git

	# Getting version
	cd "${TEMPDIR}"/${PACKAGE}
	VERSION="$(dpkg-parsechangelog | awk '/Version:/ { print $2 }' | awk -F- '{ print $1 }')"

	# Getting revision
	cd "${TEMPDIR}"/${PACKAGE}
	REVISION="$(git log | grep -m1 Date | awk -FDate: '{ print $2 }' | sed -e 's/+.*$//')"
	REVISION="$(date -d "${REVISION}" +%Y%m%d.%H%M%S)"

	# Check for existing package
	if [ ! -f "${SERVER}"/${PACKAGE}_${VERSION}+${REVISION}.dsc ] || [ "${1}" = "--force" ]
	then
		UPDATE_INDICES="true"

		# Renaming directory
		mv "${TEMPDIR}"/${PACKAGE} "${TEMPDIR}"/${PACKAGE}-${VERSION}+${REVISION}

		# Building package
		cd "${TEMPDIR}"/${PACKAGE}-${VERSION}+${REVISION}
		rm -rf .git
		dch --force-bad-version --newversion ${VERSION}+${REVISION} --distribution UNRELEASED Autobuild snapshot of git ${REVISION}.
		dpkg-buildpackage -rfakeroot -k${KEY} -sa

		# Removing sources
		rm -rf "${TEMPDIR}"/${PACKAGE}-${VERSION}+${REVISION}

		# Creating directory
		mkdir -p "${SERVER}"

		# Removing old packages
		if [ -f "${SERVER}"/"${PACKAGE}"*.changes ]
		then
			for FILE in $(awk {'print $5'} "${SERVER}"/"${PACKAGE}"*.changes | grep -e ".*\.deb$" -e ".*\.diff.gz$" -e ".*\.dsc$" -e ".*\.tar.gz$" -e ".*\.udeb$")
			do
				rm -f "${SERVER}"/"${FILE}"
			done
		fi

		rm -f "${SERVER}"/"${PACKAGE}"*.changes

		# Installing new packages
		for FILE in $(awk {'print $5'} "${TEMPDIR}"/"${PACKAGE}"*.changes | grep -e ".*\.deb$" -e ".*\.diff.gz$" -e ".*\.dsc$" -e ".*\.tar.gz$" -e ".*\.udeb$")
		do
			mv "${TEMPDIR}"/"${FILE}" "${SERVER}"
		done

		mv "${TEMPDIR}"/"${PACKAGE}"*.changes "${SERVER}"
	else
		# Remove sources
		rm -rf "${TEMPDIR}"/${PACKAGE}
	fi
done

if [ "${UPDATE_INDICES}" = "true" ]
then
	LAST_UPDATE="$(date -R)"

	cd "${SERVER}"

	apt-ftparchive packages . /dev/null > Packages
	gzip -9 -c Packages > Packages.gz
	bzip2 -9 -c Packages > Packages.bz2

	apt-ftparchive sources . /dev/null > Sources
	gzip -9 -c Sources > Sources.gz
	bzip2 -9 -c Sources > Sources.bz2

	if [ -f release.conf ]
	then
		apt-ftparchive -c release.conf -o APT::FTPArchive::Release::Description="Last updated: `date -R`" release ./ >> Release.tmp
		mv Release.tmp Release

		rm -f Release.gpg
		gpg --default-key ${KEY} --quiet --output Release.gpg -ba Release
	fi
fi

# Reading timestamp
if [ -z "${LAST_UPDATE}" ]
then
	LAST_UPDATE="$(awk -F: '/Last update:/ { print $2":"$3":"$4 }' ${SERVER}/LAST_BUILD | sed -e 's/    //')"
fi

# Writing timestamp
cat > "${SERVER}"/LAST_BUILD << EOF
Last run begin: ${DATE_START}
Last run end:   $(date -R)

Last update:    ${LAST_UPDATE}
EOF

# Removing build directory
rm -rf "${TEMPDIR}"

echo "$(date +%b\ %d\ %H:%M:%S) ${HOSTNAME} live-snapshots: end build." >> "${LOGFILE}"