blob: c5957b2b1d4fd316eeb7f5838a1c58ad8fe8933d [file] [log] [blame]
Anthony Young4f279222011-09-13 21:51:28 -07001#!/usr/bin/env bash
Jesse Andrews18ebd862011-09-19 14:23:42 -07002
Anthony Youngdfc07482011-11-15 15:29:37 -08003# Debug stuff
4set -o errexit
5set -o xtrace
6
Jesse Andrews46ad2de2011-09-28 18:29:50 -07007# Sanity check
8if [ "$EUID" -ne "0" ]; then
9 echo "This script must be run with root privileges."
10 exit 1
11fi
12
Anthony Young9ff71ac2011-10-14 15:02:20 -070013# Keep track of ubuntu version
14UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
Jesse Andrews46ad2de2011-09-28 18:29:50 -070015
Anthony Younge8efef72011-10-14 12:00:50 -070016# Move to top devstack dir
17cd ..
18
Anthony Youngcf145b72011-10-13 15:07:36 -070019# Abort if localrc is not set
20if [ ! -e ./localrc ]; then
21 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
22 echo "See stack.sh for required passwords."
23 exit 1
24fi
25
Anthony Young2f140202011-09-26 13:02:40 -070026# Source params
27source ./stackrc
28
Anthony Young84064da2011-09-26 16:19:50 -070029# Store cwd
30CWD=`pwd`
31
Anthony Young72d69632011-09-12 21:09:55 -070032# Configurable params
33BRIDGE=${BRIDGE:-br0}
Jesse Andrews82040df2011-10-22 20:56:23 -070034GUEST_NAME=${GUEST_NAME:-STACK}
35GUEST_IP=${GUEST_IP:-192.168.1.50}
36GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
37GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
38GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.1.1}
Anthony Youngae8bc122011-10-17 10:38:05 -070039NAMESERVER=${NAMESERVER:-`cat /etc/resolv.conf | grep nameserver | head -1 | cut -d " " -f2`}
Anthony Young72d69632011-09-12 21:09:55 -070040COPYENV=${COPYENV:-1}
Anthony Younge8fed482011-09-26 19:50:43 -070041DEST=${DEST:-/opt/stack}
Anthony Young25369c92011-10-14 16:50:27 -070042WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
Anthony Young72d69632011-09-12 21:09:55 -070043
Anthony Young1c364642011-09-13 20:21:42 -070044# 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 -070045# By default, n-vol is disabled for lxc, as iscsitarget doesn't work properly in lxc
Tres Henryca85b792011-10-28 14:00:21 -070046STACKSH_PARAMS=${STACKSH_PARAMS:-"ENABLED_SERVICES=g-api,g-reg,key,n-api,n-cpu,n-net,n-sch,n-vnc,horizon,mysql,rabbit"}
Anthony Young1c364642011-09-13 20:21:42 -070047
Anthony Younga34b6952011-09-26 15:24:59 -070048# Option to use the version of devstack on which we are currently working
49USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
50
Anthony Young936c9282011-09-13 23:36:43 -070051
Anthony Young190321e2011-09-13 23:21:29 -070052# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070053apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070054
55# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070056if ! which cgdelete | grep -q cgdelete; then
termiebd550ed2011-09-28 16:54:25 -050057 apt-get install -y g++ bison flex libpam0g-dev make
58 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 -070059 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
60 cd libcgroup-0.37.1
61 ./configure
62 make install
Dean Troyer62a6deb2011-09-21 20:06:01 -050063 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070064fi
65
Anthony Young60534962011-09-13 10:40:04 -070066# Create lxc configuration
Jesse Andrews82040df2011-10-22 20:56:23 -070067LXC_CONF=/tmp/$GUEST_NAME.conf
Anthony Young40203cb2011-09-13 09:17:56 -070068cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070069lxc.network.type = veth
70lxc.network.link = $BRIDGE
71lxc.network.flags = up
Jesse Andrews82040df2011-10-22 20:56:23 -070072lxc.network.ipv4 = $GUEST_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070073# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070074lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070075EOF
76
Anthony Young60534962011-09-13 10:40:04 -070077# Shutdown any existing container
Jesse Andrews82040df2011-10-22 20:56:23 -070078lxc-stop -n $GUEST_NAME
Anthony Young60534962011-09-13 10:40:04 -070079
Anthony Young4f279222011-09-13 21:51:28 -070080# This kills zombie containers
Jesse Andrews82040df2011-10-22 20:56:23 -070081if [ -d /cgroup/$GUEST_NAME ]; then
82 cgdelete -r cpu,net_cls:$GUEST_NAME
Anthony Young4f279222011-09-13 21:51:28 -070083fi
Anthony Young60534962011-09-13 10:40:04 -070084
Anthony Young2f140202011-09-26 13:02:40 -070085# git clone only if directory doesn't exist already. Since ``DEST`` might not
86# be owned by the installation user, we create the directory and change the
87# ownership to the proper user.
88function git_clone {
89 if [ ! -d $2 ]; then
90 sudo mkdir $2
91 sudo chown `whoami` $2
92 git clone $1 $2
93 cd $2
94 # This checkout syntax works for both branches and tags
95 git checkout $3
96 fi
97}
Jesse Andrews18ebd862011-09-19 14:23:42 -070098
Anthony Young9ff71ac2011-10-14 15:02:20 -070099# Helper to create the container
100function create_lxc {
101 if [ "natty" = "$UBUNTU_VERSION" ]; then
Jesse Andrews82040df2011-10-22 20:56:23 -0700102 lxc-create -n $GUEST_NAME -t natty -f $LXC_CONF
Anthony Young9ff71ac2011-10-14 15:02:20 -0700103 else
Jesse Andrews82040df2011-10-22 20:56:23 -0700104 lxc-create -n $GUEST_NAME -t ubuntu -f $LXC_CONF
Anthony Young9ff71ac2011-10-14 15:02:20 -0700105 fi
106}
107
Anthony Youngbeab6392011-09-28 15:12:18 -0700108# Location of the base image directory
Anthony Young9ff71ac2011-10-14 15:02:20 -0700109if [ "natty" = "$UBUNTU_VERSION" ]; then
110 CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
111else
112 CACHEDIR=/var/cache/lxc/oneiric/rootfs-amd64
113fi
Anthony Youngbeab6392011-09-28 15:12:18 -0700114
115# Provide option to do totally clean install
116if [ "$CLEAR_LXC_CACHE" = "1" ]; then
117 rm -rf $CACHEDIR
118fi
119
120# Warm the base image on first install
121if [ ! -f $CACHEDIR/bootstrapped ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -0700122 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
123 # lazy and doesn't do anything if a container already exists)
Jesse Andrews82040df2011-10-22 20:56:23 -0700124 lxc-destroy -n $GUEST_NAME
Anthony Young60534962011-09-13 10:40:04 -0700125 # trigger the initial debootstrap
Anthony Young9ff71ac2011-10-14 15:02:20 -0700126 create_lxc
Anthony Youngbeab6392011-09-28 15:12:18 -0700127 touch $CACHEDIR/bootstrapped
Anthony Young60534962011-09-13 10:40:04 -0700128fi
129
Anthony Young6b9d6da2011-10-17 10:12:22 -0700130# Make sure that base requirements are installed
131chroot $CACHEDIR apt-get update
Anthony Youngca2c0472011-11-03 16:29:32 -0700132chroot $CACHEDIR apt-get install -y --download-only `cat files/apts/* | grep NOPRIME | cut -d\# -f1`
Anthony Youngdfc07482011-11-15 15:29:37 -0800133chroot $CACHEDIR apt-get install -y --force-yes `cat files/apts/* | grep -v NOPRIME | cut -d\# -f1` || true
Anthony Young6b9d6da2011-10-17 10:12:22 -0700134chroot $CACHEDIR pip install `cat files/pips/*`
135
Anthony Younge8fed482011-09-26 19:50:43 -0700136# Clean out code repos if directed to do so
137if [ "$CLEAN" = "1" ]; then
Anthony Young4f10de82011-09-26 20:03:40 -0700138 rm -rf $CACHEDIR/$DEST
Anthony Younge8fed482011-09-26 19:50:43 -0700139fi
140
Anthony Young303233e2011-09-26 13:12:57 -0700141# Cache openstack code
Anthony Younge8fed482011-09-26 19:50:43 -0700142mkdir -p $CACHEDIR/$DEST
143git_clone $NOVA_REPO $CACHEDIR/$DEST/nova $NOVA_BRANCH
144git_clone $GLANCE_REPO $CACHEDIR/$DEST/glance $GLANCE_BRANCH
145git_clone $KEYSTONE_REPO $CACHEDIR/$DESTkeystone $KEYSTONE_BRANCH
Anthony Young583bad02011-10-18 08:22:30 -0700146git_clone $NOVNC_REPO $CACHEDIR/$DEST/noVNC $NOVNC_BRANCH
Tres Henryca85b792011-10-28 14:00:21 -0700147git_clone $HORIZON_REPO $CACHEDIR/$DEST/horizon $HORIZON_BRANCH $HORIZON_TAG
Anthony Younge8fed482011-09-26 19:50:43 -0700148git_clone $NOVACLIENT_REPO $CACHEDIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
149git_clone $OPENSTACKX_REPO $CACHEDIR/$DEST/openstackx $OPENSTACKX_BRANCH
Anthony Young028cad12011-10-17 14:10:42 -0700150git_clone $KEYSTONE_REPO $CACHEDIR/$DEST/keystone $KEYSTONE_BRANCH
151git_clone $NOVNC_REPO $CACHEDIR/$DEST/novnc $NOVNC_BRANCH
Anthony Young303233e2011-09-26 13:12:57 -0700152
Anthony Younga34b6952011-09-26 15:24:59 -0700153# Use this version of devstack?
154if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700155 rm -rf $CACHEDIR/$DEST/devstack
156 cp -pr $CWD $CACHEDIR/$DEST/devstack
Anthony Younga34b6952011-09-26 15:24:59 -0700157fi
158
Jesse Andrews70188b32011-10-26 21:59:20 -0700159# pre-cache uec images
160for image_url in ${IMAGE_URLS//,/ }; do
161 IMAGE_FNAME=`basename "$image_url"`
162 if [ ! -f $CACHEDIR/$IMAGE_FNAME ]; then
163 wget -c $image_url -O $CACHEDIR/$IMAGE_FNAME
164 fi
165 cp $CACHEDIR/$IMAGE_FNAME $CACHEDIR/$DEST/devstack/files
166done
167
Anthony Young60534962011-09-13 10:40:04 -0700168# Destroy the old container
Jesse Andrews82040df2011-10-22 20:56:23 -0700169lxc-destroy -n $GUEST_NAME
Anthony Young60534962011-09-13 10:40:04 -0700170
Anthony Young23761c32011-09-16 14:54:20 -0700171# If this call is to TERMINATE the container then exit
172if [ "$TERMINATE" = "1" ]; then
173 exit
174fi
175
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700176# Create the container
Anthony Young9ff71ac2011-10-14 15:02:20 -0700177create_lxc
Anthony Youngb3c04542011-09-13 01:28:18 -0700178
Anthony Youngf9998ab2011-09-13 10:43:44 -0700179# Specify where our container rootfs lives
Jesse Andrews82040df2011-10-22 20:56:23 -0700180ROOTFS=/var/lib/lxc/$GUEST_NAME/rootfs/
Anthony Young72d69632011-09-12 21:09:55 -0700181
termiebd550ed2011-09-28 16:54:25 -0500182# Create a stack user that is a member of the libvirtd group so that stack
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700183# is able to interact with libvirt.
184chroot $ROOTFS groupadd libvirtd
Anthony Younge8fed482011-09-26 19:50:43 -0700185chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700186
187# a simple password - pass
188echo stack:pass | chroot $ROOTFS chpasswd
189
termiebd550ed2011-09-28 16:54:25 -0500190# and has sudo ability (in the future this should be limited to only what
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700191# stack requires)
192echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
193
Dean Troyer62a6deb2011-09-21 20:06:01 -0500194# Copy kernel modules
195mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
196cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
197cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
198
Anthony Young34c47022011-09-13 22:14:37 -0700199# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700200function cp_it {
201 if [ -e $1 ] || [ -d $1 ]; then
termie1c7f0c92011-09-28 17:09:00 -0500202 cp -pRL $1 $2
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700203 fi
204}
205
Anthony Youngb3c04542011-09-13 01:28:18 -0700206# Copy over your ssh keys and env if desired
207if [ "$COPYENV" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700208 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
209 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
210 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
211 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
212 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700213fi
214
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700215# Make our ip address hostnames look nice at the command prompt
Anthony Younge8fed482011-09-26 19:50:43 -0700216echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700217echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
218
Anthony Youngadbe9c12011-09-26 19:58:49 -0700219# Give stack ownership over $DEST so it may do the work needed
220chroot $ROOTFS chown -R stack $DEST
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700221
Anthony Young72d69632011-09-12 21:09:55 -0700222# Configure instance network
223INTERFACES=$ROOTFS/etc/network/interfaces
224cat > $INTERFACES <<EOF
225auto lo
226iface lo inet loopback
227
228auto eth0
229iface eth0 inet static
Jesse Andrews82040df2011-10-22 20:56:23 -0700230 address $GUEST_IP
231 netmask $GUEST_NETMASK
232 gateway $GUEST_GATEWAY
Anthony Young72d69632011-09-12 21:09:55 -0700233EOF
234
Anthony Young1c364642011-09-13 20:21:42 -0700235# Configure the runner
Anthony Younge8fed482011-09-26 19:50:43 -0700236RUN_SH=$ROOTFS/$DEST/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700237cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700238#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700239# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700240echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young9ff71ac2011-10-14 15:02:20 -0700241# Make there is a default route - needed for natty
242if ! route | grep -q default; then
Jesse Andrews82040df2011-10-22 20:56:23 -0700243 sudo ip route add default via $GUEST_GATEWAY
Anthony Young9ff71ac2011-10-14 15:02:20 -0700244fi
Anthony Young72d69632011-09-12 21:09:55 -0700245sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700246
Anthony Young927a6562011-09-14 01:58:01 -0700247# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700248killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700249
Anthony Young99003e72011-09-13 02:05:12 -0700250# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700251sudo apt-get update
252sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngadbe9c12011-09-26 19:58:49 -0700253if [ ! -d "$DEST/devstack" ]; then
254 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700255fi
Anthony Young9ff71ac2011-10-14 15:02:20 -0700256cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
termie73774442011-09-28 19:02:28 -0500257echo >> /$DEST/run.sh.log
258echo >> /$DEST/run.sh.log
259echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700260EOF
261
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700262# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700263chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700264
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700265# Make runner launch on boot
Anthony Young9ff71ac2011-10-14 15:02:20 -0700266RC_LOCAL=$ROOTFS/etc/init.d/local
Anthony Young72d69632011-09-12 21:09:55 -0700267cat > $RC_LOCAL <<EOF
268#!/bin/sh -e
Anthony Youngadbe9c12011-09-26 19:58:49 -0700269su -c "$DEST/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700270EOF
Anthony Young9ff71ac2011-10-14 15:02:20 -0700271chmod +x $RC_LOCAL
272chroot $ROOTFS sudo update-rc.d local defaults 80
Anthony Young72d69632011-09-12 21:09:55 -0700273
Anthony Young72d69632011-09-12 21:09:55 -0700274# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700275if ! mount | grep -q cgroup; then
276 mkdir -p /cgroup
277 mount none -t cgroup /cgroup
278fi
Anthony Young72d69632011-09-12 21:09:55 -0700279
280# Start our container
Jesse Andrews82040df2011-10-22 20:56:23 -0700281lxc-start -d -n $GUEST_NAME
termie73774442011-09-28 19:02:28 -0500282
Anthony Young25369c92011-10-14 16:50:27 -0700283if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
284 # Done creating the container, let's tail the log
285 echo
286 echo "============================================================="
287 echo " -- YAY! --"
288 echo "============================================================="
289 echo
290 echo "We're done creating the container, about to start tailing the"
291 echo "stack.sh log. It will take a second or two to start."
292 echo
293 echo "Just CTRL-C at any time to stop tailing."
termie73774442011-09-28 19:02:28 -0500294
Anthony Young25369c92011-10-14 16:50:27 -0700295 while [ ! -e "$ROOTFS/$DEST/run.sh.log" ]; do
296 sleep 1
297 done
termie73774442011-09-28 19:02:28 -0500298
Anthony Young25369c92011-10-14 16:50:27 -0700299 tail -F $ROOTFS/$DEST/run.sh.log &
300
301 TAIL_PID=$!
302
303 function kill_tail() {
Anthony Young04db1552011-10-17 09:40:45 -0700304 kill $TAIL_PID
Anthony Young25369c92011-10-14 16:50:27 -0700305 exit 1
306 }
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700307
Anthony Young25369c92011-10-14 16:50:27 -0700308 # Let Ctrl-c kill tail and exit
309 trap kill_tail SIGINT
310
311 echo "Waiting stack.sh to finish..."
312 while ! cat $ROOTFS/$DEST/run.sh.log | grep -q 'All done' ; do
313 sleep 5
314 done
315
316 kill $TAIL_PID
Jesse Andrewsad21d1a2011-10-26 12:39:00 -0700317
318 if grep -q "stack.sh failed" $ROOTFS/$DEST/run.sh.log; then
319 exit 1
320 fi
321
Anthony Young25369c92011-10-14 16:50:27 -0700322 echo ""
323 echo "Finished - Zip-a-dee Doo-dah!"
324fi