-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·667 lines (584 loc) · 18.6 KB
/
build.sh
File metadata and controls
executable file
·667 lines (584 loc) · 18.6 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#! /bin/bash
# build.sh - a script to build Fedora packages localy using mock and upload them to COPR
#
# Copyright (C) 2015 Heiko Adams heiko.adams@gmail.com
#
# This source 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 2 of the License, or (at your option)
# any later version.
#
# This code 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.
#
# A copy of the GNU General Public License is available on the World Wide Web
# at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
# to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.
#
function initConfig {
if [ ! -d "$HOME/.config/minibuild/" ]; then
mkdir -p "$HOME"/.config/minibuild/
notificationSend "config directory created! Please check the minibuild-configuration!"
exit 1
else
if [ ! -e "$HOME/.config/minibuild/chroots.conf" ]; then
touch "$HOME"/.config/minibuild/chroots.conf
notificationSend "chroots.conf created!"
exit 1
fi
if [ ! -e "$HOME/.config/minibuild/coprs.conf" ]; then
touch "$HOME"/.config/minibuild/coprs.conf
notificationSend "coprs.conf created!"
exit 1
fi
if [ ! -e "$HOME/.config/minibuild/ftp.conf" ]; then
touch "$HOME"/.config/minibuild/ftp.conf
notificationSend "ftp.conf created!"
exit 1
fi
fi
}
function prepareBuild {
local WDIR
WDIR=$(pwd)
if [ "$WDIR" != "$HOME/rpmbuild" ]; then
cd "$HOME/rpmbuild" || exit
WORKDIR=$(pwd)
readonly WORKDIR
fi
}
function debugMsg {
echo
echo """$1"""
}
function notificationSend {
local MSG=$1
local TITLE=$2
if [ -z "$TITLE" ]; then
TITLE="MiniBuild"
fi
$NOTIFY """$TITLE""" """$MSG"""
debugMsg "$MSG"
}
function initVars {
# benötigte Variable befüllen
# benötigte Programme suchen
RPMBUILD=$(command -v rpmbuild)
readonly RPMBUILD
RPMLINT=$(command -v rpmlint)
readonly RPMLINT
RPMDEPLINT=$(command -v rpmdeplint)
readonly RPMDEPLINT
NOTIFY=$(command -v notify-send)
readonly NOTIFY
WGET=$(command -v wget)
readonly WGET
MOCK=$(command -v mock)
readonly MOCK
CURL=$(command -v curl)
readonly CURL
CLI=$(command -v copr-cli)
readonly CLI
LFTP=$(command -v lftp)
readonly LFTP
SYSTEMD=$(command -v systemd-nspawn)
readonly LFTP
# Paketspezifische Variablen füllen
ARCH=$(grep BuildArch: SPECS/"$PRJ".spec | awk '{print $2}');
readonly ARCH
SRC=$(grep Source: SPECS/"$PRJ".spec | awk '{print $2}');
readonly SRC
PRJURL=$(grep URL: SPECS/"$PRJ".spec | awk '{print $2}');
readonly PRJURL
NAME=$(grep Name: SPECS/"$PRJ".spec | head -1 | awk '{print $2}');
readonly NAME
PRJNAME=$(grep prjname SPECS/"$PRJ".spec | head -1 | awk '{print $3}');
readonly PRJNAME
PKGNAME=$(grep pkgname SPECS/"$PRJ".spec | head -1 | awk '{print $3}');
readonly PKGNAME
BRANCH=$(grep branch SPECS/"$PRJ".spec | head -1 | awk '{print $3}');
readonly BRANCH
VERSION=$(grep Version: SPECS/"$PRJ".spec | head -1 | awk '{print $2}');
readonly VERSION
COMMIT=$(grep commit SPECS/"$PRJ".spec | head -1 | awk '{print $3}');
readonly COMMIT
BZR_REV=$(grep bzr_rev SPECS/"$PRJ".spec | head -1 | awk '{print $3}');
readonly BZR_REV
# sonstige Variable befüllen
CPU=$(uname -m)
readonly CPU
MOCKVER=$(mock --version)
readonly MOCKVER
# Wenn im SPEC keine BuildArch angegeben ist, für die eigene Prozessor-
# Architektur bauen
if [ -z "$ARCH" ]; then
readonly BARCH=$CPU
else
readonly BARCH=$ARCH
fi
# Falls keine Angabe zum Source-Tag gefunden wurde, im Source0-Tag
# nachschauen
if [ -z "$SRC" ]; then
SOURCE=$(grep Source0: SPECS/"$PRJ".spec | awk '{print $2}');
readonly SOURCE
else
readonly SOURCE=$SRC
fi
# Wenn die Quellen aus Git kommen, auch noch den Git-Hash berechnen
if [ -n "$COMMIT" ]; then
readonly HASH=${COMMIT:0:7};
fi
# Wenn mock > 1.2.19 ist, systemd-nspawn verwenden, sofern installiert
if [ -n "$SYSTEMD" ] && [ "$MOCKVER" \> "1.2.19" ] ; then
readonly NSPAWN="--new-chroot"
else
debugMsg "mock version $MOCKVER is too old for systemd-nspawn, using chroot!"
readonly NSPAWN="--old-chroot"
fi
}
function moveLocal {
local ARCHDIR
local FILES
local RPMFILE
local DIRS
local COUNTER
local SPEC
# Das src.rpm wird nicht benötigt und deshalb gelöscht
find "$HOME/rpmbuild/RPMS/" -maxdepth 1 -name "*$PRJ*src.rpm" -type f -exec rm -f '{}' \;
DIRS=$(ls "$HOME/rpmbuild/RPMS/")
for ARCHDIR in $DIRS; do
FILES=$(find "$HOME/rpmbuild/RPMS/" -maxdepth 1 -name "$CPU" -type f 2> /dev/null | wc -l)
if [ "$FILES" != "0" ]; then
echo
echo "removing existing RPMs from $HOME/rpmbuild/RPMS/$ARCHDIR/"
find "$HOME/rpmbuild/RPMS/$ARCHDIR/" -name "*$PRJ*" -type f -exec rm -f '{}' \;
echo "copying RPMs to $HOME/rpmbuild/RPMS/$ARCHDIR/"
mv -f "$HOME/rpmbuild/RPMS/*$ARCHDIR*.rpm" "$HOME/rpmbuild/RPMS/$ARCHDIR/"
if [ -n "$RPMLINT" ]; then
RPMFILE=$(find . -path "./RPMS/$ARCHDIR/$NAME-$VERSION*.rpm" -type f)
COUNTER=$(find . -path "./RPMS/$ARCHDIR/$NAME-$VERSION*.rpm" -type f | wc -l)
SPEC=$(readlink -f "./SPECS/$PRJ.spec")
echo "Valid $PRJ Pakete mit rpmlint"
if [ "$COUNTER" == 1 ]; then
$RPMLINT "$SRPM" "$RPMFILE" "$SPEC"
fi
else
echo "rpmlint is missing"
fi
fi
done
}
function downloadSources {
local MATCH
local URL
local DEST
# URL für den Download der Sourcen zusammenbauen
MATCH="%{url}"
URL=${URL//$MATCH/$PRJURL}
MATCH="%{name}"
URL=${SOURCE//$MATCH/$NAME}
MATCH="%{version}"
URL=${URL//$MATCH/$VERSION}
if [ -n "$PRJNAME" ]; then
MATCH="%{prjname}"
URL=${URL//$MATCH/$PRJNAME}
fi
if [ -n "$PKGNAME" ]; then
MATCH="%{pkgname}"
URL=${URL//$MATCH/$PKGNAME}
fi
if [ -n "$BRANCH" ]; then
MATCH="%{branch}"
URL=${URL//$MATCH/$BRANCH}
fi
if [ -n "$COMMIT" ]; then
MATCH="%{commit}"
URL=${URL//$MATCH/$COMMIT}
fi
if [ -n "$HASH" ]; then
MATCH="%{githash}"
URL=${URL//$MATCH/$HASH}
fi
if [ -n "$BZR_REV" ]; then
MATCH="%{bzr_rev}"
URL=${URL//$MATCH/$BZR_REV}
fi
# Dateinamen des lokalen Sourcen-Archivs generieren
# shellcheck disable=SC1001
DEST=$(echo "$URL" | awk -F\/ '{print $NF}')
# Wenn eine URL als Source angegeben ist, die Datei herunterladen
if [ "${URL:0:3}" == "ftp" ] || [ "${URL:0:4}" == "http" ]; then
echo "removing old sources ..."
find "$HOME/rpmbuild/SOURCES/" -name "*.tar.*" -type f -exec rm -f '{}' \;
find "$HOME/rpmbuild/SOURCES/" -name "*.zip" -type f -exec rm -f '{}' \;
if [ -n "$WGET" ]; then
echo "downloading sources from $URL ..."
$WGET "$URL" --quiet --show-progress -O "SOURCES/$DEST"
RC=$?
if [ $RC != 0 ]; then
notificationSend "download failed! (code $RC)"
exit
fi
else
notificationSend "wget is missing!"
exit
fi
fi
}
function get_mock_root {
if [ -n "$MOCK" ]; then
RELEASE=$(grep VERSION_ID /etc/os-release)
RELEASE=${RELEASE#*=}
readonly ROOT="fedora-$RELEASE-$CPU"
fi
}
function buildRPMs {
local DIR
local DIRS
local BUILD
local SOURCEFILE
local RC
echo
echo "removing existing SRPMs ..."
find "$HOME/rpmbuild/SRPMS/" -name "*src.rpm" -type f -exec rm -f '{}' \;
echo "removing old logs ..."
find . -name "*log" -type f -exec rm -f '{}' \;
echo "cleaning build directory ..."
DIRS=$(find "$HOME/rpmbuild" -name "BUILD*" -type d)
get_mock_root
for DIR in $DIRS ; do
rm -rf "${DIR:?}/*"
done
echo
echo -e "generating $PRJ source package"
# SRPM erstellen
if $USERPMBUILD ; then
if [ -n "$RPMBUILD" ]; then
$RPMBUILD -bs "$HOME/rpmbuild/SPECS/$PRJ.spec"
RC=$?
else
notificationSend "rpmbuild is missing!"
exit
fi
else
if [ -n "$MOCK" ]; then
$MOCK -r "$ROOT" \
--dnf \
--buildsrpm \
--bootstrap-chroot \
--spec="$HOME/rpmbuild/SPECS/$PRJ.spec" \
--sources="$HOME/rpmbuild/SOURCES" \
--resultdir="$HOME/rpmbuild/SRPMS" \
$NSPAWN
RC=$?
else
notificationSend "mock is missing!"
exit
fi
fi
if [ "$RC" != 0 ]; then
notificationSend "generating srpm failed! (code $RC)"
exit
fi
# Pfad zum SRPM generieren
SOURCEFILE=$(find . -path "./SRPMS/$PRJ*" -type f)
SRPM=$(readlink -f "$SOURCEFILE")
readonly SRPM
SRCRPM=$(basename "$SRPM")
readonly SRCRPM
if [ -z "$SRPM" ]; then
notificationSend "can't find the srpm!"
exit
fi
if [ -n "$RPMLINT" ]; then
echo
echo "validating $PRJ source package with rpmlint"
SPEC=$(readlink -f "./SPECS/$PRJ.spec")
$RPMLINT "$SRPM" "$SPEC"
fi
if [ -n "$RPMDEPLINT" ]; then
echo
echo "validating $PRJ source package with rpmdeplint"
$RPMDEPLINT check --arch "$CPU" "$SRPM"
fi
# Das Binary bauen und paketieren
if $BUILDRPM ; then
echo
echo "generating binary package ..."
if [ -n "$MOCK" ]; then
$MOCK -r "$ROOT" \
--rebuild "$SRPM" \
--target="$BARCH" \
--dnf \
--resultdir="$HOME/rpmbuild/RPMS/$BARCH" \
--bootstrap-chroot \
$NSPAWN
RC=$?
if [ "$RC" != 0 ]; then
notificationSend "build failed! (code $RC)"
exit
fi
else
notificationSend "mock is missing"
exit
fi
fi
if [ "${BUILD,,}" == "q" ]; then
exit
fi
}
function uploadSources {
cd "$WORKDIR/SRPMS" || exit
# FTP-Zugangsdaten auslesen sowie URL des SRPM auslesen
if [ -s "$HOME/.config/minibuild/ftp.conf" ]; then
# shellcheck disable=SC1091
source "$HOME/.config/minibuild/ftp.conf"
else
notificationSend "ftp-credentials not configured!"
exit
fi
# Das fertige SRPM auf den FTP-Server hochladen, damit COPR
# es verwenden kann
if [ -n "$FTPHOST" ]; then
echo
echo "uploading $SRPM to ftp ..."
if [ -n "$LFTP" ]; then
local FTPURL="ftp://$FTPUSER:$FTPPWD@$FTPHOST"
local LCD="$WORKDIR/SRPMS"
local DELETE="--delete"
local NOVERFIY="set ssl:verify-certificate no;"
$LFTP -c "set ftp:list-options -a; $NOVERFIY
open '$FTPURL';
lcd $LCD;
cd $FTPPATH;
mirror --reverse \
$DELETE \
--verbose \
--exclude-glob *.log"
elif [ -n "$CURL" ]; then
$CURL --ftp-ssl -# -k -T "$SRPM" -u "$FTPUSER:$FTPPWD" "ftp://$FTPHOST/$FTPPATH"
RC=$?
if [ "$RC" != 0 ]; then
notificationSend "upload failed! (code $RC)"
exit
fi
else
notificationSend "curl and lftp missing!"
exit
fi
else
notificationSend "ftp-credentials not configured!"
exit
fi
}
function buildCOPR {
local COPRS
local USE
local CHROOT
local CHROOTS
local COUNTER=0
# FTP-Zugangsdaten auslesen sowie URL des SRPM auslesen
if [ -s "$HOME/.config/minibuild/ftp.conf" ]; then
# shellcheck disable=SC1091
source "$HOME/.config/minibuild/ftp.conf"
else
notificationSend "ftp-credentials not configured!"
exit
fi
# COPR, übernehmen Sie
if [ -n "$CLI" ]; then
if [ -z "$COPRNAME" ]; then
if [ -s "$HOME/.config/minibuild/coprs.conf" ]; then
echo
echo "searching coprs.conf for a matching copr ..."
COPRS=$(grep "$PRJ" "$HOME/.config/minibuild/coprs.conf")
COUNTER=$(grep -c "$PRJ" "$HOME/.config/minibuild/coprs.conf")
COPRS=${COPRS#*=}
if [[ -n "$COPRS" && "$COUNTER" == 1 ]]; then
read -r -p "using copr $COPRS? (y/n) " USE
if [ "${USE,,}" == "y" ]; then
COPR=$COPRS
echo "using copr $COPRS for creating packages..."
fi
fi
fi
# Kein passendes COPR gefunden -> das COPR CLI befragen
if [[ -z "$COPR" || $COUNTER != 1 ]]; then
echo "looking for a matching copr ..."
for COPRS in $($CLI list | grep Name | awk '{print $2}' | grep "$PRJ"); do
read -r -p "using copr $COPRS? (y/n) " USE
if [ "${USE,,}" == "y" ]; then
COPR=$COPRS
break
fi
done
fi
# Noch immer kein passendes COPR gefunden -> User fragen
if [ -z "$COPR" ]; then
read -r -p "using copr: " COPR
fi
else
COPR=$COPRNAME
fi
# COPR-Build veranlassen
if [ -n "$COPR" ]; then
# Nachschauen, ob ein Projekt/COPR nur für bestimmte
# chroots gebaut werden soll
if [ -s "$HOME/.config/minibuild/chroots.conf" ]; then
CHROOT=$(grep "$PRJ" "$HOME/.config/minibuild/chroots.conf")
if [ -z "$CHROOT" ]; then
CHROOT=$(grep "$COPR" "$HOME/.config/minibuild/chroots.conf")
fi
CHROOTS=${CHROOT#*=}
fi
# Paket(e) bauen
if [ -z "$CHROOTS" ]; then
echo
if ! $UPLOADFTP ; then
$CLI build "$COPR" "$SRPM"
else
$CLI build "$COPR" "http://$HTTPHOST/$HTTPPATH/$SRCRPM"
fi
else
echo
OLDIFS=$IFS
IFS=","
for CHROOT in $CHROOTS; do
CMDLINE="$CMDLINE -r $CHROOT"
done
IFS=$OLDIFS
if ! $UPLOADFTP ; then
$CLI build "$COPR" "$CMDLINE" "$SRPM"
else
$CLI build "$COPR" "$CMDLINE" "http://$HTTPHOST/$HTTPPATH/$SRCRPM"
fi
fi
fi
else
notificationSend "copr-cli is missing!"
exit
fi
}
function buildProject {
if [ -e !"SPECS/$PRJ.spec" ]; then
notificationSend "the submitted spec file does not exist!"
exit
fi
initVars
initConfig
downloadSources
buildRPMs
if $BUILDRPM ; then
moveLocal
fi
if $UPLOADFTP ; then
uploadSources
fi
if $COPRBUILD ; then
buildCOPR
fi
}
function cmdline {
# got this idea from here:
# http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/
local ARG
local PARAM
for ARG; do
local DELIM=""
case "$ARG" in
#translate --gnu-long-options to -g (short options)
--spec) PARAM="${PARAM}-s "
;;
--build) PARAM="${PARAM}-b " # Build binary rpm for testing local
;;
--upload) PARAM="${PARAM}-u " # upload srpm to ftp space
;;
--copr) PARAM="${PARAM}-c " # use copr for building binary rpms
;;
--name) PARAM="${PARAM}-n " # name of the copr to use for building binary rpms
;;
--rpmbuild) PARAM="${PARAM}-r " # use rpmbuild for building srpms
;;
#pass through anything else
*) [[ "${ARG:0:1}" == "-" ]] || DELIM="\""
PARAM="${PARAM}${DELIM}${ARG}${DELIM} "
;;
esac
done
#Reset the positional parameters to the short options
eval set -- "$PARAM"
local BUILD=false
local UPLOAD=true
local COPR=true
local SRPMRPMBUILD=true
local NAME
while getopts ":s:b:u:c:n:r:" opt; do
case $opt in
s)
readonly PRJ=$OPTARG
;;
b)
if [ "$OPTARG" == "no" ]; then
BUILD=false
elif [ "$OPTARG" == "yes" ]; then
BUILD=true
fi
;;
u)
if [ "$OPTARG" == "no" ]; then
UPLOAD=false
elif [ "$OPTARG" == "yes" ]; then
UPLOAD=true
fi
;;
c)
if [ "$OPTARG" == "no" ]; then
COPR=false
elif [ "$OPTARG" == "yes" ]; then
COPR=true
fi
;;
r)
if [ "$OPTARG" == "no" ]; then
SRPMRPMBUILD=false
elif [ "$OPTARG" == "yes" ]; then
SRPMRPMBUILD=true
fi
;;
n)
if [ -n "$OPTARG" ]; then
NAME=$OPTARG
fi
;;
\?)
echo "invalid argument: -$OPTARG" >&2
exit 1
;;
:)
echo "argument -$OPTARG requires a value." >&2
exit 1
;;
esac
done
readonly BUILDRPM=$BUILD
readonly UPLOADFTP=$UPLOAD
readonly COPRBUILD=$COPR
readonly COPRNAME=$NAME
readonly USERPMBUILD=$SRPMRPMBUILD
[[ -z $PRJ ]] \
&& echo "You must provide --spec file" && exit
return 0
}
function main {
cmdline "$ARGS"
prepareBuild
buildProject
}
ARGS=("$@")
readonly ARGS
main
cd ..
exit 0