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