Project

General

Profile

« Previous | Next » 

Revision ab943fc9

Added by Renato Botelho about 10 years ago

Move snapshot related functions to builder_common.sh and leave build_snapshots.sh a simple script that update repo and call build.sh, without need to know anything about build.conf variables

View differences:

tools/build_snapshots.sh
35 35

  
36 36
NO_UPLOAD=""
37 37
LOOPED_SNAPSHOTS=""
38
export minsleepvalue=${minsleepvalue:-"28800"}
39
export maxsleepvalue=${maxsleepvalue:-"86400"}
38 40

  
39 41
# Handle command line arguments
40 42
while test "$1" != "" ; do
......
42 44
	--noupload|-u)
43 45
		NO_UPLOAD="-u"
44 46
		;;
45
	--looped)
47
	--looped|-l)
46 48
		LOOPED_SNAPSHOTS="true"
47 49
	esac
48 50
	shift
49 51
done
50 52

  
51
# Source ${PRODUCT_NAME} / FreeSBIE variables
52
# *** DO NOT SOURCE BUILDER_COMMON.SH!
53
# *** IT WILL BREAK EVERYTHING FOR 
54
# *** SOME UNKNOWN LAYERING REASON.
55
# *** 04/07/2008, 11/04/2009                      
56
echo ">>> Execing build.conf"
57
. ${BUILDER_TOOLS}/builder_defaults.sh
58

  
59
if [ -z "${RSYNCIP}" -a -z "${NO_UPLOAD}" ]; then
60
	echo ">>> ERROR: RSYNCIP is not defined"
61
	exit 1
62
fi
63

  
64
if [ -z "${RSYNCUSER}" -a -z "${NO_UPLOAD}" ]; then
65
	echo ">>> ERROR: RSYNCUSER is not defined"
66
	exit 1
67
fi
68

  
69
if [ -z "${RSYNCPATH}" -a -z "${NO_UPLOAD}" ]; then
70
	echo ">>> ERROR: RSYNCPATH is not defined"
71
	exit 1
72
fi
73

  
74
if [ -z "${RSYNCLOGS}" -a -z "${NO_UPLOAD}" ]; then
75
	echo ">>> ERROR: RSYNCLOGS is not defined"
76
	exit 1
77
fi
78

  
79 53
# Keeps track of how many time builder has looped
80 54
BUILDCOUNTER=0
81 55

  
82
# Local variables that are used by builder scripts
83
STAGINGAREA=${SCRATCHDIR}/staging
84
RSYNCKBYTELIMIT="248000"
85

  
86
export SNAPSHOTSLOGFILE=${SNAPSHOTSLOGFILE:-"$SCRATCHDIR/snapshots-build.log"}
87
export SNAPSHOTSLASTUPDATE=${SNAPSHOTSLASTUPDATE:-"$SCRATCHDIR/snapshots-lastupdate.log"}
88

  
89
# Ensure directories exist
90
mkdir -p $STAGINGAREA
91

  
92
echo "" > $SNAPSHOTSLOGFILE
93
echo "" > $SNAPSHOTSLASTUPDATE
56
git_last_commit() {
57
	export CURRENT_COMMIT=$(git -C ${BUILDER_ROOT} log -1 --format='%H')
58
	export CURRENT_AUTHOR=$(git -C ${BUILDER_ROOT} log -1 --format='%an')
59
}
94 60

  
95 61
# This routine is called in between runs. We
96 62
# will sleep for a bit and check for new commits
97 63
# in between sleeping for short durations.
98
sleep_between_runs() {
64
snapshots_sleep_between_runs() {
99 65
	COUNTER=0
100 66
	while [ $COUNTER -lt $maxsleepvalue ]; do
101 67
		sleep 60
......
103 69
		git -C "${BUILDER_ROOT}" pull -q
104 70
		git_last_commit
105 71
		if [ "${LAST_COMMIT}" != "${CURRENT_COMMIT}" ]; then
106
			update_status ">>> New commit: $CURRENT_AUTHOR - $CURRENT_COMMIT .. No longer sleepy."
72
			${BUILDER_ROOT}/build.sh --snapshot-update-status ">>> New commit: $CURRENT_AUTHOR - $CURRENT_COMMIT .. No longer sleepy."
107 73
			COUNTER=$(($maxsleepvalue + 60))
108 74
			export LAST_COMMIT="${CURRENT_COMMIT}"
109 75
		fi
110 76
		COUNTER=$(($COUNTER + 60))
111 77
	done
112 78
	if [ $COUNTER -ge $maxsleepvalue ]; then
113
		update_status ">>> Sleep timer expired. Restarting build."
79
		${BUILDER_ROOT}/build.sh --snapshot-update-status ">>> Sleep timer expired. Restarting build."
114 80
		maxsleepvalue=0
115 81
		COUNTER=0
116 82
	fi
117 83
}
118 84

  
119
# This routine is called to write out to stdout
120
# a string. The string is appended to $SNAPSHOTSLOGFILE
121
# and we scp the log file to the builder host if
122
# needed for the real time logging functions.
123
update_status() {
124
	if [ "$1" = "" ]; then
125
		return
126
	fi
127
	echo $1
128
	echo "`date` -|- $1" >> $SNAPSHOTSLOGFILE
129
	if [ -z "${NO_UPLOAD}" ]; then
130
		LU=`cat $SNAPSHOTSLASTUPDATE`
131
		CT=`date "+%H%M%S"`
132
		# Only update every minute
133
		if [ "$LU" != "$CT" ]; then 
134
			ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCLOGS}"
135
			scp -q $SNAPSHOTSLOGFILE ${RSYNCUSER}@${RSYNCIP}:${RSYNC_LOGS}/build.log
136
			date "+%H%M%S" > $SNAPSHOTSLASTUPDATE
137
		fi
138
	fi
139
}
85
git_last_commit
140 86

  
141
# Copy the current log file to $filename.old on
142
# the snapshot www server (real time logs)
143
rotate_logfile() {
144
	if [ -n "$MASTER_BUILDER_SSH_LOG_DEST" -a -z "${NO_UPLOAD}" ]; then
145
		scp -q $SNAPSHOTSLOGFILE ${RSYNCUSER}@${RSYNCIP}:${RSYNC_LOGS}/build.log.old
146
	fi
87
# Main builder loop
88
while [ /bin/true ]; do
89
	BUILDCOUNTER=$((${BUILDCOUNTER}+1))
147 90

  
148
	# Cleanup log file
149
	echo "" > $SNAPSHOTSLOGFILE
150
}
151

  
152
dobuilds() {
153
	# Build images
154
	(cd ${BUILDER_ROOT} && ./build.sh --flash-size '1g 2g 4g' "iso memstick memstickserial memstickadi fullupdate nanobsd nanobsd-vga")
155
	# Copy files
156
	copy_to_staging_iso_updates
157
	copy_to_staging_nanobsd '1g 2g 4g'
158
}
159

  
160
copy_to_staging_nanobsd() {
161
	for NANOTYPE in nanobsd nanobsd-vga; do
162
		for FILESIZE in ${1}; do
163
			FILENAMEFULL="${PRODUCT_NAME}-${PRODUCT_VERSION}-${FILESIZE}-${TARGET}-${NANOTYPE}${TIMESTAMP_SUFFIX}.img.gz"
164
			FILENAMEUPGRADE="${PRODUCT_NAME}-${PRODUCT_VERSION}-${FILESIZE}-${TARGET}-${NANOTYPE}-upgrade${TIMESTAMP_SUFFIX}.img.gz"
165
			mkdir -p $STAGINGAREA/nanobsd
166
			mkdir -p $STAGINGAREA/nanobsdupdates
167

  
168
			cp $IMAGES_FINAL_DIR/$FILENAMEFULL $STAGINGAREA/nanobsd/ 2>/dev/null
169
			cp $IMAGES_FINAL_DIR/$FILENAMEUPGRADE $STAGINGAREA/nanobsdupdates 2>/dev/null
170

  
171
			if [ -f $STAGINGAREA/nanobsd/$FILENAMEFULL ]; then
172
				md5 $STAGINGAREA/nanobsd/$FILENAMEFULL > $STAGINGAREA/nanobsd/$FILENAMEFULL.md5 2>/dev/null
173
				sha256 $STAGINGAREA/nanobsd/$FILENAMEFULL > $STAGINGAREA/nanobsd/$FILENAMEFULL.sha256 2>/dev/null
174
			fi
175
			if [ -f $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE ]; then
176
				md5 $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE > $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE.md5 2>/dev/null
177
				sha256 $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE > $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE.sha256 2>/dev/null
178
			fi
179

  
180
			# Copy NanoBSD auto update:
181
			if [ -f $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE ]; then
182
				cp $STAGINGAREA/nanobsdupdates/$FILENAMEUPGRADE $STAGINGAREA/latest-${NANOTYPE}-$FILESIZE.img.gz 2>/dev/null
183
				sha256 $STAGINGAREA/latest-${NANOTYPE}-$FILESIZE.img.gz > $STAGINGAREA/latest-${NANOTYPE}-$FILESIZE.img.gz.sha256 2>/dev/null
184
				# NOTE: Updates need a file with output similar to date output
185
				# Use the file generated at start of dobuilds() to be consistent on times
186
				cp $BUILTDATESTRINGFILE $STAGINGAREA/version-${NANOTYPE}-$FILESIZE
187
			fi
188
		done
91
	${BUILDER_ROOT}/build.sh --clean-builder | while read LINE; do
92
		${BUILDER_ROOT}/build.sh --snapshot-update-status "${LINE}"
189 93
	done
190
}
191

  
192
copy_to_staging_iso_updates() {
193
	# Copy ISOs
194
	md5 ${ISOPATH}.gz > ${ISOPATH}.md5
195
	sha256 ${ISOPATH}.gz > ${ISOPATH}.sha256
196
	cp ${ISOPATH}* $STAGINGAREA/ 2>/dev/null
197

  
198
	# Copy memstick items
199
	md5 ${MEMSTICKPATH}.gz > ${MEMSTICKPATH}.md5
200
	sha256 ${MEMSTICKPATH}.gz > ${MEMSTICKPATH}.sha256
201
	cp ${MEMSTICKPATH}* $STAGINGAREA/ 2>/dev/null
202

  
203
	md5 ${MEMSTICKSERIALPATH}.gz > ${MEMSTICKSERIALPATH}.md5
204
	sha256 ${MEMSTICKSERIALPATH}.gz > ${MEMSTICKSERIALPATH}.sha256
205
	cp ${MEMSTICKSERIALPATH}* $STAGINGAREA/ 2>/dev/null
206

  
207
	md5 ${MEMSTICKADIPATH}.gz > ${MEMSTICKADIPATH}.md5
208
	sha256 ${MEMSTICKADIPATH}.gz > ${MEMSTICKADIPATH}.sha256
209
	cp ${MEMSTICKADIPATH}* $STAGINGAREA/ 2>/dev/null
210

  
211
	md5 ${UPDATES_TARBALL_FILENAME} > ${UPDATES_TARBALL_FILENAME}.md5
212
	sha256 ${UPDATES_TARBALL_FILENAME} > ${UPDATES_TARBALL_FILENAME}.sha256
213
	cp ${UPDATES_TARBALL_FILENAME}* $STAGINGAREA/ 2>/dev/null
214
	# NOTE: Updates need a file with output similar to date output
215
	# Use the file generated at start of dobuilds() to be consistent on times
216
	if [ -z "${_IS_RELEASE}" ]; then
217
		cp $BUILTDATESTRINGFILE $STAGINGAREA/version 2>/dev/null
218
	fi
219
}
220

  
221
scp_files() {
222
	if [ -z "${RSYNC_COPY_ARGUMENTS:-}" ]; then
223
		RSYNC_COPY_ARGUMENTS="-ave ssh --timeout=60 --bwlimit=${RSYNCKBYTELIMIT}" #--bwlimit=50
224
	fi
225
	update_status ">>> Copying files to ${RSYNCIP}"
226

  
227
	rm -f $SCRATCHDIR/ssh-snapshots*
228

  
229
	# Ensure directory(s) are available
230
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/livecd_installer"
231
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/updates"
232
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/nanobsd"
233
	if [ -d $STAGINGAREA/virtualization ]; then
234
		ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/virtualization"
235
	fi
236
	ssh ${RSYNCUSER}@${RSYNCIP} "mkdir -p ${RSYNCPATH}/.updaters"
237
	# ensure permissions are correct for r+w
238
	ssh ${RSYNCUSER}@${RSYNCIP} "chmod -R ug+rw /usr/local/www/snapshots/FreeBSD_${FREEBSD_PARENT_BRANCH}/${TARGET}/."
239
	ssh ${RSYNCUSER}@${RSYNCIP} "chmod -R ug+rw ${RSYNCPATH}/."
240
	ssh ${RSYNCUSER}@${RSYNCIP} "chmod -R ug+rw ${RSYNCPATH}/*/."
241
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/${PRODUCT_NAME}-*iso* \
242
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/livecd_installer/
243
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/${PRODUCT_NAME}-memstick* \
244
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/livecd_installer/
245
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/${PRODUCT_NAME}-*Update* \
246
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/updates/
247
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/nanobsd/* \
248
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/nanobsd/
249
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/nanobsdupdates/* \
250
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/updates/
251
	if [ -d $STAGINGAREA/virtualization ]; then
252
		rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/virtualization/* \
253
			${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/virtualization/
254
	fi
255

  
256
	# Rather than copy these twice, use ln to link to the latest one.
257

  
258
	ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest.tgz"
259
	ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest.tgz.sha256"
260

  
261
	LATESTFILENAME="`ls $UPDATESDIR/*.tgz | grep Full | grep -v md5 | grep -v sha256 | tail -n1`"
262
	LATESTFILENAME=`basename ${LATESTFILENAME}`
263
	ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${LATESTFILENAME} \
264
		${RSYNCPATH}/.updaters/latest.tgz"
265
	ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${LATESTFILENAME}.sha256 \
266
		${RSYNCPATH}/.updaters/latest.tgz.sha256"
267 94

  
268
	for i in 1g 2g 4g
269
	do
270
		ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest-nanobsd-${i}.img.gz"
271
		ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest-nanobsd-${i}.img.gz.sha256"
272
		ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest-nanobsd-vga-${i}.img.gz"
273
		ssh ${RSYNCUSER}@${RSYNCIP} "rm -f ${RSYNCPATH}/.updaters/latest-nanobsd-vga-${i}.img.gz.sha256"
274

  
275
		FILENAMEUPGRADE="${PRODUCT_NAME}-${PRODUCT_VERSION}-${i}-${TARGET}-nanobsd-upgrade${TIMESTAMP_SUFFIX}.img.gz"
276
		ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${FILENAMEUPGRADE} \
277
			${RSYNCPATH}/.updaters/latest-nanobsd-${i}.img.gz"
278
		ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${FILENAMEUPGRADE}.sha256 \
279
			${RSYNCPATH}/.updaters/latest-nanobsd-${i}.img.gz.sha256"
280

  
281
		FILENAMEUPGRADE="${PRODUCT_NAME}-${PRODUCT_VERSION}-${i}-${TARGET}-nanobsd-vga-upgrade${TIMESTAMP_SUFFIX}.img.gz"
282
		ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${FILENAMEUPGRADE} \
283
			${RSYNCPATH}/.updaters/latest-nanobsd-vga-${i}.img.gz"
284
		ssh ${RSYNCUSER}@${RSYNCIP} "ln -s ${RSYNCPATH}/updates/${FILENAMEUPGRADE}.sha256 \
285
			${RSYNCPATH}/.updaters/latest-nanobsd-vga-${i}.img.gz.sha256"
95
	${BUILDER_ROOT}/build.sh ${NO_UPLOAD} --flash-size '1g 2g 4g' --snapshots | while read LINE; do
96
		${BUILDER_ROOT}/build.sh --snapshot-update-status "${LINE}"
286 97
	done
287 98

  
288
	rsync $RSYNC_COPY_ARGUMENTS $STAGINGAREA/version* \
289
		${RSYNCUSER}@${RSYNCIP}:${RSYNCPATH}/.updaters
290
	update_status ">>> Finished copying files."
291
}
292

  
293
cleanup_builds() {
294
	# Remove prior builds
295
	update_status ">>> Cleaning up after prior builds..."
296
	rm -rf $STAGINGAREA/*
297
	rm -rf $IMAGES_FINAL_DIR/*
298
	(cd ${BUILDER_ROOT} && ./build.sh --clean-builder)
299
}
300

  
301
build_loop_operations() {
302
	update_status ">>> Starting build loop operations"
303
	# --- Items we need to run for a complete build run ---
304
	# Cleanup prior builds
305
	cleanup_builds
306
	# Update pkgs if necessary
307
	if pkg update -r ${PRODUCT_NAME} >/dev/null 2>&1; then
308
		update_status ">>> Updating builder packages... "
309
		pkg upgrade -r ${PRODUCT_NAME} -y -q >/dev/null 2>&1
310
	fi
311
	# Do the builds
312
	dobuilds
313
	# SCP files to snapshot web hosting area
314
	if [ -z "${NO_UPLOAD}" ]; then
315
		scp_files
99
	if [ -z "${LOOPED_SNAPSHOTS}" ]; then
100
		# only one build required, exiting
101
		exit
316 102
	fi
317
	# Alert the world that we have some snapshots ready.
318
	update_status ">>> Builder run is complete."
319
}
320

  
321
if [ -z "${LOOPED_SNAPSHOTS}" ]; then
322
	build_loop_operations
323
else
324
	# Main builder loop
325
	while [ /bin/true ]; do
326
		BUILDCOUNTER=`expr $BUILDCOUNTER + 1`
327
		update_status ">>> Starting builder run #${BUILDCOUNTER}..."
328

  
329
		# Launch the snapshots builder script and pipe its
330
		# contents to the while loop so we can record the 
331
		# script progress in real time to the public facing
332
		# snapshot server (${RSYNCIP}).
333
		( build_loop_operations ) | while read LINE
334
		do
335
			update_status "$LINE"
336
		done
337 103

  
338
		export minsleepvalue=28800
339
		export maxsleepvalue=86400
104
	# Initialize variables that keep track of last commit
105
	[ -z "${LAST_COMMIT}" ] \
106
		&& export LAST_COMMIT=${CURRENT_COMMIT}
340 107

  
341
		# Initialize variables that keep track of last commit
342
		[ -z "${LAST_COMMIT}" ] \
343
			&& export LAST_COMMIT="$(git -C ${BUILDER_ROOT} log | head -n1 | cut -d' ' -f2)"
108
	${BUILDER_ROOT}/build.sh --snapshot-update-status ">>> Sleeping for at least $minsleepvalue, at most $maxsleepvalue in between snapshot builder runs.  Last known commit ${LAST_COMMIT}"
109
	${BUILDER_ROOT}/build.sh --snapshot-update-status ">>> Freezing build process at $(date)."
110
	sleep $minsleepvalue
111
	${BUILDER_ROOT}/build.sh --snapshot-update-status ">>> Thawing build process and resuming checks for pending commits at $(date)."
344 112

  
345
		update_status ">>> Sleeping for at least $minsleepvalue, at most $maxsleepvalue in between snapshot builder runs.  Last known commit ${LAST_COMMIT}"
346
		update_status ">>> Freezing build process at `date`."
347
		sleep $minsleepvalue
348
		update_status ">>> Thawing build process and resuming checks for pending commits at `date`."
349

  
350
		# Count some sheep or wait until a new commit turns up 
351
		# for one days time.  We will wake up if a new commit
352
		# is detected during sleepy time.
353
		sleep_between_runs $maxsleepvalue
354

  
355
		# If REBOOT_AFTER_SNAPSHOT_RUN is defined reboot
356
		# the box after the run. 
357
		if [ ! -z "${REBOOT_AFTER_SNAPSHOT_RUN:-}" ]; then
358
			update_status ">>> Rebooting `hostname` due to \$REBOOT_AFTER_SNAPSHOT_RUN"
359
			shutdown -r now
360
			kill $$
361
		fi
362
		# Rotate log file (.old)
363
		rotate_logfile
364

  
365
		# Set a common DATESTRING for the build if not set from builder_defaults.sh.
366
		# Rely on builder_defaults.sh doing the right job the first time included from this script.
367
		# NOTE: This is needed to have autoupdate detect a new version.
368
		# Override it here to have continuous builds with proper labels
369
		rm -f $DATESTRINGFILE
370
		rm -f $BUILTDATESTRINGFILE
371
		unset TIMESTAMP_SUFFIX
372
		unset DATESTRING
373
		unset BUILTDATESTRING
374
		unset ISOPATH
375
		unset MEMSTICKPATH
376
		unset MEMSTICKSERIALPATH
377
		unset MEMSTICKADIPATH
378
		unset UPDATES_TARBALL_FILENAME
379
		# builder_defaults.sh will set variables with correct timestamp
380
		. ${BUILDER_TOOLS}/builder_defaults.sh
381
	done
382
fi
113
	# Count some sheep or wait until a new commit turns up
114
	# for one days time.  We will wake up if a new commit
115
	# is detected during sleepy time.
116
	snapshots_sleep_between_runs $maxsleepvalue
117
done

Also available in: Unified diff