blob: df9e32e9887e81462cc48cee7398bf82c051d299 [file] [log] [blame]
Anthony Young4f279222011-09-13 21:51:28 -07001#!/usr/bin/env bash
Jesse Andrews18ebd862011-09-19 14:23:42 -07002
Jesse Andrews46ad2de2011-09-28 18:29:50 -07003# Sanity check
4if [ "$EUID" -ne "0" ]; then
5 echo "This script must be run with root privileges."
6 exit 1
7fi
8
Anthony Young9ff71ac2011-10-14 15:02:20 -07009# Keep track of ubuntu version
10UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
Jesse Andrews46ad2de2011-09-28 18:29:50 -070011
Anthony Younge8efef72011-10-14 12:00:50 -070012# Move to top devstack dir
13cd ..
14
Anthony Youngcf145b72011-10-13 15:07:36 -070015# Abort if localrc is not set
16if [ ! -e ./localrc ]; then
17 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
18 echo "See stack.sh for required passwords."
19 exit 1
20fi
21
Anthony Young2f140202011-09-26 13:02:40 -070022# Source params
23source ./stackrc
24
Anthony Young84064da2011-09-26 16:19:50 -070025# Store cwd
26CWD=`pwd`
27
Anthony Young72d69632011-09-12 21:09:55 -070028# Configurable params
29BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -070030CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -070031CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
32CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
33CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
34CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Youngae8bc122011-10-17 10:38:05 -070035NAMESERVER=${NAMESERVER:-`cat /etc/resolv.conf | grep nameserver | head -1 | cut -d " " -f2`}
Anthony Young72d69632011-09-12 21:09:55 -070036COPYENV=${COPYENV:-1}
Anthony Younge8fed482011-09-26 19:50:43 -070037DEST=${DEST:-/opt/stack}
Anthony Young25369c92011-10-14 16:50:27 -070038WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
Anthony Young72d69632011-09-12 21:09:55 -070039
Anthony Young1c364642011-09-13 20:21:42 -070040# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
41STACKSH_PARAMS=${STACKSH_PARAMS:-}
42
Anthony Younga34b6952011-09-26 15:24:59 -070043# Option to use the version of devstack on which we are currently working
44USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
45
Anthony Young936c9282011-09-13 23:36:43 -070046
Anthony Young190321e2011-09-13 23:21:29 -070047# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070048apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070049
50# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070051if ! which cgdelete | grep -q cgdelete; then
termiebd550ed2011-09-28 16:54:25 -050052 apt-get install -y g++ bison flex libpam0g-dev make
53 wget http://sourceforge.net/projects/libcg/files/libcgroup/v0.37.1/libcgroup-0.37.1.tar.bz2/download -O /tmp/libcgroup-0.37.1.tar.bz2
Anthony Young190321e2011-09-13 23:21:29 -070054 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
55 cd libcgroup-0.37.1
56 ./configure
57 make install
Dean Troyer62a6deb2011-09-21 20:06:01 -050058 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070059fi
60
Anthony Young60534962011-09-13 10:40:04 -070061# Create lxc configuration
62LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070063cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070064lxc.network.type = veth
65lxc.network.link = $BRIDGE
66lxc.network.flags = up
67lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070068# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070069lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070070EOF
71
Anthony Young60534962011-09-13 10:40:04 -070072# Shutdown any existing container
73lxc-stop -n $CONTAINER
74
Anthony Young4f279222011-09-13 21:51:28 -070075# This kills zombie containers
76if [ -d /cgroup/$CONTAINER ]; then
77 cgdelete -r cpu,net_cls:$CONTAINER
78fi
Anthony Young60534962011-09-13 10:40:04 -070079
Anthony Young2f140202011-09-26 13:02:40 -070080# git clone only if directory doesn't exist already. Since ``DEST`` might not
81# be owned by the installation user, we create the directory and change the
82# ownership to the proper user.
83function git_clone {
84 if [ ! -d $2 ]; then
85 sudo mkdir $2
86 sudo chown `whoami` $2
87 git clone $1 $2
88 cd $2
89 # This checkout syntax works for both branches and tags
90 git checkout $3
91 fi
92}
Jesse Andrews18ebd862011-09-19 14:23:42 -070093
Anthony Young9ff71ac2011-10-14 15:02:20 -070094# Helper to create the container
95function create_lxc {
96 if [ "natty" = "$UBUNTU_VERSION" ]; then
97 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
98 else
99 lxc-create -n $CONTAINER -t ubuntu -f $LXC_CONF
100 fi
101}
102
Anthony Youngbeab6392011-09-28 15:12:18 -0700103# Location of the base image directory
Anthony Young9ff71ac2011-10-14 15:02:20 -0700104if [ "natty" = "$UBUNTU_VERSION" ]; then
105 CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
106else
107 CACHEDIR=/var/cache/lxc/oneiric/rootfs-amd64
108fi
Anthony Youngbeab6392011-09-28 15:12:18 -0700109
110# Provide option to do totally clean install
111if [ "$CLEAR_LXC_CACHE" = "1" ]; then
112 rm -rf $CACHEDIR
113fi
114
115# Warm the base image on first install
116if [ ! -f $CACHEDIR/bootstrapped ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -0700117 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
118 # lazy and doesn't do anything if a container already exists)
119 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -0700120 # trigger the initial debootstrap
Anthony Young9ff71ac2011-10-14 15:02:20 -0700121 create_lxc
Anthony Youngbeab6392011-09-28 15:12:18 -0700122 touch $CACHEDIR/bootstrapped
Anthony Young60534962011-09-13 10:40:04 -0700123fi
124
Anthony Young6b9d6da2011-10-17 10:12:22 -0700125# Make sure that base requirements are installed
126chroot $CACHEDIR apt-get update
127chroot $CACHEDIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
Anthony Young028cad12011-10-17 14:10:42 -0700128chroot $CACHEDIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
Anthony Young6b9d6da2011-10-17 10:12:22 -0700129chroot $CACHEDIR pip install `cat files/pips/*`
130
Anthony Younge8fed482011-09-26 19:50:43 -0700131# Clean out code repos if directed to do so
132if [ "$CLEAN" = "1" ]; then
Anthony Young4f10de82011-09-26 20:03:40 -0700133 rm -rf $CACHEDIR/$DEST
Anthony Younge8fed482011-09-26 19:50:43 -0700134fi
135
Anthony Young303233e2011-09-26 13:12:57 -0700136# Cache openstack code
Anthony Younge8fed482011-09-26 19:50:43 -0700137mkdir -p $CACHEDIR/$DEST
138git_clone $NOVA_REPO $CACHEDIR/$DEST/nova $NOVA_BRANCH
139git_clone $GLANCE_REPO $CACHEDIR/$DEST/glance $GLANCE_BRANCH
140git_clone $KEYSTONE_REPO $CACHEDIR/$DESTkeystone $KEYSTONE_BRANCH
Anthony Young583bad02011-10-18 08:22:30 -0700141git_clone $NOVNC_REPO $CACHEDIR/$DEST/noVNC $NOVNC_BRANCH
Anthony Younge8fed482011-09-26 19:50:43 -0700142git_clone $DASH_REPO $CACHEDIR/$DEST/dash $DASH_BRANCH $DASH_TAG
Anthony Younge8fed482011-09-26 19:50:43 -0700143git_clone $NOVACLIENT_REPO $CACHEDIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
144git_clone $OPENSTACKX_REPO $CACHEDIR/$DEST/openstackx $OPENSTACKX_BRANCH
Anthony Young028cad12011-10-17 14:10:42 -0700145git_clone $KEYSTONE_REPO $CACHEDIR/$DEST/keystone $KEYSTONE_BRANCH
146git_clone $NOVNC_REPO $CACHEDIR/$DEST/novnc $NOVNC_BRANCH
Anthony Young303233e2011-09-26 13:12:57 -0700147
Anthony Younga34b6952011-09-26 15:24:59 -0700148# Use this version of devstack?
149if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700150 rm -rf $CACHEDIR/$DEST/devstack
151 cp -pr $CWD $CACHEDIR/$DEST/devstack
Anthony Younga34b6952011-09-26 15:24:59 -0700152fi
153
Anthony Young60534962011-09-13 10:40:04 -0700154# Destroy the old container
155lxc-destroy -n $CONTAINER
156
Anthony Young23761c32011-09-16 14:54:20 -0700157# If this call is to TERMINATE the container then exit
158if [ "$TERMINATE" = "1" ]; then
159 exit
160fi
161
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700162# Create the container
Anthony Young9ff71ac2011-10-14 15:02:20 -0700163create_lxc
Anthony Youngb3c04542011-09-13 01:28:18 -0700164
Anthony Youngf9998ab2011-09-13 10:43:44 -0700165# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -0700166ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
167
termiebd550ed2011-09-28 16:54:25 -0500168# Create a stack user that is a member of the libvirtd group so that stack
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700169# is able to interact with libvirt.
170chroot $ROOTFS groupadd libvirtd
Anthony Younge8fed482011-09-26 19:50:43 -0700171chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700172
173# a simple password - pass
174echo stack:pass | chroot $ROOTFS chpasswd
175
termiebd550ed2011-09-28 16:54:25 -0500176# and has sudo ability (in the future this should be limited to only what
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700177# stack requires)
178echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
179
Dean Troyer62a6deb2011-09-21 20:06:01 -0500180# Copy kernel modules
181mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
182cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
183cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
184
Anthony Young34c47022011-09-13 22:14:37 -0700185# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700186function cp_it {
187 if [ -e $1 ] || [ -d $1 ]; then
termie1c7f0c92011-09-28 17:09:00 -0500188 cp -pRL $1 $2
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700189 fi
190}
191
Anthony Youngb3c04542011-09-13 01:28:18 -0700192# Copy over your ssh keys and env if desired
193if [ "$COPYENV" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700194 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
195 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
196 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
197 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
198 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700199fi
200
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700201# Make our ip address hostnames look nice at the command prompt
Anthony Younge8fed482011-09-26 19:50:43 -0700202echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700203echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
204
Anthony Youngadbe9c12011-09-26 19:58:49 -0700205# Give stack ownership over $DEST so it may do the work needed
206chroot $ROOTFS chown -R stack $DEST
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700207
Anthony Young72d69632011-09-12 21:09:55 -0700208# Configure instance network
209INTERFACES=$ROOTFS/etc/network/interfaces
210cat > $INTERFACES <<EOF
211auto lo
212iface lo inet loopback
213
214auto eth0
215iface eth0 inet static
216 address $CONTAINER_IP
217 netmask $CONTAINER_NETMASK
218 gateway $CONTAINER_GATEWAY
219EOF
220
Anthony Young1c364642011-09-13 20:21:42 -0700221# Configure the runner
Anthony Younge8fed482011-09-26 19:50:43 -0700222RUN_SH=$ROOTFS/$DEST/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700223cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700224#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700225# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700226echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young9ff71ac2011-10-14 15:02:20 -0700227# Make there is a default route - needed for natty
228if ! route | grep -q default; then
229 sudo ip route add default via $CONTAINER_GATEWAY
230fi
Anthony Young72d69632011-09-12 21:09:55 -0700231sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700232
Anthony Young927a6562011-09-14 01:58:01 -0700233# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700234killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700235
Anthony Young99003e72011-09-13 02:05:12 -0700236# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700237sudo apt-get update
238sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngadbe9c12011-09-26 19:58:49 -0700239if [ ! -d "$DEST/devstack" ]; then
240 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700241fi
Anthony Young9ff71ac2011-10-14 15:02:20 -0700242cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
termie73774442011-09-28 19:02:28 -0500243echo >> /$DEST/run.sh.log
244echo >> /$DEST/run.sh.log
245echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700246EOF
247
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700248# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700249chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700250
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700251# Make runner launch on boot
Anthony Young9ff71ac2011-10-14 15:02:20 -0700252RC_LOCAL=$ROOTFS/etc/init.d/local
Anthony Young72d69632011-09-12 21:09:55 -0700253cat > $RC_LOCAL <<EOF
254#!/bin/sh -e
Anthony Youngadbe9c12011-09-26 19:58:49 -0700255su -c "$DEST/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700256EOF
Anthony Young9ff71ac2011-10-14 15:02:20 -0700257chmod +x $RC_LOCAL
258chroot $ROOTFS sudo update-rc.d local defaults 80
Anthony Young72d69632011-09-12 21:09:55 -0700259
Anthony Young72d69632011-09-12 21:09:55 -0700260# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700261if ! mount | grep -q cgroup; then
262 mkdir -p /cgroup
263 mount none -t cgroup /cgroup
264fi
Anthony Young72d69632011-09-12 21:09:55 -0700265
266# Start our container
267lxc-start -d -n $CONTAINER
termie73774442011-09-28 19:02:28 -0500268
Anthony Young25369c92011-10-14 16:50:27 -0700269if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
270 # Done creating the container, let's tail the log
271 echo
272 echo "============================================================="
273 echo " -- YAY! --"
274 echo "============================================================="
275 echo
276 echo "We're done creating the container, about to start tailing the"
277 echo "stack.sh log. It will take a second or two to start."
278 echo
279 echo "Just CTRL-C at any time to stop tailing."
termie73774442011-09-28 19:02:28 -0500280
Anthony Young25369c92011-10-14 16:50:27 -0700281 while [ ! -e "$ROOTFS/$DEST/run.sh.log" ]; do
282 sleep 1
283 done
termie73774442011-09-28 19:02:28 -0500284
Anthony Young25369c92011-10-14 16:50:27 -0700285 tail -F $ROOTFS/$DEST/run.sh.log &
286
287 TAIL_PID=$!
288
289 function kill_tail() {
Anthony Young04db1552011-10-17 09:40:45 -0700290 kill $TAIL_PID
Anthony Young25369c92011-10-14 16:50:27 -0700291 exit 1
292 }
293
294 # Let Ctrl-c kill tail and exit
295 trap kill_tail SIGINT
296
297 echo "Waiting stack.sh to finish..."
298 while ! cat $ROOTFS/$DEST/run.sh.log | grep -q 'All done' ; do
299 sleep 5
300 done
301
302 kill $TAIL_PID
303 echo ""
304 echo "Finished - Zip-a-dee Doo-dah!"
305fi