Merge "Updates to Caching"
[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 - 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 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-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_BASE} $1
93 }
94
95 parse_cmdline "$@"
96
97 # Add release rpm to make targets if defined
98 MAKE_TARGETS+=$RELEASE_RPM
99
100 # Install build dependencies
101 for pkg in $REQUIRED_PKGS; do
102   if ! rpm -q $pkg > /dev/null; then
103     if ! sudo yum -y install $pkg > /dev/null; then
104       echo "Required package $pkg missing and installation failed."
105       exit 1
106     fi
107   fi
108 done
109
110 if [ -n "$RELEASE" ]; then MAKE_ARGS+="RELEASE=$RELEASE "; fi
111
112 # Get the Old Cache
113 if [[ -n "$CACHE_DEST" && -n "$MAKE_TARGETS" ]]; then
114     echo "Retrieving Cache"
115     if [ -f $CACHE_DEST/${CACHE_NAME}.tgz ]; then
116         echo "Cache found at ${CACHE_DEST}/${CACHE_NAME}.tgz"
117         rm -rf $BUILD_BASE/$CACHE_DIR
118         echo "Unpacking Cache to $BUILD_BASE"
119         tar -xvzf $CACHE_DEST/${CACHE_NAME}.tgz -C ${BUILD_BASE}
120         if [ -f $BUILD_BASE/.cache ]; then
121             echo "Rebuilding .cache file"
122             if [ ! -d $BUILD_BASE/$CACHE_DIR ]; then
123                 mkdir $BUILD_BASE/$CACHE_DIR
124             fi
125             for i in $(ls $BUILD_BASE/$CACHE_DIR); do
126                 grep $i $BUILD_BASE/.cache >> $BUILD_BASE/$CACHE_DIR/.cache
127             done
128         fi
129         echo "Cache contents after unpack:"
130         ls -l $BUILD_BASE/$CACHE_DIR
131     else
132         echo "No Cache Found"
133     fi
134 fi
135
136 # Ensure the build cache dir exists
137 if [ ! -d "$BUILD_BASE/$CACHE_DIR" ]; then
138     echo "Creating Build Cache Directory"
139     mkdir $BUILD_BASE/$CACHE_DIR
140 fi
141
142 # Conditionally execute RPM build checks if the specs change and target is not rpm or iso
143 if [[ "$MAKE_TARGETS" == "images" ]]; then
144     commit_file_list=$(git show --pretty="format:" --name-status)
145     if git show -s | grep "force-build-rpms"; then
146         MAKE_TARGETS+=" rpms"
147     elif [[ $commit_file_list == *"A$(printf '\t')"* || $commit_file_list == *build/Makefile* ]]; then
148         # Makefile forces all rpms to be checked
149         MAKE_TARGETS+=" rpms-check"
150     else
151         # Spec files are selective
152         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-undercloud.spec* ]]; then
153             MAKE_TARGETS+=" undercloud-rpm-check"
154         fi
155         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-release.spec* ]]; then
156             MAKE_TARGETS+=" release-rpm-check"
157         fi
158         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-common.spec* ]]; then
159             MAKE_TARGETS+=" common-rpm-check"
160         fi
161         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex.spec* ]]; then
162             MAKE_TARGETS+=" opendaylight-rpm-check"
163         fi
164         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-onos.spec* ]]; then
165             MAKE_TARGETS+=" onos-rpm-check"
166         fi
167     fi
168 fi
169
170 # Make sure python is installed
171 if ! rpm -q python34-devel > /dev/null; then
172     sudo yum install -y epel-release
173     if ! sudo yum install -y python34-devel; then
174         echo "Failed to install python34-devel package..."
175         exit 1
176     fi
177 fi
178
179 # Execute make against targets
180 for t in $MAKE_TARGETS; do
181     run_make $t
182 done
183
184 echo "Build Complete"
185
186 # Build new Cache
187 if [ -n "$CACHE_DEST" ]; then
188     echo "Building Cache"
189     ls -lh $BUILD_BASE/$CACHE_DIR/
190     # ensure the destination exists
191     if [ ! -d $CACHE_DEST ]; then mkdir -p $CACHE_DEST; fi
192     # ensure a sub cache dir exists to mirror the build base for extraction
193     if [ ! -d $BUILD_BASE/$CACHE_DIR/$CACHE_DIR/ ]; then mkdir -p $BUILD_BASE/$CACHE_DIR/$CACHE_DIR/; fi
194     # move directly cached files to cache dir for future extraction
195     for i in $(cat $BUILD_BASE/$CACHE_DIR/.cache | awk '{ print $2 }'); do
196         if [ -f $i ]; then mv $i $BUILD_BASE/$CACHE_DIR/$CACHE_DIR/; fi
197     done
198     # roll the cache tarball
199     tar --atime-preserve --dereference -C ${BUILD_BASE}/$CACHE_DIR -caf $CACHE_DEST/${CACHE_NAME}.tgz .
200     if [ -f "${CACHE_DEST}/${CACHE_NAME}.tgz" ]; then
201       echo "Cache Build Complete"
202     else
203       echo "WARN: Cache file did not build correctly"
204     fi
205 fi
206 echo "Complete"