b5bfc8ca28b3646cb21563c7bd947622efe188dc
[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_TARGET="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_TARGET="iso"
57                 echo "Building opnfv-apex RPMs and ISO"
58                 shift 1
59             ;;
60         --rpms )
61                 MAKE_TARGET="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 parse_cmdline "$@"
80
81 if [ -n "$RELEASE" ]; then MAKE_ARGS+="RELEASE=$RELEASE "; fi
82
83 # Get the Old Cache
84 if [ -n "$CACHE_DEST" ]; then
85     echo "Retrieving Cache"
86     if [ -f $CACHE_DEST/${CACHE_NAME}.tgz ]; then
87         rm -rf $BUILD_BASE/$CACHE_DIR
88         cp -f $CACHE_DEST/${CACHE_NAME}.tgz $BUILD_BASE/${CACHE_NAME}.tgz
89         tar xzf $BUILD_BASE/${CACHE_NAME}.tgz
90     elif [ ! -d $BUILD_BASE/$CACHE_DIR ]; then
91         mkdir $BUILD_BASE/$CACHE_DIR
92     fi
93 fi
94
95 #create build_output for legecy functionality compatibiltiy in jenkins
96 if [[ ! -d ../build_output  ]]; then
97     rm -f ../build_output
98     ln -s build/noarch/ ../build_output
99 fi
100
101 # Execute Make
102 make $MAKE_ARGS -C ${BUILD_BASE} $MAKE_TARGET
103 echo "Build Complete"
104
105 # Build new Cache
106 if [ -n "$CACHE_DEST" ]; then
107     echo "Building Cache"
108     tar --atime-preserve --dereference -C $BUILD_BASE -caf $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DIR
109     echo "Copying Cache"
110     if [ ! -d $CACHE_DEST ]; then mkdir -p $CACHE_DEST; fi
111     cp $BUILD_BASE/${CACHE_NAME}.tgz $CACHE_DEST/${CACHE_NAME}.tgz
112 fi
113 echo "Complete"