2 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
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
23 dist="fedora-19-x86_64"
24 pkg_dist_suffix="fc19"
25 mock_cmd="/usr/bin/mock"
39 # Maven is not installed at the system wide level but at the Jenkins level
40 # We need to map our Maven call to the Jenkins installed version
41 mvn_cmd="$JENKINS_HOME/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.0.4/bin/mvn"
43 # Define our push repositories here. We do it in code since we can't
44 # use a POM file for the RPM pushes (we're doing "one-off" file pushes)
46 # Repositories will be of the form baseRepositoryId-$dist{-testing}
47 # -testing if it's a snapshot build (not using -snapshot as the name as
48 # -testing is the more common testing repo suffix for yum)
49 baseURL="http://nexus.opendaylight.org/content/repositories/opendaylight-yum-"
50 baseRepositoryId="opendaylight-yum-"
54 readonly RCPARMSERROR=65
55 readonly RCRPMBUILDERROR=66
56 readonly RCRMOCKERROR=67
65 local level=$1; shift;
67 if [ $level -le $loglevel ]; then
76 if [ "$outstr" != "" ]; then
81 echo "Usage: `basename $0` [OPTION...]"
84 echo " --buildtype TYPE build type, either snapshot or release"
85 echo " --buildroot DIRECTORY build root path"
86 echo " --buildtag tag the tmpbuild directory, i.e. Jenkins build number"
87 echo " --cleanroot clean buildroot directory before building"
88 echo " --cleantmp clean tmpbuild directory before building"
89 echo " --dist DIST distribution"
90 echo " --distsuffix SUFFIX package distribution suffix"
91 echo " --getsource METHOD method for getting source clone|snapshot|buildroot"
94 echo " --release RELEASE release tag (not used yet)"
95 echo " --version VERSION version tag"
97 echo "Repo sync options:"
98 echo " --repourl REPOURL url of the repo, include http://"
99 echo " --repouser REPOUSER user for repo"
100 echo " --repopw REPOPW password for repo"
102 echo "Deployment options:"
103 echo " --pushrpms push the built rpms to a maven repository"
104 echo " --mvn_cmd fully qualified path to where the mvn command"
105 echo " (defaults to Jenkins installation of Maven 3.0.4)"
106 echo " --baseURL base deployment URL. \$dist will be added to the end of this"
107 echo " If this is a snapshot build then -testing be added at the end"
108 echo " --baseRepositoryId base repository name. \$dist will be added to the end of this"
109 echo " If this is a snapshot build then -testing be added at the end"
112 echo " --mockinit Run mock init"
113 echo " --mockmvn replace the maven command used within mock"
116 echo " -?, -h, --help Display this help and exit"
117 echo " --debug Enable bash debugging output"
118 echo " --mockdebug Enable mock debugging output"
121 echo " --shortcircuits INDEX Start build with project index"
122 echo " --shortcircuite INDEX End build with project index"
123 echo " --listprojects List the projects and indexes"
124 echo " --timesuffix TIME Set the time suffix to use for build output"
130 readonly PJ_INTEGRATION=0
131 readonly PJ_CONTROLLER=1
133 readonly PJ_OPENFLOWJAVA=3
134 readonly PJ_OPENFLOWPLUGIN=4
135 readonly PJ_DEPENDENCIES=5
136 readonly PJ_DISTRIBUTION=6
137 readonly PJ_OPENDOVE=7
138 readonly PJ_LISPFLOWMAPPING=8
141 projects[$PJ_INTEGRATION]="integration"
142 projects[$PJ_CONTROLLER]="controller"
143 projects[$PJ_OVSDB]="ovsdb"
144 projects[$PJ_OPENFLOWJAVA]="openflowjava"
145 projects[$PJ_OPENFLOWPLUGIN]="openflowplugin"
146 projects[$PJ_DEPENDENCIES]="controller-dependencies"
147 projects[$PJ_DISTRIBUTION]="distribution"
148 projects[$PJ_OPENDOVE]="opendove"
149 projects[$PJ_LISPFLOWMAPPING]="lispflowmapping"
151 versions[$PJ_INTEGRATION]=""
152 versions[$PJ_CONTROLLER]=""
153 versions[$PJ_OVSDB]=""
154 versions[$PJ_OPENFLOWJAVA]=""
155 versions[$PJ_OPENFLOWPLUGIN]=""
156 versions[$PJ_DEPENDENCIES]=""
157 versions[$PJ_DISTRIBUTION]=""
158 versions[$PJ_OPENDOVE]=""
159 versions[$PJ_LISPFLOWMAPPING]=""
161 suffix[$PJ_INTEGRATION]=""
162 suffix[$PJ_CONTROLLER]=""
164 suffix[$PJ_OPENFLOWJAVA]=""
165 suffix[$PJ_OPENFLOWPLUGIN]=""
166 suffix[$PJ_DEPENDENCIES]=""
167 suffix[$PJ_DISTRIBUTION]=""
168 suffix[$PJ_OPENDOVE]=""
169 suffix[$PJ_LISPFLOWMAPPING]=""
171 gitprojects="$PJ_INTEGRATION $PJ_CONTROLLER $PJ_OVSDB $PJ_OPENFLOWJAVA $PJ_OPENFLOWPLUGIN $PJ_OPENDOVE $PJ_LISPFLOWMAPPING"
173 # Clone the projects.
174 function clone_source {
175 for i in $gitprojects; do
176 # We only care about a shallow clone (no need to grab the entire project)
177 log $LOGINFO "Cloning ${projects[$i]} to $buildroot/${projects[$i]}"
178 git clone --depth 0 https://git.opendaylight.org/gerrit/p/${projects[$i]}.git $buildroot/${projects[$i]}
182 # Copy the projects from snapshots.
183 # shague: Fill in with the nexus info.
184 # Make mk_snapshot_archives that just sets up the version strings.
185 function snapshot_source {
186 log $LOGINFO "$FUNCNAME: Not implemented yet."
189 # Archive the projects to creates the SOURCES for rpmbuild:
190 # - xz the source for later use by rpmbuild.
191 # - get the version and git hashes to produce a versions and suffix for each project.
192 # - copy the archives to the SOURCES dir
193 # shague: need another archive method for snapshot getsource builds since
194 # the source did not come from a git repo.
195 function mk_git_archives {
198 for i in $gitprojects; do
199 if [ "$version" == "" ]; then
200 cd $buildroot/${projects[$i]}
201 suffix[$i]="snap.$timesuffix.git.$(git log -1 --pretty=format:%h)"
203 suffix[$i]="snap.$version"
206 cd $buildroot/integration/packaging/rpm
207 # integration uses the controller.spec because there isn't an integration.spec to query.
208 if [ ${projects[$i]} == ${projects[$PJ_INTEGRATION]} ]; then
209 versions[$i]="$( rpm -q --queryformat="%{version}\n" --specfile opendaylight-${projects[$PJ_CONTROLLER]}.spec | head -n 1 | awk '{print $1}').${suffix[$i]}"
211 versions[$i]="$( rpm -q --queryformat="%{version}\n" --specfile opendaylight-${projects[$i]}.spec | head -n 1 | awk '{print $1}').${suffix[$i]}"
214 log $LOGINFO "Building archive: $tmpbuild/opendaylight-${projects[$i]}-${versions[$i]}.tar.xz"
215 cd $buildroot/${projects[$i]}
216 git archive --prefix=opendaylight-${projects[$i]}-${versions[$i]}/ HEAD | \
217 xz > $tmpbuild/opendaylight-${projects[$i]}-${versions[$i]}.tar.xz
221 # Use the controller versions because these projects don't have a repo.
222 for i in `seq $PJ_DEPENDENCIES $PJ_DISTRIBUTION`; do
223 suffix[$i]=${suffix[$PJ_CONTROLLER]}
224 versions[$i]=${versions[$PJ_CONTROLLER]}
227 # Don't forget any patches.
228 cp $buildroot/integration/packaging/rpm/opendaylight-integration-fix-paths.patch $tmpbuild
231 # Pushes rpms to the specified Nexus repository
232 # This only happens if pushrpms is true
234 if [ $pushrpms = 1 ]; then
235 allrpms=`find $tmpbuild/repo -iname '*.rpm'`
237 log $LOGINFO "RPMS found"
238 for i in $allrpms; do
243 log $LOGINFO "::::: pushing RPMs"
245 for i in $allrpms; do
246 rpmname=`rpm -qp --queryformat="%{name}" $i`
247 rpmversion=`rpm -qp --queryformat="%{version}" $i`
248 distro=`echo $dist | tr - .`
250 if [ `echo $i | grep 'src.rpm'` ]; then
251 rpmrelease=`rpm -qp --queryformat="%{release}.src" $i`
254 rpmrelease=`rpm -qp --queryformat="%{release}.%{arch}" $i`
259 if [ "$buildtype" == "snapshot" ]; then
260 repositoryId="${baseRepositoryId}${dist}-testing"
261 pushURL="${baseURL}${dist}-testing"
263 repositoryId="${baseRepositoryId}${dist}"
264 pushURL="${baseURL}${dist}"
267 # Note version is the full version+release+{arch|src}
268 # if it is not configured this way on pushes then a download
269 # of the artifact will result in just the name-version.rpm
270 # instead of name-version-release.{arch|src}.rpm
271 $mvn_cmd org.apache.maven.plugins:maven-deploy-plugin:2.8.1:deploy-file \
272 -Dfile=$i -DrepositoryId=$repositoryId \
273 -Durl=$pushURL -DgroupId=$groupId \
274 -Dversion=$rpmversion-$rpmrelease -DartifactId=$rpmname \
282 Building controller using:
284 suffix: $pkg_dist_suffix
285 buildtype: $buildtype
288 getsource: $getsource
289 buildroot: $buildroot
298 # Build a single project.
299 function build_project {
301 local versionmajor="$2"
302 local versionsnapsuffix="$3"
305 log $LOGINFO "::::: building opendaylight-$project.rpm"
308 cp -f $buildroot/integration/packaging/rpm/opendaylight-$project.spec \
312 # Find lines starting with Version: and replace the rest of the line with the versionsnapsuffix
313 sed -r -i -e '/^Version:/s/\s*$/'".$versionsnapsuffix/" opendaylight-$project.spec
315 # Set the write values in the spec files.
317 ${projects[$PJ_CONTROLLER]})
318 # Set the version for the integration source.
319 # Find lines with opendaylight-integration-%{version} and replace %{version} with the version.
320 sed -r -i -e '/opendaylight-integration-\%\{version\}/s/\%\{version\}/'"${versions[$PJ_INTEGRATION]}"'/g' \
321 opendaylight-$project.spec
324 ${projects[$PJ_DEPENDENCIES]})
325 # Set the version for ovsdb in the dependencies spec.
326 # Find lines with opendaylight-ovsdb-%{version} and replace %{version} with the version.
327 sed -r -i -e '/opendaylight-ovsdb-\%\{version\}/s/\%\{version\}/'"${versions[$PJ_OVSDB]}"'/g' \
328 opendaylight-$project.spec
329 # Find lines with opendaylight-ovsdb-dependencies-%{version} and replace %{version} with the version.
330 sed -r -i -e '/opendaylight-ovsdb-dependencies-\%\{version\}/s/\%\{version\}/'"${versions[$PJ_OVSDB]}"'/g' \
331 opendaylight-$project.spec
338 # Rewrite the mvn command in the rpmbuild if the user requests it.
339 if [ "$mockmvn" != "" ]; then
340 # Find lines starting with export MAVEN_OPTS= and repalce the whole line with $mockmvn
341 sed -r -i -e '/^export MAVEN_OPTS=./c\ '"$mockmvn" opendaylight-$project.spec
344 # Build the source RPM for use by mock later.
345 #rm -f SRPMS/*.src.rpm
346 log $LOGINFO "::::: building opendaylight-$project.src.rpm in rpmbuild"
347 rpmbuild -bs --define '%_topdir '"`pwd`" --define '%_sourcedir %{_topdir}' \
348 --define "%dist .$pkg_dist_suffix" opendaylight-$project.spec
351 if [ $rc != 0 ]; then
352 log $LOGERROR "rpmbuild of $project.src.rpm failed (rc=$rc)."
353 exit $RCRPMBUILDERROR
356 log $LOGINFO "::::: building opendaylight-$project.rpm in mock"
358 resultdir="repo/$project.$pkg_dist_suffix.noarch.snap"
360 # Build the rpm using mock.
361 # Keep the build because we will need the distribution zip file for later
362 # when building the controller-dependencies.rpm.
363 eval $mock_cmd $mockdebug -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
364 -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
365 SRPMS/opendaylight-$project-$versionmajor-*.src.rpm
368 if [ $rc != 0 ]; then
369 log $LOGERROR "mock of $project.rpm failed (rc=$rc)."
373 # Copy the distribution zip from the controller and ovsdb projects
374 # for use in the dependencies.rpm.
377 log $LOGINFO "::::: Copying $project distribution.zip."
378 eval $mock_cmd $mockdebug -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
379 -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
380 --copyout \"builddir/build/BUILD/opendaylight-$project-$versionmajor/opendaylight/distribution/opendaylight/target/distribution.opendaylight-osgipackage.zip\" \"$resultdir/opendaylight-$project-$versionmajor.zip\"
382 ln -sf $resultdir/opendaylight-$project-$versionmajor.zip $tmpbuild
384 if [ ! -e $tmpbuild/opendaylight-$project-$versionmajor.zip ]; then
385 log $LOGERROR "cannot find $project distribution zip file (rc=$rc1:$rc2)."
391 log $LOGINFO "::::: Copying $project distribution.zip."
392 # Grab the version from the pom.xml file.
393 # Get the lines between the parent tags, then get the line with the version tag and remove leading space and
394 # the tag itself, then remove the trailing tag to be left with the version.
395 pomversion=`sed -n -e '/<parent>/,/<\/parent>/p' "$buildroot"/ovsdb/pom.xml | sed -n -e '/<version>/s/.*<version>//p' | sed -n -e 's/<\/version>//p'`
396 eval $mock_cmd $mockdebug -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
397 -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
398 --copyout \"builddir/build/BUILD/opendaylight-$project-$versionmajor/distribution/opendaylight/target/distribution.$project-$pomversion-osgipackage.zip\" \"$resultdir/opendaylight-$project-$versionmajor.zip\"
400 ln -sf $resultdir/opendaylight-$project-$versionmajor.zip $tmpbuild
402 if [ ! -e $tmpbuild/opendaylight-ovsdb-$versionmajor.zip ]; then
403 log $LOGERROR "cannot find $project distribution zip file (rc=$rc1:$rc2)."
413 # Main function that builds the rpm's for snapshot's.
414 function build_snapshot {
415 mk_git_archives $timesuffix
417 # Initialize our mock build location (we'll be using --no-clean later)
418 # If we don't do the first init we can't build since the environment
419 # doesn't get setup correctly!
420 if [ $mockinit -eq 1 ]; then
421 eval $mock_cmd $mockdebug -r $dist --init
427 if [ $shortcircuits -ne 0 ]; then
431 if [ $shortcircuite -ne 0 ]; then
435 echo "start:end = $start:$end"
437 for i in `seq $start $end`; do
438 build_project ${projects[$i]} ${versions[$i]} ${suffix[$i]}
442 log $LOGINFO "::::: All projects have been built"
448 # Main function that builds the rpm's for release's.
449 # shague: should be similar to snapshot but use a different version or tag.
450 # spec files should be updated with correct version. If so, do
451 function build_release {
452 log $LOGINFO "$FUNCNAME: Not implemented yet."
455 function parse_options {
463 mockdebug="-v"; shift;
467 shift; buildtype="$1"; shift;
468 if [ "$buildtype" != "snapshot" ] && [ "$buildtype" != "release" ]; then
469 usage $RCPARMSERROR "Invalid build type.";
474 shift; buildroot="$1"; shift;
475 if [ "$buildroot" == "" ]; then
476 usage $RCPARMSERROR "Missing build root.";
478 if [ ! -d "$buildroot" ]; then
479 usage $RCPARMSERROR "Invalid build root path."
484 shift; buildtag="$1"; shift;
485 if [ "$buildtag" == "" ]; then
486 usage $RCPARMSERROR "Missing build tag.";
499 shift; getsource="$1"; shift;
500 if [ "$getsource" != "clone" ] && [ "$getsource" != "snapshot" ] && \
501 [ "$getsource" != "buildroot" ]; then
502 usage $RCPARMSERROR "Invalid getsource method.";
507 shift; dist="$1"; shift;
508 if [ "$dist" == "" ]; then
509 $RCPARMSERROR "Missing distribution.";
514 shift; pkg_dist_suffix="$1"; shift;
515 if [ "$pkg_dist_suffix" == "" ]; then
516 $RCPARMSERROR "Missing package distribution suffix.";
521 shift; release="$1"; shift;
522 if [ "$release" == "" ]; then
523 $RCPARMSERROR "Missing release.";
528 shift; version="$1"; shift;
529 if [ "$version" == "" ]; then
530 $RCPARMSERROR "Missing version.";
535 shift; repourl="$1"; shift;
536 if [ "$repourl" == "" ]; then
537 $RCPARMSERROR "Missing repo url.";
542 shift; repouser="$1"; shift;
543 if [ "$repouser" == "" ]; then
544 $RCPARMSERROR "Missing repo user.";
549 shift; repopw="$1"; shift;
550 if [ "$repopw" == "" ]; then
551 $RCPARMSERROR "Missing repo pw.";
560 shift; timesuffix="$1"; shift;
561 if [ "$timesuffix" == "" ]; then
562 usage $RCPARMSERROR "Missing time suffix.";
567 shift; mvn_cmd="$1"; shift;
568 if [ "$mvn_cmd" == "" ]; then
569 $RCPARMSERROR "Missing mvn_cmd.";
578 shift; mockmvn="$1"; shift;
579 if [ "$mockmvn" == "" ]; then
580 $RCPARMSERROR "Missing mockmvn.";
585 shift; baseURL="$1"; shift;
586 if [ "$baseURL" == "" ]; then
587 $RCPARMSERROR "Missing baseURL.";
592 shift; baseRepositoryId="$1"; shift;
593 if [ "$baseRepositoryId" == "" ]; then
594 $RCPARMSERROR "Missing baseRepositoryId.";
599 shift; shortcircuits=$1; shift;
603 shift; shortcircuite=$1; shift;
607 listprojects=1; shift;
617 log $LOGINFO "Ignoring unknown option: $1"; shift;
623 #################### main ####################
625 if [ "$timesuffix" == "" ]; then
626 timesuffix="$(date +%F_%T | tr -d .:- | tr _ .)"
628 date_start=$(date +%s)
632 if [ $listprojects -eq 1 ]; then
633 for i in `seq $PJ_FIRST $PJ_LAST`; do
634 echo "$i ${projects[$i]}"
639 # Some more error checking...
640 if [ -z $buildroot ]; then
641 log $LOGERROR "Mising buildroot"
645 if [ $cleanroot -eq 1 ] && [ $getsource = "buildroot" ]; then
646 log $LOGERROR "Aborting. You probably do not want to clean the directory" \
647 "containing the source."
651 # Can change tmpbuild to be an index or other tag
652 if [ -n "$buildtag" ]; then
653 tmpbuild="$buildroot/bld_$buildtag"
655 tmpbuild="$buildroot/bld"
660 if [ $cleanroot -eq 1 ]; then
665 if [ $cleantmp -eq 1 ]; then
669 # Setup the temp build directory.
670 mkdir -p $tmpbuild/repo
685 for i in `seq $PJ_INTEGRATION $PJ_OPENFLOWPLUGIN`; do
686 if [ ! -d ${projects[$i]} ]; then
687 log $LOGERROR "Missing ${projects[$i]}"
694 if [ "$buildtype" = "snapshot" ]; then
695 log $LOGINFO "Building a snapshot build"
698 log $LOGINFO "Release builds are not supported yet."
703 diff=$(($date_end - $date_start))
704 log $LOGINFO "Build took $(($diff / 3600 % 24)) hours $(($diff / 60 % 60)) minutes and $(($diff % 60)) seconds."