Merge "Add VTN Coordinator tarball to the Virtualization Edition."
[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     # We only care about a shallow clone (no need to grab the entire project)
76     git clone --depth 0 https://git.opendaylight.org/gerrit/p/controller.git $buildroot/controller
77     git clone --depth 0 https://git.opendaylight.org/gerrit/p/integration.git $buildroot/integration
78 }
79
80 # Copy the projects from snapshots.
81 # shague: Fill in with the nexus info.
82 function snapshot_source {
83     echo "$FUNCNAME: Not implemented yet."
84 }
85
86 # xz the source for later use by rpmbuild.
87 # shague: need another archive method for snapshot getsource builds since
88 # the source did not come from a git repo.
89 function mk_archives {
90     cd $buildroot/integration/packaging/rpm/fedora
91     git archive HEAD opendaylight-controller.sysconfig opendaylight-controller.systemd \
92         | xz > $buildroot/tmpbuild/opendaylight-controller-integration-$versionmajor.tar.xz
93
94     cd $buildroot/controller
95     git archive --prefix=opendaylight-controller-$versionmajor/ HEAD | \
96         xz > $buildroot/tmpbuild/opendaylight-controller-$versionmajor.tar.xz
97 }
98
99 # shague: Fill in with Nexus info.
100 function push_rpms {
101     echo "$FUNCNAME: Not implemented yet."
102 }
103
104 function show_vars {
105      cat << EOF
106 Building controller using:
107 distribution: $dist
108 buildtype:    $buildtype
109 release:      $release
110 version:      $version
111 getsource:    $getsource
112 buildroot:    $buildroot
113 EOF
114 }
115
116 # Main function that builds the rpm's for snapshot's.
117 function build_snapshot {
118     cp -f $buildroot/integration/packaging/rpm/fedora/opendaylight-controller.spec \
119         $buildroot/tmpbuild
120
121     mk_versionsnapsuffix
122
123         cd $buildroot/tmpbuild
124
125     # append snap suffix to version
126         versionmajor="$( rpm -q --queryformat="%{version}\n" --specfile opendaylight-controller.spec | head -n 1 | awk '{print $1}').$versionsnapsuffix"
127
128 # test code to short circuit the controller build
129 #if [ 2 = 1 ]; then
130         sed -r -i -e '/^Version:/s/\s*$/'".$versionsnapsuffix/" opendaylight-controller.spec
131
132         mk_archives
133
134         cd $buildroot/tmpbuild
135         #name="$(rpm -q --queryformat="%{name}\n" --specfile *.spec | head -n 1)"
136
137     # Build the source RPM for use by mock later.
138         #rm -f SRPMS/*.src.rpm
139         rpmbuild -bs --define '%_topdir '"`pwd`" --define '%_sourcedir %{_topdir}' \
140        --define "%dist .$pkg_dist_suffix" opendaylight-controller.spec
141
142     if [ $? != 0 ]; then
143         echo "rpmbuild of controller.src.rpm failed."
144         exit 2
145     fi
146
147         echo ":::::"
148         echo "::::: building opendaylight-controller.rpm in mock"
149         echo ":::::"
150
151         resultdir="repo/controller.$pkg_dist_suffix.noarch.snap"
152
153     # Initialize our mock build location (we'll be using --no-clean later)
154     # If we don't do the first init we can't build since the environment
155     # doesn't get setup correctly!
156     eval $mock_cmd -r $dist --init
157
158     # Build the rpm using mock.
159     # Keep the build because we will need the controller.zip file for later
160     # when building the controller-dependencies.rpm.
161     eval $mock_cmd -v -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
162         -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
163         SRPMS/opendaylight-controller-$versionmajor-*.src.rpm
164
165     if [ $? != 0 ]; then
166         echo "mock of controller.src.rpm failed."
167         exit 2
168     fi
169
170 #else
171 #    versionmajor=0.1.0.snap.20131203.165045.git.c406e47
172 #    versionsnapsuffix=snap.20131203.165045.git.c406e47
173 #    resultdir="repo/controller.$pkg_dist_suffix.noarch.snap"
174 #fi
175
176     # Now build the dependencies RPM
177
178     # Copy the controller.zip for use in the dependencies.rpm.
179         eval $mock_cmd -v -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
180         -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
181         --copyout \"builddir/build/BUILD/opendaylight-controller-$versionmajor/opendaylight/distribution/opendaylight/target/distribution.opendaylight-osgipackage.zip\" \"$resultdir/opendaylight-controller-$versionmajor.zip\"
182
183     ln -sf $resultdir/opendaylight-controller-$versionmajor.zip \
184         $buildroot/tmpbuild
185
186     cp -f $buildroot/integration/packaging/rpm/fedora/opendaylight-controller-dependencies.spec \
187         $buildroot/tmpbuild
188     sed -r -i -e '/^Version:/s/\s*$/'".$versionsnapsuffix/" opendaylight-controller-dependencies.spec
189     rpmbuild -bs --define '%_topdir '"`pwd`" --define '%_sourcedir %{_topdir}' \
190         --define "%dist .$pkg_dist_suffix" opendaylight-controller-dependencies.spec
191
192     if [ $? != 0 ]; then
193         echo "rpmbuild of controller-dependencies.src.rpm failed."
194         exit 2
195     fi
196
197     echo ":::::"
198     echo "::::: building opendaylight-controller-dependencies.rpm in mock"
199     echo ":::::"
200
201     resultdir="repo/controller-dependencies.$pkg_dist_suffix.noarch.snap"
202
203     eval $mock_cmd -v -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
204         -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
205         SRPMS/opendaylight-controller-dependencies-$versionmajor-*.src.rpm
206
207     if [ $? != 0 ]; then
208         echo "mock of controller-dependencies.src.rpm failed."
209         exit 2
210     fi
211
212     push_rpms
213 }
214
215 # Main function that builds the rpm's for release's.
216 # shague: should be similar to snapshot but use a different version or tag.
217 function build_release {
218     echo "$FUNCNAME: Not implemented yet."
219 }
220
221 function parse_options {
222     while true ; do
223         case "$1" in
224         --debug)
225             set -vx; shift;
226             ;;
227
228         --buildtype)
229             shift; buildtype="$1"; shift;
230             if [ "$buildtype" != "snapshot" ] && [ "$buildtype" != "release" ]; then
231                 usage 1 "Invalid build type.";
232             fi
233             ;;
234
235         --buildroot)
236             shift; buildroot="$1"; shift;
237             if [ "$buildroot" == "" ]; then
238                 usage 1 "Missing build root.";
239             fi
240             if [ ! -d "$buildroot" ]; then
241                 usage 1 "Invalid build root path."
242             fi
243             ;;
244
245         --buildnumber)
246             shift; buildnumber="$1"; shift;
247             if [ "$buildnumber" == ""  ]; then
248                 usage 1 "Missing build number.";
249             fi
250             ;;
251
252         --cleanroot)
253             cleanroot=1; shift;
254             ;;
255
256         --getsource)
257             shift; getsource="$1"; shift;
258             if [ "$getsource" != "clone" ] && [ "$getsource" != "snapshot" ] && \
259                [ "$getsource" != "buildroot" ]; then
260                 usage 1 "Invalid getsource method.";
261             fi
262             ;;
263
264         --dist)
265             shift; dist="$1"; shift;
266             if [ "$dist" == "" ]; then
267                 usage 1 "Missing distribution.";
268             fi
269             ;;
270
271         --release)
272             shift; release="$1"; shift;
273             if [ "$release" == "" ]; then
274                 usage 1 "Missing release.";
275             fi
276             ;;
277
278         --version)
279             shift; version="$1"; shift;
280             if [ "$version" == "" ]; then
281                 usage 1 "Missing version.";
282             fi
283             ;;
284
285         --repourl)
286             shift; repourl="$1"; shift;
287             if [ "$repourl" == "" ]; then
288                 usage 1 "Missing repo url.";
289             fi
290             ;;
291
292         --repouser)
293             shift; repouser="$1"; shift;
294             if [ "$repouser" == "" ]; then
295                 usage 1 "Missing repo user.";
296             fi
297             ;;
298
299         --repopw)
300             shift; repopw="$1"; shift;
301             if [ "$repopw" == "" ]; then
302                 usage 1 "Missing repo pw.";
303             fi
304             ;;
305
306         -? | -h | --help)
307             usage 0
308             ;;
309         "")
310             break
311             ;;
312         *)
313             echo "Ignoring unknown option: $1"; shift;
314         esac
315     done
316 }
317
318
319 #################### main ####################
320
321 parse_options "$@"
322
323 # Some more error checking...
324 if [ $cleanroot = 1 ] && [ $getsource = "buildroot" ]; then
325     echo "Aborting. You probably do not want to clean the directory" \
326          "containing the source."
327     exit 1
328 fi
329
330 show_vars
331
332 if [ $cleanroot = 1 ]; then
333     rm -rf $buildroot
334     mkdir -p $buildroot
335 fi
336
337 # Setup the temp build directory.
338 mkdir -p $buildroot/tmpbuild/repo
339
340 # Get the source.
341 case "$getsource" in
342 clone)
343     clone_source;
344     ;;
345
346 snapshot)
347     snapshot_source;
348     ;;
349
350 buildroot)
351     if [ ! -d "controller" ] || [ ! -d "integration" ]; then
352         echo "Problem with controller or integration projects in buildroot."
353     fi
354     ;;
355 esac
356
357 if [ "$buildtype" = "snapshot" ]; then
358     echo "Building a snapshot build"
359     build_snapshot
360 else
361     build_release
362 fi
363
364 exit 0