forked from griddynamics/openstack-rhel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_helpers
252 lines (201 loc) · 8.56 KB
/
build_helpers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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"
}