Add script to allow jenkins to automate the rpm builds.
[integration.git] / packaging / rpm / buildrpm.sh
1 #!/bin/bash
2 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
3
4 # shague todo:
5 # - Add -r option for mock to choose the distribution
6 # - add option to pass in spec file name, maybe use spec.in template
7 # - add option to pass in version and release
8
9 #set -vx
10
11 buildtype="snapshot"
12 buildroot=`pwd`
13 buildnumber=0
14 cleanroot=0
15 getsource="buildroot"
16 version=""
17 release=""
18 versionsnapsuffix=""
19 versionmajor=""
20 repourl=""
21 repouser=""
22 repopw=""
23 dist="fedora-19-x86_64"
24 pkg_dist_suffix="fc19"
25 mock_cmd='/usr/bin/mock'
26
27
28 function usage {
29     local rc=$1
30     local outstr=$2
31
32     if [ "$outstr" != "" ]; then
33         echo "$outstr"
34         echo
35     fi
36
37     echo "Usage: `basename $0` [OPTION...]"
38     echo
39     echo "Build options:"
40     echo "  --buildtype TYPE       build type, either snapshot or release"
41     echo "  --buildroot DIRECTORY  build root path"
42     echo "  --buildnumber NUMBER   jenkins build number"
43     echo "  --cleanroot            clean buildroot directory before building"
44     echo "  --dist DIST            distribution"
45     echo "  --getsource METHOD     method for getting source clone|snapshot|buildroot"
46     echo
47     echo "Tag options:"
48     echo "  --release RELEASE      release tag (not used yet)"
49     echo "  --version VERSION      version tag"
50     echo
51     echo "Repo sync options:"
52     echo "  --repourl REPOURL      url of the repo, include http://"
53     echo "  --repouser REPOUSER    user for repo"
54     echo "  --repopw REPOPW        password for repo"
55     echo
56     echo "Help options:"
57     echo "  -?, -h, --help  Display this help and exit"
58     echo "  --debug         Enable bash debugging output"
59     exit $rc
60 }
61
62 # Make a snapshot version tag using the git hash and date
63 # shague: Modify to use ODL build numbering"
64 function mk_versionsnapsuffix {
65     if [ "$version" = "" ]; then
66         cd $buildroot/controller
67         versionsnapsuffix="snap.$(date +%F_%T | tr -d .:- | tr _ .).git.$(git log -1 --pretty=format:%h)"
68     else
69         versionsnapsuffix="snap.$version"
70     fi
71 }
72
73 # Clone the projects.
74 function clone_source {
75     git clone https://git.opendaylight.org/gerrit/p/controller.git $buildroot/controller
76     git clone https://git.opendaylight.org/gerrit/p/integration.git $buildroot/integration
77 }
78
79 # Copy the projects from snapshots.
80 # shague: Fill in with the nexus info.
81 function snapshot_source {
82     echo "$FUNCNAME: Not implemented yet."
83 }
84
85 # xz the source for later use by rpmbuild.
86 # shague: need another archive method for snapshot getsource builds since
87 # the source did not come from a git repo.
88 function mk_archives {
89     cd $buildroot/integration/packaging/rpm/fedora
90     git archive HEAD opendaylight-controller.sysconfig opendaylight-controller.systemd \
91         | xz > $buildroot/tmpbuild/opendaylight-controller-integration-$versionmajor.tar.xz
92
93     cd $buildroot/controller
94     git archive --prefix=opendaylight-controller-$versionmajor/ HEAD | \
95         xz > $buildroot/tmpbuild/opendaylight-controller-$versionmajor.tar.xz
96 }
97
98 # shague: Fill in with Nexus info.
99 function push_rpms {
100     echo "$FUNCNAME: Not implemented yet."
101 }
102
103 function show_vars {
104      cat << EOF
105 Building controller using:
106 distribution: $dist
107 buildtype:    $buildtype
108 release:      $release
109 version:      $version
110 getsource:    $getsource
111 buildroot:    $buildroot
112 EOF
113 }
114
115 # Main function that builds the rpm's for snapshot's.
116 function build_snapshot {
117     cp -f $buildroot/integration/packaging/rpm/fedora/opendaylight-controller.spec \
118         $buildroot/tmpbuild
119
120     mk_versionsnapsuffix
121
122         cd $buildroot/tmpbuild
123
124     # append snap suffix to version
125         versionmajor="$( rpm -q --queryformat="%{version}\n" --specfile opendaylight-controller.spec | head -n 1 | awk '{print $1}').$versionsnapsuffix"
126
127 # test code to short circuit the controller build
128 #if [ 2 = 1 ]; then
129         sed -r -i -e '/^Version:/s/\s*$/'".$versionsnapsuffix/" opendaylight-controller.spec
130
131         mk_archives
132
133         cd $buildroot/tmpbuild
134         #name="$(rpm -q --queryformat="%{name}\n" --specfile *.spec | head -n 1)"
135
136     # Build the source RPM for use by mock later.
137         #rm -f SRPMS/*.src.rpm
138         rpmbuild -bs --define '%_topdir '"`pwd`" --define '%_sourcedir %{_topdir}' \
139        --define "%dist .$pkg_dist_suffix" opendaylight-controller.spec
140
141     if [ $? != 0 ]; then
142         echo "rpmbuild of controller.src.rpm failed."
143         exit 2
144     fi
145
146         echo ":::::"
147         echo "::::: building opendaylight-controller.rpm in mock"
148         echo ":::::"
149
150         resultdir="repo/controller.$pkg_dist_suffix.noarch.snap"
151
152     # Build the rpm using mock.
153     # Keep the build because we will need the controller.zip file for later
154     # when building the controller-dependencies.rpm.
155     eval $mock_cmd -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
156         -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
157         SRPMS/opendaylight-controller-$versionmajor-*.src.rpm
158
159     if [ $? != 0 ]; then
160         echo "mock of controller.src.rpm failed."
161         exit 2
162     fi
163
164 #else
165 #    versionmajor=0.1.0.snap.20131203.165045.git.c406e47
166 #    versionsnapsuffix=snap.20131203.165045.git.c406e47
167 #    resultdir="repo/controller.$pkg_dist_suffix.noarch.snap"
168 #fi
169
170     # Now build the dependencies RPM
171
172     # Copy the controller.zip for use in the dependencies.rpm.
173         eval $mock_cmd -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
174         -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
175         --copyout \"builddir/build/BUILD/opendaylight-controller-$versionmajor/opendaylight/distribution/opendaylight/target/distribution.opendaylight-osgipackage.zip\" \"$resultdir/opendaylight-controller-$versionmajor.zip\"
176
177     ln -sf $resultdir/opendaylight-controller-$versionmajor.zip \
178         $buildroot/tmpbuild
179
180     cp -f $buildroot/integration/packaging/rpm/fedora/opendaylight-controller-dependencies.spec \
181         $buildroot/tmpbuild
182     sed -r -i -e '/^Version:/s/\s*$/'".$versionsnapsuffix/" opendaylight-controller-dependencies.spec
183     rpmbuild -bs --define '%_topdir '"`pwd`" --define '%_sourcedir %{_topdir}' \
184         --define "%dist .$pkg_dist_suffix" opendaylight-controller-dependencies.spec
185
186     if [ $? != 0 ]; then
187         echo "rpmbuild of controller-dependencies.src.rpm failed."
188         exit 2
189     fi
190
191     echo ":::::"
192     echo "::::: building opendaylight-controller-dependencies.rpm in mock"
193     echo ":::::"
194
195     resultdir="repo/controller-dependencies.$pkg_dist_suffix.noarch.snap"
196
197     eval $mock_cmd -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
198         -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
199         SRPMS/opendaylight-controller-dependencies-$versionmajor-*.src.rpm
200
201     if [ $? != 0 ]; then
202         echo "mock of controller-dependencies.src.rpm failed."
203         exit 2
204     fi
205
206     push_rpms
207 }
208
209 # Main function that builds the rpm's for release's.
210 # shague: should be similar to snapshot but use a different version or tag.
211 function build_release {
212     echo "$FUNCNAME: Not implemented yet."
213 }
214
215 function parse_options {
216     while true ; do
217         case "$1" in
218         --debug)
219             set -vx; shift;
220             ;;
221
222         --buildtype)
223             shift; buildtype="$1"; shift;
224             if [ "$buildtype" != "snapshot" ] && [ "$buildtype" != "release" ]; then
225                 usage 1 "Invalid build type.";
226             fi
227             ;;
228
229         --buildroot)
230             shift; buildroot="$1"; shift;
231             if [ "$buildroot" == "" ]; then
232                 usage 1 "Missing build root.";
233             fi
234             if [ ! -d "$buildroot" ]; then
235                 usage 1 "Invalid build root path."
236             fi
237             ;;
238
239         --buildnumber)
240             shift; buildnumber="$1"; shift;
241             if [ "$buildnumber" == ""  ]; then
242                 usage 1 "Missing build number.";
243             fi
244             ;;
245
246         --cleanroot)
247             cleanroot=1; shift;
248             ;;
249
250         --getsource)
251             shift; getsource="$1"; shift;
252             if [ "$getsource" != "clone" ] && [ "$getsource" != "snapshot" ] && \
253                [ "$getsource" != "buildroot" ]; then
254                 usage 1 "Invalid getsource method.";
255             fi
256             ;;
257
258         --dist)
259             shift; dist="$1"; shift;
260             if [ "$dist" == "" ]; then
261                 usage 1 "Missing distribution.";
262             fi
263             ;;
264
265         --release)
266             shift; release="$1"; shift;
267             if [ "$release" == "" ]; then
268                 usage 1 "Missing release.";
269             fi
270             ;;
271
272         --version)
273             shift; version="$1"; shift;
274             if [ "$version" == "" ]; then
275                 usage 1 "Missing version.";
276             fi
277             ;;
278
279         --repourl)
280             shift; repourl="$1"; shift;
281             if [ "$repourl" == "" ]; then
282                 usage 1 "Missing repo url.";
283             fi
284             ;;
285
286         --repouser)
287             shift; repouser="$1"; shift;
288             if [ "$repouser" == "" ]; then
289                 usage 1 "Missing repo user.";
290             fi
291             ;;
292
293         --repopw)
294             shift; repopw="$1"; shift;
295             if [ "$repopw" == "" ]; then
296                 usage 1 "Missing repo pw.";
297             fi
298             ;;
299
300         -? | -h | --help)
301             usage 0
302             ;;
303         "")
304             break
305             ;;
306         *)
307             echo "Ignoring unknown option: $1"; shift;
308         esac
309     done
310 }
311
312
313 #################### main ####################
314
315 parse_options "$@"
316
317 # Some more error checking...
318 if [ $cleanroot = 1 ] && [ $getsource = "buildroot" ]; then
319     echo "Aborting. You probably do not want to clean the directory" \
320          "containing the source."
321     exit 1
322 fi
323
324 show_vars
325
326 if [ $cleanroot = 1 ]; then
327     rm -rf $buildroot
328     mkdir -p $buildroot
329 fi
330
331 # Setup the temp build directory.
332 mkdir -p $buildroot/tmpbuild/repo
333
334 # Get the source.
335 case "$getsource" in
336 clone)
337     clone_source;
338     ;;
339
340 snapshot)
341     snapshot_source;
342     ;;
343
344 buildroot)
345     if [ ! -d "controller" ] || [ ! -d "integration" ]; then
346         echo "Problem with controller or integration projects in buildroot."
347     fi
348     ;;
349 esac
350
351 if [ "$buildtype" = "snapshot" ]; then
352     echo "Building a snapshot build"
353     build_snapshot
354 else
355     build_release
356 fi
357
358 exit 0