Merge "Updating OpenDaylight image script to use RDO trunk BGPVPN package."
[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 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-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         --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         *)
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 # Add release rpm to make targets if defined
93 MAKE_TARGETS+=$RELEASE_RPM
94
95 # Install build dependencies
96 for pkg in $REQUIRED_PKGS; do
97   if ! rpm -q $pkg > /dev/null; then
98     if ! sudo yum -y install $pkg > /dev/null; then
99       echo "Required package $pkg missing and installation failed."
100       exit 1
101     fi
102   fi
103 done
104
105 if [ -n "$RELEASE" ]; then MAKE_ARGS+="RELEASE=$RELEASE "; fi
106
107 # Get the Old Cache
108 if [ -n "$CACHE_DEST" ]; then
109     echo "Retrieving Cache"
110     if [ -f $CACHE_DEST/${CACHE_NAME}.tgz ]; then
111         echo "Cache found at ${CACHE_DEST}/${CACHE_NAME}.tgz"
112         rm -rf $BUILD_BASE/$CACHE_DIR
113         echo "Unpacking Cache to $BUILD_BASE"
114         tar -xvzf $CACHE_DEST/${CACHE_NAME}.tgz -C ${BUILD_BASE}
115         echo "Cache contents after unpack:"
116         ls -l $BUILD_BASE/$CACHE_DIR
117     elif [ ! -d $BUILD_BASE/$CACHE_DIR ]; then
118         mkdir $BUILD_BASE/$CACHE_DIR
119     fi
120 fi
121
122 # Conditionally execute RPM build checks if the specs change and target is not rpm or iso
123 if [[ "$MAKE_TARGETS" == "images" ]]; then
124     commit_file_list=$(git show --pretty="format:" --name-status)
125     if git show -s | grep "force-build-rpms"; then
126         MAKE_TARGETS+=" rpms"
127     elif [[ $commit_file_list == *"A$(printf '\t')"* || $commit_file_list == *build/Makefile* ]]; then
128         # Makefile forces all rpms to be checked
129         MAKE_TARGETS+=" rpms-check"
130     else
131         # Spec files are selective
132         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-undercloud.spec* ]]; then
133             MAKE_TARGETS+=" undercloud-rpm-check"
134         fi
135         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-release.spec* ]]; then
136             MAKE_TARGETS+=" release-rpm-check"
137         fi
138         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-common.spec* ]]; then
139             MAKE_TARGETS+=" common-rpm-check"
140         fi
141         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex.spec* ]]; then
142             MAKE_TARGETS+=" opendaylight-rpm-check"
143         fi
144         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-onos.spec* ]]; then
145             MAKE_TARGETS+=" onos-rpm-check"
146         fi
147     fi
148 fi
149
150 # Make sure python is installed
151 if ! rpm -q python34-devel > /dev/null; then
152     sudo yum install -y epel-release
153     if ! sudo yum install -y python34-devel; then
154         echo "Failed to install python34-devel package..."
155         exit 1
156     fi
157 fi
158
159 # Execute make against targets
160 for t in $MAKE_TARGETS; do
161     run_make $t
162 done
163
164 echo "Build Complete"
165
166 # Build new Cache
167 if [ -n "$CACHE_DEST" ]; then
168     echo "Building Cache"
169     if [ ! -d $CACHE_DEST ]; then mkdir -p $CACHE_DEST; fi
170     tar --atime-preserve --dereference -C $BUILD_BASE -caf $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DIR -C ${CACHE_DEST}/
171     if [ -f "${CACHE_DEST}/${CACHE_NAME}.tgz" ]; then
172       echo "Cache Build Complete"
173     else
174       echo "WARN: Cache file did not build correctly"
175     fi
176 fi
177 echo "Complete"