c65b5ff71800420e9ad32e5544c8a7dde0675591
[ganeti-github.git] / devel / build_chroot
1 #!/bin/bash
2 #Configuration
3 : ${ARCH:=amd64}
4 : ${DIST_RELEASE:=squeeze}
5 : ${CONF_DIR:=/etc/schroot/chroot.d}
6 : ${CHROOT_DIR:=/srv/chroot}
7 : ${ALTERNATIVE_EDITOR:=/usr/bin/vim.basic}
8 # The value of DATA_DIR is read as well from the environment.
9
10 #Automatically generated variables
11 CHROOTNAME=$DIST_RELEASE-$ARCH
12 CHNAME=building_$CHROOTNAME
13 TEMP_CHROOT_CONF=$CONF_DIR/$CHNAME.conf
14 FINAL_CHROOT_CONF=$CHROOTNAME.conf
15 ROOT=`pwd`
16 CHDIR=$ROOT/$CHNAME
17 USER=`whoami`
18 COMP_FILENAME=$CHROOTNAME.tar.gz
19 COMP_FILEPATH=$ROOT/$COMP_FILENAME
20 TEMP_DATA_DIR=`mktemp -d`
21 ACTUAL_DATA_DIR=$DATA_DIR
22 ACTUAL_DATA_DIR=${ACTUAL_DATA_DIR:-$TEMP_DATA_DIR}
23
24 #Runnability checks
25 if [ $USER != 'root' ]
26 then
27   echo "This script requires root permissions to run"
28   exit
29 fi
30
31 if [ -f $TEMP_CHROOT_CONF ]
32 then
33   echo "The configuration file name for the temporary chroot"
34   echo "  $TEMP_CHROOT_CONF"
35   echo "already exists."
36   echo "Remove it or change the CHNAME value in the script."
37   exit
38 fi
39
40 #Create configuration dir and files if they do not exist
41 if [ ! -d $ACTUAL_DATA_DIR ]
42 then
43   mkdir $ACTUAL_DATA_DIR
44   echo "The data directory"
45   echo "  $ACTUAL_DATA_DIR"
46   echo "has been created."
47 fi
48
49 if [ ! -f $ACTUAL_DATA_DIR/final.schroot.conf.in ]
50 then
51   cat <<END >$ACTUAL_DATA_DIR/final.schroot.conf.in
52 [${CHROOTNAME}]
53 description=Debian ${DIST_RELEASE} ${ARCH}
54 groups=src
55 source-root-groups=root
56 type=file
57 file=${CHROOT_DIR}/${COMP_FILENAME}
58 END
59   echo "The file"
60   echo " $ACTUAL_DATA_DIR/final.schroot.conf.in"
61   echo "has been created with default configurations."
62 fi
63
64 if [ ! -f $ACTUAL_DATA_DIR/temp.schroot.conf.in ]
65 then
66   cat <<END >$ACTUAL_DATA_DIR/temp.schroot.conf.in
67 [${CHNAME}]
68 description=Debian ${DIST_RELEASE} ${ARCH}
69 directory=${CHDIR}
70 groups=src
71 users=root
72 type=directory
73 END
74   echo "The file"
75   echo " $ACTUAL_DATA_DIR/temp.schroot.conf.in"
76   echo "has been created with default configurations."
77 fi
78
79 #Stop on errors
80 set -e
81
82 #Cleanup
83 rm -rf $CHDIR
84 mkdir $CHDIR
85
86 #Install tools for building chroots
87 apt-get install -y schroot debootstrap
88
89 shopt -s expand_aliases
90 alias in_chroot='schroot -c $CHNAME -d / '
91 function subst_variables {
92   sed \
93     -e "s/\${ARCH}/$ARCH/" \
94     -e "s*\${CHDIR}*$CHDIR*" \
95     -e "s/\${CHNAME}/$CHNAME/" \
96     -e "s/\${CHROOTNAME}/$CHROOTNAME/" \
97     -e "s*\${CHROOT_DIR}*$CHROOT_DIR*" \
98     -e "s/\${COMP_FILENAME}/$COMP_FILENAME/" \
99     -e "s/\${DIST_RELEASE}/$DIST_RELEASE/" $@
100 }
101
102 #Generate chroot configurations
103 cat $ACTUAL_DATA_DIR/temp.schroot.conf.in | subst_variables > $TEMP_CHROOT_CONF
104 cat $ACTUAL_DATA_DIR/final.schroot.conf.in | subst_variables > $FINAL_CHROOT_CONF
105
106 #Install the base system
107 debootstrap --arch $ARCH $DIST_RELEASE $CHDIR
108
109 APT_INSTALL="apt-get install -y --no-install-recommends"
110
111 echo "deb http://backports.debian.org/debian-backports" \
112      "$DIST_RELEASE-backports main contrib non-free" \
113      > $CHDIR/etc/apt/sources.list.d/backports.list
114
115 #Install all the packages
116 in_chroot -- \
117   apt-get update
118
119 #Install selected packages from backports
120 in_chroot -- \
121   apt-get -y --no-install-recommends -t squeeze-backports install \
122     git \
123     git-email \
124     vim
125
126 in_chroot -- \
127   $APT_INSTALL python-setuptools build-essential python-dev sudo automake \
128                fakeroot rsync locales less
129
130 echo "en_US.UTF-8 UTF-8" >> $CHDIR/etc/locale.gen
131
132 in_chroot -- \
133   locale-gen
134
135 in_chroot -- \
136   $APT_INSTALL lvm2 ssh bridge-utils iproute iputils-arping \
137                ndisc6 python python-pyopenssl openssl \
138                python-pyparsing python-simplejson \
139                python-pyinotify python-pycurl python-yaml socat fping
140
141 in_chroot -- \
142   $APT_INSTALL python-paramiko qemu-utils
143
144 in_chroot -- \
145   easy_install affinity bitarray ipaddr
146
147 #Haskell packages
148 in_chroot -- \
149   $APT_INSTALL ghc6 \
150                libghc6-parallel-dev libghc6-deepseq-dev \
151                libghc6-curl-dev
152
153 in_chroot -- \
154   $APT_INSTALL cabal-install
155
156 in_chroot -- \
157   cabal update
158
159 in_chroot -- \
160   $APT_INSTALL libpcre3-dev
161
162 in_chroot -- \
163   cabal install --global \
164     network==2.3 \
165     regex-pcre==0.94.2 \
166     hinotify==0.3.2 \
167     hslogger==1.1.4 \
168     attoparsec==0.10.1.1\
169     quickcheck==2.5.1.1 \
170     crypto==4.2.4 \
171     monadcatchio-transformers==0.2.2.0 \
172     mtl==2.0.1.0 \
173     hashable==1.1.2.0 \
174     case-insensitive==0.3 \
175     parsec==3.0.1 \
176     network==2.3 \
177     snap-server==0.8.1 \
178     text==0.11.3.0 \
179     vector==0.9.1 \
180     json==0.4.4
181
182 #Python development tools
183 in_chroot -- \
184   $APT_INSTALL pandoc python-epydoc graphviz
185
186 in_chroot -- \
187   easy_install sphinx==1.1.3 \
188                logilab-common \
189                logilab-astng==0.23.1 \
190                pylint==0.25.1 \
191                pep8==1.2 \
192                coverage
193
194 #Haskell development tools
195 in_chroot -- \
196   cabal install --global \
197     hunit==1.2.5.2 \
198     happy==1.18.10 \
199     hlint==1.8.34 \
200     hscolour==1.20.3 \
201     temporary==1.1.2.3 \
202     test-framework==0.6.1 \
203     test-framework-hunit==0.2.7 \
204     test-framework-quickcheck2==0.2.12.3
205
206 in_chroot -- \
207   cabal install --global shelltestrunner
208
209 #Tools for creating debian packages
210 in_chroot -- \
211   $APT_INSTALL python-docutils debhelper quilt
212
213 #Set default editor
214 in_chroot -- \
215   update-alternatives --set editor $ALTERNATIVE_EDITOR
216
217 rm -f $COMP_FILEPATH
218 echo "Creating compressed schroot image..."
219 cd $CHDIR
220 tar czf $COMP_FILEPATH ./*
221 cd $ROOT
222
223 rm -rf $CHDIR
224 rm -f $TEMP_CHROOT_CONF
225 rm -rf $TEMP_DATA_DIR
226
227 echo "Chroot created. In order to run it:"
228 echo " * Copy the file $FINAL_CHROOT_CONF to $CONF_DIR/$FINAL_CHROOT_CONF"
229 echo " * Copy the file $COMP_FILEPATH to $CHROOT_DIR/$COMP_FILENAME"
230
231 echo "Then run \"schroot -c $CHROOTNAME\""