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