blob: c441835cf783cde668131c6adae2f5feacc4f809 [file] [log] [blame]
Anthony Young4f279222011-09-13 21:51:28 -07001#!/usr/bin/env bash
Jesse Andrews18ebd862011-09-19 14:23:42 -07002
Anthony Young2f140202011-09-26 13:02:40 -07003# Source params
4source ./stackrc
5
Anthony Young84064da2011-09-26 16:19:50 -07006# Store cwd
7CWD=`pwd`
8
Anthony Young72d69632011-09-12 21:09:55 -07009# Configurable params
10BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -070011CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -070012CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
13CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
14CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
15CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young77dbb072011-09-14 00:49:39 -070016NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070017COPYENV=${COPYENV:-1}
Anthony Younge8fed482011-09-26 19:50:43 -070018DEST=${DEST:-/opt/stack}
Anthony Young72d69632011-09-12 21:09:55 -070019
Anthony Young1c364642011-09-13 20:21:42 -070020# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
21STACKSH_PARAMS=${STACKSH_PARAMS:-}
22
Anthony Younga34b6952011-09-26 15:24:59 -070023# Option to use the version of devstack on which we are currently working
24USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
25
Anthony Young936c9282011-09-13 23:36:43 -070026# Warn users who aren't on natty
27if ! grep -q natty /etc/lsb-release; then
28 echo "WARNING: this script has only been tested on natty"
29fi
30
Anthony Young190321e2011-09-13 23:21:29 -070031# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070032apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070033
34# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070035if ! which cgdelete | grep -q cgdelete; then
termiebd550ed2011-09-28 16:54:25 -050036 apt-get install -y g++ bison flex libpam0g-dev make
37 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 -070038 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
39 cd libcgroup-0.37.1
40 ./configure
41 make install
Dean Troyer62a6deb2011-09-21 20:06:01 -050042 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070043fi
44
Anthony Young60534962011-09-13 10:40:04 -070045# Create lxc configuration
46LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070047cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070048lxc.network.type = veth
49lxc.network.link = $BRIDGE
50lxc.network.flags = up
51lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070052# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070053lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070054EOF
55
Anthony Young60534962011-09-13 10:40:04 -070056# Shutdown any existing container
57lxc-stop -n $CONTAINER
58
Anthony Young4f279222011-09-13 21:51:28 -070059# This kills zombie containers
60if [ -d /cgroup/$CONTAINER ]; then
61 cgdelete -r cpu,net_cls:$CONTAINER
62fi
Anthony Young60534962011-09-13 10:40:04 -070063
Anthony Young2f140202011-09-26 13:02:40 -070064# git clone only if directory doesn't exist already. Since ``DEST`` might not
65# be owned by the installation user, we create the directory and change the
66# ownership to the proper user.
67function git_clone {
68 if [ ! -d $2 ]; then
69 sudo mkdir $2
70 sudo chown `whoami` $2
71 git clone $1 $2
72 cd $2
73 # This checkout syntax works for both branches and tags
74 git checkout $3
75 fi
76}
Jesse Andrews18ebd862011-09-19 14:23:42 -070077
Anthony Youngbeab6392011-09-28 15:12:18 -070078# Location of the base image directory
Anthony Young60534962011-09-13 10:40:04 -070079CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Youngbeab6392011-09-28 15:12:18 -070080
81# Provide option to do totally clean install
82if [ "$CLEAR_LXC_CACHE" = "1" ]; then
83 rm -rf $CACHEDIR
84fi
85
86# Warm the base image on first install
87if [ ! -f $CACHEDIR/bootstrapped ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -070088 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
89 # lazy and doesn't do anything if a container already exists)
90 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -070091 # trigger the initial debootstrap
92 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
93 chroot $CACHEDIR apt-get update
Anthony Young3d6aab92011-09-28 13:21:46 -070094 chroot $CACHEDIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server|munin-node)"`
Jesse Andrews4d282182011-09-16 11:27:43 -070095 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Youngbeab6392011-09-28 15:12:18 -070096 touch $CACHEDIR/bootstrapped
Anthony Young60534962011-09-13 10:40:04 -070097fi
98
Anthony Younge8fed482011-09-26 19:50:43 -070099# Clean out code repos if directed to do so
100if [ "$CLEAN" = "1" ]; then
Anthony Young4f10de82011-09-26 20:03:40 -0700101 rm -rf $CACHEDIR/$DEST
Anthony Younge8fed482011-09-26 19:50:43 -0700102fi
103
Anthony Young303233e2011-09-26 13:12:57 -0700104# Cache openstack code
Anthony Younge8fed482011-09-26 19:50:43 -0700105mkdir -p $CACHEDIR/$DEST
106git_clone $NOVA_REPO $CACHEDIR/$DEST/nova $NOVA_BRANCH
107git_clone $GLANCE_REPO $CACHEDIR/$DEST/glance $GLANCE_BRANCH
108git_clone $KEYSTONE_REPO $CACHEDIR/$DESTkeystone $KEYSTONE_BRANCH
109git_clone $NOVNC_REPO $CACHEDIR/$DEST/novnc $NOVNC_BRANCH
110git_clone $DASH_REPO $CACHEDIR/$DEST/dash $DASH_BRANCH $DASH_TAG
111git_clone $NIXON_REPO $CACHEDIR/$DEST/nixon $NIXON_BRANCH
112git_clone $NOVACLIENT_REPO $CACHEDIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
113git_clone $OPENSTACKX_REPO $CACHEDIR/$DEST/openstackx $OPENSTACKX_BRANCH
114git_clone $MUNIN_REPO $CACHEDIR/$DEST/openstack-munin $MUNIN_BRANCH
Anthony Young303233e2011-09-26 13:12:57 -0700115
Anthony Younga34b6952011-09-26 15:24:59 -0700116# Use this version of devstack?
117if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700118 rm -rf $CACHEDIR/$DEST/devstack
119 cp -pr $CWD $CACHEDIR/$DEST/devstack
Anthony Younga34b6952011-09-26 15:24:59 -0700120fi
121
Anthony Young60534962011-09-13 10:40:04 -0700122# Destroy the old container
123lxc-destroy -n $CONTAINER
124
Anthony Young23761c32011-09-16 14:54:20 -0700125# If this call is to TERMINATE the container then exit
126if [ "$TERMINATE" = "1" ]; then
127 exit
128fi
129
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700130# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -0700131lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -0700132
Anthony Youngf9998ab2011-09-13 10:43:44 -0700133# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -0700134ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
135
termiebd550ed2011-09-28 16:54:25 -0500136# Create a stack user that is a member of the libvirtd group so that stack
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700137# is able to interact with libvirt.
138chroot $ROOTFS groupadd libvirtd
Anthony Younge8fed482011-09-26 19:50:43 -0700139chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700140
141# a simple password - pass
142echo stack:pass | chroot $ROOTFS chpasswd
143
termiebd550ed2011-09-28 16:54:25 -0500144# and has sudo ability (in the future this should be limited to only what
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700145# stack requires)
146echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
147
Dean Troyer62a6deb2011-09-21 20:06:01 -0500148# Copy kernel modules
149mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
150cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
151cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
152
Anthony Young34c47022011-09-13 22:14:37 -0700153# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700154function cp_it {
155 if [ -e $1 ] || [ -d $1 ]; then
156 cp -pr $1 $2
157 fi
158}
159
Anthony Youngb3c04542011-09-13 01:28:18 -0700160# Copy over your ssh keys and env if desired
161if [ "$COPYENV" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700162 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
163 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
164 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
165 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
166 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700167fi
168
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700169# Make our ip address hostnames look nice at the command prompt
Anthony Younge8fed482011-09-26 19:50:43 -0700170echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700171echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
172
Anthony Youngadbe9c12011-09-26 19:58:49 -0700173# Give stack ownership over $DEST so it may do the work needed
174chroot $ROOTFS chown -R stack $DEST
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700175
Anthony Young72d69632011-09-12 21:09:55 -0700176# Configure instance network
177INTERFACES=$ROOTFS/etc/network/interfaces
178cat > $INTERFACES <<EOF
179auto lo
180iface lo inet loopback
181
182auto eth0
183iface eth0 inet static
184 address $CONTAINER_IP
185 netmask $CONTAINER_NETMASK
186 gateway $CONTAINER_GATEWAY
187EOF
188
Anthony Young1c364642011-09-13 20:21:42 -0700189# Configure the runner
Anthony Younge8fed482011-09-26 19:50:43 -0700190RUN_SH=$ROOTFS/$DEST/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700191cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700192#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700193# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700194echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700195sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700196
Anthony Young927a6562011-09-14 01:58:01 -0700197# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700198killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700199
Anthony Young99003e72011-09-13 02:05:12 -0700200# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700201sudo apt-get update
202sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngadbe9c12011-09-26 19:58:49 -0700203if [ ! -d "$DEST/devstack" ]; then
204 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700205fi
Anthony Youngadbe9c12011-09-26 19:58:49 -0700206cd $DEST/devstack && $STACKSH_PARAMS ./stack.sh > /$DEST/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700207EOF
208
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700209# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700210chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700211
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700212# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700213RC_LOCAL=$ROOTFS/etc/rc.local
214cat > $RC_LOCAL <<EOF
215#!/bin/sh -e
Anthony Youngadbe9c12011-09-26 19:58:49 -0700216su -c "$DEST/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700217EOF
218
Anthony Young72d69632011-09-12 21:09:55 -0700219# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700220if ! mount | grep -q cgroup; then
221 mkdir -p /cgroup
222 mount none -t cgroup /cgroup
223fi
Anthony Young72d69632011-09-12 21:09:55 -0700224
225# Start our container
226lxc-start -d -n $CONTAINER