441acc65d9f194b47db97cdcaac56862aee13d4b
[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 function checking_compass_build() {
25     echo ""
26     commit=`git rev-parse HEAD`
27     echo "commit id: $commit"
28     echo "Checking for presence of compass.conf in the current patch"
29     git diff-tree --no-commit-id --name-only -r ${commit} | grep compass.conf
30     result=`git diff-tree --no-commit-id --name-only -r ${commit} | grep compass.conf`
31     if [ -z "${result}" ]; then
32        echo "Does not include the file compass.conf"
33        compass_build_flag=0
34     else
35        source $WORKSPACE/ci/compass.conf
36        echo "Includes compass.conf"
37        compass_build_flag=1
38     fi
39 }
40
41
42 function usage() {
43     echo ""
44     echo "Usage --> $0 [-p package_type] [-o output_dir] [-h]"
45     echo "  package_type : centos/ubuntu/both ;  default is centos"
46     echo "  output_dir : stores rpm and debian packages"
47     echo "  -h : Help section"
48     echo ""
49 }
50
51 output_dir=""
52 type=""
53
54 function run() {
55    case $1 in
56       centos)
57          if [ ${apex_build_flag} -eq 0 ];then
58             cd $WORKSPACE/ci/build_rpm
59             sudo docker build -t kvm_rpm .
60             sudo docker run --privileged=true -v $WORKSPACE:/opt/kvmfornfv -t  kvm_rpm \
61                          /opt/kvmfornfv/ci/build_interface.sh $1
62          else
63             cd $WORKSPACE/ci/
64             echo $output_dir
65             cp $WORKSPACE/ci/build_rpm/Dockerfile .
66             sudo docker build -t kvm_apex .
67             sudo docker run --privileged=true -v $WORKSPACE:/opt/kvmfornfv -t  kvm_apex  \
68                          /opt/kvmfornfv/ci/installer_build.sh build_output apex
69          fi
70       ;;
71       ubuntu)
72          if [ ${compass_build_flag} -eq 0 ]; then
73             cd $WORKSPACE/ci/build_deb
74             sudo docker build -t kvm_deb .
75             sudo docker run -v $WORKSPACE:/opt/kvmfornfv -t  kvm_deb \
76                         /opt/kvmfornfv/ci/build_interface.sh $1
77          else
78             cd $WORKSPACE/ci/
79             echo $output_dir
80             cp $WORKSPACE/ci/build_deb/Dockerfile .
81             sudo docker build -t kvm_docker .
82             sudo docker run --privileged=true -v $WORKSPACE:/opt/kvmfornfv -t  kvm_docker  \
83                          /opt/kvmfornfv/ci/installer_build.sh build_output compass
84          fi
85       ;;
86       *) echo "Not supported system"; exit 1;;
87    esac
88 }
89
90 function build_package() {
91     choice=$1
92     case "$choice" in
93         "centos"|"ubuntu")
94             echo "Build $choice Rpms/Debians"
95             run $choice
96         ;;
97         "both")
98             echo "Build $choice Debians and Rpms"
99             run "centos"
100             run "ubuntu"
101         ;;
102         *)
103             echo "Invalid package option"
104             usage
105             exit 1
106         ;;
107     esac
108 }
109
110 ##  --- Parse command line arguments / parameters ---
111 while getopts ":o:p:h" option; do
112     case $option in
113         p) # package
114           type=$OPTARG
115           ;;
116         o) # output_dir
117           output_dir=$OPTARG
118           ;;
119         :)
120           echo "Option -$OPTARG requires an argument."
121           usage
122           exit 1
123           ;;
124         h)
125           usage
126           exit 0
127           ;;
128         *)
129           echo "Unknown option: $OPTARG."
130           usage
131           exit 1
132           ;;
133         ?)
134           echo "[WARNING] Unknown parameters!!!"
135           echo "Using default values for package generation & output"
136     esac
137 done
138
139 if [[ -z "$type" ]]
140 then
141     type='centos'
142 fi
143
144 if [[ -z "$output_dir" ]]
145 then
146     output_dir=$WORKSPACE/build_output
147 fi
148
149 job_type=`echo $JOB_NAME | cut -d '-' -f 2`
150
151 echo ""
152 echo "Building for $type package in $output_dir"
153 echo ""
154
155 checking_compass_build
156 checking_apex_build
157 mkdir -p $output_dir
158 build_package $type
159
160 # Renaming the rpms in the format kvmfornfv-xxxxxxxx-apex-kernel-4.4.6_rt14.el7.centos.x86_64.rpm
161 if [ ${apex_build_flag} -eq 1 ];then
162     cd ${output_dir}
163     echo "Renaming the rpms"
164     source $WORKSPACE/ci/apex.conf
165     echo "${commit_id}"
166     short_hash=`git rev-parse --short=8 ${commit_id}`
167     echo "$short_hash"
168     rename 's/^/kvmfornfv-'${short_hash}'-apex-/' kernel-*
169     variable=`ls kvmfornfv-* | grep "devel" | awk -F "_" '{print $3}' | awk -F "." '{print $1}'`
170     rename "s/${variable}/centos/" kvmfornfv-*
171 fi
172
173 # Uploading rpms only for daily job
174 if [ $job_type == "verify" ]; then
175    if [ $type == "centos" ]; then
176       echo "Removing kernel-debuginfo rpm from output_dir"
177       rm -f ${output_dir}/kernel-debug*
178       echo "Checking packages in output_dir"
179       ls -lrth ${output_dir}
180    else
181      echo "Removing debug debian from output_dir"
182      rm -f ${output_dir}/*dbg*
183      echo "Checking packages in output_dir"
184      ls -lrth ${output_dir}
185    fi
186 fi