summaryrefslogtreecommitdiff
path: root/bin/build-rpm-package
blob: 0247b92b06279a2e1b53aca1a2f6abe63f5abccf (plain)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/bin/bash

# Copyright (C) 2011-2013 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
# This programme is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This programme is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

export PATH=~/bin:$PATH

GIT_USER="gituser"
GIT_HOSTNAME="git.mydomain.org"

RPMEMAIL="firstname.lastname@mydomain.org"
RPMFULLNAME="Firstname Lastname"
GPG_KEY=
RPM_DISTS_SUPPORTED="fedora epel opensuse"
FEDORA_DISTROS="18,19,20,rawhide"
EPEL_DISTROS="6,7"
RPM_REPOS_BASE=/var/www/

COMPONENT_MAIN="main"
COMPONENT_NIGHTLY="nightly"
COMPONENT_BUNDLES="bundle-release1 bundle-release2"
REPOS_SERVER="packages.mydomain.org"
PACKAGES_WITHOUT_OTHERMIRROR="keyring"
GNUPGHOME=$HOME/.gnupg

OPENSUSE_DOWNLOAD_URL="http://download.opensuse.org/distribution/#VERSION#/repo/oss/suse/"

test -z $1 && { echo "usage: $(basename $0) [<subpath>/]<git-project> {main,main/<codename>,nightly,nightly/<codename>} [<git-checkout>]"; exit -1; }

PREFIX=$(echo `basename $0` | cut -d"-" -f1)
test -f ~/.buildscripts/$PREFIX.conf && . ~/.buildscripts/$PREFIX.conf || { echo "$0 has no valid context prefix..."; exit -1; }

FORCE_BUILD=${FORCE_BUILD:-"no"}
RPM_BUILD_FOR=${RPM_BUILD_FOR:-"fedora:$FEDORA_DISTROS epel:$EPEL_DISTROS"}

set -ex

set_vars() {
	TEMP_BASE="$HOME/tmp/"
	mkdir -p "$TEMP_BASE"
	chmod 2770 "$TEMP_BASE"

	# first argv is the name of the Git project
	PROJECT_PATH="$1"
	PROJECT_PATH=${PROJECT_PATH/%.git/}
	PROJECT="$(basename $PROJECT_PATH)"

	# grab repository component area from command line (2nd argv) or guess it
	ARGV2_COMPONENT="$(echo "$2/" | cut -d"/" -f1)"
	ARGV2_CODENAME="$(echo "$2/" | cut -d"/" -f2)"
	COMPONENT="${ARGV2_COMPONENT:-${COMPONENT:-$COMPONENT_NIGHTLY}}"
	CODENAMES="${ARGV2_CODENAME:-${CODENAMES}}"
	[ -n "$ARGV2_CODENAME" ] && FORCE_BUILD="yes" || true
	DATE="${DATE:-$(date +%Y%m%d)}"
	if [ "x$COMPONENT" = "x$COMPONENT_MAIN" ]; then
		CHECKOUT="${3:-build-main}"
	elif echo "$COMPONENT_RELEASES" | grep "$COMPONENT" >/dev/null; then
		CHECKOUT="${3:-build-$COMPONENT}"
	elif [ "x$COMPONENT" = "x$COMPONENT_NIGHTLY" ]; then
		CHECKOUT="${3:-master}"
	else
		echo "error: no such package component area for this Git project. Aborting..."
		exit -1
	fi
	# the DATE might be given as ,,today'' from the command line
	[ "x$DATE" = "xtoday" ] && DATE="$(date +%Y%m%d)"

	# setting paths
	PROJECT_DIR="$HOME/build/$COMPONENT/$PROJECT"
	PKGDIST="$HOME/pkg-dist/$COMPONENT/$PROJECT"

	# lock file
	LOCK_FILE=$PROJECT_DIR/../.$PROJECT.lock

	# creating paths
	mkdir -p "$TEMP_BASE"
	mkdir -p "$PROJECT_DIR"
	mkdir -p "$PKGDIST"

	return 0
}

prepare_workspace() {
	# make sure our local working copy is up to date...
	if [ -d "$PROJECT_DIR/.git" ]; then 
		cd "$PROJECT_DIR" && git reset --hard
		git checkout --force $CHECKOUT || git checkout --force -b $CHECKOUT
		git pull origin $CHECKOUT
		git fetch origin upstream:upstream || true
		git fetch origin pristine-tar:pristine-tar || true
		# and again, get the $CHECKOUT refspec in pure state
		git reset --hard
		git clean -df
	else
		cd "$(dirname $PROJECT_DIR)"
		git clone git://$GIT_HOSTNAME/$PROJECT_PATH.git
		cd "$PROJECT"
		git checkout --force $CHECKOUT || git checkout --force -b $CHECKOUT;
		git fetch origin upstream:upstream
		git fetch origin pristine-tar:pristine-tar || true
		git clean -df
	fi

	GIT_OBJECT_ID=`git show-ref -s heads/master`
	cd "$PROJECT_DIR"

	if [ "x$ARGV2_CODENAME" != "x" ]; then
		if echo "$FEDORA_DISTROS" | grep $ARGV2_CODENAME >/dev/null; then
			RPM_BUILD_FOR="fedora:$ARGV2_CODENAME"
		elif echo "$EPEL_DISTROS" | grep $ARGV2_CODENAME >/dev/null; then
			RPM_BUILD_FOR="epel:$ARGV2_CODENAME"
		fi
	fi
	return 0
}

clear_pkgdist() {
	# pkgdist directory cleanup
	echo "$RPM_BUILD_FOR" | sed -e 's/ /\n/g' | while read line; do
		l_DIST="$(echo ${line/: /:} | cut -d":" -f1 | tr [:upper:] [:lower:])"
		l_CODENAMES="${CODENAMES:-$(echo ${line/: /:} | cut -d":" -f2- | sed -e 's/,/ /g' | tr [:upper:] [:lower:])}"
		echo "$RPM_DISTS_SUPPORTED" | grep $l_DIST >/dev/null && {
			for l_CODENAME in $l_CODENAMES; do

				test -z $CODENAMES || echo $line | grep $CODENAMES || break

				for l_ARCH in x86_64 i386; do
					if [ "x$SKIP_ARCH" != "x$l_ARCH" ]; then
						mkdir -p "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH"
						rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild/SOURCES/"*
						rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild/SRPMS/$PROJECT-"*.src.rpm
						rm -f "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/"*.rpm
					fi
				done
			done
		}
	done
	return 0
}

build_packages() {
	echo "$RPM_BUILD_FOR" | sed -e 's/ /\n/g' | while read line; do
		l_DIST="$(echo ${line/: /:} | cut -d":" -f1 | tr [:upper:] [:lower:])"
		l_CODENAMES="${CODENAMES:-$(echo ${line/: /:} | cut -d":" -f2- | sed -e 's/,/ /g' | tr [:upper:] [:lower:])}"
		echo "$RPM_DISTS_SUPPORTED" | grep $l_DIST >/dev/null && {
			for l_CODENAME in $l_CODENAMES; do

				test -z $CODENAMES || echo $line | grep $CODENAMES || break

				# create rpmbuild subdirectories
				mkdir -p $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild/SOURCES

				# tar upstream sources from our Git clone
				TEMP_DIR="$(mktemp -d --tmpdir=$TEMP_BASE)"
				mkdir -p "$TEMP_DIR/$PROJECT"
				chmod 2770 "$TEMP_DIR" -Rf

				cd "$PROJECT_DIR"
				git clone "$PROJECT_DIR" "$TEMP_DIR/$PROJECT/"
				cd $TEMP_DIR
				GITREV=$(cd $PROJECT && gitrevno && cd - 1>/dev/null)

				# create git changelog immediately prior to building the SRPM package
				cd $PROJECT && git --no-pager log --since "2 years ago" --format="%ai %aN (%h) %n%n%x09*%w(68,0,10) %s%d%n" > ChangeLog.gitlog && cd - 1>/dev/null

				rm -Rf "$PROJECT/.git"
				mv $PROJECT/$PROJECT.spec .

				UPSTREAM_VERSION=$(cat $PROJECT.spec | egrep "^Version:.*" | awk '{ print $2 }')
				PKG_RELEASE=$(cat $PROJECT.spec | egrep "^Release:.*" | awk '{ print $2 }')
				PKG_SRCRELEASE=$(echo $PKG_RELEASE | sed -e 's/%{?dist}//')

				IS_NOARCH=$(cat $PROJECT.spec | egrep "^BuildArch:.*noarch\$" 1>/dev/null 2>/dev/null && echo yes || echo no)

				if [ "$COMPONENT" = "$COMPONENT_NIGHTLY" ]; then
					IS_RELEASE="0"
				else
					IS_RELEASE="1"
				fi
				sed -i $PROJECT.spec -e "s/%{?dist}/.$IS_RELEASE.git$DATE.$GITREV.$COMPONENT%{?dist}/"

				# apply patches from debian/patches/* so that they end up in the tarbal
				# ... esp. relevant for NX (redistributed)
				if [ -f $PROJECT/debian/patches/series ]; then
					( cd $PROJECT && QUILT_PATCHES=debian/patches quilt push -a && rm .pc/ -Rf; )
				fi

				cat $PROJECT.spec | egrep "^Source[1-9]+:.*" | awk '{ print $2 }' | while read source_file; do
					find $PROJECT/rpm/$source_file -maxdepth 0 1> /dev/null && cp $PROJECT/rpm/$source_file $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild/SOURCES/ && continue
					find $PROJECT/$source_file -maxdepth 0 1> /dev/null && cp $PROJECT/$source_file $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild/SOURCES/
				done

				mv $TEMP_DIR/$PROJECT $TEMP_DIR/$PROJECT-$UPSTREAM_VERSION
				tar -czf $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild/SOURCES/$PROJECT-$UPSTREAM_VERSION.tar.gz $PROJECT-$UPSTREAM_VERSION

				# create the SRPM package
				rpmbuild -D "%_topdir $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild" -bs $PROJECT.spec

				# clean up the Git clone from the temp folder
				cd && rm $TEMP_DIR/$PROJECT -Rf

				# modify changelog for this build
				### TODO: add changelog entry for this automatic build

				mkdir -p $PKGDIST/$l_DIST/$l_CODENAME/{x86_64,i386}

				# TODO: obtain packages from our YUM repository???
				#OTHERMIRROR=""
				#if [ "x$COMPONENT" = "x$COMPONENT_NIGHTLY" ]; then
				#	echo $PACKAGE_WITHOUT_OTHERMIRROR | grep $PROJECT >/dev/null || OTHERMIRROR="deb http://$REPOS_SERVER/$l_DIST $l_CODENAME $COMPONENT_MAIN $COMPONENT"
				#else
				#	echo $PACKAGE_WITHOUT_OTHERMIRROR | grep $PROJECT >/dev/null || OTHERMIRROR="deb http://$REPOS_SERVER/$l_DIST $l_CODENAME $COMPONENT"
				#fi

				if [ "x$SKIP_ARCH" != "xx86_64" ] || [ "$IS_NOARCH" = "yes" ]; then
					rm -f $PKGDIST/$l_DIST/$l_CODENAME/x86_64/build.log
					if [ "x$PKGDIST" = "xopensuse"]; then
						mkdir -p "/var/cache/obs-build/$PKGDIST/$l_DIST/$l_CODENAME/x86_64/"
						OPENSUSE_DOWNLOAD_URL=$(echo "$OPENSUSE_DOWNLOAD_URL" | sed 's/#VERSION#/$l_DIST/')
						if obs-build --repo "$OPENSUSE_DOWNLOAD_URL" \
						             --root "/var/cache/obs-build/$PKGDIST/$l_DIST/$l_CODENAME/x86_64/" \
						             $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild/SRPMS/$PROJECT-$UPSTREAM_VERSION-$PKG_SRCRELEASE.$IS_RELEASE.git$DATE.$GITREV.$COMPONENT.src.rpm; then
							rpmsign-unattended -D "%_gpg_name debian@x2go.org" --addsign $PKGDIST/$l_DIST/$l_CODENAME/x86_64/*.rpm
						else
							rm -Rf "$TEMP_DIR"
							exit -1
						fi
					else
						while [ -d ~mock/${l_DIST}-${l_CODENAME}-x86_64 ]; do
							echo "Waiting for some other build to finish..."
							sleep 30
						done
						if mock -r ${l_DIST}-${l_CODENAME}-x86_64 --result $PKGDIST/$l_DIST/$l_CODENAME/x86_64 $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild/SRPMS/$PROJECT-$UPSTREAM_VERSION-$PKG_SRCRELEASE.$IS_RELEASE.git$DATE.$GITREV.$COMPONENT.src.rpm; then
							rpmsign-unattended -D "%_gpg_name debian@x2go.org" --addsign $PKGDIST/$l_DIST/$l_CODENAME/x86_64/*.rpm
							cat $PKGDIST/$l_DIST/$l_CODENAME/x86_64/build.log
						else
							cat $PKGDIST/$l_DIST/$l_CODENAME/x86_64/build.log
							rm -Rf "$TEMP_DIR"
							exit -1
						fi
					fi
				fi
				if [ "x$SKIP_ARCH" != "xi386" ] && [ "$IS_NOARCH" != "yes" ]; then
					rm -f $PKGDIST/$l_DIST/$l_CODENAME/i386/build.log
					while [ -d ~mock/${l_DIST}-${l_CODENAME}-i386 ]; do
						echo "Waiting for some other build to finish..."
						sleep 30
					done
					if nice mock -r ${l_DIST}-${l_CODENAME}-i386  --result $PKGDIST/$l_DIST/$l_CODENAME/i386 $PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH/rpmbuild/SRPMS/$PROJECT-$UPSTREAM_VERSION-$PKG_SRCRELEASE.$IS_RELEASE.git$DATE.$GITREV.$COMPONENT.src.rpm; then
						rpmsign-unattended -D "%_gpg_name debian@x2go.org" --addsign $PKGDIST/$l_DIST/$l_CODENAME/i386/*.rpm
						cat $PKGDIST/$l_DIST/$l_CODENAME/i386/build.log
					else
						cat $PKGDIST/$l_DIST/$l_CODENAME/i386/build.log
						rm -Rf "$TEMP_DIR"
						exit -1
					fi
				fi
			done
		}
	done
	rm -Rf "$TEMP_DIR"
	return 0
}

upload_packages() {
	# dupload the new packages to the reprepro repository

	echo "$RPM_BUILD_FOR" | sed -e 's/ /\n/g' | while read line; do
		l_DIST="$(echo ${line/: /:} | cut -d":" -f1 | tr [:upper:] [:lower:])"
		l_CODENAMES="${CODENAMES:-$(echo ${line/: /:} | cut -d":" -f2- | sed -e 's/,/ /g' | tr [:upper:] [:lower:])}"
		for l_CODENAME in $l_CODENAMES; do

			test -z $CODENAMES || echo $line | grep $CODENAMES || break

			for l_ARCH in x86_64 i386; do
				if [ "x$SKIP_ARCH" != "x$l_ARCH" ]; then

					# create remote directories in archive
					0</dev/null ssh $REPOS_SERVER mkdir -p $RPM_REPOS_BASE/$l_DIST/$l_CODENAME/$COMPONENT/$l_ARCH/rpms/$PROJECT
					0</dev/null ssh $REPOS_SERVER mkdir -p $RPM_REPOS_BASE/$l_DIST/$l_CODENAME/$COMPONENT/$l_ARCH/repodata

					# remove rpm packages of the same name (pattern)
					0</dev/null ssh $REPOS_SERVER rm -f $RPM_REPOS_BASE/$l_DIST/$l_CODENAME/$COMPONENT/$l_ARCH/rpms/$PROJECT/*.rpm

					# copy rpms into repo
					cd "$PKGDIST/$l_DIST/$l_CODENAME/$l_ARCH"
					scp *.rpm $REPOS_SERVER:$RPM_REPOS_BASE/$l_DIST/$l_CODENAME/$COMPONENT/$l_ARCH/rpms/$PROJECT/ || true

					0</dev/null ssh $REPOS_SERVER "cd $RPM_REPOS_BASE/$l_DIST/$l_CODENAME/$COMPONENT/$l_ARCH/ && createrepo ."

				fi
			done
		done
	done
	return 0
}

wait_for_lock() {
	while [ -f $LOCK_FILE ]; do
		pid=$(head -n1 $LOCK_FILE)
		if ! ps $pid 1>/dev/null; then rm -f $LOCK_FILE
		else
			echo "PROJECT directory is locked, sleeping for 10 seconds..."
			sleep 10
		fi
	done
}

lock_workspace() {
	wait_for_lock
	echo $$ > $LOCK_FILE
}

unlock_workspace() {
	rm -f $LOCK_FILE
}

delay_build() {
	sleep $[ ( ( $RANDOM % 10 )  + 1 ) * 10 ]s
}


### MAIN ###
set_vars $@ && {
	if [ "x$(basename $0)" = "x$PREFIX-build-rpm-package" ] || [ "x$(basename $0)" = "x$PREFIX-build+upload-rpm-package" ]; then
		cd $PROJECT_DIR && pkgneedsbuild $CHECKOUT || [ "$FORCE_BUILD" = "yes" ] && {
			if [ "x$FORCE_BUILD" = "xyes" ]; then
				delay_build
			fi
			lock_workspace
			prepare_workspace && {
				unlock_workspace
				clear_pkgdist
				build_packages
			}
			unlock_workspace
		}
	fi
	if [ "x$(basename $0)" = "x$PREFIX-upload-rpm-package" ] || [ "x$(basename $0)" = "x$PREFIX-build+upload-rpm-package" ]; then
		upload_packages
	fi
}