Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# build.sh
4
#
5
# part of pfSense (https://www.pfsense.org)
6
# Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
# All rights reserved.
8
#
9
# Licensed under the Apache License, Version 2.0 (the "License");
10
# you may not use this file except in compliance with the License.
11
# You may obtain a copy of the License at
12
#
13
# http://www.apache.org/licenses/LICENSE-2.0
14
#
15
# Unless required by applicable law or agreed to in writing, software
16
# distributed under the License is distributed on an "AS IS" BASIS,
17
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
# See the License for the specific language governing permissions and
19
# limitations under the License.
20

    
21
set +e
22
usage() {
23
	echo "Usage $0 [options] [ iso | ova | memstick | memstickserial | memstickadi | all | none ]"
24
	echo "		all = iso memstick memstickserial memstickadi"
25
	echo "		none = upgrade only pkg repo"
26
	echo "	[ options ]: "
27
	echo "		--no-buildworld|-c - Will set NO_BUILDWORLD NO_BUILDKERNEL to not build kernel and world"
28
	echo "		--no-cleanobjdir|-d - Will not clean FreeBSD object built dir to allow restarting a build with NO_CLEAN"
29
	echo "		--resume-image-build|-r - Includes -c -d and also will just move directly to image creation using pre-staged data"
30
	echo "		--setup - Install required repo and ports builder require to work"
31
	echo "		--update-sources - Refetch FreeBSD sources"
32
	echo "		--rsync-repos - rsync pkg repos"
33
	echo "		--clean-builder - clean all builder used data/resources"
34
	echo "		--build-kernels - build all configured kernels"
35
	echo "		--build-kernel argument - build specified kernel. Example --build-kernel KERNEL_NAME"
36
	echo "		--install-extra-kernels argument - Put extra kernel(s) under /kernel image directory. Example --install-extra-kernels KERNEL_NAME_WRAP"
37
	echo "		--snapshots - Build snapshots and upload them to RSYNCIP"
38
	echo "		--poudriere-snapshots - Update poudriere packages and send them to PKG_RSYNC_HOSTNAME"
39
	echo "		--setup-poudriere - Install poudriere and create necessary jails and ports tree"
40
	echo "		--create-unified-patch - Create a big patch with all changes done on FreeBSD"
41
	echo "		--update-poudriere-jails [-a ARCH_LIST] - Update poudriere jails using current patch versions"
42
	echo "		--update-poudriere-ports [-a ARCH_LIST]- Update poudriere ports tree"
43
	echo "		--update-pkg-repo [-a ARCH_LIST]- Rebuild necessary ports on poudriere and update pkg repo"
44
	echo "		--do-not-upload|-u - Do not upload pkgs or snapshots"
45
	echo "		-V VARNAME - print value of variable VARNAME"
46
	exit 1
47
}
48

    
49
export BUILDER_ROOT=$(realpath $(dirname ${0}))
50
export BUILDER_TOOLS="${BUILDER_ROOT}/tools"
51

    
52
unset _SKIP_REBUILD_PRESTAGE
53
unset _USE_OLD_DATESTRING
54
unset pfPORTTOBUILD
55
unset IMAGETYPE
56
unset DO_NOT_UPLOAD
57
unset SNAPSHOTS
58
unset POUDRIERE_SNAPSHOTS
59
unset ARCH_LIST
60
BUILDACTION="images"
61

    
62
# Maybe use options for nocleans etc?
63
while test "$1" != ""; do
64
	case "${1}" in
65
		--no-buildworld|-c)
66
			export NO_BUILDWORLD=YES
67
			export NO_BUILDKERNEL=YES
68
			;;
69
		--no-cleanobjdir|-d)
70
			export NO_CLEAN_FREEBSD_OBJ=YES
71
			;;
72
		--resume-image-build|-r)
73
			export NO_BUILDWORLD=YES
74
			export NO_BUILDKERNEL=YES
75
			export NO_CLEAN_FREEBSD_OBJ=YES
76
			export DO_NOT_SIGN_PKG_REPO=YES
77
			_SKIP_REBUILD_PRESTAGE=YES
78
			_USE_OLD_DATESTRING=YES
79
			;;
80
		--setup)
81
			BUILDACTION="builder_setup"
82
			;;
83
		--rsync-repos)
84
			BUILDACTION="rsync_repos"
85
			export DO_NOT_SIGN_PKG_REPO=YES
86
			;;
87
		--build-kernels)
88
			BUILDACTION="buildkernels"
89
			;;
90
		--install-extra-kernels)
91
			shift
92
			if [ $# -eq 0 ]; then
93
				echo "--build-kernel needs extra parameter."
94
				echo
95
				usage
96
			fi
97
			export INSTALL_EXTRA_KERNELS="${1}"
98
			;;
99
		--snapshots)
100
			export SNAPSHOTS=1
101
			;;
102
		--poudriere-snapshots)
103
			export POUDRIERE_SNAPSHOTS=1
104
			;;
105
		--build-kernel)
106
			BUILDACTION="buildkernel"
107
			shift
108
			if [ $# -eq 0 ]; then
109
				echo "--build-kernel needs extra parameter."
110
				echo
111
				usage
112
			fi
113
			export BUILD_KERNELS="${1}"
114
			;;
115
		--update-sources)
116
			BUILDACTION="updatesources"
117
			;;
118
		--clean-builder)
119
			BUILDACTION="cleanbuilder"
120
			;;
121
		--setup-poudriere)
122
			BUILDACTION="setup_poudriere"
123
			;;
124
		--create-unified-patch)
125
			BUILDACTION="create_unified_patch"
126
			;;
127
		--update-poudriere-jails)
128
			BUILDACTION="update_poudriere_jails"
129
			;;
130
		-a)
131
			shift
132
			if [ $# -eq 0 ]; then
133
				echo "-a needs extra parameter."
134
				echo
135
				usage
136
			fi
137
			export ARCH_LIST="${1}"
138
			;;
139
		--update-poudriere-ports)
140
			BUILDACTION="update_poudriere_ports"
141
			;;
142
		--update-pkg-repo)
143
			BUILDACTION="update_pkg_repo"
144
			;;
145
		--do-not-upload|-u)
146
			export DO_NOT_UPLOAD=1
147
			;;
148
		all|none|*iso*|*ova*|*memstick*|*memstickserial*|*memstickadi*)
149
			BUILDACTION="images"
150
			IMAGETYPE="${1}"
151
			;;
152
		-V)
153
			_USE_OLD_DATESTRING=YES
154
			shift
155
			[ -n "${1}" ] \
156
				&& var_to_print="${1}"
157
			;;
158
		--snapshot-update-status)
159
			shift
160
			snapshot_status_message="${1}"
161
			BUILDACTION="snapshot_status_message"
162
			_USE_OLD_DATESTRING=YES
163
			;;
164
		*)
165
			_USE_OLD_DATESTRING=YES
166
			usage
167
	esac
168
	shift
169
done
170

    
171
# Suck in local vars
172
. ${BUILDER_TOOLS}/builder_defaults.sh
173

    
174
# Suck in script helper functions
175
. ${BUILDER_TOOLS}/builder_common.sh
176

    
177
# Print var required with -V and exit
178
if [ -n "${var_to_print}"  ]; then
179
	eval "echo \$${var_to_print}"
180
	exit 0
181
fi
182

    
183
# Update snapshot status and exit
184
if [ "${BUILDACTION}" = "snapshot_status_message" ]; then
185
	if [ -z "${POUDRIERE_SNAPSHOTS}" ]; then
186
		export SNAPSHOTS=1
187
	fi
188
	snapshots_update_status "${snapshot_status_message}"
189
	exit 0
190
fi
191

    
192
# This should be run first
193
launch
194

    
195
case $BUILDACTION in
196
	builder_setup)
197
		builder_setup
198
	;;
199
	buildkernels)
200
		update_freebsd_sources
201
		build_all_kernels
202
	;;
203
	buildkernel)
204
		update_freebsd_sources
205
		build_all_kernels
206
	;;
207
	cleanbuilder)
208
		clean_builder
209
	;;
210
	images)
211
		# It will be handled below
212
	;;
213
	updatesources)
214
		update_freebsd_sources
215
	;;
216
	setup_poudriere)
217
		poudriere_init
218
	;;
219
	create_unified_patch)
220
		poudriere_create_patch
221
	;;
222
	update_poudriere_jails)
223
		poudriere_update_jails
224
	;;
225
	update_poudriere_ports)
226
		poudriere_update_ports
227
	;;
228
	rsync_repos)
229
		unset SKIP_FINAL_RSYNC
230
		pkg_repo_rsync "${CORE_PKG_PATH}"
231
	;;
232
	update_pkg_repo)
233
		if [ -z "${DO_NOT_UPLOAD}" -a ! -f /usr/local/bin/rsync ]; then
234
			echo "ERROR: rsync is not installed, aborting..."
235
			exit 1
236
		fi
237
		poudriere_bulk
238
	;;
239
	*)
240
		usage
241
	;;
242
esac
243

    
244
if [ "${BUILDACTION}" != "images" ]; then
245
	finish
246
	exit 0
247
fi
248

    
249
if [ -n "${SNAPSHOTS}" -a -z "${DO_NOT_UPLOAD}" ]; then
250
	_required=" \
251
		RSYNCIP \
252
		RSYNCUSER \
253
		RSYNCPATH \
254
		PKG_RSYNC_HOSTNAME \
255
		PKG_RSYNC_USERNAME \
256
		PKG_RSYNC_SSH_PORT \
257
		PKG_RSYNC_DESTDIR \
258
		PKG_REPO_SERVER_DEVEL \
259
		PKG_REPO_SERVER_RELEASE \
260
		PKG_REPO_SERVER_STAGING \
261
		PKG_REPO_BRANCH_DEVEL \
262
		PKG_REPO_BRANCH_RELEASE"
263

    
264
	for _var in ${_required}; do
265
		eval "_value=\${$_var}"
266
		if [ -z "${_value}" ]; then
267
			echo ">>> ERROR: ${_var} is not defined"
268
			exit 1
269
		fi
270
	done
271

    
272
	if [ ! -f /usr/local/bin/rsync ]; then
273
		echo "ERROR: rsync is not installed, aborting..."
274
		exit 1
275
	fi
276
fi
277

    
278
if [ $# -gt 1 ]; then
279
	echo "ERROR: Too many arguments given."
280
	echo
281
	usage
282
fi
283

    
284
if [ -n "${SNAPSHOTS}" -a -z "${IMAGETYPE}" ]; then
285
	IMAGETYPE="all"
286
fi
287

    
288
if [ -z "${IMAGETYPE}" ]; then
289
	echo "ERROR: Need to specify image type to build."
290
	echo
291
	usage
292
fi
293

    
294
if [ "$IMAGETYPE" = "none" ]; then
295
	_IMAGESTOBUILD=""
296
elif [ "$IMAGETYPE" = "all" ]; then
297
	_IMAGESTOBUILD="iso memstick memstickserial"
298
	if [ "${TARGET}" = "amd64" ]; then
299
		_IMAGESTOBUILD="${_IMAGESTOBUILD} memstickadi"
300
		if [ -n "${_IS_RELEASE}"  ]; then
301
			_IMAGESTOBUILD="${_IMAGESTOBUILD} ova"
302
		fi
303
	fi
304
else
305
	_IMAGESTOBUILD="${IMAGETYPE}"
306
fi
307

    
308
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
309

    
310
if [ -n "${SNAPSHOTS}" ]; then
311
	snapshots_update_status ">>> Starting snapshot build operations"
312

    
313
	if pkg update -r ${PRODUCT_NAME} >/dev/null 2>&1; then
314
		snapshots_update_status ">>> Updating builder packages... "
315
		pkg upgrade -r ${PRODUCT_NAME} -y -q >/dev/null 2>&1
316
	fi
317
fi
318

    
319
if [ -z "${_SKIP_REBUILD_PRESTAGE}" ]; then
320
	[ -n "${CORE_PKG_PATH}" -a -d "${CORE_PKG_PATH}" ] \
321
		&& rm -rf ${CORE_PKG_PATH}
322

    
323
	# Cleanup environment before start
324
	clean_builder
325

    
326
	# Make sure source directories are present.
327
	update_freebsd_sources
328
	git_last_commit
329

    
330
	# Ensure binaries are present that builder system requires
331
	builder_setup
332

    
333
	# Build world, kernel and install
334
	make_world
335

    
336
	# Build kernels
337
	build_all_kernels
338

    
339
	# Install kernel on installer
340
	installkernel ${INSTALLER_CHROOT_DIR} ${PRODUCT_NAME}
341

    
342
	# Prepare pre-final staging area
343
	clone_to_staging_area
344

    
345
	# Install packages needed for Product
346
	install_pkg_install_ports
347
fi
348

    
349
# Create core repo
350
core_pkg_create_repo
351

    
352
# Send core repo to staging area
353
pkg_repo_rsync "${CORE_PKG_PATH}" ignore_final_rsync
354

    
355
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
356

    
357
# XXX: Figure out why wait is failing and proper fix
358
# Global variable to keep track of process running in bg
359
export _bg_pids=""
360

    
361
for _IMGTOBUILD in $_IMAGESTOBUILD; do
362
	# Clean up items that should be cleaned each run
363
	staginareas_clean_each_run
364

    
365
	case "${_IMGTOBUILD}" in
366
		iso)
367
			if [ -n "${ISO_VARIANTS}" ]; then
368
				for _variant in ${ISO_VARIANTS}; do
369
					create_iso_image ${_variant}
370
				done
371
			else
372
				create_iso_image
373
			fi
374
			;;
375
		memstick)
376
			if [ -n "${MEMSTICK_VARIANTS}" ]; then
377
				for _variant in ${MEMSTICK_VARIANTS}; do
378
					create_memstick_image ${_variant}
379
				done
380
			else
381
				create_memstick_image
382
			fi
383
			;;
384
		memstickserial)
385
			create_memstick_serial_image
386
			;;
387
		memstickadi)
388
			create_memstick_adi_image
389
			;;
390
		ova)
391
			old_custom_package_list="${custom_package_list}"
392
			export custom_package_list="${custom_package_list} ${PRODUCT_NAME}-pkg-Open-VM-Tools"
393
			install_pkg_install_ports
394
			create_ova_image
395
			export custom_package_list="${old_custom_package_list}"
396
			install_pkg_install_ports
397
			;;
398
	esac
399
done
400

    
401
if [ -n "${_bg_pids}" ]; then
402
	if [ -n "${SNAPSHOTS}" ]; then
403
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
404
	else
405
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
406
	fi
407
	wait
408

    
409
	# XXX: For some reason wait is failing, workaround it tracking all PIDs
410
	while [ -n "${_bg_pids}" ]; do
411
		_tmp_pids="${_bg_pids}"
412
		unset _bg_pids
413
		for p in ${_tmp_pids}; do
414
			[ -z "${p}" ] \
415
				&& continue
416

    
417
			kill -0 ${p} >/dev/null 2>&1 \
418
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
419
		done
420
		[ -n "${_bg_pids}" ] \
421
			&& sleep 1
422
	done
423
fi
424

    
425
if [ -n "${SNAPSHOTS}" ]; then
426
	if [ "${IMAGETYPE}" = "none" -a -z "${DO_NOT_UPLOAD}" ]; then
427
		pkg_repo_rsync "${CORE_PKG_PATH}"
428
	elif [ "${IMAGETYPE}" != "none" ]; then
429
		snapshots_create_sha256
430
		# SCP files to snapshot web hosting area
431
		if [ -z "${DO_NOT_UPLOAD}" ]; then
432
			snapshots_scp_files
433
		fi
434
	fi
435
	# Alert the world that we have some snapshots ready.
436
	snapshots_update_status ">>> Builder run is complete."
437
fi
438

    
439
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
440
(cd ${IMAGES_FINAL_DIR} && find ${IMAGES_FINAL_DIR} -type f)
441

    
442
set -e
443
# Run final finish routines
444
finish
(8-8/9)