live-build-cgi.cron 6.82 KB
Newer Older
1
2
#!/bin/sh

3
## live-build(7) - System Build Scripts
4
## Copyright (C) 2006-2013 Daniel Baumann <mail@daniel-baumann.ch>
5
##
6
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
7
8
9
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.

10
# Including common functions
11
. /usr/lib/live/build.sh
12
13

# Reading defaults
14
if [ -r /etc/default/live-build-cgi ]
15
then
16
	. /etc/default/live-build-cgi
17
else
18
	echo "E: /etc/default/live-build-cgi missing"
19
20
21
22
	exit 1
fi

# Exit if disabled
23
if [ "${_WEBBUILD}" != "true" ]
24
25
26
27
then
	exit 0
fi

28
29
_HOSTNAME="$(hostname -f)"

30
31
# Turn on debug if true
if [ "${_DEBUG}" = "true" ]
32
33
34
35
then
	set -x
fi

36
37
38
# Locknum sets the lock enumeration for multiple instances of l-b-cgi
_LOCKNUM="$(expr 1 + $(expr $(date +%M) % ${_INSTANCES}))"

39
# Checking lock file
40
if [ -f /var/lock/live-build-cgi-${_LOCKNUM}.lock ]
41
then
42
	echo "E: live-build-cgi instance ${_LOCKNUM} already/still running."
43
44
45
46
	exit 1
fi

# Creating lock trap
47
trap "test -f /var/lock/live-build-cgi-${_LOCKNUM}.lock && rm -f /var/lock/live-build-cgi-${_LOCKNUM}.lock; exit 0" 0 1 2 3 9 15
48
49

# Creating lock file
50
touch /var/lock/live-build-cgi-${_LOCKNUM}.lock
51
52
53
54
55
56

# Cleanup old builds: cron should be run at least once per hour to take effect
if ls "${_DESTDIR}"/`date -d yesterday +%Y%m%d.%H`* > /dev/null 2>&1
then
	rm -rf "${_DESTDIR}"/`date -d yesterday +%Y%m%d.%H`*

57
	echo "`date +%b\ %d\ %H:%M:%S` ${_HOSTNAME} live-build-cgi: remove web build (`date -d yesterday +%Y%m%d.%H`*)." >> /var/log/live/live-build
58
59
fi

60
# Ok from here spin through the live-build-cgi files we have waiting to build
61
62
63
#if ls "${_TEMPDIR}"/*.build > /dev/null 2>&1
if Find_files ${_TEMPDIR}/*.build
then
64
65
66
67
68
	_FILE=$(ls "${_TEMPDIR}"/*.build | sort | head -1)
	# Pull in variables from the build file.
	. "${_FILE}"

	# Drop out some build data for information if something goes wrong.
69
	echo "`date +%b\ %d\ %H:%M:%S` ${_HOSTNAME} live-build-cgi: begin web build (${_BUILD})." >> /var/log/live/live-build
70
71

	# Clone the git config to target build directory
72
	git clone ${_LB_GIT} "${_TEMPDIR}"/"${_BUILD}" >> /var/log/live/live-build
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
	_ERRORGIT="${?}"

	# Moving build file to the tempdir and remove from submission area.
	mv "${_TEMPDIR}"/"${_BUILD}".build "${_TEMPDIR}"/"${_BUILD}"/

	# Sanity check to default to something since we are going to build from git clone.
	if [ -z "${_LB_CONFIG}" ]
	then
		_LB_CONFIG="standard"
	fi

	# Now set a variable for the correct build path
	_BUILD_PATH="${_TEMPDIR}/${_BUILD}/images/${_LB_CONFIG}"

	_DATE_START="`date -R`"
	echo "Begin live-build-cgi: ${_DATE_START}" > "${_BUILD_PATH}"/log
	echo "Info live-build-cgi: Config tree cloned from ${_LB_GIT} for build ${_BUILD}" >> "${_BUILD_PATH}"/log

	# Creating directory for the config/package-lists
	mkdir -p "${_BUILD_PATH}"/config/package-lists

	# Add cgipackages.list.chroot
	if [ -n "${_LB_CGIPACKAGES}" ]
	then
		echo "${_LB_CGIPACKAGES}" > "${_BUILD_PATH}"/config/package-lists/cgipackages.list.chroot
	fi

	# Generating config here
	if [ "${_ERRORGIT}" -eq "0" ]
	then
		cd "${_BUILD_PATH}"
		# Do config with git and stack on config file params to build
		lb config ${_CGI_CONFIG_APPEND} --distribution "${LB_DISTRIBUTION}" --binary-images "${LB_BINARY_IMAGES}" --architectures "${LIVE_IMAGE_ARCHITECTURE}" --archive-areas "${LIVE_IMAGE_ARCHIVE_AREAS}" --chroot-filesystem "${LB_CHROOT_FILESYSTEM}" --linux-flavours "${LB_LINUX_FLAVOURS}" --security "${LB_SECURITY}" --apt-indices "${LB_APT_INDICES}" --bootappend-install "${LB_BOOTAPPEND_INSTALL}" --bootappend-live "${LB_BOOTAPPEND_LIVE}" --bootloader "${LB_BOOTLOADER}" --debian-installer "${LB_DEBIAN_INSTALLER}" --iso-application "${LB_ISO_APPLICATION}" --iso-preparer "${LB_ISO_PREPARER}" --iso-publisher "${LB_ISO_PUBLISHER}" --iso-volume "${LB_ISO_VOLUME}" --memtest "${LB_MEMTEST}" --net-root-path "${LB_NET_ROOT_PATH}" --net-root-server "${LB_NET_ROOT_SERVER}" --source-images "${LB_SOURCE_IMAGES}" --source "${LB_SOURCE}"
		_ERRORCONFIG="${?}"
	else
		echo "Config stage skipped. The git clone produced an error: ${_ERRORGIT}" >> "${_BUILD_PATH}"/log
	fi

	# Run build if config went ok
	if [ "${_ERRORCONFIG}" -eq "0" ]
	then
114
115
116
		# We need to finish script.
		set +e
		lb build noauto >> "${_BUILD_PATH}"/log 2>&1
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
		_ERRORBUILD="${?}"
	else
		echo "Build stage skipped. Config produced an error: ${_ERRORCONFIG}" >> "${_BUILD_PATH}"/log
	fi

	_DATE_END="`date -R`"
	echo "End: ${_DATE_END}" >> "${_BUILD_PATH}"/log

	# Creating image directory
	mkdir -p "${_DESTDIR}"/"${_BUILD}"

	# Creating mail and logging.
	if [ "${_ERRORGIT}" -eq "0" ] && [ "${_ERRORCONFIG}" -eq "0" ] && [ "${_ERRORBUILD}" -eq "0" ]
	then
		_STATUS="maybe-successful"
132
		echo "${REMOTE_ADDR} - - `date +[%d/%b/%Y:%H:%m:%S\ %z]` \"${_BUILD} ${LIVE_IMAGE_ARCHITECTURE}/${LB_DISTRIBUTION}/${_LB_CGIPACKAGES}\" 200 - \"-\" \"\"">>/var/log/live/live-build-cgi-status
133
134
	else
		_STATUS="maybe-failed"
135
		echo "${REMOTE_ADDR} - - `date +[%d/%b/%Y:%H:%m:%S\ %z]` \"${_BUILD} ${LIVE_IMAGE_ARCHITECTURE}/${LB_DISTRIBUTION}/${_LB_CGIPACKAGES}\" 404 - \"-\" \"\"">>/var/log/live/live-build-cgi-status
136
137
138
139
140
141
142
143
144
145
	fi

	sed -e "s/BUILD/${_BUILD}/g" \
	    -e "s/EMAIL/${_EMAIL}/" \
	    -e "s/VERSION/${VERSION}/" \
	    -e "s/DATE_START/${_DATE_START}/" \
	    -e "s/DATE_END/${_DATE_END}/" \
	    -e "s/STATUS/${_STATUS}/" \
	    -e "s#SERVER#${_SERVER}#" \
	"${_TEMPLATES}"/mail.txt > "${_DESTDIR}"/"${_BUILD}"/mail
146

147
148
149
150
151
152
153
154
cat >> "${_DESTDIR}"/"${_BUILD}"/mail << EOF

---

Here's a list of all build-options that were used to build your image:

EOF

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
	cat "${_TEMPDIR}"/"${_BUILD}"/"${_BUILD}".build >> "${_DESTDIR}"/"${_BUILD}"/mail

	# Moving binary image
	#if ls "${_TEMPDIR}"/"${_BUILD}"/binary*.* > /dev/null 2>&1
	if Find_files ${_BUILD_PATH}/binary*.*
	then
		mv "${_BUILD_PATH}"/binary*.* "${_DESTDIR}"/"${_BUILD}"
	fi

	# Moving source image
	#if ls "${_TEMPDIR}"/"${_BUILD}"/source.* > /dev/null 2>&1
	if Find_files ${_BUILD_PATH}/source.*
	then
		mv "${_BUILD_PATH}"/source.* "${_DESTDIR}"/"${_BUILD}"
	fi

	# Moving build
	mv "${_TEMPDIR}"/"${_BUILD}"/"${_BUILD}".build "${_DESTDIR}"/"${_BUILD}"/build

	# Moving log
	mv "${_BUILD_PATH}"/log "${_DESTDIR}"/"${_BUILD}"

	# Generating md5sum
	cd "${_DESTDIR}"/"${_BUILD}"
	md5sum * > md5sum
	cd "${OLDPWD}"

	# Sending mail
	cat "${_DESTDIR}"/"${_BUILD}"/mail | /usr/sbin/sendmail -t

	# Unmounting devpts-live
	#if ls "${_TEMPDIR}"/"${_BUILD}"/chroot/dev/pts/* > /dev/null 2>&1
	if Find_files ${_BUILD_PATH}/chroot/dev/pts/*
	then
		umount "${_BUILD_PATH}"/chroot/dev/pts
	fi

	# Unmounting proc
	if [ -f "${_BUILD_PATH}"/chroot/proc/version ]
	then
		umount "${_BUILD_PATH}"/chroot/proc
	fi

	# Unmounting sysfs
	if [ -d "${_BUILD_PATH}"/chroot/sys/kernel ]
	then
		umount "${_BUILD_PATH}"/chroot/sys
	fi

	# Removing build directory
	rm -rf "${_TEMPDIR}"/"${_BUILD}"

207
	echo "`date +%b\ %d\ %H:%M:%S` ${_HOSTNAME} live-build-cgi: end web build (${_BUILD}: ${_STATUS})." >> /var/log/live/live-build
208
fi