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