blob: 507f93e357ad64140b4e6a7992ce1f00abbd0812 [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 Young77dbb072011-09-14 00:49:39 -070035NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070036COPYENV=${COPYENV:-1}
Anthony Younge8fed482011-09-26 19:50:43 -070037DEST=${DEST:-/opt/stack}
Anthony Young72d69632011-09-12 21:09:55 -070038
Anthony Young1c364642011-09-13 20:21:42 -070039# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
40STACKSH_PARAMS=${STACKSH_PARAMS:-}
41
Anthony Younga34b6952011-09-26 15:24:59 -070042# Option to use the version of devstack on which we are currently working
43USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
44
Anthony Young936c9282011-09-13 23:36:43 -070045
Anthony Young190321e2011-09-13 23:21:29 -070046# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070047apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070048
49# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070050if ! which cgdelete | grep -q cgdelete; then
termiebd550ed2011-09-28 16:54:25 -050051 apt-get install -y g++ bison flex libpam0g-dev make
52 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 -070053 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
54 cd libcgroup-0.37.1
55 ./configure
56 make install
Dean Troyer62a6deb2011-09-21 20:06:01 -050057 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070058fi
59
Anthony Young60534962011-09-13 10:40:04 -070060# Create lxc configuration
61LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070062cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070063lxc.network.type = veth
64lxc.network.link = $BRIDGE
65lxc.network.flags = up
66lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070067# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070068lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070069EOF
70
Anthony Young60534962011-09-13 10:40:04 -070071# Shutdown any existing container
72lxc-stop -n $CONTAINER
73
Anthony Young4f279222011-09-13 21:51:28 -070074# This kills zombie containers
75if [ -d /cgroup/$CONTAINER ]; then
76 cgdelete -r cpu,net_cls:$CONTAINER
77fi
Anthony Young60534962011-09-13 10:40:04 -070078
Anthony Young2f140202011-09-26 13:02:40 -070079# git clone only if directory doesn't exist already. Since ``DEST`` might not
80# be owned by the installation user, we create the directory and change the
81# ownership to the proper user.
82function git_clone {
83 if [ ! -d $2 ]; then
84 sudo mkdir $2
85 sudo chown `whoami` $2
86 git clone $1 $2
87 cd $2
88 # This checkout syntax works for both branches and tags
89 git checkout $3
90 fi
91}
Jesse Andrews18ebd862011-09-19 14:23:42 -070092
Anthony Young9ff71ac2011-10-14 15:02:20 -070093# Helper to create the container
94function create_lxc {
95 if [ "natty" = "$UBUNTU_VERSION" ]; then
96 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
97 else
98 lxc-create -n $CONTAINER -t ubuntu -f $LXC_CONF
99 fi
100}
101
Anthony Youngbeab6392011-09-28 15:12:18 -0700102# Location of the base image directory
Anthony Young9ff71ac2011-10-14 15:02:20 -0700103if [ "natty" = "$UBUNTU_VERSION" ]; then
104 CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
105else
106 CACHEDIR=/var/cache/lxc/oneiric/rootfs-amd64
107fi
Anthony Youngbeab6392011-09-28 15:12:18 -0700108
109# Provide option to do totally clean install
110if [ "$CLEAR_LXC_CACHE" = "1" ]; then
111 rm -rf $CACHEDIR
112fi
113
114# Warm the base image on first install
115if [ ! -f $CACHEDIR/bootstrapped ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -0700116 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
117 # lazy and doesn't do anything if a container already exists)
118 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -0700119 # trigger the initial debootstrap
Anthony Young9ff71ac2011-10-14 15:02:20 -0700120 create_lxc
Anthony Young60534962011-09-13 10:40:04 -0700121 chroot $CACHEDIR apt-get update
Jesse Andrews9b6741e2011-10-02 10:01:00 -0700122 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 -0700123 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Youngbeab6392011-09-28 15:12:18 -0700124 touch $CACHEDIR/bootstrapped
Anthony Young60534962011-09-13 10:40:04 -0700125fi
126
Anthony Younge8fed482011-09-26 19:50:43 -0700127# Clean out code repos if directed to do so
128if [ "$CLEAN" = "1" ]; then
Anthony Young4f10de82011-09-26 20:03:40 -0700129 rm -rf $CACHEDIR/$DEST
Anthony Younge8fed482011-09-26 19:50:43 -0700130fi
131
Anthony Young303233e2011-09-26 13:12:57 -0700132# Cache openstack code
Anthony Younge8fed482011-09-26 19:50:43 -0700133mkdir -p $CACHEDIR/$DEST
134git_clone $NOVA_REPO $CACHEDIR/$DEST/nova $NOVA_BRANCH
135git_clone $GLANCE_REPO $CACHEDIR/$DEST/glance $GLANCE_BRANCH
136git_clone $KEYSTONE_REPO $CACHEDIR/$DESTkeystone $KEYSTONE_BRANCH
137git_clone $NOVNC_REPO $CACHEDIR/$DEST/novnc $NOVNC_BRANCH
138git_clone $DASH_REPO $CACHEDIR/$DEST/dash $DASH_BRANCH $DASH_TAG
Anthony Younge8fed482011-09-26 19:50:43 -0700139git_clone $NOVACLIENT_REPO $CACHEDIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
140git_clone $OPENSTACKX_REPO $CACHEDIR/$DEST/openstackx $OPENSTACKX_BRANCH
Anthony Young303233e2011-09-26 13:12:57 -0700141
Anthony Younga34b6952011-09-26 15:24:59 -0700142# Use this version of devstack?
143if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700144 rm -rf $CACHEDIR/$DEST/devstack
145 cp -pr $CWD $CACHEDIR/$DEST/devstack
Anthony Younga34b6952011-09-26 15:24:59 -0700146fi
147
Anthony Young60534962011-09-13 10:40:04 -0700148# Destroy the old container
149lxc-destroy -n $CONTAINER
150
Anthony Young23761c32011-09-16 14:54:20 -0700151# If this call is to TERMINATE the container then exit
152if [ "$TERMINATE" = "1" ]; then
153 exit
154fi
155
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700156# Create the container
Anthony Young9ff71ac2011-10-14 15:02:20 -0700157create_lxc
Anthony Youngb3c04542011-09-13 01:28:18 -0700158
Anthony Youngf9998ab2011-09-13 10:43:44 -0700159# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -0700160ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
161
termiebd550ed2011-09-28 16:54:25 -0500162# Create a stack user that is a member of the libvirtd group so that stack
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700163# is able to interact with libvirt.
164chroot $ROOTFS groupadd libvirtd
Anthony Younge8fed482011-09-26 19:50:43 -0700165chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700166
167# a simple password - pass
168echo stack:pass | chroot $ROOTFS chpasswd
169
termiebd550ed2011-09-28 16:54:25 -0500170# and has sudo ability (in the future this should be limited to only what
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700171# stack requires)
172echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
173
Dean Troyer62a6deb2011-09-21 20:06:01 -0500174# Copy kernel modules
175mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
176cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
177cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
178
Anthony Young34c47022011-09-13 22:14:37 -0700179# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700180function cp_it {
181 if [ -e $1 ] || [ -d $1 ]; then
termie1c7f0c92011-09-28 17:09:00 -0500182 cp -pRL $1 $2
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700183 fi
184}
185
Anthony Youngb3c04542011-09-13 01:28:18 -0700186# Copy over your ssh keys and env if desired
187if [ "$COPYENV" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700188 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
189 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
190 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
191 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
192 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700193fi
194
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700195# Make our ip address hostnames look nice at the command prompt
Anthony Younge8fed482011-09-26 19:50:43 -0700196echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700197echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
198
Anthony Youngadbe9c12011-09-26 19:58:49 -0700199# Give stack ownership over $DEST so it may do the work needed
200chroot $ROOTFS chown -R stack $DEST
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700201
Anthony Young72d69632011-09-12 21:09:55 -0700202# Configure instance network
203INTERFACES=$ROOTFS/etc/network/interfaces
204cat > $INTERFACES <<EOF
205auto lo
206iface lo inet loopback
207
208auto eth0
209iface eth0 inet static
210 address $CONTAINER_IP
211 netmask $CONTAINER_NETMASK
212 gateway $CONTAINER_GATEWAY
213EOF
214
Anthony Young1c364642011-09-13 20:21:42 -0700215# Configure the runner
Anthony Younge8fed482011-09-26 19:50:43 -0700216RUN_SH=$ROOTFS/$DEST/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700217cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700218#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700219# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700220echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young9ff71ac2011-10-14 15:02:20 -0700221# Make there is a default route - needed for natty
222if ! route | grep -q default; then
223 sudo ip route add default via $CONTAINER_GATEWAY
224fi
Anthony Young72d69632011-09-12 21:09:55 -0700225sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700226
Anthony Young927a6562011-09-14 01:58:01 -0700227# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700228killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700229
Anthony Young99003e72011-09-13 02:05:12 -0700230# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700231sudo apt-get update
232sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngadbe9c12011-09-26 19:58:49 -0700233if [ ! -d "$DEST/devstack" ]; then
234 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700235fi
Anthony Young9ff71ac2011-10-14 15:02:20 -0700236cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
termie73774442011-09-28 19:02:28 -0500237echo >> /$DEST/run.sh.log
238echo >> /$DEST/run.sh.log
239echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700240EOF
241
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700242# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700243chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700244
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700245# Make runner launch on boot
Anthony Young9ff71ac2011-10-14 15:02:20 -0700246RC_LOCAL=$ROOTFS/etc/init.d/local
Anthony Young72d69632011-09-12 21:09:55 -0700247cat > $RC_LOCAL <<EOF
248#!/bin/sh -e
Anthony Youngadbe9c12011-09-26 19:58:49 -0700249su -c "$DEST/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700250EOF
Anthony Young9ff71ac2011-10-14 15:02:20 -0700251chmod +x $RC_LOCAL
252chroot $ROOTFS sudo update-rc.d local defaults 80
Anthony Young72d69632011-09-12 21:09:55 -0700253
Anthony Young72d69632011-09-12 21:09:55 -0700254# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700255if ! mount | grep -q cgroup; then
256 mkdir -p /cgroup
257 mount none -t cgroup /cgroup
258fi
Anthony Young72d69632011-09-12 21:09:55 -0700259
260# Start our container
261lxc-start -d -n $CONTAINER
termie73774442011-09-28 19:02:28 -0500262
263# Done creating the container, let's tail the log
264echo
265echo "============================================================="
266echo " -- YAY! --"
267echo "============================================================="
268echo
269echo "We're done creating the container, about to start tailing the"
270echo "stack.sh log. It will take a second or two to start."
271echo
272echo "Just CTRL-C at any time to stop tailing."
273
274while [ ! -e "$ROOTFS/$DEST/run.sh.log" ]; do
275 sleep 1
276done
277
Anthony Youngd1b1cdb2011-10-13 15:25:38 -0700278tail -F $ROOTFS/$DEST/run.sh.log