Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
253 lines (201 sloc) 8.56 KB
debug_info() {
echo "-============= Current variable state: ==================-"
echo "prj: $prj"
echo "GithubUserProject: $GithubUserProject"
echo "GithubSource: $GithubSource"
echo "TarballsHome: $TarballsHome"
echo "OurSpec: $OurSpec"
echo "CurTarballVer: $CurTarballVer"
echo "ActualTarBall: $ActualTarBall"
echo "ActualVersion: $ActualVersion"
echo "ActualRelease: $ActualRelease"
echo "SpecVersion: $SpecVersion"
echo "SpecRelease: $SpecRelease"
echo "DevMileStone: $DevMileStone"
echo "GitCurBranch: $GitCurBranch"
echo "RpmTree: $RpmTree"
echo "RepoPath: $RepoPath"
echo "LocalSourcePath: $LocalSourcePath"
echo "JobName: $JobName"
}
check_tarball_update() {
[ $Debug ] && echo "Checking for updates..."
[ $Debug ] && echo "Version and Release from tarball URL = $ActualVersion - $ActualRelease"
[ $Debug ] && echo "Version and Release from SPEC file = $SpecVersion - $SpecRelease"
if [[ "$ActualRelease" != "$SpecRelease" ]]; then
[ $Debug ] && echo "TarBall update!"
FireNewBuild=1
fi
}
check_spec_update() {
SpecGitRev="$(curl -s http://github.com/api/v2/json/commits/list/$GithubUserProject/$GitCurBranch/$OurSpec | perl -MJSON::XS -e "\$a='';while(<>){\$a.=\$_} \$d=decode_json(\$a);print \$d->{'commits'}[0]->{'id'}")"
SpecOurRev="$(git log --pretty=format:"%H" -1 $OurSpec)"
[ $Debug ] && echo "SPEC Github Revision = $SpecGitRev"
[ $Debug ] && echo "SPEC Local Revision = $SpecOurRev"
if [[ "$SpecGitRev" != "$SpecOurRev" ]]; then
# Git SPEC revision was updated in upstream repository, need to build it
[ $Debug ] && echo "SPEC github update!"
FireNewBuild=1
fi
}
check_git_update() {
# NOT TESTED YET
SourceGitRev="$(curl -s http://github.com/api/v2/json/commits/list/$GithubSource/$GitCurBranch | perl -MJSON::XS -e "\$a='';while(<>){\$a.=\$_} \$d=decode_json(\$a);print \$d->{'commits'}[0]->{'id'}")"
[ ! -f "$LocalSourcePath/$prj-$ActualVersion" ] && SourceOurRev="$(git --git-dir="$LocalSourcePath/$prj-$ActualVersion/.git" log --pretty=format:"%H" -1)"
[ $Debug ] && echo "Github SOURCE Revision = $SourceGitRev"
[ $Debug ] && echo "Local SOURCE Revision = $SourceOurRev"
if [[ "$SourceGitRev" != "$SourceOurRev" ]]; then
# Git SOURCECODE revision was updated in upstream repository, need to build it
[ $Debug ] && echo "SOURCE github update!"
FireNewBuild=1
fi
}
check_for_update() {
check_tarball_update
check_spec_update
[[ $FireNewBuild == "1" ]] && [ $Debug ] && echo "There is UPDATE"
}
check_enviroment() {
[ $Debug ] && echo "Checking enviroment"
#Check if we are in git repo
if [ ! -d ".git" ]; then
echo "Need to run from Git repo!"
exit -1
fi
#Check if job is running or fail
if [[ "$dirname" =~ /jobs/.*/workspace$ ]]; then
# Query job status
JobInQueue="$(curl -s http://$jenkins/job/$JobName/api/json | perl -MJSON::XS -e "\$a='';while(<>){\$a.=\$_} \$d=decode_json(\$a);print \$d->{'inQueue'}")"
if [[ "$JobInQueue" == 1 ]]; then
[ $Debug ] && echo "Job already in queue. Giving up"
exit 0
fi
# Checkin for fail of last build
LastBuildFailed=$(curl -s http://$jenkins/job/$JobName/api/json | perl -MJSON::XS -e "\$a='';while(<>){\$a.=\$_} \$d=decode_json(\$a);print \$d->{'lastBuild'}{'number'} == \$d->{'lastUnsuccessfulBuild'}{'number'} ? 1 : 0")
if [[ "$LastBuildFailed" == 1 ]]; then
[ $Debug ] && echo "Last build fail. User intervention required"
exit 0
fi
else
echo "This script is designed to run from within Jenkins workspace"
exit -1
fi
}
update_spec() {
## Update SPEC file and get tarball
[ $Debug ] && echo "Updating SPEC..."
perl -pi -e "s,^Source0:.*$,Source0: $TarballsHome/$ActualTarBall ," $OurSpec
perl -pi -e 's,^(Version:).*$,${1}\t'$ActualVersion',' $OurSpec
perl -pi -e 's,^(Release:).*%{\?dist}$,${1}\t'$ActualRelease'%{\?dist},' $OurSpec
[ $Debug ] && echo "Updating SPEC source to $TarballsHome/$ActualTarBall"
[ $Debug ] && echo "Updating SPEC Version to $ActualVersion"
[ $Debug ] && echo "Updating SPEC Release to $ActualRelease"
# rpmdev-bumpspec --comment="- Update to $ActualRelease" $OurSpec
}
create_srpm() {
# Remove RPM of this build if exist
rm -f "$RepoPath/RPMS/$GitCurBranch/*$ActualRelease*.src.rpm"
[ $Debug ] && echo "Building SRPMs.."
rpmbuild -bs $OurSpec
if [ "$?" != "0" ]; then
[ $Debug ] && echo "Build SRPM fail."
exit -1
fi
}
create_rpm() {
BuildLog=`mktemp`
[ $Debug ] && echo "Building RPMs..Logging to $BuildLog"
rpmbuild -bb $OurSpec > $BuildLog 2>&1
if [ "$?" != "0" ]; then
[ $Debug ] && echo "Build RPM fail. See logs in $BuildLog"
debug_info
exit -1
else
[ $Debug ] && echo "Build success. Removing logs"
DONERPM="$(cat $BuildLog |grep 'Wrote:' | sed 's/Wrote://')"
[ $Debug ] && echo "RPM Done: $DONERPM"
rm -f $BuildLog
fi
}
commit_spec() {
[ $Debug ] && echo "Commiting update to $ActualRelease"
git add "$OurSpec"
git commit -m "Update to $ActualRelease"
if [ "$?" != "1" ]; then
git push
fi
if [ "$?" != "0" ]; then
check_changes
fi
}
get_tarball() {
[ $Debug ] && echo "Retriving $TarballsHome/$ActualTarBall"
if [ ! -f "$RpmTree/SOURCES/$ActualTarBall" ]; then
wget -O "$RpmTree/SOURCES/$ActualTarBall" "$TarballsHome/$ActualTarBall" || ( echo "Retriving $TarballsHome/$ActualTarBall fail" ; exit 0 )
fi
}
sign_rpm() {
[ $Debug ] && echo "Signing RPMs.."
for FILE in $DONERPM; do
[ $Debug ] && echo "Signing FILE"
./sign_rpm $FILE
done
}
undo_changes() {
[ $Debug ] && echo "Undo Changes"
git checkout -- "$OurSpec";
}
do_mock() {
[ $Debug ] && echo "Starting MOCK"
rm -f /tmp/openstack-nova-*-0.$ActualRelease.*.src.rpm
cp $RepoPath/SRPMS/openstack-nova-*-0.$ActualRelease.*.src.rpm /tmp/
sudo mock /tmp/openstack-nova-*-0.$ActualRelease.*.src.rpm
}
check_changes() {
[ $Debug ] && echo "Checking for commits while building.."
# Somebody pushed a commit to origin since we last time pulled.
# Need to check - if that commit was to our file or to some other file?
GitHubUrl="http://github.com/api/v2/json/commits/list/$GithubUserProject/$GitCurBranch"
LastRepoCommit="$(curl -s $GitHubUrl | perl -MJSON::XS -e "\$a='';while(<>){\$a.=\$_} \$d=decode_json(\$a);print \$d->{'commits'}[0]->{'id'}")"
LastSpecCommit="$(curl -s $GitHubUrl/$OLDSPEC | perl -MJSON::XS -e "\$a='';while(<>){\$a.=\$_} \$d=decode_json(\$a);print \$d->{'commits'}[0]->{'id'}")"
if [[ "$LastRepoCommit" != "$LastSpecCommit" ]]; then
# Last Git repo commit was not to our specfile
# Probably we can safely do git pull to merge following by git push
git pull
if [ "$?" != "0" ]; then
echo "Sorry, automatic merge failed"
echo "Human intervention required, giving up here"
exit -1
fi
git push
else
# Last commit was to our specfile
git pull
echo "There should be a conflict above, please fix by hands and commit"
exit -1
fi
}
update_repo() {
if [ ! -d "$RepoPath" ]; then
mkdir -p "$RepoPath"
fi
[ $Debug ] && echo "Moving RPM to repo.."
#rm -fr $RepoPath/python-nova-$SpecVersion-$SpecRelease.*.rpm $RepoPath/openstack-nova-$SpecVersion-$SpecRelease.*.rpm $RepoPath/openstack-nova-{api,compute,doc,instancemonitor,network,noVNC,objectstore,scheduler,volume}-$SpecVersion-$SpecRelease.*.rpm $RepoPath/openstack-nova-node-*-$SpecVersion-$SpecRelease.*.rpm
for FILE in $DONERPM; do
RMFILE="$(echo "$FILE" | sed -e 's!.*/!!' | sed 's/'$ActualRelease'/'$SpecRelease'/' )"
[ $Debug ] && echo "Removing $RepoPath/$RMFILE.."
[ -f $RepoPath/$RMFILE ] && rm -f $RepoPath/$RMFILE
mv $FILE "$RepoPath";
[ $Debug ] && echo "Moving $FILE to repo.."
done;
}
get_git() {
[ $Debug ] && echo "Getting repo http://github.com/$GithubSource"
[ ! -f "$LocalSourcePath/$prj-$ActualVersion" ] && rm -rf "$LocalSourcePath/$prj-$ActualVersion"
mkdir -p "$LocalSourcePath/$prj-$ActualVersion" ||exit -1
git clone "https://github.com/$GithubSource" "$LocalSourcePath/$prj-$ActualVersion"
cd $LocalSourcePath
tar -czf "$RpmTree/SOURCES/$prj-$ActualVersion.tar.gz" "$prj-$ActualVersion"
cd $dirname
[ $Debug ] && echo "Writing TarBall $RpmTree/SOURCES/$prj-$ActualVersion.tar.gz"
}