Change keystone CLI to OSC
[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 -e
12
13 display_usage ()
14 {
15 cat << EOF
16 $0 Builds the Apex OPNFV Deployment Toolchain
17
18 usage: $0 [ -c cache_dir ] -r release_name [ --iso | --rpms ]
19
20 OPTIONS:
21   -c cache destination - directory of cached files, defaults to ./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 BUILD_BASE=$(readlink -e ../build/)
34 CACHE_DEST=""
35 CACHE_DIR="cache"
36 CACHE_NAME="apex-cache"
37 PYTHON_TESTS="TRUE"
38 MAKE_TARGETS="images"
39 REQUIRED_PKGS="rpm-build python-docutils"
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-dir)
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         --skip-python-tests )
68                 PYTHON_TESTS="FALSE"
69                 echo "Skipping Python Tests"
70                 shift 1
71             ;;
72         --debug )
73                 debug="TRUE"
74                 echo "Enable debug output"
75                 shift 1
76             ;;
77         *)
78                 display_usage
79                 exit 1
80             ;;
81     esac
82   done
83
84 }
85
86 run_make() {
87   make $MAKE_ARGS -C ${BUILD_BASE} $1
88 }
89
90 parse_cmdline "$@"
91
92 # Install build dependencies
93 for pkg in $REQUIRED_PKGS; do
94   if ! rpm -q $pkg > /dev/null; then
95     if ! sudo yum -y install $pkg > /dev/null; then
96       echo "Required package $pkg missing and installation failed."
97       exit 1
98     fi
99   fi
100 done
101
102 if [ -n "$RELEASE" ]; then MAKE_ARGS+="RELEASE=$RELEASE "; fi
103
104 # Get the Old Cache
105 if [ -n "$CACHE_DEST" ]; then
106     echo "Retrieving Cache"
107     if [ -f $CACHE_DEST/${CACHE_NAME}.tgz ]; then
108         echo "Cache found at ${CACHE_DEST}/${CACHE_NAME}.tgz"
109         rm -rf $BUILD_BASE/$CACHE_DIR
110         echo "Unpacking Cache to $BUILD_BASE"
111         tar -xvzf $CACHE_DEST/${CACHE_NAME}.tgz -C ${BUILD_BASE}
112         echo "Cache contents after unpack:"
113         ls -l $BUILD_BASE/$CACHE_DIR
114     elif [ ! -d $BUILD_BASE/$CACHE_DIR ]; then
115         mkdir $BUILD_BASE/$CACHE_DIR
116     fi
117 fi
118
119 # Conditionally execute RPM build checks if the specs change and target is not rpm or iso
120 if [[ "$MAKE_TARGETS" == "images" ]]; then
121     commit_file_list=$(git show --pretty="format:" --name-only)
122     if [[ $commit_file_list == *build/Makefile* ]]; then
123         # Makefile forces all rpms to be checked
124         MAKE_TARGETS+=" rpms-check"
125     else
126         # Spec files are selective
127         if [[ $commit_file_list == *build/opnfv-apex-undercloud.spec* ]]; then
128             MAKE_TARGETS+=" undercloud-rpm-check"
129         fi
130         if [[ $commit_file_list == *build/opnfv-apex-common.spec* ]]; then
131             MAKE_TARGETS+=" common-rpm-check"
132         fi
133         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
134             MAKE_TARGETS+=" opendaylight-rpm-check"
135         fi
136         if [[ $commit_file_list == *build/opnfv-apex-onos.spec* ]]; then
137             MAKE_TARGETS+=" onos-rpm-check"
138         fi
139         if [[ $commit_file_list == *build/opnfv-apex-opendaylight-sfc.spec* ]]; then
140             MAKE_TARGETS+=" opendaylight-sfc-rpm-check"
141         fi
142     fi
143 fi
144
145 # Make sure python is installed
146 if ! rpm -q python34-devel > /dev/null; then
147     sudo yum install -y epel-release
148     if ! sudo yum install -y python34-devel; then
149         echo "Failed to install python34-devel package..."
150         exit 1
151     fi
152 fi
153
154 if [ "$PYTHON_TESTS" == "TRUE" ]; then
155     # Make sure coverage is installed
156     if ! python3 -c "import coverage" &> /dev/null; then sudo easy_install-3.4 coverage; fi
157
158     run_make python-tests
159     pushd ../tests/ > /dev/null
160     percent=$(coverage3 report --include '*lib/python/*' -m | grep TOTAL | tr -s ' ' | awk '{ print $4 }' | cut -d % -f 1)
161     if [[ percent -lt 80 ]]; then
162         echo "Python Coverage: $percent"
163         echo "WARNING: Does not meet 80% requirement"
164     fi
165     popd
166 fi
167
168 # Execute make against targets
169 for t in $MAKE_TARGETS; do
170     run_make $t
171 done
172
173 echo "Build Complete"
174
175 # Build new Cache
176 if [ -n "$CACHE_DEST" ]; then
177     echo "Building Cache"
178     if [ ! -d $CACHE_DEST ]; then mkdir -p $CACHE_DEST; fi
179     tar --atime-preserve --dereference -C $BUILD_BASE -caf $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DIR -C ${CACHE_DEST}/
180     if [ -f "${CACHE_DEST}/${CACHE_NAME}.tgz" ]; then
181       echo "Cache Build Complete"
182     else
183       echo "WARN: Cache file did not build correctly"
184     fi
185 fi
186 echo "Complete"