Add tools for building deb packages to build_chroot
[ganeti-local] / 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 alias subst_variables='sed \
92   -e "s/\${ARCH}/$ARCH/" \
93   -e "s*\${CHDIR}*$CHDIR*" \
94   -e "s/\${CHNAME}/$CHNAME/" \
95   -e "s/\${CHROOTNAME}/$CHROOTNAME/" \
96   -e "s*\${CHROOT_DIR}*$CHROOT_DIR*" \
97   -e "s/\${COMP_FILENAME}/$COMP_FILENAME/" \
98   -e "s/\${DIST_RELEASE}/$DIST_RELEASE/"'
99
100 #Generate chroot configurations
101 cat $ACTUAL_DATA_DIR/temp.schroot.conf.in | subst_variables > $TEMP_CHROOT_CONF
102 cat $ACTUAL_DATA_DIR/final.schroot.conf.in | subst_variables > $FINAL_CHROOT_CONF
103
104 #Install the base system
105 debootstrap --arch $ARCH $DIST_RELEASE $CHDIR
106
107 APT_INSTALL="apt-get install -y --no-install-recommends"
108
109 echo "deb http://backports.debian.org/debian-backports" \
110      "$DIST_RELEASE-backports main contrib non-free" \
111      > $CHDIR/etc/apt/sources.list.d/backports.list
112
113 #Install all the packages
114 in_chroot -- \
115   apt-get update
116
117 #Install selected packages from backports
118 in_chroot -- \
119   apt-get -y --no-install-recommends -t squeeze-backports install \
120     git \
121     vim
122
123 in_chroot -- \
124   $APT_INSTALL python-setuptools build-essential python-dev sudo automake \
125                fakeroot rsync locales less
126
127 echo "en_US.UTF-8 UTF-8" >> $CHDIR/etc/locale.gen
128
129 in_chroot -- \
130   locale-gen
131
132 in_chroot -- \
133   $APT_INSTALL lvm2 ssh bridge-utils iproute iputils-arping \
134                ndisc6 python python-pyopenssl openssl \
135                python-pyparsing python-simplejson \
136                python-pyinotify python-pycurl python-yaml socat fping
137
138 in_chroot -- \
139   $APT_INSTALL python-paramiko qemu-utils
140
141 in_chroot -- \
142   easy_install affinity bitarray ipaddr
143
144 #Haskell packages
145 in_chroot -- \
146   $APT_INSTALL ghc6 \
147                libghc6-parallel-dev libghc6-deepseq-dev \
148                libghc6-curl-dev
149
150 in_chroot -- \
151   $APT_INSTALL cabal-install
152
153 in_chroot -- \
154   cabal update
155
156 in_chroot -- \
157   $APT_INSTALL libpcre3-dev
158
159 in_chroot -- \
160   cabal install --global \
161     network==2.3 \
162     regex-pcre==0.94.2 \
163     hinotify==0.3.2 \
164     hslogger==1.1.4 \
165     attoparsec==0.10.1.1\
166     quickcheck==2.5.1.1 \
167     crypto==4.2.4 \
168     monadcatchio-transformers==0.2.2.0 \
169     mtl==2.0.1.0 \
170     hashable==1.1.2.0 \
171     case-insensitive==0.3 \
172     parsec==3.0.1 \
173     network==2.3 \
174     snap-server==0.8.1 \
175     text==0.11.3.0 \
176     vector==0.9.1 \
177     json==0.4.4
178
179 #Python development tools
180 in_chroot -- \
181   $APT_INSTALL pandoc python-epydoc graphviz
182
183 in_chroot -- \
184   easy_install sphinx==1.1.3 \
185                logilab-common \
186                logilab-astng==0.23.1 \
187                pylint==0.25.1 \
188                pep8==1.2 \
189                coverage
190
191 #Haskell development tools
192 in_chroot -- \
193   cabal install --global \
194     hunit==1.2.5.2 \
195     happy==1.18.10 \
196     hlint==1.8.34 \
197     hscolour==1.20.3 \
198     temporary==1.1.2.3 \
199     test-framework==0.6.1 \
200     test-framework-hunit==0.2.7 \
201     test-framework-quickcheck2==0.2.12.3
202
203 in_chroot -- \
204   cabal install --global shelltestrunner
205
206 #Tools for creating debian packages
207 in_chroot -- \
208   apt-get install python-docutils debhelper quilt
209
210 #Set default editor
211 in_chroot -- \
212   update-alternatives --set editor $ALTERNATIVE_EDITOR
213
214 rm -f $COMP_FILEPATH
215 echo "Creating compressed schroot image..."
216 cd $CHDIR
217 tar czf $COMP_FILEPATH ./*
218 cd $ROOT
219
220 rm -rf $CHDIR
221 rm -f $TEMP_CHROOT_CONF
222 rm -rf $TEMP_DATA_DIR
223
224 echo "Chroot created. In order to run it:"
225 echo " * Copy the file $FINAL_CHROOT_CONF to $CONF_DIR/$FINAL_CHROOT_CONF"
226 echo " * Copy the file $COMP_FILEPATH to $CHROOT_DIR/$COMP_FILENAME"
227
228 echo "Then run \"schroot -c $CHROOTNAME\""