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