blob: 9d8ce926137bf4d1138cac5df3ad965d84af2757 [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
Tres Henryca85b792011-10-28 14:00:21 -070042STACKSH_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 -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
Anthony Youngca2c0472011-11-03 16:29:32 -0700128chroot $CACHEDIR apt-get install -y --download-only `cat files/apts/* | grep NOPRIME | cut -d\# -f1`
129chroot $CACHEDIR apt-get install -y --force-yes `cat files/apts/* | grep -v NOPRIME | cut -d\# -f1`
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
Tres Henryca85b792011-10-28 14:00:21 -0700143git_clone $HORIZON_REPO $CACHEDIR/$DEST/horizon $HORIZON_BRANCH $HORIZON_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
Jesse Andrews70188b32011-10-26 21:59:20 -0700155# pre-cache uec images
156for image_url in ${IMAGE_URLS//,/ }; do
157 IMAGE_FNAME=`basename "$image_url"`
158 if [ ! -f $CACHEDIR/$IMAGE_FNAME ]; then
159 wget -c $image_url -O $CACHEDIR/$IMAGE_FNAME
160 fi
161 cp $CACHEDIR/$IMAGE_FNAME $CACHEDIR/$DEST/devstack/files
162done
163
Anthony Young60534962011-09-13 10:40:04 -0700164# Destroy the old container
Jesse Andrews82040df2011-10-22 20:56:23 -0700165lxc-destroy -n $GUEST_NAME
Anthony Young60534962011-09-13 10:40:04 -0700166
Anthony Young23761c32011-09-16 14:54:20 -0700167# If this call is to TERMINATE the container then exit
168if [ "$TERMINATE" = "1" ]; then
169 exit
170fi
171
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700172# Create the container
Anthony Young9ff71ac2011-10-14 15:02:20 -0700173create_lxc
Anthony Youngb3c04542011-09-13 01:28:18 -0700174
Anthony Youngf9998ab2011-09-13 10:43:44 -0700175# Specify where our container rootfs lives
Jesse Andrews82040df2011-10-22 20:56:23 -0700176ROOTFS=/var/lib/lxc/$GUEST_NAME/rootfs/
Anthony Young72d69632011-09-12 21:09:55 -0700177
termiebd550ed2011-09-28 16:54:25 -0500178# Create a stack user that is a member of the libvirtd group so that stack
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700179# is able to interact with libvirt.
180chroot $ROOTFS groupadd libvirtd
Anthony Younge8fed482011-09-26 19:50:43 -0700181chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700182
183# a simple password - pass
184echo stack:pass | chroot $ROOTFS chpasswd
185
termiebd550ed2011-09-28 16:54:25 -0500186# and has sudo ability (in the future this should be limited to only what
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700187# stack requires)
188echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
189
Dean Troyer62a6deb2011-09-21 20:06:01 -0500190# Copy kernel modules
191mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
192cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
193cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
194
Anthony Young34c47022011-09-13 22:14:37 -0700195# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700196function cp_it {
197 if [ -e $1 ] || [ -d $1 ]; then
termie1c7f0c92011-09-28 17:09:00 -0500198 cp -pRL $1 $2
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700199 fi
200}
201
Anthony Youngb3c04542011-09-13 01:28:18 -0700202# Copy over your ssh keys and env if desired
203if [ "$COPYENV" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700204 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
205 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
206 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
207 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
208 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700209fi
210
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700211# Make our ip address hostnames look nice at the command prompt
Anthony Younge8fed482011-09-26 19:50:43 -0700212echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700213echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
214
Anthony Youngadbe9c12011-09-26 19:58:49 -0700215# Give stack ownership over $DEST so it may do the work needed
216chroot $ROOTFS chown -R stack $DEST
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700217
Anthony Young72d69632011-09-12 21:09:55 -0700218# Configure instance network
219INTERFACES=$ROOTFS/etc/network/interfaces
220cat > $INTERFACES <<EOF
221auto lo
222iface lo inet loopback
223
224auto eth0
225iface eth0 inet static
Jesse Andrews82040df2011-10-22 20:56:23 -0700226 address $GUEST_IP
227 netmask $GUEST_NETMASK
228 gateway $GUEST_GATEWAY
Anthony Young72d69632011-09-12 21:09:55 -0700229EOF
230
Anthony Young1c364642011-09-13 20:21:42 -0700231# Configure the runner
Anthony Younge8fed482011-09-26 19:50:43 -0700232RUN_SH=$ROOTFS/$DEST/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700233cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700234#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700235# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700236echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young9ff71ac2011-10-14 15:02:20 -0700237# Make there is a default route - needed for natty
238if ! route | grep -q default; then
Jesse Andrews82040df2011-10-22 20:56:23 -0700239 sudo ip route add default via $GUEST_GATEWAY
Anthony Young9ff71ac2011-10-14 15:02:20 -0700240fi
Anthony Young72d69632011-09-12 21:09:55 -0700241sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700242
Anthony Young927a6562011-09-14 01:58:01 -0700243# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700244killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700245
Anthony Young99003e72011-09-13 02:05:12 -0700246# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700247sudo apt-get update
248sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngadbe9c12011-09-26 19:58:49 -0700249if [ ! -d "$DEST/devstack" ]; then
250 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700251fi
Anthony Young9ff71ac2011-10-14 15:02:20 -0700252cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
termie73774442011-09-28 19:02:28 -0500253echo >> /$DEST/run.sh.log
254echo >> /$DEST/run.sh.log
255echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700256EOF
257
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700258# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700259chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700260
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700261# Make runner launch on boot
Anthony Young9ff71ac2011-10-14 15:02:20 -0700262RC_LOCAL=$ROOTFS/etc/init.d/local
Anthony Young72d69632011-09-12 21:09:55 -0700263cat > $RC_LOCAL <<EOF
264#!/bin/sh -e
Anthony Youngadbe9c12011-09-26 19:58:49 -0700265su -c "$DEST/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700266EOF
Anthony Young9ff71ac2011-10-14 15:02:20 -0700267chmod +x $RC_LOCAL
268chroot $ROOTFS sudo update-rc.d local defaults 80
Anthony Young72d69632011-09-12 21:09:55 -0700269
Anthony Young72d69632011-09-12 21:09:55 -0700270# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700271if ! mount | grep -q cgroup; then
272 mkdir -p /cgroup
273 mount none -t cgroup /cgroup
274fi
Anthony Young72d69632011-09-12 21:09:55 -0700275
276# Start our container
Jesse Andrews82040df2011-10-22 20:56:23 -0700277lxc-start -d -n $GUEST_NAME
termie73774442011-09-28 19:02:28 -0500278
Anthony Young25369c92011-10-14 16:50:27 -0700279if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
280 # Done creating the container, let's tail the log
281 echo
282 echo "============================================================="
283 echo " -- YAY! --"
284 echo "============================================================="
285 echo
286 echo "We're done creating the container, about to start tailing the"
287 echo "stack.sh log. It will take a second or two to start."
288 echo
289 echo "Just CTRL-C at any time to stop tailing."
termie73774442011-09-28 19:02:28 -0500290
Anthony Young25369c92011-10-14 16:50:27 -0700291 while [ ! -e "$ROOTFS/$DEST/run.sh.log" ]; do
292 sleep 1
293 done
termie73774442011-09-28 19:02:28 -0500294
Anthony Young25369c92011-10-14 16:50:27 -0700295 tail -F $ROOTFS/$DEST/run.sh.log &
296
297 TAIL_PID=$!
298
299 function kill_tail() {
Anthony Young04db1552011-10-17 09:40:45 -0700300 kill $TAIL_PID
Anthony Young25369c92011-10-14 16:50:27 -0700301 exit 1
302 }
Vishvananda Ishaya9b353672011-10-20 10:07:10 -0700303
Anthony Young25369c92011-10-14 16:50:27 -0700304 # Let Ctrl-c kill tail and exit
305 trap kill_tail SIGINT
306
307 echo "Waiting stack.sh to finish..."
308 while ! cat $ROOTFS/$DEST/run.sh.log | grep -q 'All done' ; do
309 sleep 5
310 done
311
312 kill $TAIL_PID
Jesse Andrewsad21d1a2011-10-26 12:39:00 -0700313
314 if grep -q "stack.sh failed" $ROOTFS/$DEST/run.sh.log; then
315 exit 1
316 fi
317
Anthony Young25369c92011-10-14 16:50:27 -0700318 echo ""
319 echo "Finished - Zip-a-dee Doo-dah!"
320fi