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