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