3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
5 # stefan.k.berg@ericsson.com
6 # jonas.bjurel@ericsson.com
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Apache License, Version 2.0
9 # which accompanies this distribution, and is available at
10 # http://www.apache.org/licenses/LICENSE-2.0
11 ##############################################################################
13 trap 'echo "Exiting ..."; \
14 if [ -f ${LOCK_FILE} ]; then \
15 if [ $(cat ${LOCK_FILE}) -eq $$ ]; then \
20 ############################################################################
21 # BEGIN of usage description
26 $0 Builds the Fuel@OPNFV stack
28 usage: $0 [-s spec-file] [-c cache-URI] [-l log-file] [-f Flags] build-directory
31 -s spec-file ($BUILD_SPEC), define the build-spec file, default ../build/config.mk
32 -c cache base URI ($BUILD_CACHE_URI), specifies the base URI to a build cache to be used/updated - the name is automatically generated from the md5sum of the spec-file, http://, ftp://, file://[absolute path] suported.
34 -l log-file ($BUILD_LOG), specifies the output log-file (stdout and stderr), if not specified logs are output to console as normal
35 -v version tag to be applied to the build result
36 -r alternative remote access method script/program. curl is default.
37 -t run small build-script unit test.
38 -T run large build-script unit test.
39 -f build flags ($BUILD_FLAGS):
40 o s: Do nothing, succeed
42 o t: run build unit tests
43 o i: run interactive (-t flag to docker run)
44 o P: Populate a new local cache and push it to the (-c cache-URI) cache artifactory if -c option is present, currently file://, http:// and ftp:// are supported
45 o d: Detatch - NOT YET SUPPORTED
47 build-directory ($BUILD_DIR), specifies the directory for the output artifacts (.iso file).
49 -h help, prints this help text
52 build.sh builds opnfv .iso artifact.
53 To reduce build time it uses build cache on a local or remote location. The cache is rebuilt and uploaded if either of the below conditions are met:
54 1) The P(opulate) flag is set and the -c cache-base-URI is provided, if -c is not provided the cache will stay local.
55 2) If the cache is invalidated by one of the following conditions:
56 - The config spec md5sum does not compare to the md5sum for the spec which the cache was built.
57 - The git Commit-Id on the remote repos/HEAD defined in the spec file does not correspont with the Commit-Id for what the cache was built with.
58 3) A valid cache does not exist on the specified -c cache-base-URI.
60 The cache URI object name is fuel_cache-"md5sum(spec file)"
62 Logging by default to console, but can be directed elsewhere with the -l option in which case both stdout and stderr is redirected to that destination.
64 Built in unit testing of components is enabled by adding the t(est) flag.
68 - 1-99 Unspecified build error
69 - 100-199 Build system internal error (not build it self)
70 o 101 Build system instance busy
74 build -c http://opnfv.org/artifactory/fuel/cache -d ~/jenkins/genesis/fuel/ci/output -f ti
75 NOTE: At current the build scope is set to the git root of the repository, -d destination locations outside that scope will not work
79 # END of usage description
80 ############################################################################
82 ############################################################################
83 # BEGIN of variables to customize
85 BUILD_BASE=$(readlink -e ../build/)
86 RESULT_DIR="${BUILD_BASE}/release"
87 BUILD_SPEC="${BUILD_BASE}/config.mk"
89 LOCAL_CACHE_ARCH_NAME="fuel-cache"
90 REMOTE_CACHE_ARCH_NAME="fuel_cache-$(md5sum ${BUILD_SPEC}| cut -f1 -d " ")"
91 REMOTE_ACCESS_METHD=curl
92 INCLUDE_DIR=../include
94 # END of variables to customize
95 ############################################################################
97 ############################################################################
98 # BEGIN of script assigned variables
100 SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
101 LOCK_FILE="${SCRIPT_DIR}/.build.lck"
102 CACHE_TMP="${SCRIPT_DIR}/tmp"
112 FULL_INTEGRATION_TEST=0
121 # END of script assigned variables
122 ############################################################################
124 ############################################################################
125 # BEGIN of include pragmas
127 source ${INCLUDE_DIR}/build.sh.debug
130 ############################################################################
132 ############################################################################
135 while getopts "s:c:v:f:l:r:RtTh" OPTION
149 BUILD_CACHE_URI=${OPTARG}
157 BUILD_VERSION=${OPTARG}
161 BUILD_FLAGS=${OPTARG}
164 r) REMOTE_ACCESS_METHD=${OPTARG}
177 FULL_INTEGRATION_TEST=1
181 echo "${OPTION} is not a valid argument"
188 if [ -z $BUILD_DIR ]; then
189 BUILD_DIR=$(echo $@ | cut -d ' ' -f ${OPTIND})
192 for ((i=0; i<${#BUILD_FLAGS};i++)); do
193 case ${BUILD_FLAGS:$i:1} in
218 echo "Detach is not yet supported - exiting ...."
228 echo "${BUILD_FLAGS:$i:1} is not a valid build flag - exiting ...."
235 if [ ${INTEGRATION_TEST} -eq 1 ]; then
241 if [ ! -f ${BUILD_SPEC} ]; then
242 echo "spec file does not exist: $BUILD_SPEC - exiting ...."
247 if [ -z ${BUILD_DIR} ]; then
248 echo "Missing build directory - exiting ...."
253 if [ ! -z ${BUILD_LOG} ]; then
254 if [[ ${RECURSIVE} -ne 1 ]]; then
256 eval $0 -R $@ > ${BUILD_LOG} 2>&1
259 if [ $rc -ne 0]; then
265 if [ ${TEST_SUCCEED} -eq 1 ]; then
271 if [ ${TEST_FAIL} -eq 1 ]; then
277 if [ -e ${LOCK_FILE} ]; then
278 echo "A build job is already running, exiting....."
283 echo $$ > ${LOCK_FILE}
285 if [ ! -z ${BUILD_CACHE_URI} ]; then
286 if [ ${POPULATE_CACHE} -ne 1 ]; then
287 rm -rf ${CACHE_TMP}/cache
288 mkdir -p ${CACHE_TMP}/cache
289 echo "Downloading cache archive ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME} ..."
291 ${REMOTE_ACCESS_METHD} -o ${CACHE_TMP}/cache/${LOCAL_CACHE_ARCH_NAME}.tgz ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}.tgz
294 if [ $rc -ne 0 ]; then
295 echo "Remote cache does not exist, or is not accessible - a new cache will be built ..."
298 echo "Unpacking cache archive ..."
300 tar -C ${CACHE_TMP}/cache -xvf ${CACHE_TMP}/cache/${LOCAL_CACHE_ARCH_NAME}.tgz
303 if [ $rc -ne 0 ]; then
304 echo "WARNING: The cache seems to be corrupt or has trailing garbage, will try to use brute force"
305 echo "Info about the cache below:"
307 file ${CACHE_TMP}/cache/${LOCAL_CACHE_ARCH_NAME}.tgz
308 tar -C ${CACHE_TMP}/cache -tvf ${CACHE_TMP}/cache/${LOCAL_CACHE_ARCH_NAME}.tgz
310 echo "Current time is: `date`"
312 pushd ${CACHE_TMP}/cache
313 gunzip -dcq ${CACHE_TMP}/cache/${LOCAL_CACHE_ARCH_NAME}.tgz | tar -xvf -
317 if [ $rc -ne 0 ]; then
318 echo "ERROR: Not able to resolve the cache corruption"
321 echo "The chache corruption was resolved"
322 cp ${CACHE_TMP}/cache/cache/.versions ${BUILD_BASE}/.
324 make -C ${BUILD_BASE} validate-cache;
327 if [ $rc -ne 0 ]; then
328 echo "Cache invalid - a new cache will be built "
331 echo "Cache is up to date and will be used"
332 cp -rf ${CACHE_TMP}/cache/cache/. ${BUILD_BASE}
336 echo "Cache archive is intact"
337 cp ${CACHE_TMP}/cache/cache/.versions ${BUILD_BASE}/.
339 make -C ${BUILD_BASE} validate-cache;
343 if [ $rc -ne 0 ]; then
344 echo "Cache invalid - a new cache will be built "
347 echo "Cache is up to date and will be used"
348 cp -rf ${CACHE_TMP}/cache/cache/. ${BUILD_BASE}
351 rm -rf ${CACHE_TMP}/cache
356 if [ ${POPULATE_CACHE} -eq 1 ]; then
357 if [ ${DEBUG} -eq 0 ]; then
359 cd ${BUILD_BASE} && make clean
362 if [ $rc -ne 0 ]; then
363 echo "Build - make clean failed, exiting ..."
370 if [ ! -z ${BUILD_VERSION} ]; then
371 MAKE_ARGS+="REVSTATE=${BUILD_VERSION} "
374 if [ ${UNIT_TEST} -eq 1 ]; then
375 MAKE_ARGS+="UNIT_TEST=TRUE "
377 MAKE_ARGS+="UNIT_TEST=FALSE "
380 if [ ${INTERACTIVE} -eq 1 ]; then
381 MAKE_ARGS+="INTERACTIVE=TRUE "
383 MAKE_ARGS+="INTERACTIVE=FALSE "
388 if [ ${DEBUG} -eq 0 ]; then
390 cd ${BUILD_BASE} && make ${MAKE_ARGS}
393 if [ $rc -gt 0 ]; then
394 echo "Build: make all failed, exiting ..."
402 make -C ${BUILD_BASE} prepare-cache
406 if [ $rc -gt 0 ]; then
407 echo "Build: make prepare-cache failed - exiting ..."
411 echo "Copying built OPNFV .iso file to target directory ${BUILD_DIR} ..."
413 mkdir -p ${BUILD_DIR}
414 cp ${BUILD_BASE}/.versions ${BUILD_DIR}
415 cp ${RESULT_DIR}/*.iso* ${BUILD_DIR}
417 if [ $POPULATE_CACHE -eq 1 ]; then
418 if [ ! -z ${BUILD_CACHE_URI} ]; then
419 echo "Building cache ..."
420 tar --dereference -C ${BUILD_BASE} -caf ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz ${CACHE_DIR}
422 tar -C ${CACHE_TMP}/cache -tvf ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz
425 if [ $rc -ne 0 ]; then
426 echo "WARNING the cache archive generated seems to be corrupt, or containing trailing garbage"
428 echo "The Cache archive build is intact"
430 echo "Uploading cache ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}"
431 ${REMOTE_ACCESS_METHD} -T ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}.tgz
432 rm ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz
439 ############################################################################