cleanup in mkfedora : use mirroring/ as a skeleton for the standard repos part of...
[build.git] / build.common
1 # -*-Shell-script-*-
2 #
3 # Common functions for build scripts used by various packages
4 # incorporated (e.g., build, myplc, myplc-devel, vserver-reference)
5 #
6 # Marc E. Fiuczynski <mef@cs.princeton.edu>
7 # Copyright (C) 2007 The Trustees of Princeton University
8 #
9 # $Id$
10 #
11
12 ### xxx thierry : looks like we should bump this to fedora 8
13 ### however, rather than the actual default,
14 ### this looks more like the bottom of what we're able to build
15 ### so I leave it with fc4
16 function pl_getDefaultDistro() {
17     # FC4 is currently the default release
18     echo "Fedora"
19     return 0
20 }
21
22 function pl_getDefaultRelease() {
23     # FC4 is currently the default release
24     echo "4"
25     return 0
26 }
27
28 function pl_getDistro() {
29     defaultDistro=$(pl_getDefaultDistro)
30     if [ -f "/etc/redhat-release" ] ; then
31         distro=$(awk ' { print $1 } ' /etc/redhat-release)
32     else
33         distro=$defaultDistro
34     fi
35     echo "$distro"
36     return 0
37 }
38
39 ### xxx thierry : when running in a centOS box, this issues a warning message
40 ### because 5.2 is not an integer and bash cannot compare that
41 ### seems harmless though
42 function pl_getRelease() {
43     defaultRelease=$(pl_getDefaultRelease)
44     if [ -f "/etc/redhat-release" ] ; then
45         release=$(awk ' { if ($1=="Fedora" && $2=="Core") print $4 ; if (($1=="Fedora" && $2!="Core")||$1=="CentOS") print $3 } ' /etc/redhat-release)
46         [ $release -lt $defaultRelease ] && release=$defaultRelease
47     else
48         release=$defaultRelease
49     fi
50     echo "$release"
51     return 0
52 }
53
54 # vserver expects something like fc4 or f7
55 # for fedora and centos only for now
56 function pl_getReleaseName () {
57     distro=$1; shift
58     release=$1; shift
59     case $distro in
60         [Ff]edora*)
61             if [ "$release" -le 6 ] ; then
62                 releasename=fc$release
63             else
64                 releasename=f$release
65             fi ;;
66         [Cc]entOS*)
67             old_IFS="$IFS"
68             IFS="."
69             set -- $release
70             IFS="$old_IFS"
71             releasename=centos$1
72             ;;
73         *)
74             releasename="unknown-name-for-${pl_DISTRO}-please-edit-build.common"
75             echo 1>&2 "build.common: WARNING - releasename not set for distro=$distro" 
76             return 1
77             ;;
78     esac
79     echo "$releasename"
80     return 0
81 }
82
83 # figure out which redhat distro we are using (fedora, centos, redhat)
84 pl_DISTRO=$(pl_getDistro)
85
86 # select basearch of the host devel environment - protected for macos for local tests
87 pl_DISTRO_ARCH=$(uname -i 2>/dev/null|| echo unknownarch)
88
89 # the release number (plain number)
90 pl_DISTRO_RELEASE=$(pl_getRelease)
91
92 # the release name - for vserver build - like fc4, f8 or centos4
93 pl_DISTRO_NAME=$(pl_getReleaseName $pl_DISTRO $pl_DISTRO_RELEASE)
94
95 # get path to appropriate yumgroups.xml file
96 # Thierry: quick & dirty improvement 
97 # this file is updated by the toplevel build, from groups/<pldistro>.xml
98 pl_DISTRO_YUMGROUPS="../../../RPMS/yumgroups.xml"
99
100 function pl_process_fedora_options () {
101     # Get options
102     shiftcount=0
103     while getopts "l:r:a:h" opt ; do
104         case $opt in
105             l)
106                 pl_DISTRO_URL=$OPTARG
107                 let shiftcount=$shiftcount+2
108                 ;;
109             r)
110                 pl_DISTRO_RELEASE=$OPTARG
111                 let shiftcount=$shiftcount+2
112                 ;;
113             a)
114                 pl_DISTRO_ARCH=$OPTARG
115                 let shiftcount=$shiftcount+2
116                 ;;
117             h|*)
118                 echo "Usage: $0 [OPTION]..."
119                 echo "  -l url          distro mirror location (default: $pl_DISTRO_URL)"
120                 echo "  -r release      distro release number (default: $pl_DISTRO_RELEASE)"
121                 echo "  -a arch         distro architecture (default: $pl_DISTRO_ARCH)"
122                 echo "where distro can be either fedora, centos, or redhat"
123                 echo "  -h              This message"
124                 exit 1
125                 ;;
126         esac
127     done
128     return $shiftcount
129 }
130
131 ######################################## handling a root image
132 function pl_root_rpm_macros () {
133     cat <<EOF
134 %_install_langs C:en_US:en
135 %_netsharedpath /proc:/dev/pts:/usr/share/info
136 %_excludedocs 1
137 %__file_context_path /dev/null
138 EOF
139 }
140
141 function pl_root_makedevs() {
142     vroot=$1
143     # Clean ${vroot}/dev, but only when ${vroot}!=""
144     [ -n $vroot ] && rm -rf $vroot/dev
145     
146     mkdir -p $vroot/dev
147     mknod -m 666 $vroot/dev/null c 1 3
148     mknod -m 666 $vroot/dev/zero c 1 5
149     mknod -m 666 $vroot/dev/full c 1 7
150     mknod -m 644 $vroot/dev/random c 1 8
151     mknod -m 644 $vroot/dev/urandom c 1 9
152     mknod -m 666 $vroot/dev/tty c 5 0
153     mknod -m 666 $vroot/dev/ptmx c 5 2
154     # For bash command substitution
155     ln -nsf ../proc/self/fd $vroot/dev/fd
156
157     # For df and linuxconf
158     touch $vroot/dev/hdv1
159
160     # For pseudo ttys
161     mkdir -p $vroot/dev/pts
162
163     # (Might have to remove the following for vserver-reference.)
164
165     # for tmpfs mount
166     mkdir -p $vroot/dev/shm
167
168     # For TUN/TAP
169     mkdir -p $vroot/dev/net
170     mknod -m 600 $vroot/dev/net/tun c 10 200
171
172     # For mkinitrd (in case a kernel is being installed)
173     # As well for loop back mounting within a vserver. 
174     for i in $(seq 0 255) ; do
175         mknod -m 640 $vroot/dev/loop$i b 7 $i
176     done
177 }
178
179 function mkfedora_usage() {
180     echo "Usage: pl_root_mkfedora [OPTION]... basedir pldistro pkgsfile(s)"
181     echo "      -l url          Fedora mirror location."
182     echo "                      Defaults are searched in <pldistro>.mirrors"
183     echo "      -v              Be verbose"
184     echo "      -h              This message"
185     echo " target selection (defaults based on current build vserver)"
186     echo "      -r release      Fedora release number (default: $releasever)"
187     echo "      -a arch         Fedora architecture (default: $basearch)"
188     exit 1
189 }
190
191 function pl_root_mkfedora () {
192
193     echo "* Entering pl_root_mkfedora" "$@"
194
195     if [ $UID -ne 0 ] ; then
196         echo "Error: You must run this script as root."
197         exit 1
198     fi
199
200 # Verbosity
201     verbose=0
202
203 # Release and architecture to install : defaults to current vserver's settings or previously parsed fedora_options
204     releasever=$pl_DISTRO_RELEASE
205     basearch=$pl_DISTRO_ARCH
206
207 # Get options
208 ###    mirrors=""
209 ###    while getopts "l:r:a:vh" opt ; do
210     while getopts "vh" opt ; do
211         case $opt in
212 ###         l) mirrors="$mirrors $OPTARG" ;;
213 ###         r) releasever=$OPTARG ;;
214 ###         a) basearch=$OPTARG ;;
215             v) verbose=1; set -x ;;
216             h|*) mkfedora_usage ;;
217         esac
218     done
219
220     shift $(($OPTIND - 1))
221     [[ "$#" -lt 3 ]] && mkfedora_usage
222     vroot=$1 ; shift
223     pldistro=$1 ; shift
224     pkgsfile="$@"
225     vroot=$(cd $vroot && pwd -P)
226     [ -d $vroot ] || mkfedora_usage
227
228
229     # parse pkgsfile and add to local vars
230     fcdistro=${pl_DISTRO_NAME}
231     pkgs_packages=$(pl_parsePkgs package $fcdistro $pldistro $pkgsfile) 
232     pkgs_groups=$(pl_parsePkgs group $fcdistro $pldistro $pkgsfile)
233     # packages to exclude - obsolete, was maybe useful when installing a group
234     pkgs_excludes=$(pl_parsePkgs exclude $fcdistro $pldistro $pkgsfile) 
235     pkgs_junk=$(pl_parsePkgs junk $fcdistro $pldistro $pkgsfile)
236     pkgs_precious=$(pl_parsePkgs precious $fcdistro $pldistro $pkgsfile)
237     # formerly related to mkfedora -k
238     pkgs_kexcludes=$(pl_parsePkgs kexclude $fcdistro $pldistro $pkgsfile)
239     # get mirrors if not specified with -l
240     if [ -z "$mirrors" ] ; then
241         mirrorsfile=$(pl_locateDistroFile ../build/ $pldistro "$pldistro.mirrors")
242         mirrors=$(pl_parsePkgs mirror $fcdistro $pldistro $mirrorsfile)
243     fi
244
245     exclude_line=""
246     # add them manually as the output of pl_parsePkgs is line-separated
247     if [ -n "$pkgs_kexcludes" ] ; then
248         exclude_line="exclude="
249         for kexclude in $pkgs_kexcludes ; do
250             exclude_line="$exclude_line $kexclude"
251         done
252     fi
253
254     echo "$0: candidate mirrors"
255     for mirror in $mirrors ; do
256         echo "* candidate mirror $mirror"
257     done
258
259     # the repo part of the final yum.conf
260     yum_conf_repos=$vroot/xxxmkfedora-repos.confxxx
261     if ! yumconf_mirrors $yum_conf_repos ../build/ $fcdistro "$exclude_line" $mirrors ; then
262         echo xxx -- error ; return 1
263     fi
264     
265     public_gpg_key=$(yumconf_gpgkey $yum_conf_repos)
266
267     # Do not tolerate errors
268     set -e
269
270     ## make rpms ignore installing stuff to special fs entries like /proc
271     # Because of https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=52725
272     # you have to use at least one language beside 'C'
273     # Prevent all locales from being installed in reference image
274     mkdir -p $vroot/etc/rpm
275     pl_root_rpm_macros > $vroot/etc/rpm/macros 
276
277     # Trick rpm and yum, who read the real root /etc/rpm/macros file
278     # rather than the one installed in the reference image, despite what
279     # you might expect the --root and --installroot options to mean. Both
280     # programs always read $HOME/.rpmmacros.
281     export HOME=$vroot/tmp
282     mkdir -p $vroot/tmp
283     pl_root_rpm_macros > $vroot/tmp/.rpmmacros
284
285     function mkfedora_cleanup () {
286         umount -l $vroot/proc
287         umount -l $vroot/dev/shm
288         umount -l $vroot/dev/pts
289     }
290
291     # Clean up before exiting if anything goes wrong
292     trap "mkfedora_cleanup" ERR INT
293
294     # Mount in reference image
295     mount -t devpts none $vroot/dev/pts
296     mount -t tmpfs none $vroot/dev/shm
297     mkdir -p $vroot/proc
298     mount -t proc none $vroot/proc
299     
300     # Create a /var/lib dirs for yum & rpm
301     mkdir -p $vroot/var/lib/yum
302     mkdir -p $vroot/var/lib/rpm
303     mkdir -p $vroot/usr/share/info
304
305     # Create a dummy /etc/fstab in reference image
306     mkdir -p $vroot/etc
307     cat >$vroot/etc/fstab <<EOF
308 # This fake fstab exists only to please df and linuxconf.
309 /dev/hdv1       /       ext2    defaults        1 1
310 EOF
311     cp $vroot/etc/fstab $vroot/etc/mtab
312
313     # Necessary for some scripts
314     mkdir -p $vroot/etc/sysconfig
315     echo "NETWORKING=yes" > $vroot/etc/sysconfig/network
316
317     # Initialize RPM database in reference image
318     mkdir -p $vroot/var/lib/rpm
319     rpm --root $vroot --initdb
320     rpm --root $vroot --import $public_gpg_key
321
322     # Initialize yum in reference image
323     mkdir -p $vroot/var/cache/yum $vroot/var/log
324
325 # yum.conf is for building only - store in different location than /etc/yum.conf
326     yum_conf=$vroot/etc/mkfedora-yum.conf
327     cat > $yum_conf <<EOF
328 [main]
329 cachedir=/var/cache/yum
330 debuglevel=2
331 logfile=/var/log/yum.log
332 pkgpolicy=newest
333 distroverpkg=redhat-release
334 tolerant=1
335 exactarch=1
336 retries=20
337 obsoletes=1
338 gpgcheck=0
339 # Prevent yum-2.4 from loading additional repository definitions
340 # (e.g., from /etc/yum.repos.d/)
341 reposdir=/dev/null
342 EOF
343     
344     cat $yum_conf_repos >> $yum_conf
345
346     # If we are being built as part of an automated RPM build, solve the
347     # bootstrap problem by including any just built packages in the yum
348     # configuration. This cooperates with the PlanetLab build system.
349     if [ -n "$RPM_BUILD_DIR" ] ; then
350         RPM_RPMS_DIR=$(cd $(dirname $RPM_BUILD_DIR)/RPMS && pwd -P)
351         # If run under sudo, allow user to delete the headers/ and
352         # repodata/ directories.
353         if [ -n "$SUDO_USER" ] ; then
354             chown -R $SUDO_USER $RPM_RPMS_DIR
355         fi
356         cat >> $yum_conf <<EOF
357
358 [bootstrap]
359 name=Bootstrap - $basearch - $RPM_RPMS_DIR/
360 baseurl=file://$RPM_RPMS_DIR/
361 EOF
362 fi
363
364     echo "========== Dumping $yum_conf"
365     cat $yum_conf
366     echo "========== EndDump $yum_conf"
367
368     yum_options=""
369     yum --help | grep verbose &> /dev/null && yum_options="$yum_options --verbose"
370     yum_options="$yum_options -y"
371     yum_options="$yum_options -c $yum_conf"
372     yum_options="$yum_options --installroot=$vroot"
373
374     exclude_arg=""
375     for exclude in $pkgs_excludes; do
376         exclude_arg="$exclude_arg --exclude $exclude"
377     done
378
379     # glibc must be specified explicitly for the correct arch to be
380     # chosen.
381     echo "* Installing glibc"
382     yum $yum_options $exclude_arg install glibc
383
384     # Go, baby, go
385     if [ -n "$pkgs_packages" ] ; then
386         echo "* Installing optional packages" $pkgs_packages
387         yum $yum_options $exclude_arg install $pkgs_packages
388         if ! rpm --root $vroot -q $pkgs_packages >/dev/null ; then
389             echo "* Warning: Missing packages"
390             rpm --root $vroot -q $pkgs_packages | grep "not installed"
391         fi
392     fi
393
394     if [ -n "$pkgs_groups" ] ; then
395        ## call yum sequentially to get finer-grained info on dependencies
396         for grp in $pkgs_groups ; do
397             echo "* Installing optional group $grp" 
398             yum $yum_options $exclude_arg groupinstall "$grp"
399         done
400     fi
401
402     # formerly in bootcd/prep.sh : to optimize footprint
403     if [ -n "$pkgs_junk" ] ; then
404         echo "* Removing unnecessary junk"
405         pushd $vroot
406         # Save precious files
407         [ -n "$pkgs_precious" ] && tar --ignore-failed-read -cpf precious.tar $pkgs_precious
408         # Remove unnecessary junk
409         [ -n "$pkgs_junk" ] && rm -rf $pkgs_junk
410         # Restore precious files
411         [ -n "$pkgs_precious" ] && tar -xpf precious.tar && rm -f precious.tar
412         popd
413     fi
414
415     # Clean yum cache
416     echo "* Cleaning up"
417     yum $yum_options clean all
418
419     # Clean RPM state
420     rm -f $vroot/var/lib/rpm/__db*
421
422     # Set time zone to UTC
423     if [ -f $vroot/usr/share/zoneinfo/UTC -a -f $vroot/etc/localtime ] ; then
424         rm -f $vroot/etc/localtime
425         ln -s /usr/share/zoneinfo/UTC $vroot/etc/localtime
426     fi
427
428     echo "Dumping current list of rpms in /etc/mkfedora-rpms.txt"
429     chroot $vroot rpm -aq | sort > $vroot/etc/mkfedora-rpms.txt
430
431     # remove trap handler, as we are about to call it directly.
432     trap - ERR INT
433
434     # Clean up
435     mkfedora_cleanup
436
437     return 0
438 }
439
440 function pl_root_tune_image () {
441     root=$1; shift
442
443     # Disable all services in reference image
444     chroot $root sh -c "/sbin/chkconfig --list | awk '{ print \$1 }' | xargs -i /sbin/chkconfig {} off"
445
446     # FC2 minilogd starts up during shutdown and makes unmounting
447     # impossible. Just get rid of it.
448     rm -f $root/sbin/minilogd
449     ln -nsf /bin/true $root/sbin/minilogd
450
451     # This tells the Boot Manager that it is okay to update
452     # /etc/resolv.conf and /etc/hosts whenever the network configuration
453     # changes. Users are free to delete this file.
454     touch $root/etc/AUTO_UPDATE_NET_FILES
455 }
456
457 # Move specified directories out of a src tree into a dst tree, and
458 # then for each moved directory create a symlink in src to dst.
459 function pl_move_dirs() {
460     root=$1
461     data=$2
462     store=$3
463     shift 3
464
465     mkdir -p $root/data
466     for datadir in "$@" ; do
467         mkdir -p ${data}${datadir}
468         if [ -d ${root}/${datadir} -a ! -h ${root}/${datadir} ] ; then
469             (cd ${root} && find ./${datadir} | cpio -p -d -u ../${data}/)
470         fi
471         rm -rf ${root}/${datadir}
472         mkdir -p $(dirname ${root}/${datadir})
473         ln -nsf ${store}/${datadir} ${root}/${datadir}
474     done
475 }
476
477 # Construct an image file from given some directory
478 # XXX in the future maybe use livecdtools?
479 function pl_make_image() {
480     root=$1
481     image=$2
482     extraspace=$3
483
484     # Leave about 100 MB free space and allow for about 20% inode overhead
485     bytes=$((($(du -sb $root | cut -f1) + $extraspace) * 120 / 100))
486     bs=4096
487     blocks=$(($bytes / $bs))
488     dd bs=$bs count=$blocks if=/dev/zero of=$image
489     mkfs.ext3 -b $bs -j -F $image
490
491     # Temporarily mount it
492     tmp=$(mktemp -d tmp.XXXXXX)
493     mount -o loop $image $tmp
494     trap "umount $tmp; rmdir $tmp" ERR INT
495
496     # Move files to it
497     (cd $root && tar cpf - .) | (cd $tmp && tar xpf -)
498
499     # Unmount it
500     umount $tmp
501     rmdir $tmp
502     trap - ERR INT
503 }
504
505 # Fix permissions on tmp directories
506 function pl_fixtmp_permissions() {
507     root=$1
508     chmod 1777 $root/tmp $root/usr/tmp $root/var/tmp
509 }
510
511 function pl_fixdirs() {
512     root=$1
513     datadirs=$2
514     for datadir in datadirs ; do
515         if [ -h ${root}/${datadir} ] ; then
516             rm -f ${root}/${datadir}
517             mkdir -p ${root}/${datadir}
518         fi
519     done
520 }
521
522 ########## .pkgs format
523 # comments start with a # - this is needed only if you use a keyword in a comment
524
525 function pl_getPkgsAttribute () {
526     keyword=$1; shift
527     file=$1; shift
528     # remove any initial white spaces from the result
529     grep -v '^#' $file | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,," -e "s,^[[:space:]][[:space:]]*,,"
530 }
531
532 # for a given keyword like 'package' :
533 # we support fcdistro-dependant format, for tokens (pkgname) without whitespace
534 # you can e.g. use
535 # package: pkg1 .. pkgn 
536 # package+f8: pkg1 .. pkgn
537 # package-f8: pkg1 .. pkgn
538
539 # values can contain @arch@, @fcdistro@ or @pldistro@ that are replaced with the current values
540 #
541 function pl_parsePkgs () {
542
543     keyword=$1;shift
544     fcdistro=$1; shift
545     pldistro=$1; shift
546     # remaining arguments are paths to the pkgs files
547
548     # grab regular descriptions
549     all=$(grep -v '^#' "$@" | grep --regexp="^${keyword}:" | sed -e "s,${keyword}:,,")
550     # grab additions
551     add=$(grep -v '^#' "$@" | grep --regexp="^${keyword}+${fcdistro}:" | sed -e "s,${keyword}+${fcdistro}:,,")
552     # grab exclusions
553     sub=$(grep -v '^#' "$@" | grep --regexp="^${keyword}-${fcdistro}:" | sed -e "s,${keyword}-${fcdistro}:,,")
554
555     for i in $all $add; do
556         for exclude in $sub; do
557             [ "$i" = "$exclude" ] && continue 2
558         done
559         echo "$i " | sed -e "s,@arch@,$pl_DISTRO_ARCH,g" -e "s,@fcdistro@,$fcdistro,g" -e "s,@pldistro@,$pldistro,g"
560     done
561     return 0
562 }
563
564 function pl_getPackages() { fcdistro=$1; shift ; pldistro=$1; shift ; pl_parsePkgs package $fcdistro $pldistro "$@" ; }
565 function pl_getGroups() { fcdistro=$1; shift ; pldistro=$1; shift ; pl_parsePkgs group $fcdistro $pldistro "$@" ; }
566
567 # locates a pldistro-dependant file
568 # tries first in build/<pldistro>/, then in build/planetlab/
569 function pl_locateDistroFile () {
570     builddir=$1; shift
571     pldistro=$1; shift
572     pkgsfile=$1; shift
573
574     pkgspath=""
575     # locate it
576     paths="$builddir/config.$pldistro/$pkgsfile $builddir/config.planetlab/$pkgsfile"
577     for path in $paths; do
578         if [ -f $path ] ; then
579             pkgspath=$path
580             break
581         fi
582     done
583     if [ -z "$pkgspath" ] ; then
584         echo 1>&2 "pl_locateDistroFile - in $(pwd) : cannot locate $pkgsfile in $builddir"
585         echo 1>&2 "candidates were $paths"
586         echo "not-found-by-pl_locateDistroFile"
587         return 1
588     else
589         echo 1>&2 "pl_locateDistroFile: using $pkgspath"
590         echo $pkgspath
591         return 0
592     fi
593 }
594
595 function yumgroups_from_pkgs () {
596     builddir=$1; shift
597     pldistro=$1; shift
598     fcdistro=$1; shift
599     pkgsnames=$@
600
601     sedargs="-e s,@FCDISTRO@,$fcdistro,g"
602
603    cat <<__header
604 <?xml version="1.0"?>
605 <!DOCTYPE comps PUBLIC "-//Red Hat, Inc.//DTD Comps info//EN" "comps.dtd">
606 <comps>
607 __header
608
609     for pkgsname in $pkgsnames; do
610         pkgsfile=$(pl_locateDistroFile $builddir $pldistro $pkgsname)
611         packages=$(pl_getPackages $fcdistro $pldistro $pkgsfile)
612
613         groupname=$(pl_getPkgsAttribute groupname $pkgsfile | sed $sedargs)
614         groupdesc=$(pl_getPkgsAttribute groupdesc $pkgsfile | sed $sedargs)
615
616         if [ -z "$groupname" -o -z "$groupdesc" ] ; then
617             echo "Cannot find groupname: and groupdesc: in $pkgsfile -- skipped" 1>&2
618             continue
619         fi
620         
621         cat << __group_header
622   <group>
623     <id>$(echo $groupname|tr A-Z a-z)</id>
624     <name>$groupname</name>
625     <description>$groupdesc</description>
626     <uservisible>true</uservisible>
627     <packagelist>
628 __group_header
629         for package in $packages; do 
630             echo "<packagereq type=\"mandatory\">$package</packagereq>"
631         done
632         cat << __group_footer
633     </packagelist>
634   </group>
635 __group_footer
636     done
637 cat <<__footer
638 </comps>
639 __footer
640 }
641
642
643 function build_fetch () {
644     curl --fail --silent --max-time 60 --output /dev/null "$1" 
645 }
646
647 # tries to compute a valid yum.conf for that pldistro from the template in mirroring/
648 # returns 0 and writes <dest_yumconf> on success
649 # returns 1 on failure, in which case <dest_yumconf> is deleted
650 function yumconf_mirrors () {
651     dest_yumconf=$1; shift
652     builddir=$1; shift
653     fcdistro=$1; shift
654     exclude_line="$1" ; shift
655     mirrors="$@"
656
657     template=$builddir/mirroring/$fcdistro/yum.repos.d/building.repo.in
658     
659     if [ ! -f $template ] ; then
660         echo "yumconf_mirrors: cannot locate template $template"
661         rm -f $dest_yumconf
662         return 1
663     fi
664
665     for mirror in $mirrors; do
666         if yumconf_mirror $dest_yumconf $template "$exclude_line" $mirror; then
667             return 0
668         fi
669     done
670     rm -f $dest_yumconf
671     return 1
672 }
673
674 # computes a yum.conf from the template, and checks that all baseurl defined in there are valid repos
675 # returns 0 on success and 1 on failure
676 function yumconf_mirror () {
677     dest_yumconf=$1; shift
678     template=$1; shift
679     exclude_line="$1" ; shift
680     mirror=$1; shift
681
682     sed -e "s,@MIRRORURL@,$mirror,g" \
683         -e "/baseurl=/i\\
684 $exclude_line" $template > $dest_yumconf
685     
686     # capture all lines defining baseurl
687     baseurl_defs=$(grep '^baseurl=' $dest_yumconf)
688     if [ -z "$baseurl_defs" ] ; then
689         return 1
690     fi
691
692     for baseurl_def in $baseurl_defs; do
693         baseurl=$(echo $baseurl_def | sed \
694             -e s,baseurl=,, \
695             -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
696         repomd=$baseurl/repodata/repomd.xml
697
698         echo "* Trying to fetch $repomd"
699         if ! build_fetch $repomd ; then
700             echo "* Failed to fetch $repomd"
701             return 1
702         fi
703     done
704     echo "* Selected mirror $mirror"
705     return 0
706 }
707
708 # from a yum.conf as generated above, computes the (first) gpgkey url
709 function yumconf_gpgkey () {
710     dest_yumconf=$1; shift
711
712     first_line=$(grep '^gpgkey=' $dest_yumconf | head -1)
713     values=$(echo $first_line | sed -e s,gpgkey=,,)
714     value=$(echo $values | awk '{print $1;}' | sed -e 's,$basearch,'"$pl_DISTRO_ARCH",g)
715     [ -n "$value" ] || return 1
716     echo $value
717     return 0
718 }