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
40 parse_cmdline() {
41   while [ "${1:0:1}" = "-" ]
42   do
43     case "$1" in
44         -h|--help)
45                 display_usage
46                 exit 0
47             ;;
48         -c|--cache-dest)
49                 CACHE_DEST=${2}
50                 shift 2
51             ;;
52         -r|--release)
53                 RELEASE=${2}
54                 shift 2
55             ;;
56         --iso )
57                 MAKE_TARGETS="iso"
58                 echo "Building opnfv-apex RPMs and ISO"
59                 shift 1
60             ;;
61         --rpms )
62                 MAKE_TARGETS="rpms"
63                 echo "Buiding opnfv-apex RPMs"
64                 shift 1
65             ;;
66         --debug )
67                 debug="TRUE"
68                 echo "Enable debug output"
69                 shift 1
70             ;;
71         --build-cache )
72                 MAKE_TARGETS=""
73                 echo "Building Cache"
74                 shift 1
75             ;;
76         *)
77                 display_usage
78                 exit 1
79             ;;
80     esac
81   done
82
83 }
84
85 run_make() {
86   make $MAKE_ARGS -C ${BUILD_BASE} $1
87 }
88
89 parse_cmdline "$@"
90
91 # Install build dependencies
92 for pkg in $REQUIRED_PKGS; do
93   if ! rpm -q $pkg > /dev/null; then
94     if ! sudo yum -y install $pkg > /dev/null; then
95       echo "Required package $pkg missing and installation failed."
96       exit 1
97     fi
98   fi
99 done
100
101 if [ -n "$RELEASE" ]; then MAKE_ARGS+="RELEASE=$RELEASE "; fi
102
103 # Get the Old Cache
104 if [[ -n "$CACHE_DEST" && -n "$MAKE_TARGETS" ]]; then
105     echo "Retrieving Cache"
106     if [ -f $CACHE_DEST/${CACHE_NAME}.tgz ]; then
107         echo "Cache found at ${CACHE_DEST}/${CACHE_NAME}.tgz"
108         rm -rf $BUILD_BASE/$CACHE_DIR
109         echo "Unpacking Cache to $BUILD_BASE"
110         tar -xvzf $CACHE_DEST/${CACHE_NAME}.tgz -C ${BUILD_BASE}
111         if [ -f $BUILD_BASE/.cache ]; then
112             echo "Rebuilding .cache file"
113             if [ ! -d $BUILD_BASE/$CACHE_DIR ]; then
114                 mkdir $BUILD_BASE/$CACHE_DIR
115             fi
116             for i in $(ls $BUILD_BASE/$CACHE_DIR); do
117                 grep $i $BUILD_BASE/.cache >> $BUILD_BASE/$CACHE_DIR/.cache
118             done
119         fi
120         echo "Cache contents after unpack:"
121         ls -l $BUILD_BASE/$CACHE_DIR
122     else
123         echo "No Cache Found"
124     fi
125 fi
126
127 # Ensure the build cache dir exists
128 if [ ! -d "$BUILD_BASE/$CACHE_DIR" ]; then
129     echo "Creating Build Cache Directory"
130     mkdir $BUILD_BASE/$CACHE_DIR
131 fi
132
133 # Conditionally execute RPM build checks if the specs change and target is not rpm or iso
134 if [[ "$MAKE_TARGETS" == "images" ]]; then
135     commit_file_list=$(git show --pretty="format:" --name-status)
136     if git show -s | grep "force-build-rpms"; then
137         MAKE_TARGETS+=" rpms"
138     elif [[ $commit_file_list == *"A$(printf '\t')"* || $commit_file_list == *build/Makefile* ]]; then
139         # Makefile forces all rpms to be checked
140         MAKE_TARGETS+=" rpms-check"
141     else
142         # Spec files are selective
143         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-undercloud.spec* ]]; then
144             MAKE_TARGETS+=" undercloud-rpm-check"
145         fi
146         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-common.spec* ]]; then
147             MAKE_TARGETS+=" common-rpm-check"
148         fi
149         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex.spec* ]]; then
150             MAKE_TARGETS+=" opendaylight-rpm-check"
151         fi
152         if [[ $commit_file_list == *build/rpm_specs/opnfv-apex-onos.spec* ]]; then
153             MAKE_TARGETS+=" onos-rpm-check"
154         fi
155     fi
156 fi
157
158 # Make sure python is installed
159 if ! rpm -q python34-devel > /dev/null; then
160     sudo yum install -y epel-release
161     if ! sudo yum install -y python34-devel; then
162         echo "Failed to install python34-devel package..."
163         exit 1
164     fi
165 fi
166
167 # Execute make against targets
168 for t in $MAKE_TARGETS; do
169     run_make $t
170 done
171
172 echo "Build Complete"
173
174 # Build new Cache
175 if [ -n "$CACHE_DEST" ]; then
176     echo "Building Cache"
177     ls -lh $BUILD_BASE/$CACHE_DIR/
178     # ensure the destination exists
179     if [ ! -d $CACHE_DEST ]; then mkdir -p $CACHE_DEST; fi
180     # ensure a sub cache dir exists to mirror the build base for extraction
181     if [ ! -d $BUILD_BASE/$CACHE_DIR/$CACHE_DIR/ ]; then mkdir -p $BUILD_BASE/$CACHE_DIR/$CACHE_DIR/; fi
182     # move directly cached files to cache dir for future extraction
183     for i in $(cat $BUILD_BASE/$CACHE_DIR/.cache | awk '{ print $2 }'); do
184         if [ -f $i ]; then mv $i $BUILD_BASE/$CACHE_DIR/$CACHE_DIR/; fi
185     done
186     # roll the cache tarball
187     tar --atime-preserve --dereference -C ${BUILD_BASE}/$CACHE_DIR -caf $CACHE_DEST/${CACHE_NAME}.tgz .
188     if [ -f "${CACHE_DEST}/${CACHE_NAME}.tgz" ]; then
189       echo "Cache Build Complete"
190     else
191       echo "WARN: Cache file did not build correctly"
192     fi
193 fi
194 echo "Complete"