blob: 1f85b331b294fbc407834d797bc7ee6c6972d69e [file] [log] [blame]
Anthony Young4f279222011-09-13 21:51:28 -07001#!/usr/bin/env bash
Jesse Andrews18ebd862011-09-19 14:23:42 -07002
Anthony Youngd8c259a2011-09-26 14:04:13 -07003# Use stackrc.example if stackrc is missing
4if [ ! -e ./stackrc ]; then
5 read -n1 -p "No stackrc present. Copy stackrc.example to stackrc? (y/n) "
6 echo
7 [[ $REPLY = [yY] ]] && cp stackrc.example stackrc|| { echo "Aborting: Missing stackrc"; exit 1; }
8fi
9
Anthony Young2f140202011-09-26 13:02:40 -070010# Source params
11source ./stackrc
12
Anthony Young84064da2011-09-26 16:19:50 -070013# Store cwd
14CWD=`pwd`
15
Anthony Young72d69632011-09-12 21:09:55 -070016# Configurable params
17BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -070018CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -070019CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
20CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
21CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
22CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young77dbb072011-09-14 00:49:39 -070023NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070024COPYENV=${COPYENV:-1}
Anthony Younge8fed482011-09-26 19:50:43 -070025DEST=${DEST:-/opt/stack}
Anthony Young72d69632011-09-12 21:09:55 -070026
Anthony Young1c364642011-09-13 20:21:42 -070027# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
28STACKSH_PARAMS=${STACKSH_PARAMS:-}
29
Anthony Younga34b6952011-09-26 15:24:59 -070030# Option to use the version of devstack on which we are currently working
31USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
32
Anthony Young936c9282011-09-13 23:36:43 -070033# Warn users who aren't on natty
34if ! grep -q natty /etc/lsb-release; then
35 echo "WARNING: this script has only been tested on natty"
36fi
37
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
Anthony Young856d09f2011-09-19 19:49:20 -070043 apt-get install -y g++ bison flex libpam0g-dev
Anthony Young190321e2011-09-13 23:21:29 -070044 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
45 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 Young60534962011-09-13 10:40:04 -070085# Warm the base image on first install
86CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young01ad91a2011-09-26 13:25:11 -070087if [ ! -d $CACHEDIR ]; 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 Youngbf188ef2011-09-19 20:23:42 -070094 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 -070095 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Young60534962011-09-13 10:40:04 -070096fi
97
Anthony Younge8fed482011-09-26 19:50:43 -070098# Clean out code repos if directed to do so
99if [ "$CLEAN" = "1" ]; then
100 rm -rf $NOVA_REPO $CACHEDIR/$DEST
101fi
102
Anthony Young303233e2011-09-26 13:12:57 -0700103# Cache openstack code
Anthony Younge8fed482011-09-26 19:50:43 -0700104mkdir -p $CACHEDIR/$DEST
105git_clone $NOVA_REPO $CACHEDIR/$DEST/nova $NOVA_BRANCH
106git_clone $GLANCE_REPO $CACHEDIR/$DEST/glance $GLANCE_BRANCH
107git_clone $KEYSTONE_REPO $CACHEDIR/$DESTkeystone $KEYSTONE_BRANCH
108git_clone $NOVNC_REPO $CACHEDIR/$DEST/novnc $NOVNC_BRANCH
109git_clone $DASH_REPO $CACHEDIR/$DEST/dash $DASH_BRANCH $DASH_TAG
110git_clone $NIXON_REPO $CACHEDIR/$DEST/nixon $NIXON_BRANCH
111git_clone $NOVACLIENT_REPO $CACHEDIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
112git_clone $OPENSTACKX_REPO $CACHEDIR/$DEST/openstackx $OPENSTACKX_BRANCH
113git_clone $MUNIN_REPO $CACHEDIR/$DEST/openstack-munin $MUNIN_BRANCH
Anthony Young303233e2011-09-26 13:12:57 -0700114
Anthony Younga34b6952011-09-26 15:24:59 -0700115# Use this version of devstack?
116if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700117 rm -rf $CACHEDIR/$DEST/devstack
118 cp -pr $CWD $CACHEDIR/$DEST/devstack
Anthony Younga34b6952011-09-26 15:24:59 -0700119fi
120
Anthony Young60534962011-09-13 10:40:04 -0700121# Destroy the old container
122lxc-destroy -n $CONTAINER
123
Anthony Young23761c32011-09-16 14:54:20 -0700124# If this call is to TERMINATE the container then exit
125if [ "$TERMINATE" = "1" ]; then
126 exit
127fi
128
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700129# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -0700130lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -0700131
Anthony Youngf9998ab2011-09-13 10:43:44 -0700132# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -0700133ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
134
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700135# Create a stack user that is a member of the libvirtd group so that stack
136# is able to interact with libvirt.
137chroot $ROOTFS groupadd libvirtd
Anthony Younge8fed482011-09-26 19:50:43 -0700138chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700139
140# a simple password - pass
141echo stack:pass | chroot $ROOTFS chpasswd
142
143# and has sudo ability (in the future this should be limited to only what
144# stack requires)
145echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
146
Dean Troyer62a6deb2011-09-21 20:06:01 -0500147# Copy kernel modules
148mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
149cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
150cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
151
Anthony Young34c47022011-09-13 22:14:37 -0700152# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700153function cp_it {
154 if [ -e $1 ] || [ -d $1 ]; then
155 cp -pr $1 $2
156 fi
157}
158
Anthony Youngb3c04542011-09-13 01:28:18 -0700159# Copy over your ssh keys and env if desired
160if [ "$COPYENV" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700161 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
162 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
163 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
164 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
165 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700166fi
167
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700168# Make our ip address hostnames look nice at the command prompt
Anthony Younge8fed482011-09-26 19:50:43 -0700169echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700170echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
171
Anthony Younge8fed482011-09-26 19:50:43 -0700172# Give stack ownership over /$DEST so it may do the work needed
173chroot $ROOTFS chown -R stack /$DEST
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700174
Anthony Young72d69632011-09-12 21:09:55 -0700175# Configure instance network
176INTERFACES=$ROOTFS/etc/network/interfaces
177cat > $INTERFACES <<EOF
178auto lo
179iface lo inet loopback
180
181auto eth0
182iface eth0 inet static
183 address $CONTAINER_IP
184 netmask $CONTAINER_NETMASK
185 gateway $CONTAINER_GATEWAY
186EOF
187
Anthony Young1c364642011-09-13 20:21:42 -0700188# Configure the runner
Anthony Younge8fed482011-09-26 19:50:43 -0700189RUN_SH=$ROOTFS/$DEST/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700190cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700191#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700192# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700193echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700194sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700195
Anthony Young927a6562011-09-14 01:58:01 -0700196# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700197killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700198
Anthony Young99003e72011-09-13 02:05:12 -0700199# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700200sudo apt-get update
201sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Younge8fed482011-09-26 19:50:43 -0700202if [ ! -d "/$DEST/devstack" ]; then
203 git clone git://github.com/cloudbuilders/devstack.git /$DEST/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700204fi
Anthony Younge8fed482011-09-26 19:50:43 -0700205cd /$DEST/devstack && $STACKSH_PARAMS ./stack.sh > /$DEST/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700206EOF
207
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700208# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700209chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700210
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700211# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700212RC_LOCAL=$ROOTFS/etc/rc.local
213cat > $RC_LOCAL <<EOF
214#!/bin/sh -e
Anthony Younge8fed482011-09-26 19:50:43 -0700215su -c "/$DEST/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700216EOF
217
Anthony Young72d69632011-09-12 21:09:55 -0700218# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700219if ! mount | grep -q cgroup; then
220 mkdir -p /cgroup
221 mount none -t cgroup /cgroup
222fi
Anthony Young72d69632011-09-12 21:09:55 -0700223
224# Start our container
225lxc-start -d -n $CONTAINER