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