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"
111 FULL_INTEGRATION_TEST=0
120 # END of script assigned variables
121 ############################################################################
123 ############################################################################
124 # BEGIN of include pragmas
126 source ${INCLUDE_DIR}/build.sh.debug
129 ############################################################################
131 ############################################################################
134 while getopts "s:c:v:f:l:r:RtTh" OPTION
148 BUILD_CACHE_URI=${OPTARG}
156 BUILD_VERSION=${OPTARG}
160 BUILD_FLAGS=${OPTARG}
163 r) REMOTE_ACCESS_METHD=${OPTARG}
176 FULL_INTEGRATION_TEST=1
180 echo "${OPTION} is not a valid argument"
187 if [ -z $BUILD_DIR ]; then
188 BUILD_DIR=$(echo $@ | cut -d ' ' -f ${OPTIND})
191 for ((i=0; i<${#BUILD_FLAGS};i++)); do
192 case ${BUILD_FLAGS:$i:1} in
217 echo "Detach is not yet supported - exiting ...."
227 echo "${BUILD_FLAGS:$i:1} is not a valid build flag - exiting ...."
234 if [ ${INTEGRATION_TEST} -eq 1 ]; then
240 if [ ! -f ${BUILD_SPEC} ]; then
241 echo "spec file does not exist: $BUILD_SPEC - exiting ...."
246 if [ -z ${BUILD_DIR} ]; then
247 echo "Missing build directory - exiting ...."
252 if [ ! -z ${BUILD_LOG} ]; then
253 if [[ ${RECURSIVE} -ne 1 ]]; then
255 eval $0 -R $@ > ${BUILD_LOG} 2>&1
258 if [ $rc -ne 0]; then
264 if [ ${TEST_SUCCEED} -eq 1 ]; then
270 if [ ${TEST_FAIL} -eq 1 ]; then
276 if [ -e ${LOCK_FILE} ]; then
277 echo "A build job is already running, exiting....."
282 echo $$ > ${LOCK_FILE}
284 if [ ! -z ${BUILD_CACHE_URI} ]; then
285 if [ ${POPULATE_CACHE} -ne 1 ]; then
288 echo "Downloading cach file ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME} ..."
290 ${REMOTE_ACCESS_METHD} -o /tmp/cache/${LOCAL_CACHE_ARCH_NAME}.tgz ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}.tgz
293 if [ $rc -ne 0 ]; then
294 echo "Remote cache does not exist, or is not accessible - a new cache will be built ..."
297 echo "Unpacking cache file ..."
298 tar -C /tmp/cache -xvf /tmp/cache/${LOCAL_CACHE_ARCH_NAME}.tgz
299 cp /tmp/cache/cache/.versions ${BUILD_BASE}/.
301 make -C ${BUILD_BASE} validate-cache;
305 if [ $rc -ne 0 ]; then
306 echo "Cache invalid - a new cache will be built "
309 cp -rf /tmp/cache/cache/. ${BUILD_BASE}
316 if [ ${POPULATE_CACHE} -eq 1 ]; then
317 if [ ${DEBUG} -eq 0 ]; then
319 cd ${BUILD_BASE} && make clean
322 if [ $rc -ne 0 ]; then
323 echo "Build - make clean failed, exiting ..."
330 if [ ! -z ${BUILD_VERSION} ]; then
331 MAKE_ARGS+="REVSTATE=${BUILD_VERSION} "
334 if [ ${UNIT_TEST} -eq 1 ]; then
335 MAKE_ARGS+="UNIT_TEST=TRUE "
337 MAKE_ARGS+="UNIT_TEST=FALSE "
340 if [ ${INTERACTIVE} -eq 1 ]; then
341 MAKE_ARGS+="INTERACTIVE=TRUE "
343 MAKE_ARGS+="INTERACTIVE=FALSE "
348 if [ ${DEBUG} -eq 0 ]; then
350 cd ${BUILD_BASE} && make ${MAKE_ARGS}
353 if [ $rc -gt 0 ]; then
354 echo "Build: make all failed, exiting ..."
362 make -C ${BUILD_BASE} prepare-cache
366 if [ $rc -gt 0 ]; then
367 echo "Build: make prepare-cache failed - exiting ..."
371 echo "Copying built OPNFV .iso file to target directory ${BUILD_DIR} ..."
373 mkdir -p ${BUILD_DIR}
374 cp ${BUILD_BASE}/.versions ${BUILD_DIR}
375 cp ${RESULT_DIR}/*.iso* ${BUILD_DIR}
377 if [ $POPULATE_CACHE -eq 1 ]; then
378 if [ ! -z ${BUILD_CACHE_URI} ]; then
379 echo "Building cache ..."
380 tar --dereference -C ${BUILD_BASE} -caf ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz ${CACHE_DIR}
381 echo "Uploading cache ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}"
382 ${REMOTE_ACCESS_METHD} -T ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}.tgz
383 rm ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz
390 ############################################################################