Adds python IP utility library
[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
39 parse_cmdline() {
40   while [ "${1:0:1}" = "-" ]
41   do
42     case "$1" in
43         -h|--help)
44                 display_usage
45                 exit 0
46             ;;
47         -c|--cache-dir)
48                 CACHE_DEST=${2}
49                 shift 2
50             ;;
51         -r|--release)
52                 RELEASE=${2}
53                 shift 2
54             ;;
55         --iso )
56                 MAKE_TARGETS="iso"
57                 echo "Building opnfv-apex RPMs and ISO"
58                 shift 1
59             ;;
60         --rpms )
61                 MAKE_TARGETS="rpms"
62                 echo "Buiding opnfv-apex RPMs"
63                 shift 1
64             ;;
65         --debug )
66                 debug="TRUE"
67                 echo "Enable debug output"
68                 shift 1
69             ;;
70         *)
71                 display_usage
72                 exit 1
73             ;;
74     esac
75   done
76
77 }
78
79 run_make() {
80   make $MAKE_ARGS -C ${BUILD_BASE} $1
81 }
82
83 parse_cmdline "$@"
84
85 if [ -n "$RELEASE" ]; then MAKE_ARGS+="RELEASE=$RELEASE "; fi
86
87 # Get the Old Cache
88 if [ -n "$CACHE_DEST" ]; then
89     echo "Retrieving Cache"
90     if [ -f $CACHE_DEST/${CACHE_NAME}.tgz ]; then
91         rm -rf $BUILD_BASE/$CACHE_DIR
92         cp -f $CACHE_DEST/${CACHE_NAME}.tgz $BUILD_BASE/${CACHE_NAME}.tgz
93         tar xzf $BUILD_BASE/${CACHE_NAME}.tgz
94     elif [ ! -d $BUILD_BASE/$CACHE_DIR ]; then
95         mkdir $BUILD_BASE/$CACHE_DIR
96     fi
97 fi
98
99 #create build_output for legecy functionality compatibiltiy in jenkins
100 if [[ ! -d ../build_output  ]]; then
101     rm -f ../build_output
102     ln -s build/noarch/ ../build_output
103 fi
104
105 # Conditionally execute RPM build checks if the specs change and target is not rpm or iso
106 if [[ "$MAKE_TARGETS" == "images" ]]; then
107     commit_file_list=$(git show --pretty="format:" --name-only)
108     if [[ $commit_file_list == *build/Makefile* ]]; then
109         # Makefile forces all rpms to be checked
110         MAKE_TARGETS+=" rpms-check"
111     else
112         # Spec files are selective
113         if [[ $commit_file_list == *build/opnfv-apex-undercloud.spec* ]]; then
114             MAKE_TARGETS+=" undercloud-rpm-check"
115         fi
116         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
117             MAKE_TARGETS+=" common-rpm-check"
118         fi
119         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
120             MAKE_TARGETS+=" opendaylight-rpm-check"
121         fi
122         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
123             MAKE_TARGETS+=" onos-rpm-check"
124         fi
125         if [[ $commit_file_list == *build/opnfv-apex.spec* ]]; then
126             MAKE_TARGETS+=" opendaylight-sfc-rpm-check"
127         fi
128     fi
129 fi
130
131 # Make sure python is installed
132 if ! rpm -q python34-devel > /dev/null; then
133     sudo yum install -y epel-release
134     if ! sudo yum install -y python34-devel; then
135         echo "Failed to install python34-devel package..."
136         exit 1
137     fi
138 fi
139
140 # Execute make against targets
141 for t in $MAKE_TARGETS; do
142     run_make $t
143 done
144
145 echo "Build Complete"
146
147 # Build new Cache
148 if [ -n "$CACHE_DEST" ]; then
149     echo "Building Cache"
150     tar --atime-preserve --dereference -C $BUILD_BASE -caf $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DIR
151     echo "Copying Cache"
152     if [ ! -d $CACHE_DEST ]; then mkdir -p $CACHE_DEST; fi
153     cp $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DEST/${CACHE_NAME}.tgz
154 fi
155 echo "Complete"