dd9f9fd12dd34ce2264c37948c32bbe1bfe48b83
[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
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-dir)
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         *)
72                 display_usage
73                 exit 1
74             ;;
75     esac
76   done
77
78 }
79
80 run_make() {
81   make $MAKE_ARGS -C ${BUILD_BASE} $1
82 }
83
84 parse_cmdline "$@"
85
86 # Install build dependencies
87 for pkg in $REQUIRED_PKGS; do
88   if ! rpm -q $pkg > /dev/null; then
89     if ! sudo yum -y install $pkg > /dev/null; then
90       echo "Required package $pkg missing and installation failed."
91       exit 1
92     fi
93   fi
94 done
95
96 if [ -n "$RELEASE" ]; then MAKE_ARGS+="RELEASE=$RELEASE "; fi
97
98 # Get the Old Cache
99 if [ -n "$CACHE_DEST" ]; then
100     echo "Retrieving Cache"
101     if [ -f $CACHE_DEST/${CACHE_NAME}.tgz ]; then
102         echo "Cache found at ${CACHE_DEST}/${CACHE_NAME}.tgz"
103         rm -rf $BUILD_BASE/$CACHE_DIR
104         echo "Unpacking Cache to $BUILD_BASE"
105         tar -xvzf $CACHE_DEST/${CACHE_NAME}.tgz -C ${BUILD_BASE}
106         echo "Cache contents after unpack:"
107         ls -l $BUILD_BASE/$CACHE_DIR
108     elif [ ! -d $BUILD_BASE/$CACHE_DIR ]; then
109         mkdir $BUILD_BASE/$CACHE_DIR
110     fi
111 fi
112
113 # Conditionally execute RPM build checks if the specs change and target is not rpm or iso
114 if [[ "$MAKE_TARGETS" == "images" ]]; then
115     commit_file_list=$(git show --pretty="format:" --name-only)
116     if [[ $commit_file_list == *build/Makefile* ]]; then
117         # Makefile forces all rpms to be checked
118         MAKE_TARGETS+=" rpms-check"
119     else
120         # Spec files are selective
121         if [[ $commit_file_list == *build/opnfv-apex-undercloud.spec* ]]; then
122             MAKE_TARGETS+=" undercloud-rpm-check"
123         fi
124         if [[ $commit_file_list == *build/opnfv-apex-common.spec* ]]; then
125             MAKE_TARGETS+=" common-rpm-check"
126         fi
127         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
128             MAKE_TARGETS+=" opendaylight-rpm-check"
129         fi
130         if [[ $commit_file_list == *build/opnfv-apex-onos.spec* ]]; then
131             MAKE_TARGETS+=" onos-rpm-check"
132         fi
133         if [[ $commit_file_list == *build/opnfv-apex-opendaylight-sfc.spec* ]]; then
134             MAKE_TARGETS+=" opendaylight-sfc-rpm-check"
135         fi
136     fi
137 fi
138
139 # Make sure python is installed
140 if ! rpm -q python34-devel > /dev/null; then
141     sudo yum install -y epel-release
142     if ! sudo yum install -y python34-devel; then
143         echo "Failed to install python34-devel package..."
144         exit 1
145     fi
146 fi
147
148 # Execute make against targets
149 for t in $MAKE_TARGETS; do
150     run_make $t
151 done
152
153 echo "Build Complete"
154
155 # Build new Cache
156 if [ -n "$CACHE_DEST" ]; then
157     echo "Building Cache"
158     if [ ! -d $CACHE_DEST ]; then mkdir -p $CACHE_DEST; fi
159     tar --atime-preserve --dereference -C $BUILD_BASE -caf $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DIR -C ${CACHE_DEST}/
160     if [ -f "${CACHE_DEST}/${CACHE_NAME}.tgz" ]; then
161       echo "Cache Build Complete"
162     else
163       echo "WARN: Cache file did not build correctly"
164     fi
165 fi
166 echo "Complete"