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