blob: b629183b147f2d99260624ca6c07af0b8cc2bc39 [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}
Jesse Andrews82040df2011-10-22 20:56:23 -070030GUEST_NAME=${GUEST_NAME:-STACK}
31GUEST_IP=${GUEST_IP:-192.168.1.50}
32GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
33GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
34GUEST_GATEWAY=${GUEST_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"
Anthony Youngb22263a2011-10-20 10:26:30 -070041# By default, n-vol is disabled for lxc, as iscsitarget doesn't work properly in lxc
Anthony Youngacff87a2011-10-20 10:12:58 -070042STACKSH_PARAMS=${STACKSH_PARAMS:-"ENABLED_SERVICES=g-api,g-reg,key,n-api,n-cpu,n-net,n-sch,n-vnc,dash,mysql,rabbit"}
Anthony Young1c364642011-09-13 20:21:42 -070043
Anthony Younga34b6952011-09-26 15:24:59 -070044# Option to use the version of devstack on which we are currently working
45USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
46
Anthony Young936c9282011-09-13 23:36:43 -070047
Anthony Young190321e2011-09-13 23:21:29 -070048# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070049apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070050
51# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070052if ! which cgdelete | grep -q cgdelete; then
termiebd550ed2011-09-28 16:54:25 -050053 apt-get install -y g++ bison flex libpam0g-dev make
54 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 -070055 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
56 cd libcgroup-0.37.1
57 ./configure
58 make install
Dean Troyer62a6deb2011-09-21 20:06:01 -050059 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070060fi
61
Anthony Young60534962011-09-13 10:40:04 -070062# Create lxc configuration
Jesse Andrews82040df2011-10-22 20:56:23 -070063LXC_CONF=/tmp/$GUEST_NAME.conf
Anthony Young40203cb2011-09-13 09:17:56 -070064cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070065lxc.network.type = veth
66lxc.network.link = $BRIDGE
67lxc.network.flags = up
Jesse Andrews82040df2011-10-22 20:56:23 -070068lxc.network.ipv4 = $GUEST_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070069# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070070lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070071EOF
72
Anthony Young60534962011-09-13 10:40:04 -070073# Shutdown any existing container
Jesse Andrews82040df2011-10-22 20:56:23 -070074lxc-stop -n $GUEST_NAME
Anthony Young60534962011-09-13 10:40:04 -070075
Anthony Young4f279222011-09-13 21:51:28 -070076# This kills zombie containers
Jesse Andrews82040df2011-10-22 20:56:23 -070077if [ -d /cgroup/$GUEST_NAME ]; then
78 cgdelete -r cpu,net_cls:$GUEST_NAME
Anthony Young4f279222011-09-13 21:51:28 -070079fi
Anthony Young60534962011-09-13 10:40:04 -070080
Anthony Young2f140202011-09-26 13:02:40 -070081# git clone only if directory doesn't exist already. Since ``DEST`` might not
82# be owned by the installation user, we create the directory and change the
83# ownership to the proper user.
84function git_clone {
85 if [ ! -d $2 ]; then
86 sudo mkdir $2
87 sudo chown `whoami` $2
88 git clone $1 $2
89 cd $2
90 # This checkout syntax works for both branches and tags
91 git checkout $3
92 fi
93}
Jesse Andrews18ebd862011-09-19 14:23:42 -070094
Anthony Young9ff71ac2011-10-14 15:02:20 -070095# Helper to create the container
96function create_lxc {
97 if [ "natty" = "$UBUNTU_VERSION" ]; then
Jesse Andrews82040df2011-10-22 20:56:23 -070098 lxc-create -n $GUEST_NAME -t natty -f $LXC_CONF
Anthony Young9ff71ac2011-10-14 15:02:20 -070099 else
Jesse Andrews82040df2011-10-22 20:56:23 -0700100 lxc-create -n $GUEST_NAME -t ubuntu -f $LXC_CONF
Anthony Young9ff71ac2011-10-14 15:02:20 -0700101 fi
102}
103
Anthony Youngbeab6392011-09-28 15:12:18 -0700104# Location of the base image directory
Anthony Young9ff71ac2011-10-14 15:02:20 -0700105if [ "natty" = "$UBUNTU_VERSION" ]; then
106 CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
107else
108 CACHEDIR=/var/cache/lxc/oneiric/rootfs-amd64
109fi
Anthony Youngbeab6392011-09-28 15:12:18 -0700110
111# Provide option to do totally clean install
112if [ "$CLEAR_LXC_CACHE" = "1" ]; then
113 rm -rf $CACHEDIR
114fi
115
116# Warm the base image on first install
117if [ ! -f $CACHEDIR/bootstrapped ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -0700118 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
119 # lazy and doesn't do anything if a container already exists)
Jesse Andrews82040df2011-10-22 20:56:23 -0700120 lxc-destroy -n $GUEST_NAME
Anthony Young60534962011-09-13 10:40:04 -0700121 # trigger the initial debootstrap
Anthony Young9ff71ac2011-10-14 15:02:20 -0700122 create_lxc
Anthony Youngbeab6392011-09-28 15:12:18 -0700123 touch $CACHEDIR/bootstrapped
Anthony Young60534962011-09-13 10:40:04 -0700124fi
125
Anthony Young6b9d6da2011-10-17 10:12:22 -0700126# Make sure that base requirements are installed
127chroot $CACHEDIR apt-get update
128chroot $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 -0700129chroot $CACHEDIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
Anthony Young6b9d6da2011-10-17 10:12:22 -0700130chroot $CACHEDIR pip install `cat files/pips/*`
131
Anthony Younge8fed482011-09-26 19:50:43 -0700132# Clean out code repos if directed to do so
133if [ "$CLEAN" = "1" ]; then
Anthony Young4f10de82011-09-26 20:03:40 -0700134 rm -rf $CACHEDIR/$DEST
Anthony Younge8fed482011-09-26 19:50:43 -0700135fi
136
Anthony Young303233e2011-09-26 13:12:57 -0700137# Cache openstack code
Anthony Younge8fed482011-09-26 19:50:43 -0700138mkdir -p $CACHEDIR/$DEST
139git_clone $NOVA_REPO $CACHEDIR/$DEST/nova $NOVA_BRANCH
140git_clone $GLANCE_REPO $CACHEDIR/$DEST/glance $GLANCE_BRANCH
141git_clone $KEYSTONE_REPO $CACHEDIR/$DESTkeystone $KEYSTONE_BRANCH
Anthony Young583bad02011-10-18 08:22:30 -0700142git_clone $NOVNC_REPO $CACHEDIR/$DEST/noVNC $NOVNC_BRANCH
Anthony Younge8fed482011-09-26 19:50:43 -0700143git_clone $DASH_REPO $CACHEDIR/$DEST/dash $DASH_BRANCH $DASH_TAG
Anthony Younge8fed482011-09-26 19:50:43 -0700144git_clone $NOVACLIENT_REPO $CACHEDIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
145git_clone $OPENSTACKX_REPO $CACHEDIR/$DEST/openstackx $OPENSTACKX_BRANCH
Anthony Young028cad12011-10-17 14:10:42 -0700146git_clone $KEYSTONE_REPO $CACHEDIR/$DEST/keystone $KEYSTONE_BRANCH
147git_clone $NOVNC_REPO $CACHEDIR/$DEST/novnc $NOVNC_BRANCH
Anthony Young303233e2011-09-26 13:12:57 -0700148
Anthony Younga34b6952011-09-26 15:24:59 -0700149# Use this version of devstack?
150if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700151 rm -rf $CACHEDIR/$DEST/devstack
152 cp -pr $CWD $CACHEDIR/$DEST/devstack
Anthony Younga34b6952011-09-26 15:24:59 -0700153fi
154
Anthony Young60534962011-09-13 10:40:04 -0700155# Destroy the old container
Jesse Andrews82040df2011-10-22 20:56:23 -0700156lxc-destroy -n $GUEST_NAME
Anthony Young60534962011-09-13 10:40:04 -0700157
Anthony Young23761c32011-09-16 14:54:20 -0700158# If this call is to TERMINATE the container then exit
159if [ "$TERMINATE" = "1" ]; then
160 exit
161fi
162
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700163# Create the container
Anthony Young9ff71ac2011-10-14 15:02:20 -0700164create_lxc
Anthony Youngb3c04542011-09-13 01:28:18 -0700165
Anthony Youngf9998ab2011-09-13 10:43:44 -0700166# Specify where our container rootfs lives
Jesse Andrews82040df2011-10-22 20:56:23 -0700167ROOTFS=/var/lib/lxc/$GUEST_NAME/rootfs/
Anthony Young72d69632011-09-12 21:09:55 -0700168
termiebd550ed2011-09-28 16:54:25 -0500169# Create a stack user that is a member of the libvirtd group so that stack
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700170# is able to interact with libvirt.
171chroot $ROOTFS groupadd libvirtd
Anthony Younge8fed482011-09-26 19:50:43 -0700172chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700173
174# a simple password - pass
175echo stack:pass | chroot $ROOTFS chpasswd
176
termiebd550ed2011-09-28 16:54:25 -0500177# and has sudo ability (in the future this should be limited to only what
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700178# stack requires)
179echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
180
Dean Troyer62a6deb2011-09-21 20:06:01 -0500181# Copy kernel modules
182mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
183cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
184cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
185
Anthony Young34c47022011-09-13 22:14:37 -0700186# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700187function cp_it {
188 if [ -e $1 ] || [ -d $1 ]; then
termie1c7f0c92011-09-28 17:09:00 -0500189 cp -pRL $1 $2
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700190 fi
191}
192
Anthony Youngb3c04542011-09-13 01:28:18 -0700193# Copy over your ssh keys and env if desired
194if [ "$COPYENV" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700195 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
196 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
197 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
198 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
199 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700200fi
201
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700202# Make our ip address hostnames look nice at the command prompt
Anthony Younge8fed482011-09-26 19:50:43 -0700203echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700204echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
205
Anthony Youngadbe9c12011-09-26 19:58:49 -0700206# Give stack ownership over $DEST so it may do the work needed
207chroot $ROOTFS chown -R stack $DEST
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700208
Anthony Young72d69632011-09-12 21:09:55 -0700209# Configure instance network
210INTERFACES=$ROOTFS/etc/network/interfaces
211cat > $INTERFACES <<EOF
212auto lo
213iface lo inet loopback
214
215auto eth0
216iface eth0 inet static
Jesse Andrews82040df2011-10-22 20:56:23 -0700217 address $GUEST_IP
218 netmask $GUEST_NETMASK
219 gateway $GUEST_GATEWAY
Anthony Young72d69632011-09-12 21:09:55 -0700220EOF
221
Anthony Young1c364642011-09-13 20:21:42 -0700222# Configure the runner
Anthony Younge8fed482011-09-26 19:50:43 -0700223RUN_SH=$ROOTFS/$DEST/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700224cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700225#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700226# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700227echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young9ff71ac2011-10-14 15:02:20 -0700228# Make there is a default route - needed for natty
229if ! route | grep -q default; then
Jesse Andrews82040df2011-10-22 20:56:23 -0700230 sudo ip route add default via $GUEST_GATEWAY
Anthony Young9ff71ac2011-10-14 15:02:20 -0700231fi
Anthony Young72d69632011-09-12 21:09:55 -0700232sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700233
Anthony Young927a6562011-09-14 01:58:01 -0700234# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700235killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700236
Anthony Young99003e72011-09-13 02:05:12 -0700237# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700238sudo apt-get update
239sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngadbe9c12011-09-26 19:58:49 -0700240if [ ! -d "$DEST/devstack" ]; then
241 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700242fi
Anthony Young9ff71ac2011-10-14 15:02:20 -0700243cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
termie73774442011-09-28 19:02:28 -0500244echo >> /$DEST/run.sh.log
245echo >> /$DEST/run.sh.log
246echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700247EOF
248
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700249# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700250chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700251
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700252# Make runner launch on boot
Anthony Young9ff71ac2011-10-14 15:02:20 -0700253RC_LOCAL=$ROOTFS/etc/init.d/local
Anthony Young72d69632011-09-12 21:09:55 -0700254cat > $RC_LOCAL <<EOF
255#!/bin/sh -e
Anthony Youngadbe9c12011-09-26 19:58:49 -0700256su -c "$DEST/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700257EOF
Anthony Young9ff71ac2011-10-14 15:02:20 -0700258chmod +x $RC_LOCAL
259chroot $ROOTFS sudo update-rc.d local defaults 80
Anthony Young72d69632011-09-12 21:09:55 -0700260
Anthony Young72d69632011-09-12 21:09:55 -0700261# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700262if ! mount | grep -q cgroup; then
263 mkdir -p /cgroup
264 mount none -t cgroup /cgroup
265fi
Anthony Young72d69632011-09-12 21:09:55 -0700266
267# Start our container
Jesse Andrews82040df2011-10-22 20:56:23 -0700268lxc-start -d -n $GUEST_NAME
termie73774442011-09-28 19:02:28 -0500269
Anthony Young25369c92011-10-14 16:50:27 -0700270if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
271 # Done creating the container, let's tail the log
272 echo
273 echo "============================================================="
274 echo " -- YAY! --"
275 echo "============================================================="
276 echo
277 echo "We're done creating the container, about to start tailing the"
278 echo "stack.sh log. It will take a second or two to start."
279 echo
280 echo "Just CTRL-C at any time to stop tailing."
termie73774442011-09-28 19:02:28 -0500281
Anthony Young25369c92011-10-14 16:50:27 -0700282 while [ ! -e "$ROOTFS/$DEST/run.sh.log" ]; do
283 sleep 1
284 done
termie73774442011-09-28 19:02:28 -0500285
Anthony Young25369c92011-10-14 16:50:27 -0700286 tail -F $ROOTFS/$DEST/run.sh.log &
287
288 TAIL_PID=$!
289
290 function kill_tail() {
Anthony Young04db1552011-10-17 09:40:45 -0700291 kill $TAIL_PID
Anthony Young25369c92011-10-14 16:50:27 -0700292 exit 1
293 }
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700294
Anthony Young25369c92011-10-14 16:50:27 -0700295 # Let Ctrl-c kill tail and exit
296 trap kill_tail SIGINT
297
298 echo "Waiting stack.sh to finish..."
299 while ! cat $ROOTFS/$DEST/run.sh.log | grep -q 'All done' ; do
300 sleep 5
301 done
302
303 kill $TAIL_PID
Jesse Andrewsad21d1a2011-10-26 12:39:00 -0700304
305 if grep -q "stack.sh failed" $ROOTFS/$DEST/run.sh.log; then
306 exit 1
307 fi
308
Anthony Young25369c92011-10-14 16:50:27 -0700309 echo ""
310 echo "Finished - Zip-a-dee Doo-dah!"
311fi