a52055940162ff481b69397c0249c1766ebf55e9
[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 job_type=`echo $JOB_NAME | cut -d '-' -f 2`
123
124 echo ""
125 echo "Building for $type package in $output_dir"
126 echo ""
127
128 checking_apex_build
129 mkdir -p $output_dir
130 build_package $type
131
132 # Renaming the rpms in the format kvmfornfv-xxxxxxxx-apex-kernel-4.4.6_rt14.el7.centos.x86_64.rpm
133 if [ ${apex_build_flag} -eq 1 ];then
134     cd ${output_dir}
135     echo "Renaming the rpms"
136     source $WORKSPACE/ci/apex.conf
137     echo "${commit_id}"
138     short_hash=`git rev-parse --short=8 ${commit_id}`
139     echo "$short_hash"
140     rename 's/^/kvmfornfv-'${short_hash}'-apex-/' kernel-*
141     variable=`ls kvmfornfv-* | grep "devel" | awk -F "_" '{print $3}' | awk -F "." '{print $1}'`
142     rename "s/${variable}/centos/" kvmfornfv-*
143 fi
144
145 # Uploading rpms only for daily job
146 if [ $job_type == "verify" ]; then
147    if [ $type == "centos" ]; then
148       echo "Removing kernel-debuginfo rpm from output_dir"
149       rm -f ${output_dir}/kernel-debug*
150       echo "Checking packages in output_dir"
151       ls -lrth ${output_dir}
152    else
153      echo "Removing debug debian from output_dir"
154      rm -f ${output_dir}/*dbg*
155      echo "Checking packages in output_dir"
156      ls -lrth ${output_dir}
157    fi
158 fi