Merge "Fixes nic template missing in spec and error catching"
[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         rm -rf $BUILD_BASE/$CACHE_DIR
103         cp -f $CACHE_DEST/${CACHE_NAME}.tgz $BUILD_BASE/${CACHE_NAME}.tgz
104         tar xzf $BUILD_BASE/${CACHE_NAME}.tgz
105     elif [ ! -d $BUILD_BASE/$CACHE_DIR ]; then
106         mkdir $BUILD_BASE/$CACHE_DIR
107     fi
108 fi
109
110 #create build_output for legacy functionality compatibility in jenkins
111 if [[ ! -d ../build_output  ]]; then
112     rm -f ../build_output
113     ln -s build/noarch/ ../build_output
114 fi
115
116 # Conditionally execute RPM build checks if the specs change and target is not rpm or iso
117 if [[ "$MAKE_TARGETS" == "images" ]]; then
118     commit_file_list=$(git show --pretty="format:" --name-only)
119     if [[ $commit_file_list == *build/Makefile* ]]; then
120         # Makefile forces all rpms to be checked
121         MAKE_TARGETS+=" rpms-check"
122     else
123         # Spec files are selective
124         if [[ $commit_file_list == *build/opnfv-apex-undercloud.spec* ]]; then
125             MAKE_TARGETS+=" undercloud-rpm-check"
126         fi
127         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
128             MAKE_TARGETS+=" common-rpm-check"
129         fi
130         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
131             MAKE_TARGETS+=" opendaylight-rpm-check"
132         fi
133         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
134             MAKE_TARGETS+=" onos-rpm-check"
135         fi
136         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
137             MAKE_TARGETS+=" opendaylight-sfc-rpm-check"
138         fi
139     fi
140 fi
141
142 # Make sure python is installed
143 if ! rpm -q python34-devel > /dev/null; then
144     sudo yum install -y epel-release
145     if ! sudo yum install -y python34-devel; then
146         echo "Failed to install python34-devel package..."
147         exit 1
148     fi
149 fi
150
151 # Execute make against targets
152 for t in $MAKE_TARGETS; do
153     run_make $t
154 done
155
156 echo "Build Complete"
157
158 # Build new Cache
159 if [ -n "$CACHE_DEST" ]; then
160     echo "Building Cache"
161     tar --atime-preserve --dereference -C $BUILD_BASE -caf $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DIR
162     echo "Copying Cache"
163     if [ ! -d $CACHE_DEST ]; then mkdir -p $CACHE_DEST; fi
164     cp $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DEST/${CACHE_NAME}.tgz
165 fi
166 echo "Complete"