blob: 643da7e4be251a3a48e0bda2bcb7870999f3dc9d [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
9# Warn users who aren't on natty
10if ! grep -q natty /etc/lsb-release; then
11 echo "WARNING: this script has only been tested on natty"
12fi
13
Anthony Young2f140202011-09-26 13:02:40 -070014# Source params
15source ./stackrc
16
Anthony Young84064da2011-09-26 16:19:50 -070017# Store cwd
18CWD=`pwd`
19
Anthony Young72d69632011-09-12 21:09:55 -070020# Configurable params
21BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -070022CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -070023CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
24CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
25CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
26CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young77dbb072011-09-14 00:49:39 -070027NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070028COPYENV=${COPYENV:-1}
Anthony Younge8fed482011-09-26 19:50:43 -070029DEST=${DEST:-/opt/stack}
Anthony Young72d69632011-09-12 21:09:55 -070030
Anthony Young1c364642011-09-13 20:21:42 -070031# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
32STACKSH_PARAMS=${STACKSH_PARAMS:-}
33
Anthony Younga34b6952011-09-26 15:24:59 -070034# Option to use the version of devstack on which we are currently working
35USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
36
Anthony Young936c9282011-09-13 23:36:43 -070037
Anthony Young190321e2011-09-13 23:21:29 -070038# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070039apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070040
41# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070042if ! which cgdelete | grep -q cgdelete; then
termiebd550ed2011-09-28 16:54:25 -050043 apt-get install -y g++ bison flex libpam0g-dev make
44 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 -070045 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
46 cd libcgroup-0.37.1
47 ./configure
48 make install
Dean Troyer62a6deb2011-09-21 20:06:01 -050049 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070050fi
51
Anthony Young60534962011-09-13 10:40:04 -070052# Create lxc configuration
53LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070054cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070055lxc.network.type = veth
56lxc.network.link = $BRIDGE
57lxc.network.flags = up
58lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070059# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070060lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070061EOF
62
Anthony Young60534962011-09-13 10:40:04 -070063# Shutdown any existing container
64lxc-stop -n $CONTAINER
65
Anthony Young4f279222011-09-13 21:51:28 -070066# This kills zombie containers
67if [ -d /cgroup/$CONTAINER ]; then
68 cgdelete -r cpu,net_cls:$CONTAINER
69fi
Anthony Young60534962011-09-13 10:40:04 -070070
Anthony Young2f140202011-09-26 13:02:40 -070071# git clone only if directory doesn't exist already. Since ``DEST`` might not
72# be owned by the installation user, we create the directory and change the
73# ownership to the proper user.
74function git_clone {
75 if [ ! -d $2 ]; then
76 sudo mkdir $2
77 sudo chown `whoami` $2
78 git clone $1 $2
79 cd $2
80 # This checkout syntax works for both branches and tags
81 git checkout $3
82 fi
83}
Jesse Andrews18ebd862011-09-19 14:23:42 -070084
Anthony Youngbeab6392011-09-28 15:12:18 -070085# Location of the base image directory
Anthony Young60534962011-09-13 10:40:04 -070086CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Youngbeab6392011-09-28 15:12:18 -070087
88# Provide option to do totally clean install
89if [ "$CLEAR_LXC_CACHE" = "1" ]; then
90 rm -rf $CACHEDIR
91fi
92
93# Warm the base image on first install
94if [ ! -f $CACHEDIR/bootstrapped ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -070095 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
96 # lazy and doesn't do anything if a container already exists)
97 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -070098 # trigger the initial debootstrap
99 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
100 chroot $CACHEDIR apt-get update
Jesse Andrews9b6741e2011-10-02 10:01:00 -0700101 chroot $CACHEDIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
Jesse Andrews4d282182011-09-16 11:27:43 -0700102 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Youngbeab6392011-09-28 15:12:18 -0700103 touch $CACHEDIR/bootstrapped
Anthony Young60534962011-09-13 10:40:04 -0700104fi
105
Anthony Younge8fed482011-09-26 19:50:43 -0700106# Clean out code repos if directed to do so
107if [ "$CLEAN" = "1" ]; then
Anthony Young4f10de82011-09-26 20:03:40 -0700108 rm -rf $CACHEDIR/$DEST
Anthony Younge8fed482011-09-26 19:50:43 -0700109fi
110
Anthony Young303233e2011-09-26 13:12:57 -0700111# Cache openstack code
Anthony Younge8fed482011-09-26 19:50:43 -0700112mkdir -p $CACHEDIR/$DEST
113git_clone $NOVA_REPO $CACHEDIR/$DEST/nova $NOVA_BRANCH
114git_clone $GLANCE_REPO $CACHEDIR/$DEST/glance $GLANCE_BRANCH
115git_clone $KEYSTONE_REPO $CACHEDIR/$DESTkeystone $KEYSTONE_BRANCH
116git_clone $NOVNC_REPO $CACHEDIR/$DEST/novnc $NOVNC_BRANCH
117git_clone $DASH_REPO $CACHEDIR/$DEST/dash $DASH_BRANCH $DASH_TAG
Anthony Younge8fed482011-09-26 19:50:43 -0700118git_clone $NOVACLIENT_REPO $CACHEDIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
119git_clone $OPENSTACKX_REPO $CACHEDIR/$DEST/openstackx $OPENSTACKX_BRANCH
Anthony Young303233e2011-09-26 13:12:57 -0700120
Anthony Younga34b6952011-09-26 15:24:59 -0700121# Use this version of devstack?
122if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700123 rm -rf $CACHEDIR/$DEST/devstack
124 cp -pr $CWD $CACHEDIR/$DEST/devstack
Anthony Younga34b6952011-09-26 15:24:59 -0700125fi
126
Anthony Young60534962011-09-13 10:40:04 -0700127# Destroy the old container
128lxc-destroy -n $CONTAINER
129
Anthony Young23761c32011-09-16 14:54:20 -0700130# If this call is to TERMINATE the container then exit
131if [ "$TERMINATE" = "1" ]; then
132 exit
133fi
134
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700135# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -0700136lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -0700137
Anthony Youngf9998ab2011-09-13 10:43:44 -0700138# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -0700139ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
140
termiebd550ed2011-09-28 16:54:25 -0500141# Create a stack user that is a member of the libvirtd group so that stack
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700142# is able to interact with libvirt.
143chroot $ROOTFS groupadd libvirtd
Anthony Younge8fed482011-09-26 19:50:43 -0700144chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700145
146# a simple password - pass
147echo stack:pass | chroot $ROOTFS chpasswd
148
termiebd550ed2011-09-28 16:54:25 -0500149# and has sudo ability (in the future this should be limited to only what
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700150# stack requires)
151echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
152
Dean Troyer62a6deb2011-09-21 20:06:01 -0500153# Copy kernel modules
154mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
155cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
156cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
157
Anthony Young34c47022011-09-13 22:14:37 -0700158# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700159function cp_it {
160 if [ -e $1 ] || [ -d $1 ]; then
termie1c7f0c92011-09-28 17:09:00 -0500161 cp -pRL $1 $2
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700162 fi
163}
164
Anthony Youngb3c04542011-09-13 01:28:18 -0700165# Copy over your ssh keys and env if desired
166if [ "$COPYENV" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700167 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
168 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
169 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
170 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
171 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700172fi
173
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700174# Make our ip address hostnames look nice at the command prompt
Anthony Younge8fed482011-09-26 19:50:43 -0700175echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700176echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
177
Anthony Youngadbe9c12011-09-26 19:58:49 -0700178# Give stack ownership over $DEST so it may do the work needed
179chroot $ROOTFS chown -R stack $DEST
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700180
Anthony Young72d69632011-09-12 21:09:55 -0700181# Configure instance network
182INTERFACES=$ROOTFS/etc/network/interfaces
183cat > $INTERFACES <<EOF
184auto lo
185iface lo inet loopback
186
187auto eth0
188iface eth0 inet static
189 address $CONTAINER_IP
190 netmask $CONTAINER_NETMASK
191 gateway $CONTAINER_GATEWAY
192EOF
193
Anthony Young1c364642011-09-13 20:21:42 -0700194# Configure the runner
Anthony Younge8fed482011-09-26 19:50:43 -0700195RUN_SH=$ROOTFS/$DEST/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700196cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700197#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700198# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700199echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700200sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700201
Anthony Young927a6562011-09-14 01:58:01 -0700202# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700203killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700204
Anthony Young99003e72011-09-13 02:05:12 -0700205# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700206sudo apt-get update
207sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngadbe9c12011-09-26 19:58:49 -0700208if [ ! -d "$DEST/devstack" ]; then
209 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700210fi
Anthony Youngadbe9c12011-09-26 19:58:49 -0700211cd $DEST/devstack && $STACKSH_PARAMS ./stack.sh > /$DEST/run.sh.log
termie73774442011-09-28 19:02:28 -0500212echo >> /$DEST/run.sh.log
213echo >> /$DEST/run.sh.log
214echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700215EOF
216
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700217# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700218chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700219
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700220# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700221RC_LOCAL=$ROOTFS/etc/rc.local
222cat > $RC_LOCAL <<EOF
223#!/bin/sh -e
Anthony Youngadbe9c12011-09-26 19:58:49 -0700224su -c "$DEST/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700225EOF
226
Anthony Young72d69632011-09-12 21:09:55 -0700227# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700228if ! mount | grep -q cgroup; then
229 mkdir -p /cgroup
230 mount none -t cgroup /cgroup
231fi
Anthony Young72d69632011-09-12 21:09:55 -0700232
233# Start our container
234lxc-start -d -n $CONTAINER
termie73774442011-09-28 19:02:28 -0500235
236# Done creating the container, let's tail the log
237echo
238echo "============================================================="
239echo " -- YAY! --"
240echo "============================================================="
241echo
242echo "We're done creating the container, about to start tailing the"
243echo "stack.sh log. It will take a second or two to start."
244echo
245echo "Just CTRL-C at any time to stop tailing."
246
247while [ ! -e "$ROOTFS/$DEST/run.sh.log" ]; do
248 sleep 1
249done
250
251tail -F $ROOTFS/$DEST/run.sh.log