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 \
23 $0 Builds the Fuel@OPNFV stack
25 usage: $0 [-s spec-file] [-c cache-URI] [-l log-file] [-f Flags] build-directory
28 -s spec-file ($BUILD_SPEC), define the build-spec file, default ../build/config.mk
29 -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.
31 -l log-file ($BUILD_LOG), specifies the output log-file (stdout and stderr), if not specified logs are output to console as normal
32 -v version tag to be applied to the build result
33 -r alternative remote access method script/program. curl is default.
34 -f build flags ($BUILD_FLAGS):
35 o s: Do nothing, succeed
37 o t: run build unit tests
38 o i: run interactive (-t flag to docker run)
39 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
40 o d: Detatch - NOT YET SUPPORTED
42 build-directory ($BUILD_DIR), specifies the directory for the output artifacts (.iso file).
44 -h help, prints this help text
47 build.sh builds opnfv .iso artifact.
48 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:
49 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.
50 2) If the cache is invalidated by one of the following conditions:
51 - The config spec md5sum does not compare to the md5sum for the spec which the cache was built.
52 - 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.
53 3) A valid cache does not exist on the specified -c cache-base-URI.
55 The cache URI object name is fuel_cache-"md5sum(spec file)"
57 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.
59 Built in unit testing of components is enabled by adding the t(est) flag.
63 - 1-99 Unspecified build error
64 - 100-199 Build system internal error (not build it self)
65 o 101 Build system instance busy
69 build -c http://opnfv.org/artifactory/fuel/cache -d ~/jenkins/genesis/fuel/ci/output -f ti
70 NOTE: At current the build scope is set to the git root of the repository, -d destination locations outside that scope will not work
77 make -C ${BUILD_BASE} clean
78 echo "This is a fake debug fuel .iso image" > ${BUILD_BASE}/fuel-6.0.1.iso
79 echo "This is a fake debug versions file" > ${BUILD_BASE}/.versions
80 rm -rf ${BUILD_BASE}/release
81 mkdir ${BUILD_BASE}/release
82 echo "This is a fake debug OPNFV .iso image" > ${BUILD_BASE}/release/fake-debug.iso
83 echo "This is a fake debug OPNFV .iso.txt message" > ${BUILD_BASE}/release/fake-debug.iso.txt
84 echo "This a fake debug odl build history" > ${BUILD_BASE}/opendaylight/.odl-build-history
85 echo "This a fake debug odl build log" > ${BUILD_BASE}/opendaylight/.odl-build.log
89 if [ ! -f ${BUILD_BASE}/fuel-6.0.1.iso ] || \
90 [ ! -f ${BUILD_BASE}/.versions ] || \
91 [ ! -f ${BUILD_BASE}/opendaylight/.odl-build-history ]; \
92 [ ! -f ${BUILD_BASE}/opendaylight/.odl-build.log ]; then
93 echo "Cache not present in the build system"
101 ##### Always succeed integration test ####
102 make -C ${BUILD_BASE} clean
103 echo "TEST - $0 - ALWAYS SUCCEED"
108 if [ $rc -ne 0 ]; then
114 ##### Always fail integration test ####
115 make -C ${BUILD_BASE} clean
116 echo "TEST - $0 - ALWAYS FAIL"
121 if [ $rc -eq 0 ]; then
127 ##### Populate cache integration test ####
128 make -C ${BUILD_BASE} clean
129 echo "TEST - $0 - POPULATE CACHE"
134 $0 -c ${BUILD_CACHE_URI} -f PD tmp/output
137 if [ $rc -ne 0 ]; then
143 if [ ! -f tmp/output/fake-debug.iso ] || [ ! -f tmp/output/fake-debug.iso ]; then
151 ##### Build uinge cache integration test ####
152 make -C ${BUILD_BASE} clean
153 echo "TEST - $0 - BUILD USING CACHE"
155 $0 -c ${BUILD_CACHE_URI} -f D tmp/output
158 if [ $rc -ne 0 ]; then
164 if [ ! -f tmp/output/fake-debug.iso ] || [ ! -f tmp/output/fake-debug.iso ]; then
172 #### Repopulate cache because cache non existant ####
173 make -C ${BUILD_BASE} clean
174 echo "TEST - $0 - BUILD USING CACHE"
177 $0 -c ${BUILD_CACHE_URI} -f D tmp/output
180 if [ $rc -ne 0 ]; then
186 if [ ! -f tmp/output/fake-debug.iso ] || [ ! -f tmp/output/fake-debug.iso ]; then
194 # Repopulate cache because cach is ivalidated
197 echo "All tests passed!"
203 BUILD_BASE=$(readlink -e ../build/)
204 RESULT_DIR="${BUILD_BASE}/release"
205 BUILD_SPEC="${BUILD_BASE}/config.mk"
207 LOCAL_CACHE_ARCH_NAME="fuel-cache"
208 REMOTE_CACHE_ARCH_NAME="fuel_cache-$(md5sum ${BUILD_SPEC}| cut -f1 -d " ")"
209 REMOTE_ACCESS_METHD=curl
211 #SCRIPT ASSIGNED VARIABLES
212 SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
213 LOCK_FILE="${SCRIPT_DIR}/.build.lck"
231 while getopts "s:c:v:f:l:r:RTh" OPTION
245 BUILD_CACHE_URI=${OPTARG}
253 BUILD_VERSION=${OPTARG}
257 BUILD_FLAGS=${OPTARG}
260 r) REMOTE_ACCESS_METHD=${OPTARG}
272 echo "${OPTION} is not a valid argument"
279 if [ -z $BUILD_DIR ]; then
280 BUILD_DIR=$(echo $@ | cut -d ' ' -f ${OPTIND})
283 for ((i=0; i<${#BUILD_FLAGS};i++)); do
284 case ${BUILD_FLAGS:$i:1} in
309 echo "Detach is not yet supported - exiting ...."
319 echo "${BUILD_FLAGS:$i:1} is not a valid build flag"
326 if [ ${INTEGRATION_TEST} -eq 1 ]; then
332 if [ ! -f ${BUILD_SPEC} ]; then
333 echo "spec file does not exist: $BUILD_SPEC"
338 if [ -z ${BUILD_DIR} ]; then
339 echo "Missing build directory"
344 if [ ! -z ${BUILD_LOG} ]; then
345 if [[ ${RECURSIVE} -ne 1 ]]; then
347 eval $0 -R $@ > ${BUILD_LOG} 2>&1
350 if [ $rc -ne 0]; then
356 if [ ${TEST_SUCCEED} -eq 1 ]; then
362 if [ ${TEST_FAIL} -eq 1 ]; then
368 if [ -e ${LOCK_FILE} ]; then
369 echo "A build job is already running, exiting....."
374 echo $$ > ${LOCK_FILE}
376 if [ ! -z ${BUILD_CACHE_URI} ]; then
377 if [ ${POPULATE_CACHE} -ne 1 ]; then
380 echo "Downloading cach file ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME} ..."
382 ${REMOTE_ACCESS_METHD} -o /tmp/cache/${LOCAL_CACHE_ARCH_NAME}.tgz ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}
385 if [ $rc -ne 0 ]; then
386 echo "Remote cache does not exist, or is not accessible - a new cache will be built ..."
389 echo "Unpacking cache file ..."
390 tar -C /tmp/cache -xvf /tmp/cache/${LOCAL_CACHE_ARCH_NAME}.tgz
391 cp /tmp/cache/cache/.versions ${BUILD_BASE}/.
392 echo "Validating cache content ..."
394 make -C ${BUILD_BASE} validate-cache;
396 if [ $rc -ne 0 ]; then
397 echo "Cache invalid - a new cache will be built "
400 cp -rf /tmp/cache/cache/. ${BUILD_BASE}
407 if [ ${POPULATE_CACHE} -eq 1 ]; then
408 if [ ${DEBUG} -eq 0 ]; then
410 cd ${BUILD_BASE} && make clean
413 if [ $rc -ne 0 ]; then
414 echo "Build - make clean failed, exiting ..."
421 if [ ! -z ${BUILD_VERSION} ]; then
422 MAKE_ARGS+="REVSTATE=${BUILD_VERSION} "
425 if [ ${UNIT_TEST} -eq 1 ]; then
426 MAKE_ARGS+="UNIT_TEST=TRUE "
428 MAKE_ARGS+="UNIT_TEST=FALSE "
431 if [ ${INTERACTIVE} -eq 1 ]; then
432 MAKE_ARGS+="INTERACTIVE=TRUE "
434 MAKE_ARGS+="INTERACTIVE=FALSE "
439 if [ ${DEBUG} -eq 0 ]; then
441 cd ${BUILD_BASE} && make ${MAKE_ARGS}
444 if [ $rc -gt 0 ]; then
445 echo "Build: make all failed, exiting ..."
453 make -C ${BUILD_BASE} prepare-cache
457 if [ $rc -gt 0 ]; then
458 echo "Build: make prepare-cache failed, exiting ..."
462 echo "Copying built OPNFV .iso file to target directory ${BUILD_DIR} ..."
465 cp ${BUILD_BASE}/.versions ${BUILD_DIR}
466 cp ${RESULT_DIR}/*.iso* ${BUILD_DIR}
468 if [ $POPULATE_CACHE -eq 1 ]; then
469 if [ ! -z ${BUILD_CACHE_URI} ]; then
470 echo "Building cache ..."
471 tar --dereference -C ${BUILD_BASE} -caf ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz ${CACHE_DIR}
472 echo "Uploading cache ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}"
473 ${REMOTE_ACCESS_METHD} -T ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz ${BUILD_CACHE_URI}/${REMOTE_CACHE_ARCH_NAME}
474 rm ${BUILD_BASE}/${LOCAL_CACHE_ARCH_NAME}.tgz