85d7c3986447803117b9e3dcbdb1ea9eda0a4c95
[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     blaze-builder==0.3.1.1 \
165     network==2.3 \
166     regex-pcre==0.94.2 \
167     hinotify==0.3.2 \
168     hslogger==1.1.4 \
169     attoparsec==0.10.1.1\
170     quickcheck==2.5.1.1 \
171     crypto==4.2.4 \
172     monadcatchio-transformers==0.2.2.0 \
173     mtl==2.0.1.0 \
174     hashable==1.1.2.0 \
175     case-insensitive==0.3 \
176     parsec==3.0.1 \
177     network==2.3 \
178     snap-server==0.8.1 \
179     text==0.11.3.0 \
180     vector==0.9.1 \
181     json==0.4.4
182
183 #Python development tools
184 in_chroot -- \
185   $APT_INSTALL pandoc python-epydoc graphviz
186
187 in_chroot -- \
188   easy_install sphinx==1.1.3 \
189                logilab-common \
190                logilab-astng==0.23.1 \
191                pylint==0.25.1 \
192                pep8==1.2 \
193                coverage
194
195 #Haskell development tools
196 in_chroot -- \
197   cabal install --global \
198     hunit==1.2.5.2 \
199     happy==1.18.10 \
200     hlint==1.8.34 \
201     hscolour==1.20.3 \
202     temporary==1.1.2.3 \
203     test-framework==0.6.1 \
204     test-framework-hunit==0.2.7 \
205     test-framework-quickcheck2==0.2.12.3
206
207 in_chroot -- \
208   cabal install --global shelltestrunner
209
210 #Tools for creating debian packages
211 in_chroot -- \
212   $APT_INSTALL python-docutils debhelper quilt
213
214 #Set default editor
215 in_chroot -- \
216   update-alternatives --set editor $ALTERNATIVE_EDITOR
217
218 rm -f $COMP_FILEPATH
219 echo "Creating compressed schroot image..."
220 cd $CHDIR
221 tar czf $COMP_FILEPATH ./*
222 cd $ROOT
223
224 rm -rf $CHDIR
225 rm -f $TEMP_CHROOT_CONF
226 rm -rf $TEMP_DATA_DIR
227
228 echo "Chroot created. In order to run it:"
229 echo " * Copy the file $FINAL_CHROOT_CONF to $CONF_DIR/$FINAL_CHROOT_CONF"
230 echo " * Copy the file $COMP_FILEPATH to $CHROOT_DIR/$COMP_FILENAME"
231
232 echo "Then run \"schroot -c $CHROOTNAME\""