ef06a716c1b1054d67aba6abf517a4a1278e2c36
[kvmfornfv.git] / ci / build.sh
1 #!/bin/bash
2 #
3 # Common parameter parsing for kvmfornfv scripts
4 #
5
6 function checking_apex_build() {
7     echo ""
8     commit=`git rev-parse HEAD`
9     echo "commit id: $commit"
10     echo "Checking for presence of apex.conf in the current patch"
11     git diff-tree --no-commit-id --name-only -r ${commit} | grep apex.conf
12 #    result=`git show --name-only ${commit} | grep apex.conf`
13     result=`git diff-tree --no-commit-id --name-only -r ${commit} | grep apex.conf`
14     if [ -z "${result}" ]; then
15        echo "Does not include the file apex.conf"
16        apex_build_flag=0
17     else
18        source $WORKSPACE/ci/apex.conf
19        echo "Includes apex.conf"
20        apex_build_flag=1
21     fi
22 }
23
24
25 function usage() {
26     echo ""
27     echo "Usage --> $0 [-p package_type] [-o output_dir] [-h]"
28     echo "  package_type : centos/ubuntu/both ;  default is centos"
29     echo "  output_dir : stores rpm and debian packages"
30     echo "  -h : Help section"
31     echo ""
32 }
33
34 output_dir=""
35 type=""
36
37 function run() {
38    case $1 in
39       centos)
40          if [ ${apex_build_flag} -eq 0 ];then
41             cd $WORKSPACE/ci/build_rpm
42             sudo docker build -t kvm_rpm .
43             sudo docker run --privileged=true -v $WORKSPACE:/opt/kvmfornfv -t  kvm_rpm \
44                          /opt/kvmfornfv/ci/build_interface.sh $1
45          else
46             cd $WORKSPACE/ci/
47             echo $output_dir
48             sudo docker build -t kvm_apex .
49             sudo docker run --privileged=true -v $WORKSPACE:/opt/kvmfornfv -t  kvm_apex  \
50                          /opt/kvmfornfv/ci/apex_build.sh build_output
51          fi
52       ;;
53       ubuntu)
54          cd $WORKSPACE/ci/build_deb
55          sudo docker build -t kvm_deb .
56          sudo docker run -v $WORKSPACE:/opt/kvmfornfv -t  kvm_deb \
57                       /opt/kvmfornfv/ci/build_interface.sh $1
58       ;;
59       *) echo "Not supported system"; exit 1;;
60    esac
61 }
62
63 function build_package() {
64     choice=$1
65     case "$choice" in
66         "centos"|"ubuntu")
67             echo "Build $choice Rpms/Debians"
68             run $choice
69         ;;
70         "both")
71             echo "Build $choice Debians and Rpms"
72             run "centos"
73             run "ubuntu"
74         ;;
75         *)
76             echo "Invalid package option"
77             usage
78             exit 1
79         ;;
80     esac
81 }
82
83 ##  --- Parse command line arguments / parameters ---
84 while getopts ":o:p:h" option; do
85     case $option in
86         p) # package
87           type=$OPTARG
88           ;;
89         o) # output_dir
90           output_dir=$OPTARG
91           ;;
92         :)
93           echo "Option -$OPTARG requires an argument."
94           usage
95           exit 1
96           ;;
97         h)
98           usage
99           exit 0
100           ;;
101         *)
102           echo "Unknown option: $OPTARG."
103           usage
104           exit 1
105           ;;
106         ?)
107           echo "[WARNING] Unknown parameters!!!"
108           echo "Using default values for package generation & output"
109     esac
110 done
111
112 if [[ -z "$type" ]]
113 then
114     type='centos'
115 fi
116
117 if [[ -z "$output_dir" ]]
118 then
119     output_dir=$WORKSPACE/build_output
120 fi
121
122 echo ""
123 echo "Building for $type package in $output_dir"
124 echo ""
125
126 checking_apex_build
127 mkdir -p $output_dir
128 build_package $type
129
130 # Renaming the rpms in the format kvmfornfv-xxxxxxxx-apex-kernel-4.4.6_rt14.el7.centos.x86_64.rpm
131 if [ ${apex_build_flag} -eq 1 ];then
132     cd ${output_dir}
133     echo "Renaming the rpms"
134     source $WORKSPACE/ci/apex.conf
135     echo "${commit_id}"
136     short_hash=`git rev-parse --short=8 ${commit_id}`
137     echo "$short_hash"
138     rename 's/^/kvmfornfv-'${short_hash}'-apex-/' kernel-*
139     variable=`ls kvmfornfv-* | grep "devel" | awk -F "_" '{print $3}' | awk -F "." '{print $1}'`
140     rename "s/${variable}/centos/" kvmfornfv-*
141 fi