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