Adding sleep to l2switch suite
[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=""
13 buildtag=""
14 cleanroot=0
15 cleantmp=0
16 pushrpms=0
17 getsource="buildroot"
18 branch=""
19 depth=0
20 version=""
21 edversion=""
22 specrelease="1"
23 releasetag=0
24 versiontype="spec"
25 repourl=""
26 repouser=""
27 repopw=""
28 dist="fedora-19-x86_64"
29 pkg_dist_suffix="fc19"
30 mock_cmd="/usr/bin/mock"
31 vers_controller=""
32 vers_ovsdb=""
33 suff_controller=""
34 suff_ovsdb=""
35 mockdebug=""
36 mockinit=0
37 tmpbuild=""
38 mockmvn=""
39 timesuffix=""
40 shortcircuits=0
41 shortcircuite=0
42 listprojects=0
43
44 # Maven is not installed at the system wide level but at the Jenkins level
45 # We need to map our Maven call to the Jenkins installed version
46 mvn_cmd="$JENKINS_HOME/tools/hudson.tasks.Maven_MavenInstallation/Maven_3.0.4/bin/mvn"
47
48 # Define our push repositories here. We do it in code since we can't
49 # use a POM file for the RPM pushes (we're doing "one-off" file pushes)
50 #
51 # Repositories will be of the form baseRepositoryId-$dist{-testing}
52 # -testing if it's a snapshot build (not using -snapshot as the name as
53 # -testing is the more common testing repo suffix for yum)
54 baseURL="http://nexus.opendaylight.org/content/repositories/opendaylight-yum-"
55 baseRepositoryId="opendaylight-yum-"
56
57 readonly RCSUCCESS=0
58 readonly RCERROR=64
59 readonly RCPARMSERROR=65
60 readonly RCRPMBUILDERROR=66
61 readonly RCRMOCKERROR=67
62 readonly RCGITERROR=68
63
64 readonly LOGERROR=2
65 readonly LOGINFO=5
66 readonly LOGVERBOSE=7
67 loglevel=$LOGVERBOSE
68
69
70 function log {
71     local level=$1; shift;
72
73     if [ $level -le $loglevel ]; then
74         echo "buildrpm: $@"
75     fi
76 }
77
78 function usage {
79     local rc=$1
80     local outstr=$2
81
82     if [ "$outstr" != "" ]; then
83         echo "$outstr"
84         echo
85     fi
86
87     echo "Usage: `basename $0` [OPTION...]"
88     echo
89     echo "Build options:"
90     echo "  --buildtype TYPE       build type, either snapshot or release, default snapshot"
91     echo "  --buildroot DIRECTORY  build root path. The path must exist."
92     echo "  --buildtag             tag the tmpbuild directory, i.e. Jenkins build number"
93     echo "  --cleanroot            clean buildroot directory before building"
94     echo "  --cleantmp             clean tmpbuild directory before building"
95     echo "  --dist DIST            distribution, default fedora-19-x86_64"
96     echo "  --distsuffix SUFFIX    package distribution suffix, default f19"
97     echo "  --getsource METHOD     method for getting source clone|snapshot|buildroot, default buildroot"
98     echo
99     echo "Tag options:"
100     echo "  --releasetag           use the latest release tagged repos"
101     echo "  --specrelease          version used for spec Release:, default 1"
102     echo "  --versiontype TYPE     where to find version, user|pom|spec, default spec"
103     echo "  --version VERSION      version value to use in build output. Required for --version_type user option."
104     echo "  --edversion VERSION    version value to use in build output for edition rpms"
105     echo "  --branch BRANCH        branch to use for source"
106     echo
107     echo "Repo sync options:"
108     echo "  --repourl REPOURL      url of the repo, include http://"
109     echo "  --repouser REPOUSER    user for repo"
110     echo "  --repopw REPOPW        password for repo"
111     echo
112     echo "Deployment options:"
113     echo "  --pushrpms             push the built rpms to a maven repository"
114     echo "  --mvn_cmd              fully qualified path to where the mvn command"
115     echo "                         (defaults to Jenkins installation of Maven 3.0.4)"
116     echo "  --baseURL              base deployment URL. \$dist will be added to the end of this"
117     echo "                         If this is a snapshot build then -testing be added at the end"
118     echo "  --baseRepositoryId     base repository name. \$dist will be added to the end of this"
119     echo "                         If this is a snapshot build then -testing be added at the end"
120     echo
121     echo "Mock options:"
122     echo "  --mockinit             Run mock init"
123     echo "  --mockmvn              replace the maven command used within mock"
124     echo
125     echo "Help options:"
126     echo "  -?, -h, --help  Display this help and exit"
127     echo "  --debug         Enable bash debugging output"
128     echo "  --mockdebug     Enable mock debugging output"
129     echo
130     echo "Extras:"
131     echo "  --shortcircuits INDEX  Start build with project index"
132     echo "  --shortcircuite INDEX  End build with project index"
133     echo "  --listprojects         List the projects and indexes"
134     echo "  --timesuffix TIME      Set the time suffix to use for build output"
135
136     exit $rc
137 }
138
139 readonly PJ_FIRST=0
140 readonly PJ_INTEGRATION=0
141 readonly PJ_CONTROLLER=1
142 readonly PJ_OVSDB=2
143 readonly PJ_OPENFLOWJAVA=3
144 readonly PJ_OPENFLOWPLUGIN=4
145 readonly PJ_DEPENDENCIES=5
146 readonly PJ_OPENDAYLIGHT=6
147 readonly PJ_LISPFLOWMAPPING=7
148 readonly PJ_SNMP4SDN=8
149 readonly PJ_AFFINITY=9
150 readonly PJ_YANGTOOLS=10
151 readonly PJ_BGPCEP=11
152 readonly PJ_OPENDOVE=12
153 #readonly PJ_LAST=$PJ_BGPCEP
154 readonly PJ_LAST=$PJ_OPENDOVE
155
156 projects[$PJ_INTEGRATION]="integration"
157 projects[$PJ_CONTROLLER]="controller"
158 projects[$PJ_OVSDB]="ovsdb"
159 projects[$PJ_OPENFLOWJAVA]="openflowjava"
160 projects[$PJ_OPENFLOWPLUGIN]="openflowplugin"
161 projects[$PJ_DEPENDENCIES]="controller-dependencies"
162 projects[$PJ_OPENDAYLIGHT]="opendaylight"
163 projects[$PJ_LISPFLOWMAPPING]="lispflowmapping"
164 projects[$PJ_SNMP4SDN]="snmp4sdn"
165 projects[$PJ_AFFINITY]="affinity"
166 projects[$PJ_YANGTOOLS]="yangtools"
167 projects[$PJ_BGPCEP]="bgpcep"
168 projects[$PJ_OPENDOVE]="opendove"
169
170 versions[$PJ_INTEGRATION]=""
171 versions[$PJ_CONTROLLER]=""
172 versions[$PJ_OVSDB]=""
173 versions[$PJ_OPENFLOWJAVA]=""
174 versions[$PJ_OPENFLOWPLUGIN]=""
175 versions[$PJ_DEPENDENCIES]=""
176 versions[$PJ_OPENDAYLIGHT]=""
177 versions[$PJ_LISPFLOWMAPPING]=""
178 versions[$PJ_SNMP4SDN]=""
179 versions[$PJ_AFFINITY]=""
180 versions[$PJ_YANGTOOLS]=""
181 versions[$PJ_BGPCEP]=""
182 versions[$PJ_OPENDOVE]=""
183
184 suffix[$PJ_INTEGRATION]=""
185 suffix[$PJ_CONTROLLER]=""
186 suffix[$PJ_OVSDB]=""
187 suffix[$PJ_OPENFLOWJAVA]=""
188 suffix[$PJ_OPENFLOWPLUGIN]=""
189 suffix[$PJ_DEPENDENCIES]=""
190 suffix[$PJ_OPENDAYLIGHT]=""
191 suffix[$PJ_LISPFLOWMAPPING]=""
192 suffix[$PJ_SNMP4SDN]=""
193 suffix[$PJ_AFFINITY]=""
194 suffix[$PJ_YANGTOOLS]=""
195 suffix[$PJ_BGPCEP]=""
196 suffix[$PJ_OPENDOVE]=""
197
198 gitprojects="$PJ_INTEGRATION $PJ_CONTROLLER $PJ_OVSDB $PJ_OPENFLOWJAVA $PJ_OPENFLOWPLUGIN $PJ_LISPFLOWMAPPING $PJ_SNMP4SDN $PJ_AFFINITY $PJ_YANGTOOLS $PJ_BGPCEP $PJ_OPENDOVE"
199
200 # Clone the projects.
201 function clone_source {
202     local depth=$1
203     for i in $gitprojects; do
204         if [ depth -eq 0 ]; then
205             # We only care about a shallow clone (no need to grab the entire project)
206             log $LOGINFO "Shallow cloning ${projects[$i]} to $buildroot/${projects[$i]}"
207             git clone --depth 0 https://git.opendaylight.org/gerrit/p/${projects[$i]}.git $buildroot/${projects[$i]}
208         else
209             log $LOGINFO "Full cloning ${projects[$i]} to $buildroot/${projects[$i]}"
210             git clone https://git.opendaylight.org/gerrit/p/${projects[$i]}.git $buildroot/${projects[$i]}
211         fi
212     done
213 }
214
215 # Copy the projects from snapshots.
216 # shague: Fill in with the nexus info.
217 # Make mk_snapshot_archives that just sets up the version strings.
218 function snapshot_source {
219     log $LOGINFO "$FUNCNAME: Not implemented yet."
220 }
221
222 # Checkout the latest release-tagged branch.
223 # TODO: does this work for branches or for multiple releases?
224 function checkout_release_tag {
225     local tag=""
226
227     for i in $gitprojects; do
228         # Leave integration as is. The release version has a bug. Assume the latest in the
229         # repo is good.
230         if [ "${projects[$i]}" == "${projects[$PJ_INTEGRATION]}" ]; then
231             continue
232         fi
233         cd $buildroot/${projects[$i]}
234         # Find the last release tag for the project.
235         # We assume that the first git log matching "[maven-release-plugin] prepare release"
236         # is where the tag was created.
237         tag=$(git log | grep -m 1 "\[maven-release-plugin\] prepare release" | awk '{print $4}')
238         if [ "$tag" != "" ]; then
239             git checkout --quiet "$tag"
240         else
241             log $LOGERROR "${projects[i]} is missing release tag"
242             exit $RCGITERROR
243         fi
244     done
245 }
246
247 # Checkout the requested branch.
248 function checkout_branch {
249     local branch=$1
250
251     for i in $gitprojects; do
252         cd $buildroot/${projects[$i]}
253         git checkout $branch
254     done
255 }
256
257 function set_versions {
258     local timesuffix=$1
259     local versiontype=$2
260     local version=$3
261     local buildtype=$4
262
263     for i in $gitprojects; do
264         case "$versiontype" in
265         "user")
266             # User set the version.
267             versions[$i]=$version
268             ;;
269
270         "spec")
271             # rpm query the spec file to get the Version.
272             cd $buildroot/${projects[$PJ_INTEGRATION]}/packaging/rpm
273             if [ ${projects[$i]} == ${projects[$PJ_INTEGRATION]} ]; then
274                 # use opendaylight spec since there is no integration spec
275                 versions[$i]="$( rpm -q --queryformat="%{version}\n" --specfile opendaylight.spec | head -n 1 | awk '{print $1}')"
276             else
277                 versions[$i]="$( rpm -q --queryformat="%{version}\n" --specfile opendaylight-${projects[$i]}.spec | head -n 1 | awk '{print $1}')"
278             fi
279             ;;
280
281         "pom")
282             # Find the first version tag in the pom.xml file - this should be the correct version.
283             # Then strip the version tags and end up with the version string.
284             versions[$i]=$(grep -m 1 "<version>" $buildroot/${projects[$i]}/pom.xml | sed -n -e '/<version>/s/.*<version>//p' | sed -n -e 's/<\/version>//p')
285             ;;
286         esac
287
288         if [ "$buildtype" == "snapshot" ]; then
289             cd $buildroot/${projects[$i]}
290             suffix[$i]="snap.$timesuffix.git.$(git log -1 --pretty=format:%h)"
291             versions[$i]="${versions[$i]}.${suffix[$i]}"
292         else
293             suffix[$i]=""
294         fi
295
296         # Remove any -'s from the version. rpmbuild does not like them.
297         version=${versions[$i]}
298         versions[$i]=${version//-/.}
299     done
300
301     # The dependencies and opendaylight projects do not have a repo so set them to the
302     # controller and integration versions.
303
304     # Dependencies follows the controller.
305     suffix[$PJ_DEPENDENCIES]=${suffix[$PJ_CONTROLLER]}
306     versions[$PJ_DEPENDENCIES]=${versions[$PJ_CONTROLLER]}
307
308     # Opendaylight follows integration since they are the editions, unless the
309     # user wants to set the value explicitly.
310     if [ "$edversion" == "" ]; then
311         suffix[$PJ_OPENDAYLIGHT]=${suffix[$PJ_INTEGRATION]}
312         versions[$PJ_OPENDAYLIGHT]=${versions[$PJ_INTEGRATION]}
313     else
314         suffix[$PJ_OPENDAYLIGHT]=""
315         versions[$PJ_OPENDAYLIGHT]=$edversion
316     fi
317 }
318
319 # Archive the projects to create the SOURCES for rpmbuild:
320 # - xz the source for later use by rpmbuild.
321 # - get the version and git hashes to produce a versions and suffix for each project.
322 # - copy the archives to the SOURCES dir
323 # shague: need another archive method for snapshot getsource builds since
324 # the source did not come from a git repo.
325 # versionmajor=0.1.0.snap.20140201.191633.git.7c5788d
326 # versionsnapsuffix=snap.20140201.191633.git.7c5788d
327
328 function mk_git_archives {
329 #    local timesuffix=$1
330
331     for i in $gitprojects; do
332         log $LOGINFO "Building archive: $tmpbuild/opendaylight-${projects[$i]}-${versions[$i]}.tar.xz"
333         cd $buildroot/${projects[$i]}
334         git archive --prefix=opendaylight-${projects[$i]}-${versions[$i]}/ HEAD | \
335             xz > $tmpbuild/opendaylight-${projects[$i]}-${versions[$i]}.tar.xz
336     done
337
338     # Don't forget any patches.
339     cp $buildroot/${projects[$PJ_INTEGRATION]}/packaging/rpm/opendaylight-integration-fix-paths.patch $tmpbuild
340 }
341
342 # Pushes rpms to the specified Nexus repository
343 # This only happens if pushrpms is true
344 function push_rpms {
345     if [ $pushrpms = 1 ]; then
346         allrpms=`find $tmpbuild/repo -iname '*.rpm'`
347         echo
348         log $LOGINFO "RPMS found"
349         for i in $allrpms; do
350             log $LOGINFO $i
351         done
352
353         log $LOGINFO ":::::"
354         log $LOGINFO "::::: pushing RPMs"
355         log $LOGINFO ":::::"
356         for i in $allrpms; do
357             rpmname=`rpm -qp --queryformat="%{name}" $i`
358             rpmversion=`rpm -qp --queryformat="%{version}" $i`
359             distro=`echo $dist | tr - .`
360
361             if [ `echo $i | grep 'src.rpm'` ]; then
362                 rpmrelease=`rpm -qp --queryformat="%{release}.src" $i`
363                 groupId="srpm"
364             else
365                 rpmrelease=`rpm -qp --queryformat="%{release}.%{arch}" $i`
366                 groupId="rpm"
367             fi
368
369
370             if [ "$buildtype" == "snapshot" ]; then
371                 repositoryId="${baseRepositoryId}${dist}-testing"
372                 pushURL="${baseURL}${dist}-testing"
373             else
374                 repositoryId="${baseRepositoryId}${dist}"
375                 pushURL="${baseURL}${dist}"
376             fi
377
378             # Note version is the full version+release+{arch|src}
379             # if it is not configured this way on pushes then a download
380             # of the artifact will result in just the name-version.rpm
381             # instead of name-version-release.{arch|src}.rpm
382             $mvn_cmd org.apache.maven.plugins:maven-deploy-plugin:2.8.1:deploy-file \
383                 -Dfile=$i -DrepositoryId=$repositoryId \
384                 -Durl=$pushURL -DgroupId=$groupId \
385                 -Dversion=$rpmversion-$rpmrelease -DartifactId=$rpmname \
386                 -Dtype=rpm
387         done
388     fi
389 }
390
391 function show_vars {
392      cat << EOF
393 Building controller using:
394 distribution:   $dist
395 suffix:         $pkg_dist_suffix
396 buildtype:      $buildtype
397 versiontype:    $versiontype
398 version:        $version
399 edversion:      $edversion
400 branch:         $branch
401 specrelease:    $specrelease
402 releasetag:     $releasetag
403 getsource:      $getsource
404 buildroot:      $buildroot
405 buildtag:       $buildtag
406 tmpbuild:       $tmpbuild
407 mockmvn:        $mockmvn
408 mockinit:       $mockinit
409 shortcircuits:  $shortcircuits
410 shortcircuite:  $shortcircuite
411 time:           $timesuffix
412 EOF
413 }
414
415 # Build a single project.
416 function build_project {
417     local project="$1"
418     local versionmajor="$2"
419     local versionsnapsuffix="$3"
420     local projectspec=""
421
422     if [ "$project" == "opendaylight" ]; then
423         projectspec="opendaylight"
424     else
425         projectspec="opendaylight-$project"
426     fi
427
428     log $LOGINFO ":::::"
429     log $LOGINFO "::::: building $projectspec.rpm"
430     log $LOGINFO ":::::"
431
432     cp -f $buildroot/${projects[$PJ_INTEGRATION]}/packaging/rpm/$projectspec.spec $tmpbuild
433
434     cd $tmpbuild
435     # Rewrite the Version and Release values in the spec files.
436     sed -r -i -e '/^Version:./c\Version: '"$versionmajor"  $projectspec.spec
437     if [ "$buildtype" == "release" ]; then
438         if [ "$specrelease" != "" ]; then
439             sed -r -i -e '/^Release:./c\Release: '"$specrelease%{?dist}" $projectspec.spec
440         fi
441     fi
442 #    if [ "$buildtype" == "release" ] && [ "$version" != "" ]; then
443 #        # Find lines starting with Version: and replace the whole line with the version requested
444 #        sed -r -i -e '/^Version:./c\Version: '"$versionmajor"  $projectspec.spec
445 #    else
446 #        # Find lines starting with Version: and replace the rest of the line with the versionsnapsuffix
447 #        sed -r -i -e '/^Version:/s/\s*$/'".$versionsnapsuffix/" $projectspec.spec
448 #    fi
449
450     # Set the right values in the spec files.
451     case "$project" in
452     ${projects[$PJ_CONTROLLER]})
453         # Set the version for the integration source.
454         # Find lines with opendaylight-integration-%{version} and replace %{version} with the version.
455         sed -r -i -e '/opendaylight-integration-\%\{version\}/s/\%\{version\}/'"${versions[$PJ_INTEGRATION]}"'/g' \
456             $projectspec.spec
457         ;;
458
459     ${projects[$PJ_DEPENDENCIES]})
460         # Set the version for ovsdb in the dependencies spec.
461         # Don't worry about opendaylight-controller version since the dependencies
462         # version is the same as the controller version.
463         # Find lines with opendaylight-ovsdb-%{version} and replace %{version} with the version.
464         sed -r -i -e '/opendaylight-ovsdb-\%\{version\}/s/\%\{version\}/'"${versions[$PJ_OVSDB]}"'/g' \
465             $projectspec.spec
466         # Find lines with opendaylight-ovsdb-dependencies-%{version} and replace %{version} with the version.
467         sed -r -i -e '/opendaylight-ovsdb-dependencies-\%\{version\}/s/\%\{version\}/'"${versions[$PJ_OVSDB]}"'/g' \
468             $projectspec.spec
469         ;;
470
471     *)
472         ;;
473     esac
474
475     # Rewrite the mvn command in the rpmbuild if the user requests it.
476     if [ "$mockmvn" != "" ]; then
477         # Find lines starting with export MAVEN_OPTS= and replace the whole line with $mockmvn
478         sed -r -i -e '/^export MAVEN_OPTS=./c\ '"$mockmvn" $projectspec.spec
479     fi
480
481     # Build the source RPM for use by mock later.
482     #rm -f SRPMS/*.src.rpm
483     log $LOGINFO "::::: building $projectspec.src.rpm with rpmbuild"
484     rpmbuild -bs --define '%_topdir '"`pwd`" --define '%_sourcedir %{_topdir}' \
485        --define "%dist .$pkg_dist_suffix" $projectspec.spec
486
487     rc=$?
488     if [ $rc != 0 ]; then
489         log $LOGERROR "rpmbuild of $projectspec.src.rpm failed (rc=$rc)."
490         exit $RCRPMBUILDERROR
491     fi
492
493     log $LOGINFO "::::: building $projectspec.rpm with mock"
494
495     if [ "$buildtype" == "release" ]; then
496         resultdir="repo/$projectspec.$pkg_dist_suffix.noarch"
497     else
498         resultdir="repo/$projectspec.$pkg_dist_suffix.noarch.snap"
499     fi
500
501     # Build the rpm using mock.
502     # Keep the build because we will need the distribution zip file for later
503     # when building the controller-dependencies.rpm.
504     eval $mock_cmd $mockdebug -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
505         -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
506         SRPMS/$projectspec-$versionmajor-*.src.rpm
507
508     rc=$?
509     if [ $rc != 0 ]; then
510         log $LOGERROR "mock of $projectspec.rpm failed (rc=$rc)."
511         exit $RCRMOCKERROR
512     fi
513
514     log $LOGINFO "::::: finished building $projectspec.rpm in mock"
515
516     # Copy the distribution zip from the controller and ovsdb projects
517     # for use in the dependencies.rpm.
518     case "$project" in
519     ${projects[$PJ_CONTROLLER]})
520         log $LOGINFO "::::: Copying $projectspec distribution.zip."
521         eval $mock_cmd $mockdebug -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
522             -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
523             --copyout \"builddir/build/BUILD/$projectspec-$versionmajor/opendaylight/distribution/opendaylight/target/distribution.opendaylight-osgipackage.zip\" \"$resultdir/$projectspec-$versionmajor.zip\"
524         rc1=$?
525         ln -sf $resultdir/$projectspec-$versionmajor.zip $tmpbuild
526         rc2=$?
527         if [ ! -e $tmpbuild/$projectspec-$versionmajor.zip ]; then
528             log $LOGERROR "cannot find $projectspec distribution zip file (rc=$rc1:$rc2)."
529             exit $RCERROR
530         fi
531         ;;
532
533     ${projects[$PJ_OVSDB]})
534         log $LOGINFO "::::: Copying $projectspec distribution.zip."
535         # Grab the version from the pom.xml file.
536         # Get the lines between the parent tags, then get the line with the version tag and remove leading space and
537         # the tag itself, then remove the trailing tag to be left with the version.
538         pomversion=`sed -n -e '/<parent>/,/<\/parent>/p' "$buildroot/${projects[$PJ_OVSDB]}/pom.xml" | sed -n -e '/<version>/s/.*<version>//p' | sed -n -e 's/<\/version>//p'`
539         eval $mock_cmd $mockdebug -r $dist --no-clean --no-cleanup-after --resultdir \"$resultdir\" \
540             -D \"dist .$pkg_dist_suffix\" -D \"noclean 1\" \
541             --copyout \"builddir/build/BUILD/$projectspec-$versionmajor/distribution/opendaylight/target/distribution.ovsdb-$pomversion-osgipackage.zip\" \"$resultdir/$projectspec-$versionmajor.zip\"
542         rc1=$?
543         ln -sf $resultdir/$projectspec-$versionmajor.zip $tmpbuild
544         rc2=$?
545         if [ ! -e $tmpbuild/$projectspec-$versionmajor.zip ]; then
546             log $LOGERROR "cannot find $projectspec distribution zip file (rc=$rc1:$rc2)."
547             exit $RCERROR
548         fi
549         ;;
550
551     *)
552         ;;
553     esac
554 }
555
556 # Main function that builds the rpm's for snapshot's.
557 function build_snapshot {
558     set_versions "$timesuffix" "$versiontype" "$version" "$buildtype"
559     mk_git_archives "$timesuffix"
560
561     # Initialize our mock build location (we'll be using --no-clean later)
562     # If we don't do the first init we can't build since the environment
563     # doesn't get setup correctly!
564     if [ $mockinit -eq 1 ]; then
565         eval $mock_cmd $mockdebug -r $dist --init
566     fi
567
568     start=$PJ_CONTROLLER
569     end=$PJ_LAST
570
571     if [ $shortcircuits -ne 0 ]; then
572         start=$shortcircuits
573     fi
574
575     if [ $shortcircuite -ne 0 ]; then
576         end=$shortcircuite
577     fi
578
579     echo "start:end = $start:$end"
580
581     for i in `seq $start $end`; do
582         build_project "${projects[$i]}" "${versions[$i]}" "${suffix[$i]}"
583     done
584
585     log $LOGINFO ":::::"
586     log $LOGINFO "::::: All projects have been built"
587     log $LOGINFO ":::::"
588
589     push_rpms
590 }
591
592 # Main function that builds the rpm's for release's.
593 # shague: should be similar to snapshot but use a different version or tag.
594 # spec files should be updated with correct version. If so, do
595 function build_release {
596     log $LOGINFO "$FUNCNAME: Not implemented yet."
597 }
598
599 function parse_options {
600     while true ; do
601         case "$1" in
602         --debug)
603             set -vx; shift;
604             ;;
605
606         --mockdebug)
607             mockdebug="-v"; shift;
608             ;;
609
610         --buildtype)
611             shift; buildtype="$1"; shift;
612             if [ "$buildtype" != "snapshot" ] && [ "$buildtype" != "release" ]; then
613                 usage $RCPARMSERROR "Invalid build type.";
614             fi
615             ;;
616
617         --buildroot)
618             shift; buildroot="$1"; shift;
619             if [ "$buildroot" == "" ]; then
620                 usage $RCPARMSERROR "Missing build root.";
621             fi
622             if [ ! -d "$buildroot" ]; then
623                 usage $RCPARMSERROR "Invalid build root path."
624             fi
625             ;;
626
627         --buildtag)
628             shift; buildtag="$1"; shift;
629             if [ "$buildtag" == ""  ]; then
630                 usage $RCPARMSERROR "Missing build tag.";
631             fi
632             ;;
633
634         --cleanroot)
635             cleanroot=1; shift;
636             ;;
637
638         --cleantmp)
639             cleantmp=1; shift;
640             ;;
641
642         --getsource)
643             shift; getsource="$1"; shift;
644             if [ "$getsource" != "clone" ] && [ "$getsource" != "snapshot" ] && \
645                [ "$getsource" != "buildroot" ]; then
646                 usage $RCPARMSERROR "Invalid getsource method.";
647             fi
648             ;;
649
650         --dist)
651             shift; dist="$1"; shift;
652             if [ "$dist" == "" ]; then
653                 usage $RCPARMSERROR "Missing distribution.";
654             fi
655             ;;
656
657         --distsuffix)
658             shift; pkg_dist_suffix="$1"; shift;
659             if [ "$pkg_dist_suffix" == "" ]; then
660                 usage $RCPARMSERROR "Missing package distribution suffix.";
661             fi
662             ;;
663
664         --versiontype)
665             shift; versiontype="$1"; shift;
666             if [ "$versiontype" != "user" ] && [ "$versiontype" != "pom" ] && [ "$versiontype" != "spec" ]; then
667                 usage $RCPARMSERROR "Invalid version type.";
668             fi
669             ;;
670
671         --specrelease)
672             shift; specrelease="$1"; shift;
673             if [ "$specrelease" == "" ]; then
674                 usage $RCPARMSERROR "Missing specrelease.";
675             fi
676             ;;
677
678         --releasetag)
679             releasetag=1; shift;
680             ;;
681
682         --version)
683             shift; version="$1"; shift;
684             if [ "$version" == "" ]; then
685                 usage $RCPARMSERROR "Missing version.";
686             fi
687             ;;
688
689         --edversion)
690             shift; edversion="$1"; shift;
691             if [ "$edversion" == "" ]; then
692                 usage $RCPARMSERROR "Missing edversion.";
693             fi
694             ;;
695
696         --branch)
697             shift; branch="$1"; shift;
698             if [ "$branch" == "" ]; then
699                 usage $RCPARMSERROR "Missing branch.";
700             fi
701             depth=1
702             ;;
703
704         --repourl)
705             shift; repourl="$1"; shift;
706             if [ "$repourl" == "" ]; then
707                 usage $RCPARMSERROR "Missing repo url.";
708             fi
709             ;;
710
711         --repouser)
712             shift; repouser="$1"; shift;
713             if [ "$repouser" == "" ]; then
714                 usage $RCPARMSERROR "Missing repo user.";
715             fi
716             ;;
717
718         --repopw)
719             shift; repopw="$1"; shift;
720             if [ "$repopw" == "" ]; then
721                 usage $RCPARMSERROR "Missing repo pw.";
722             fi
723             ;;
724
725         --pushrpms)
726             pushrpms=1; shift;
727             ;;
728
729         --timesuffix)
730             shift; timesuffix="$1"; shift;
731             if [ "$timesuffix" == ""  ]; then
732                 usage $RCPARMSERROR "Missing time suffix.";
733             fi
734             ;;
735
736         --mvn_cmd)
737             shift; mvn_cmd="$1"; shift;
738             if [ "$mvn_cmd" == "" ]; then
739                 usage $RCPARMSERROR "Missing mvn_cmd.";
740             fi
741             ;;
742
743         --mockinit)
744             mockinit=1; shift;
745             ;;
746
747         --mockmvn)
748             shift; mockmvn="$1"; shift;
749             if [ "$mockmvn" == "" ]; then
750                 usage $RCPARMSERROR "Missing mockmvn.";
751             fi
752             ;;
753
754         --baseURL)
755             shift; baseURL="$1"; shift;
756             if [ "$baseURL" == "" ]; then
757                 usage $RCPARMSERROR "Missing baseURL.";
758             fi
759             ;;
760
761         --baseRepositoryId)
762             shift; baseRepositoryId="$1"; shift;
763             if [ "$baseRepositoryId" == "" ]; then
764                 usage $RCPARMSERROR "Missing baseRepositoryId.";
765             fi
766             ;;
767
768         --shortcircuits)
769             shift; shortcircuits=$1; shift;
770             ;;
771
772         --shortcircuite)
773             shift; shortcircuite=$1; shift;
774             ;;
775
776         --listprojects)
777             listprojects=1; shift;
778             ;;
779
780         -? | -h | --help)
781             usage $RCSUCCESS
782             ;;
783         "")
784             break
785             ;;
786         *)
787             log $LOGINFO "Ignoring unknown option: $1"; shift;
788         esac
789     done
790 }
791
792
793 #################### main ####################
794
795 if [ "$timesuffix" == "" ]; then
796     timesuffix="$(date +%F_%T | tr -d .:- | tr _ .)"
797 fi
798 date_start=$(date +%s)
799
800 parse_options "$@"
801
802 if [ $listprojects -eq 1 ]; then
803     for i in `seq $PJ_FIRST $PJ_LAST`; do
804         echo "$i ${projects[$i]}"
805     done
806     exit $RCSUCCESS
807 fi
808
809 # Some more error checking...
810 if [ -z $buildroot ]; then
811     log $LOGERROR "Mising buildroot"
812     exit $RCPARMSERROR
813 fi
814
815 if [ $cleanroot -eq 1 ] && [ $getsource = "buildroot" ]; then
816     log $LOGERROR "Aborting. You probably do not want to clean the directory" \
817          "containing the source."
818     exit $RCPARMSERROR
819 fi
820
821 if [ "$versiontype" != "user" ] && [ "$version" == "" ]; then
822     log $LOGERROR "version type user requires a version"
823 fi
824
825 # Can change tmpbuild to be an index or other tag
826 if [ -n "$buildtag" ]; then
827     tmpbuild="$buildroot/bld_$buildtag"
828 else
829     tmpbuild="$buildroot/bld"
830 fi
831
832 show_vars
833
834 if [ $cleanroot -eq 1 ]; then
835     rm -rf $buildroot
836     mkdir -p $buildroot
837 fi
838
839 if [ $cleantmp -eq 1 ]; then
840     rm -rf $tmpbuild
841 fi
842
843 # Setup the temp build directory.
844 mkdir -p $tmpbuild/repo
845
846 # Get the source.
847 case "$getsource" in
848 clone)
849     clone_source "$depth";
850
851     if [ -n "$branch" ]; then
852         checkout_branch $branch
853     fi
854
855     if [ $releasetag -eq 1 ]; then
856         checkout_release_tag
857     fi
858     ;;
859
860 snapshot)
861     snapshot_source;
862     exit $RCSUCCESS
863     ;;
864
865 buildroot)
866     cd $buildroot
867
868     # Check if all the projects dirs are really there.
869     for i in $gitprojects; do
870         if [ ! -d ${projects[$i]} ]; then
871             log $LOGERROR "Missing ${projects[$i]}"
872             exit $RCPARMSERROR
873         fi
874     done
875
876     if [ -n "$branch" ]; then
877         checkout_branch $branch
878     fi
879
880     if [ $releasetag -eq 1 ]; then
881         checkout_release_tag
882     fi
883
884     ;;
885 esac
886 #exit $RCSUCCESS
887 if [ "$buildtype" == "snapshot" ]; then
888     log $LOGINFO "Building a snapshot build"
889     build_snapshot
890 else
891     log $LOGINFO "Building a release build"
892     build_snapshot
893 fi
894
895 date_end=$(date +%s)
896 diff=$(($date_end - $date_start))
897 log $LOGINFO "Build took $(($diff / 3600 % 24)) hours $(($diff / 60 % 60)) minutes and $(($diff % 60)) seconds."
898
899 exit $RCSUCCESS