expose /etc/resolv.conf to chroot before invoking pip or gem
[sliceimage.git] / build.sh
1 #!/bin/bash
2 #
3 # Builds all reference image for slices.  To optimize for space it
4 # will only build a complete base reference image and then
5 # builds "stub" images that are just contain the additional files
6 # and/or changes for a given reference image.  This is done to shrink
7 # the RPM itself.  These will be pieced back together with the base
8 # image by an init script that is installed on the node.
9 #
10 # Mark Huang <mlhuang@cs.princeton.edu>
11 # Marc E. Fiuczynski <mef@cs.princeton.edu>
12 # Copyright (C) 2004-2007 The Trustees of Princeton University
13 #
14
15 PATH=/sbin:/bin:/usr/sbin:/usr/bin
16
17 # In both a normal CVS environment and a PlanetLab RPM
18 # build environment, all of our dependencies are checked out into
19 # directories at the same level as us.
20 if [ -d ../build ] ; then
21     PATH=$PATH:../build
22     srcdir=..
23 else
24     echo "Error: Could not find $(cd .. && pwd -P)/build/"
25     exit 1
26 fi
27
28 export PATH
29
30 # build.common comes from the build module
31 . build.common
32
33 pl_process_fedora_options $@
34 shiftcount=$?
35 shift $shiftcount
36
37 # pldistro expected as $1 
38 pldistro=$1 ; shift
39 # this comes from spec's slicefamily
40 slicefamily=$1; shift
41
42 # Do not tolerate errors
43 set -e
44
45 # Path's to the reference images and stubs
46 # This is inherited from util-vservers
47 vrefdir=$PWD/vservers/.vref
48 vref=${vrefdir}/${slicefamily}
49 # stubs are created in a subdir per slicefamily
50 vstubdir=$PWD/vservers/.vstub/${slicefamily}
51
52 # Create paths
53 install -d -m 755 ${vref}
54 install -d -m 755 ${vstubdir}
55
56 # Some of the PlanetLab RPMs attempt to (re)start themselves in %post,
57 # unless the installation is running inside the BootCD environment. We
58 # would like to pretend that we are.
59 export PL_BOOTCD=1
60
61 # Populate image with sliceimage packages
62 pl_root_makedevs ${vref}
63 # locate the packages and groups file
64 pkgsfile=$(pl_locateDistroFile ../build/ ${pldistro} sliceimage.pkgs)
65 pl_root_mkfedora ${vref} ${pldistro} $pkgsfile
66 pl_root_tune_image ${vref}
67
68 systemslice_count=$(ls ../build/config.${pldistro}/sliceimage-*.pkgs 2> /dev/null | wc -l)
69 [ $systemslice_count -gt 0 ] && for systemslice in $(ls ../build/config.${pldistro}/sliceimage-*.pkgs) ; do
70     NAME=$(basename $systemslice .pkgs | sed -e s,sliceimage-,,)
71
72     echo " * --------START BUILDING system sliceimage ${NAME}: $(date)"
73
74     # "Parse" out the packages and groups for yum
75     systempackages=$(pl_getPackages ${pl_DISTRO_NAME} $pldistro $systemslice)
76     systemgroups=$(pl_getGroups ${pl_DISTRO_NAME} $pldistro $systemslice)
77     systempips=$(pl_getPips ${pl_DISTRO_NAME} $pldistro $systemslice)
78     systemgems=$(pl_getGems ${pl_DISTRO_NAME} $pldistro $systemslice)
79
80     vdir=${vstubdir}/${NAME}
81     rm -rf ${vdir}/*
82     install -d -m 755 ${vdir}
83
84     # Clone the base sliceimage reference to the system sliceimage reference
85     (cd ${vref} && find . | cpio -m -d -u -p ${vdir})
86     rm -f ${vdir}/var/lib/rpm/__db*
87
88     # Communicate to the initialization script from which vref this stub was cloned
89     echo ${slicefamily} > ${vdir}.cloned
90
91     # Install the system sliceimage specific packages
92     for yum_package in $systempackages; do
93         echo " * yum installing $yum_package"
94         yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y install $yum_package || \
95             echo " * WARNING image $systemslice - yum install $yum_package failed"
96     done
97     for group_plus in $systemgroups; do
98         yum_group=$(echo $group_plus | sed -e "s,+++, ,g")
99         echo " * yum groupinstalling $yum_group"
100         yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y groupinstall "$yum_group" || \
101             echo " * WARNING image $systemslice - yum groupinstall $yum_group failed"
102     done
103
104     # running pip or gem requires connectivity, and DNS
105     # so we expose the build-vm's /etc/resolv.conf to the current vdir 
106     mkdir -p ${vdir}/etc
107     cp /etc/resolv.conf ${vdir}/etc
108
109     # this requires pip to be available in sliceimage at that point
110     # fedora and debian -> python-pip
111     # on fedora the command is called pip-python (sigh.)
112     for pip in $systempips; do
113         echo " * pip installing $pip"
114         chroot ${vdir} pip -v install $pip || chroot ${vdir} pip-python -v $pip || \
115             echo " * WARNING image $systemslice - pip install $pip failed"
116     done
117
118     # same for gems; comes with ruby in fedora but ruby-devel is most likely a good thing
119     # we add --no-rdoc --no-ri to keep it low
120     for gem in $systemgems; do
121         echo " * gem installing $gem"
122         chroot ${vdir} gem install --no-rdoc --no-ri $gem || \
123             echo " * WARNING image $systemslice - gem install $gem failed"
124     done
125
126     # search e.g. sliceimage-planetflow.post in config.<pldistro> or in config.planetlab otherwise
127     postfile=$(pl_locateDistroFile ../build/ ${pldistro} sliceimage-${NAME}.post || : )
128
129     [ -f $postfile ] && /bin/bash $postfile ${vdir} || :
130
131     # Create a copy of the system sliceimage w/o the sliceimage reference files and make it smaller. 
132     # This is a three step process:
133
134     # step 1: clean out yum cache to reduce space requirements
135     yum -c ${vdir}/etc/mkfedora-yum.conf --installroot=${vdir} -y clean all
136
137     # step 2: figure out the new/changed files in ${vdir} vs. ${vref} and compute ${vdir}.changes
138     rsync -anv ${vdir}/ ${vref}/ > ${vdir}.changes
139     linecount=$(wc -l ${vdir}.changes | awk ' { print $1 } ')
140     let headcount=$linecount-3
141     let tailcount=$headcount-1
142     # get rid of the last 3 lines of the rsync output
143     head -${headcount} ${vdir}.changes > ${vdir}.changes.1
144     # get rid of the first line of the rsync output
145     tail -${tailcount} ${vdir}.changes.1 > ${vdir}.changes.2
146     # post process rsync output to get rid of symbolic link embellish output
147     awk ' { print $1 } ' ${vdir}.changes.2 > ${vdir}.changes
148     rm -f ${vdir}.changes.*
149
150     # step 3: create the ${vdir} with just the list given in ${vdir}.changes 
151     install -d -m 755 ${vdir}-tmp/
152     rm -rf ${vdir}-tmp/*
153     (cd ${vdir} && cpio -m -d -u -p ${vdir}-tmp < ${vdir}.changes)
154     rm -rf ${vdir}
155     rm -f  ${vdir}.changes
156     mv ${vdir}-tmp ${vdir}
157
158     # cleanup yum remainings
159     rm -rf ${vdir}/build ${vdir}/longbuildroot
160
161     echo " * --------DONE BUILDING system sliceimage ${NAME}: $(date)"
162 done
163
164 # search sliceimage.post in config.<pldistro> or in config.planetlab otherwise
165 postfile=$(pl_locateDistroFile ../build/ ${pldistro} sliceimage.post)
166
167 [ -f $postfile ] && /bin/bash $postfile ${vref} || :
168
169 # fix sudoers config
170 [ -f ${vref}/etc/sudoers ] && echo -e "\nDefaults\tlogfile=/var/log/sudo\n" >> ${vref}/etc/sudoers
171
172 # cleanup yum remainings
173 rm -rf ${vref}/build ${vref}/longbuildroot
174
175 exit 0