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