3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
5 # stefan.k.berg@ericsson.com
6 # jonas.bjurel@ericsson.com
8 # All rights reserved. This program and the accompanying materials
9 # are made available under the terms of the Apache License, Version 2.0
10 # which accompanies this distribution, and is available at
11 # http://www.apache.org/licenses/LICENSE-2.0
12 ##############################################################################
14 trap 'echo "Exiting ..."; \
15 if [ -f ${LOCK_FILE} ]; then \
16 if [ $(cat ${LOCK_FILE}) -eq $$ ]; then \
21 ############################################################################
22 # BEGIN of usage description
27 $0 Builds the Apex OPNFV Deployment Toolchain
29 usage: $0 [-s spec-file] [-c cache-URI] [-l log-file] [-f Flags] build-directory
32 -s spec-file ($BUILD_SPEC), define the build-spec file, default ../build/config.mk
33 -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.
35 -l log-file ($BUILD_LOG), specifies the output log-file (stdout and stderr), if not specified logs are output to console as normal
36 -v version tag to be applied to the build result
37 -r alternative remote access method script/program. curl is default.
38 -t run small build-script unit test.
39 -T run large build-script unit test.
40 -f build flags ($BUILD_FLAGS):
41 o s: Do nothing, succeed
43 o t: run build unit tests
44 o M: Use master branch code
45 o i: run interactive (-t flag to docker run)
46 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
47 o d: Detatch - NOT YET SUPPORTED
49 build-directory ($BUILD_DIR), specifies the directory for the output artifacts (.iso file).
51 -h help, prints this help text
54 build.sh builds opnfv .iso artifact.
55 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:
56 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.
57 2) If the cache is invalidated by one of the following conditions:
58 - The config spec md5sum does not compare to the md5sum for the spec which the cache was built.
59 - 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.
60 3) A valid cache does not exist on the specified -c cache-base-URI.
62 The cache URI object name is apex_cache-"md5sum(spec file)"
64 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.
66 Built in unit testing of components is enabled by adding the t(est) flag.
70 - 1-99 Unspecified build error
71 - 100-199 Build system internal error (not build it self)
72 o 101 Build system instance busy
76 build -c http://opnfv.org/artifactory/apex/cache -d ~/jenkins/genesis/apex/ci/output -f ti
77 NOTE: At current the build scope is set to the git root of the repository, -d destination locations outside that scope will not work
81 # END of usage description
82 ############################################################################
84 ############################################################################
85 # BEGIN of variables to customize
87 BUILD_BASE=$(readlink -e ../build/)
88 RESULT_DIR="${BUILD_BASE}/release"
89 BUILD_SPEC="${BUILD_BASE}/config.mk"
91 LOCAL_CACHE_ARCH_NAME="apex-cache"
92 REMOTE_CACHE_ARCH_NAME="apex_cache-$(md5sum ${BUILD_SPEC}| cut -f1 -d " ")"
93 REMOTE_ACCESS_METHD=curl
94 INCLUDE_DIR=../include
96 # END of variables to customize
97 ############################################################################
99 ############################################################################
100 # BEGIN of script assigned variables
102 SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
103 LOCK_FILE="${SCRIPT_DIR}/.build.lck"
104 CACHE_TMP="${SCRIPT_DIR}/tmp"
115 FULL_INTEGRATION_TEST=0
124 # END of script assigned variables
125 ############################################################################
127 ############################################################################
128 # BEGIN of include pragmas
130 source ${INCLUDE_DIR}/build.sh.debug
133 ############################################################################
135 ############################################################################
138 while getopts "s:c:d:v:f:l:r:RtTh" OPTION
152 BUILD_CACHE_URI=${OPTARG}
164 BUILD_VERSION=${OPTARG}
168 BUILD_FLAGS=${OPTARG}
171 r) REMOTE_ACCESS_METHD=${OPTARG}
184 FULL_INTEGRATION_TEST=1
188 echo "${OPTION} is not a valid argument"
195 if [ -z $BUILD_DIR ]; then
196 BUILD_DIR=$(echo $@ | cut -d ' ' -f ${OPTIND})
199 for ((i=0; i<${#BUILD_FLAGS};i++)); do
200 case ${BUILD_FLAGS:$i:1} in
229 echo "Detach is not yet supported - exiting ...."
239 echo "${BUILD_FLAGS:$i:1} is not a valid build flag - exiting ...."
248 if [ ${INTEGRATION_TEST} -eq 1 ]; then
254 if [ ! -f ${BUILD_SPEC} ]; then
255 echo "spec file does not exist: $BUILD_SPEC - exiting ...."
260 if [ -z ${BUILD_DIR} ]; then
261 echo "Missing build directory - exiting ...."
266 if [ ! -z ${BUILD_LOG} ]; then
267 if [[ ${RECURSIVE} -ne 1 ]]; then
269 eval $0 -R $@ > ${BUILD_LOG} 2>&1
272 if [ $rc -ne 0]; then
278 if [ ${TEST_SUCCEED} -eq 1 ]; then
284 if [ ${TEST_FAIL} -eq 1 ]; then
290 if [ -e ${LOCK_FILE} ]; then
291 echo "A build job is already running, exiting....."
296 echo $$ > ${LOCK_FILE}
298 if [ ! -z ${BUILD_CACHE_URI} ]; then
299 if [ ${POPULATE_CACHE} -ne 1 ]; then
300 rm -rf ${CACHE_TMP}/cache
301 mkdir -p ${CACHE_TMP}/cache
302 echo "Downloading cach file ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME} ..."
304 ${REMOTE_ACCESS_METHD} -o ${CACHE_TMP}/cache/${LOCAL_CACHE_ARCH_NAME}.tgz ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}.tgz
307 if [ $rc -ne 0 ]; then
308 echo "Remote cache does not exist, or is not accessible - a new cache will be built ..."
311 echo "Unpacking cache file ..."
312 tar --atime-preserve -C ${CACHE_TMP}/cache -xvf ${CACHE_TMP}/cache/${LOCAL_CACHE_ARCH_NAME}.tgz
313 cp ${CACHE_TMP}/cache/cache/.versions ${BUILD_BASE}/.
315 make -C ${BUILD_BASE} validate-cache;
319 if [ $rc -ne 0 ]; then
320 echo "Cache invalid - a new cache will be built "
323 cp -rf ${CACHE_TMP}/cache/cache/. ${BUILD_BASE}
325 rm -rf ${CACHE_TMP}/cache
330 if [ ${POPULATE_CACHE} -eq 1 ]; then
331 if [ ${DEBUG} -eq 0 ]; then
333 cd ${BUILD_BASE} && make clean
336 if [ $rc -ne 0 ]; then
337 echo "Build - make clean failed, exiting ..."
344 if [ ! -z ${BUILD_VERSION} ]; then
345 MAKE_ARGS+="REVSTATE=${BUILD_VERSION} "
348 if [ ${UNIT_TEST} -eq 1 ]; then
349 MAKE_ARGS+="UNIT_TEST=TRUE "
351 MAKE_ARGS+="UNIT_TEST=FALSE "
354 if [ ${USE_MASTER} -eq 1 ]; then
355 MAKE_ARGS+="USE_MASTER=-master "
358 if [ ${INTERACTIVE} -eq 1 ]; then
359 MAKE_ARGS+="INTERACTIVE=TRUE "
361 MAKE_ARGS+="INTERACTIVE=FALSE "
366 if [ ${DEBUG} -eq 0 ]; then
368 cd ${BUILD_BASE} && make ${MAKE_ARGS}
371 if [ $rc -gt 0 ]; then
372 echo "Build: make all failed, exiting ..."
380 make -C ${BUILD_BASE} prepare-cache
384 if [ $rc -gt 0 ]; then
385 echo "Build: make prepare-cache failed - exiting ..."
389 echo "Linking built OPNFV .iso file to target directory ${BUILD_DIR} ..."
391 mkdir -p ${BUILD_DIR}
392 ln -s ${BUILD_BASE}/.versions ${BUILD_DIR}
393 ln -s ${RESULT_DIR}/*.iso* ${BUILD_DIR}
394 echo "Linking built OPNFV .rpm files to target directory ${BUILD_DIR} ..."
395 ln -s ${BUILD_BASE}/*.rpm ${BUILD_DIR}
396 ln -s ${BUILD_BASE}/noarch/*.rpm ${BUILD_DIR}
398 if [ $POPULATE_CACHE -eq 1 ]; then
399 if [ ! -z ${BUILD_CACHE_URI} ]; then
400 echo "Building cache ..."
401 tar --atime-preserve --dereference -C ${BUILD_BASE} -caf ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz ${CACHE_DIR}
402 echo "Uploading cache ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}"
403 ${REMOTE_ACCESS_METHD} -T ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}.tgz
404 rm ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz
411 ############################################################################