modified to use common functions from build/build.common
[vserver-reference.git] / system-packages.sh
1 #!/bin/bash
2 #
3 # Download dependencies that would be necessary to build the
4 # pl_netflow and pl_conf root slices from the vserver-reference image.
5 #
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2004-2006 The Trustees of Princeton University
8 #
9 # $Id: system-packages.sh,v 1.5 2007/08/24 06:06:04 mef Exp $
10 #
11
12 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
13
14 vroot=$PWD/vservers/.vref/default
15 rpms=$PWD/vservers/system-packages
16 install -d -m 755 $rpms
17
18 # curl can't list file:// URLs
19 list ()
20 {
21     url=$1
22     if [ -e ${url#file://} ] ; then
23         /bin/ls ${url#file://}
24     else
25         curl --fail --silent --max-time 60 $url
26     fi
27 }
28
29 # Space separated list of required packages
30 planetflow="netflow"
31
32 for vref in planetflow ; do
33     packages=${!vref}
34     dependencies=()
35
36     if yum --help | grep -q shell ; then
37         while read -a words ; do
38             if [ ${#words[*]} -lt 5 ] ; then
39                 continue
40             fi
41
42             # netflow i386 3.1-23.planetlab.2006.04.04 bootstrap 61 k
43             package=${words[0]}
44             arch=${words[1]}
45             version=${words[2]}
46             # Remove the epoch from the version
47             version=${version##*:}
48             repository=${words[3]}
49
50             baseurl=
51             while read line ; do
52                 if [ -z "$line" ] ; then
53                     continue
54                 elif grep -q "^\[$repository\]" <<<$line ; then
55                     baseurl=$repository
56                 elif [ "$baseurl" = "$repository" ] && grep -q "^baseurl=" <<<$line ; then
57                     eval $line
58
59                     # We could parse headers/header.info and/or
60                     # repodata/primary.xml.gz to figure out where the
61                     # package actually is within the repository, but
62                     # it would be too much trouble. Just try
63                     # downloading it from one of the common
64                     # subdirectories.
65                     echo "* $vref: $repository $package-$version.$arch.rpm"
66                     for subdirectory in "" Fedora/RPMS Fedora $arch ; do
67                         if curl --fail --silent --max-time 60 $baseurl/$subdirectory/$package-$version.$arch.rpm \
68                             >$rpms/$package-$version.$arch.rpm ; then
69                             break
70                         fi
71                         rm -f $rpms/$package-$version.$arch.rpm
72                     done
73
74                     # Assert that we got it successfully
75                     if [ ! -f $rpms/$package-$version.$arch.rpm ] ; then
76                         echo "Failed to fetch $package-$version.$arch.rpm from $repository ($baseurl/$subdirectory)" >&2
77                         false
78                     fi
79
80                     dependencies[${#dependencies[*]}]=$package-$version.$arch.rpm
81                     break
82                 fi
83             done <$vroot/etc/yum.conf
84         done < <((yum -c $vroot/etc/yum.conf --installroot=$vroot shell <<EOF
85 install $packages
86 transaction solve
87 transaction list
88 EOF
89             ) | sed -ne '/^Installing:/,/^Transaction Summary/p' 
90         )
91     else
92         # This is pretty fucked up. Turn on verbose debugging and the
93         # --download-only option, which, contrary to what you might
94         # think the option means, downloads the headers, not the
95         # packages themselves. In any case, verbose debugging prints
96         # out the baseURL and path of each RPM that it would download
97         # if --download-only were not specified.
98         baseURL=
99         path=
100         while read line ; do
101             if [ -z "$baseURL" ] ; then
102                 baseURL=$(sed -ne 's/failover: baseURL = \(.*\)/\1/p' <<<$line)
103             elif [ -z "$path" ] ; then
104                 path=$(sed -ne 's/failover: path = \(.*\)/\1/p' <<<$line)
105             else
106                 if [ "${path##*.}" = "rpm" ] ; then
107                     echo "* $vref: $(basename $path)"
108                     curl --fail --silent --max-time 60 $baseURL/$path >$rpms/$(basename $path)
109                     dependencies[${#dependencies[*]}]=$(basename $path)
110                 fi
111                 baseURL=
112                 path=
113             fi
114         done < <(yum -d 3 -c $vroot/etc/yum.conf --installroot=$vroot -y --download-only install $packages 2>&1)
115     fi
116
117     for dependency in "${dependencies[@]}" ; do
118         echo $dependency
119     done >$rpms/$vref.lst
120 done
121
122 # Clean yum cache
123 yum -c $vroot/etc/yum.conf --installroot=$vroot -y \
124     clean all
125
126 # Clean RPM state
127 rm -f $vroot/var/lib/rpm/__db*
128
129 exit 0