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