fd079c914ed7eb9ab35fc811040bedca3bdb8ced
[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 git show -s | grep "force-build-rpms"; then
117         MAKE_TARGETS+=" rpms"
118     elif [[ $commit_file_list == *build/Makefile* ]]; then
119         # Makefile forces all rpms to be checked
120         MAKE_TARGETS+=" rpms-check"
121     else
122         # Spec files are selective
123         if [[ $commit_file_list == *build/opnfv-apex-undercloud.spec* ]]; then
124             MAKE_TARGETS+=" undercloud-rpm-check"
125         fi
126         if [[ $commit_file_list == *build/opnfv-apex-common.spec* ]]; then
127             MAKE_TARGETS+=" common-rpm-check"
128         fi
129         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
130             MAKE_TARGETS+=" opendaylight-rpm-check"
131         fi
132         if [[ $commit_file_list == *build/opnfv-apex-onos.spec* ]]; then
133             MAKE_TARGETS+=" onos-rpm-check"
134         fi
135         if [[ $commit_file_list == *build/opnfv-apex-opendaylight-sfc.spec* ]]; then
136             MAKE_TARGETS+=" opendaylight-sfc-rpm-check"
137         fi
138     fi
139 fi
140
141 # Make sure python is installed
142 if ! rpm -q python34-devel > /dev/null; then
143     sudo yum install -y epel-release
144     if ! sudo yum install -y python34-devel; then
145         echo "Failed to install python34-devel package..."
146         exit 1
147     fi
148 fi
149
150 # Execute make against targets
151 for t in $MAKE_TARGETS; do
152     run_make $t
153 done
154
155 echo "Build Complete"
156
157 # Build new Cache
158 if [ -n "$CACHE_DEST" ]; then
159     echo "Building Cache"
160     if [ ! -d $CACHE_DEST ]; then mkdir -p $CACHE_DEST; fi
161     tar --atime-preserve --dereference -C $BUILD_BASE -caf $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DIR -C ${CACHE_DEST}/
162     if [ -f "${CACHE_DEST}/${CACHE_NAME}.tgz" ]; then
163       echo "Cache Build Complete"
164     else
165       echo "WARN: Cache file did not build correctly"
166     fi
167 fi
168 echo "Complete"