Adding OVN as an SDN option to Apex
[apex.git] / ci / build.sh
1 #!/bin/sh
2 ##############################################################################
3 # Copyright (c) 2016 Dan Radez (Red Hat) and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set -xe
12
13 display_usage ()
14 {
15 cat << EOF
16 $0 Builds the Apex OPNFV Deployment Toolchain
17
18 usage: $0 [ -c cache_dest_dir ] -r release_name [ --iso | --rpms ]
19
20 OPTIONS:
21   -c cache destination - destination to save tarball of cache
22   -r release name/version of the build result
23   --iso build the iso (implies RPMs too)
24   --rpms build the rpms
25   --debug enable debug
26   -h help, prints this help text
27
28 Example:
29 build -c file:///tmp/cache -r dev123
30 EOF
31 }
32
33 APEX_ROOT=$(dirname $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd))
34 CACHE_DEST=""
35 CACHE_DIR="${APEX_ROOT}/.cache"
36 CACHE_NAME="apex-cache"
37 MAKE_TARGETS="images"
38 REQUIRED_PKGS="rpm-build python-docutils"
39 RELEASE_RPM=""
40
41 parse_cmdline() {
42   while [ "${1:0:1}" = "-" ]
43   do
44     case "$1" in
45         -h|--help)
46                 display_usage
47                 exit 0
48             ;;
49         -c|--cache-dest)
50                 CACHE_DEST=${2}
51                 shift 2
52             ;;
53         -r|--release)
54                 RELEASE=${2}
55                 shift 2
56             ;;
57         --iso )
58                 MAKE_TARGETS="iso"
59                 echo "Building opnfv-apex RPMs and ISO"
60                 shift 1
61             ;;
62         --rpms )
63                 MAKE_TARGETS="rpms"
64                 echo "Buiding opnfv-apex RPMs"
65                 shift 1
66             ;;
67         --release-rpm )
68                 RELEASE_RPM=" release-rpm"
69                 echo "Buiding opnfv-apex RPMs"
70                 shift 1
71             ;;
72         --debug )
73                 debug="TRUE"
74                 echo "Enable debug output"
75                 shift 1
76             ;;
77         --build-cache )
78                 MAKE_TARGETS=""
79                 echo "Building Cache"
80                 shift 1
81             ;;
82         *)
83                 display_usage
84                 exit 1
85             ;;
86     esac
87   done
88
89 }
90
91 run_make() {
92   make $MAKE_ARGS -C ${BUILD_DIRECTORY} $1
93 }
94
95 parse_cmdline "$@"
96
97 if [ -z "$BUILD_DIRECTORY" ]; then
98   if [ -d "${APEX_ROOT}/build" ]; then
99     BUILD_DIRECTORY="${APEX_ROOT}/build"
100   else
101     echo "Cannot find build directory, please provide BUILD_DIRECTORY environment variable...exiting"
102     exit 1
103   fi
104 elif [ ! -d "$BUILD_DIRECTORY" ]; then
105   echo "Provided build directory is invalid: ${BUILD_DIRECTORY} ...exiting"
106   exit 1
107 fi
108
109 # Add release rpm to make targets if defined
110 MAKE_TARGETS+=$RELEASE_RPM
111
112 # Install build dependencies
113 for pkg in $REQUIRED_PKGS; do
114   if ! rpm -q $pkg > /dev/null; then
115     if ! sudo yum -y install $pkg > /dev/null; then
116       echo "Required package $pkg missing and installation failed."
117       exit 1
118     fi
119   fi
120 done
121
122 if [ -n "$RELEASE" ]; then MAKE_ARGS+="RELEASE=$RELEASE "; fi
123
124 # Get the Old Cache and build new cache history file
125 if [[ -n "$CACHE_DEST" && -n "$MAKE_TARGETS" ]]; then
126     echo "Retrieving Cache"
127     if [ -f $CACHE_DEST/${CACHE_NAME}.tgz ]; then
128         echo "Cache found at ${CACHE_DEST}/${CACHE_NAME}.tgz"
129         rm -rf $CACHE_DIR
130         mkdir $CACHE_DIR
131         echo "Unpacking Cache to ${CACHE_DIR}"
132         tar -xvzf ${CACHE_DEST}/${CACHE_NAME}.tgz -C ${CACHE_DIR}
133         echo "Cache contents after unpack:"
134         ls -al ${CACHE_DIR}
135     else
136         echo "No Cache Found"
137     fi
138 fi
139
140 # Ensure the build cache dir exists
141 if [ ! -d "$CACHE_DIR" ]; then
142     rm -rf ${CACHE_DIR}
143     echo "Creating Build Cache Directory"
144     mkdir ${CACHE_DIR}
145 fi
146
147 # Conditionally execute RPM build checks if the specs change and target is not rpm or iso
148 if [[ "$MAKE_TARGETS" == "images" ]]; then
149     commit_file_list=$(git show --pretty="format:" --name-status)
150     if git show -s | grep "force-build-rpms"; then
151         MAKE_TARGETS+=" rpms"
152     elif [[ $commit_file_list == *"A$(printf '\t')"* || $commit_file_list == *build/Makefile* ]]; then
153         # Makefile forces all rpms to be checked
154         MAKE_TARGETS+=" rpms-check"
155     else
156         # Spec files are selective
157         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-undercloud.spec* ]]; then
158             MAKE_TARGETS+=" undercloud-rpm-check"
159         fi
160         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-release.spec* ]]; then
161             MAKE_TARGETS+=" release-rpm-check"
162         fi
163         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-common.spec* ]]; then
164             MAKE_TARGETS+=" common-rpm-check"
165         fi
166         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex.spec* ]]; then
167             MAKE_TARGETS+=" opendaylight-rpm-check"
168         fi
169         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-onos.spec* ]]; then
170             MAKE_TARGETS+=" onos-rpm-check"
171         fi
172     fi
173 fi
174
175 # Make sure python is installed
176 if ! rpm -q python34-devel > /dev/null; then
177     sudo yum install -y epel-release
178     if ! sudo yum install -y python34-devel; then
179         echo "Failed to install python34-devel package..."
180         exit 1
181     fi
182 fi
183
184 # Execute make against targets
185 for t in $MAKE_TARGETS; do
186     run_make $t
187 done
188
189 echo "Build Complete"
190
191 # Build new Cache
192 if [ -n "$CACHE_DEST" ]; then
193     echo "Building Cache"
194     ls -lah ${CACHE_DIR}
195     # ensure the destination exists
196     mkdir -p ${CACHE_DEST}
197     # roll the cache tarball
198     tar --atime-preserve --dereference -caf ${CACHE_DEST}/${CACHE_NAME}.tgz -C ${CACHE_DIR} .
199     if [ -f "${CACHE_DEST}/${CACHE_NAME}.tgz" ]; then
200       echo "Cache Build Complete"
201     else
202       echo "WARN: Cache file did not build correctly"
203     fi
204 fi
205 echo "Complete"