Project

General

Profile

Download (10.5 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
			_SKIP_REBUILD_PRESTAGE=YES
77
			_USE_OLD_DATESTRING=YES
78
			;;
79
		--setup)
80
			BUILDACTION="builder_setup"
81
			;;
82
		--rsync-repos)
83
			BUILDACTION="rsync_repos"
84
			;;
85
		--build-kernels)
86
			BUILDACTION="buildkernels"
87
			;;
88
		--install-extra-kernels)
89
			shift
90
			if [ $# -eq 0 ]; then
91
				echo "--build-kernel needs extra parameter."
92
				echo
93
				usage
94
			fi
95
			export INSTALL_EXTRA_KERNELS="${1}"
96
			;;
97
		--snapshots)
98
			export SNAPSHOTS=1
99
			;;
100
		--poudriere-snapshots)
101
			export POUDRIERE_SNAPSHOTS=1
102
			;;
103
		--build-kernel)
104
			BUILDACTION="buildkernel"
105
			shift
106
			if [ $# -eq 0 ]; then
107
				echo "--build-kernel needs extra parameter."
108
				echo
109
				usage
110
			fi
111
			export BUILD_KERNELS="${1}"
112
			;;
113
		--update-sources)
114
			BUILDACTION="updatesources"
115
			;;
116
		--clean-builder)
117
			BUILDACTION="cleanbuilder"
118
			;;
119
		--setup-poudriere)
120
			BUILDACTION="setup_poudriere"
121
			;;
122
		--create-unified-patch)
123
			BUILDACTION="create_unified_patch"
124
			;;
125
		--update-poudriere-jails)
126
			BUILDACTION="update_poudriere_jails"
127
			;;
128
		-a)
129
			shift
130
			if [ $# -eq 0 ]; then
131
				echo "-a needs extra parameter."
132
				echo
133
				usage
134
			fi
135
			export ARCH_LIST="${1}"
136
			;;
137
		--update-poudriere-ports)
138
			BUILDACTION="update_poudriere_ports"
139
			;;
140
		--update-pkg-repo)
141
			BUILDACTION="update_pkg_repo"
142
			;;
143
		--do-not-upload|-u)
144
			export DO_NOT_UPLOAD=1
145
			;;
146
		all|none|*iso*|*ova*|*memstick*|*memstickserial*|*memstickadi*)
147
			BUILDACTION="images"
148
			IMAGETYPE="${1}"
149
			;;
150
		-V)
151
			_USE_OLD_DATESTRING=YES
152
			shift
153
			[ -n "${1}" ] \
154
				&& var_to_print="${1}"
155
			;;
156
		--snapshot-update-status)
157
			shift
158
			snapshot_status_message="${1}"
159
			BUILDACTION="snapshot_status_message"
160
			_USE_OLD_DATESTRING=YES
161
			;;
162
		*)
163
			_USE_OLD_DATESTRING=YES
164
			usage
165
	esac
166
	shift
167
done
168

    
169
# Suck in local vars
170
. ${BUILDER_TOOLS}/builder_defaults.sh
171

    
172
# Suck in script helper functions
173
. ${BUILDER_TOOLS}/builder_common.sh
174

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

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

    
190
# This should be run first
191
launch
192

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

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

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

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

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

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

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

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

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

    
306
echo ">>> Building image type(s): ${_IMAGESTOBUILD}"
307

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

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

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

    
321
	# Cleanup environment before start
322
	clean_builder
323

    
324
	# Make sure source directories are present.
325
	update_freebsd_sources
326
	git_last_commit
327

    
328
	# Ensure binaries are present that builder system requires
329
	builder_setup
330

    
331
	# Build world, kernel and install
332
	make_world
333

    
334
	# Build kernels
335
	build_all_kernels
336

    
337
	# Install kernel on installer
338
	installkernel ${INSTALLER_CHROOT_DIR} ${PRODUCT_NAME}
339

    
340
	# Prepare pre-final staging area
341
	clone_to_staging_area
342

    
343
	# Install packages needed for Product
344
	install_pkg_install_ports
345
fi
346

    
347
# Create core repo
348
core_pkg_create_repo
349

    
350
# Send core repo to staging area
351
pkg_repo_rsync "${CORE_PKG_PATH}" ignore_final_rsync
352

    
353
export DEFAULT_KERNEL=${DEFAULT_KERNEL_ISO:-"${PRODUCT_NAME}"}
354

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

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

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

    
393
if [ -n "${_bg_pids}" ]; then
394
	if [ -n "${SNAPSHOTS}" ]; then
395
		snapshots_update_status ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
396
	else
397
		echo ">>> NOTE: waiting for jobs: ${_bg_pids} to finish..."
398
	fi
399
	wait
400

    
401
	# XXX: For some reason wait is failing, workaround it tracking all PIDs
402
	while [ -n "${_bg_pids}" ]; do
403
		_tmp_pids="${_bg_pids}"
404
		unset _bg_pids
405
		for p in ${_tmp_pids}; do
406
			[ -z "${p}" ] \
407
				&& continue
408

    
409
			kill -0 ${p} >/dev/null 2>&1 \
410
				&& _bg_pids="${_bg_pids}${_bg_pids:+ }${p}"
411
		done
412
		[ -n "${_bg_pids}" ] \
413
			&& sleep 1
414
	done
415
fi
416

    
417
if [ -n "${SNAPSHOTS}" ]; then
418
	if [ "${IMAGETYPE}" = "none" -a -z "${DO_NOT_UPLOAD}" ]; then
419
		pkg_repo_rsync "${CORE_PKG_PATH}"
420
	elif [ "${IMAGETYPE}" != "none" ]; then
421
		snapshots_create_sha256
422
		# SCP files to snapshot web hosting area
423
		if [ -z "${DO_NOT_UPLOAD}" ]; then
424
			snapshots_scp_files
425
		fi
426
	fi
427
	# Alert the world that we have some snapshots ready.
428
	snapshots_update_status ">>> Builder run is complete."
429
fi
430

    
431
echo ">>> ${IMAGES_FINAL_DIR} now contains:"
432
(cd ${IMAGES_FINAL_DIR} && find ${IMAGES_FINAL_DIR} -type f)
433

    
434
set -e
435
# Run final finish routines
436
finish
(8-8/8)