blob: d4df344d5c7e9f8ac1bec2f0f70cc7c6806cd560 [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
9# Warn users who aren't on natty
10if ! grep -q natty /etc/lsb-release; then
11 echo "WARNING: this script has only been tested on natty"
12fi
13
Anthony Younge8efef72011-10-14 12:00:50 -070014# Move to top devstack dir
15cd ..
16
Anthony Youngcf145b72011-10-13 15:07:36 -070017# Abort if localrc is not set
18if [ ! -e ./localrc ]; then
19 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
20 echo "See stack.sh for required passwords."
21 exit 1
22fi
23
Anthony Young2f140202011-09-26 13:02:40 -070024# Source params
25source ./stackrc
26
Anthony Young84064da2011-09-26 16:19:50 -070027# Store cwd
28CWD=`pwd`
29
Anthony Young72d69632011-09-12 21:09:55 -070030# Configurable params
31BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -070032CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -070033CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
34CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
35CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
36CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young77dbb072011-09-14 00:49:39 -070037NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070038COPYENV=${COPYENV:-1}
Anthony Younge8fed482011-09-26 19:50:43 -070039DEST=${DEST:-/opt/stack}
Anthony Young72d69632011-09-12 21:09:55 -070040
Anthony Young1c364642011-09-13 20:21:42 -070041# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
42STACKSH_PARAMS=${STACKSH_PARAMS:-}
43
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
63LXC_CONF=/tmp/$CONTAINER.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
68lxc.network.ipv4 = $CONTAINER_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
74lxc-stop -n $CONTAINER
75
Anthony Young4f279222011-09-13 21:51:28 -070076# This kills zombie containers
77if [ -d /cgroup/$CONTAINER ]; then
78 cgdelete -r cpu,net_cls:$CONTAINER
79fi
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 Youngbeab6392011-09-28 15:12:18 -070095# Location of the base image directory
Anthony Young60534962011-09-13 10:40:04 -070096CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Youngbeab6392011-09-28 15:12:18 -070097
98# Provide option to do totally clean install
99if [ "$CLEAR_LXC_CACHE" = "1" ]; then
100 rm -rf $CACHEDIR
101fi
102
103# Warm the base image on first install
104if [ ! -f $CACHEDIR/bootstrapped ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -0700105 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
106 # lazy and doesn't do anything if a container already exists)
107 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -0700108 # trigger the initial debootstrap
109 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
110 chroot $CACHEDIR apt-get update
Jesse Andrews9b6741e2011-10-02 10:01:00 -0700111 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 -0700112 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Youngbeab6392011-09-28 15:12:18 -0700113 touch $CACHEDIR/bootstrapped
Anthony Young60534962011-09-13 10:40:04 -0700114fi
115
Anthony Younge8fed482011-09-26 19:50:43 -0700116# Clean out code repos if directed to do so
117if [ "$CLEAN" = "1" ]; then
Anthony Young4f10de82011-09-26 20:03:40 -0700118 rm -rf $CACHEDIR/$DEST
Anthony Younge8fed482011-09-26 19:50:43 -0700119fi
120
Anthony Young303233e2011-09-26 13:12:57 -0700121# Cache openstack code
Anthony Younge8fed482011-09-26 19:50:43 -0700122mkdir -p $CACHEDIR/$DEST
123git_clone $NOVA_REPO $CACHEDIR/$DEST/nova $NOVA_BRANCH
124git_clone $GLANCE_REPO $CACHEDIR/$DEST/glance $GLANCE_BRANCH
125git_clone $KEYSTONE_REPO $CACHEDIR/$DESTkeystone $KEYSTONE_BRANCH
126git_clone $NOVNC_REPO $CACHEDIR/$DEST/novnc $NOVNC_BRANCH
127git_clone $DASH_REPO $CACHEDIR/$DEST/dash $DASH_BRANCH $DASH_TAG
Anthony Younge8fed482011-09-26 19:50:43 -0700128git_clone $NOVACLIENT_REPO $CACHEDIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
129git_clone $OPENSTACKX_REPO $CACHEDIR/$DEST/openstackx $OPENSTACKX_BRANCH
Anthony Young303233e2011-09-26 13:12:57 -0700130
Anthony Younga34b6952011-09-26 15:24:59 -0700131# Use this version of devstack?
132if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700133 rm -rf $CACHEDIR/$DEST/devstack
134 cp -pr $CWD $CACHEDIR/$DEST/devstack
Anthony Younga34b6952011-09-26 15:24:59 -0700135fi
136
Anthony Young60534962011-09-13 10:40:04 -0700137# Destroy the old container
138lxc-destroy -n $CONTAINER
139
Anthony Young23761c32011-09-16 14:54:20 -0700140# If this call is to TERMINATE the container then exit
141if [ "$TERMINATE" = "1" ]; then
142 exit
143fi
144
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700145# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -0700146lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -0700147
Anthony Youngf9998ab2011-09-13 10:43:44 -0700148# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -0700149ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
150
termiebd550ed2011-09-28 16:54:25 -0500151# Create a stack user that is a member of the libvirtd group so that stack
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700152# is able to interact with libvirt.
153chroot $ROOTFS groupadd libvirtd
Anthony Younge8fed482011-09-26 19:50:43 -0700154chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700155
156# a simple password - pass
157echo stack:pass | chroot $ROOTFS chpasswd
158
termiebd550ed2011-09-28 16:54:25 -0500159# and has sudo ability (in the future this should be limited to only what
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700160# stack requires)
161echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
162
Dean Troyer62a6deb2011-09-21 20:06:01 -0500163# Copy kernel modules
164mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
165cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
166cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
167
Anthony Young34c47022011-09-13 22:14:37 -0700168# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700169function cp_it {
170 if [ -e $1 ] || [ -d $1 ]; then
termie1c7f0c92011-09-28 17:09:00 -0500171 cp -pRL $1 $2
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700172 fi
173}
174
Anthony Youngb3c04542011-09-13 01:28:18 -0700175# Copy over your ssh keys and env if desired
176if [ "$COPYENV" = "1" ]; then
Anthony Younge8fed482011-09-26 19:50:43 -0700177 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
178 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
179 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
180 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
181 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700182fi
183
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700184# Make our ip address hostnames look nice at the command prompt
Anthony Younge8fed482011-09-26 19:50:43 -0700185echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700186echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
187
Anthony Youngadbe9c12011-09-26 19:58:49 -0700188# Give stack ownership over $DEST so it may do the work needed
189chroot $ROOTFS chown -R stack $DEST
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700190
Anthony Young72d69632011-09-12 21:09:55 -0700191# Configure instance network
192INTERFACES=$ROOTFS/etc/network/interfaces
193cat > $INTERFACES <<EOF
194auto lo
195iface lo inet loopback
196
197auto eth0
198iface eth0 inet static
199 address $CONTAINER_IP
200 netmask $CONTAINER_NETMASK
201 gateway $CONTAINER_GATEWAY
202EOF
203
Anthony Young1c364642011-09-13 20:21:42 -0700204# Configure the runner
Anthony Younge8fed482011-09-26 19:50:43 -0700205RUN_SH=$ROOTFS/$DEST/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700206cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700207#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700208# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700209echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700210sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700211
Anthony Young927a6562011-09-14 01:58:01 -0700212# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700213killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700214
Anthony Young99003e72011-09-13 02:05:12 -0700215# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700216sudo apt-get update
217sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngadbe9c12011-09-26 19:58:49 -0700218if [ ! -d "$DEST/devstack" ]; then
219 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700220fi
Anthony Youngadbe9c12011-09-26 19:58:49 -0700221cd $DEST/devstack && $STACKSH_PARAMS ./stack.sh > /$DEST/run.sh.log
termie73774442011-09-28 19:02:28 -0500222echo >> /$DEST/run.sh.log
223echo >> /$DEST/run.sh.log
224echo "All done! Time to start clicking." >> /$DEST/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700225EOF
226
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700227# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700228chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700229
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700230# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700231RC_LOCAL=$ROOTFS/etc/rc.local
232cat > $RC_LOCAL <<EOF
233#!/bin/sh -e
Anthony Youngadbe9c12011-09-26 19:58:49 -0700234su -c "$DEST/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700235EOF
236
Anthony Young72d69632011-09-12 21:09:55 -0700237# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700238if ! mount | grep -q cgroup; then
239 mkdir -p /cgroup
240 mount none -t cgroup /cgroup
241fi
Anthony Young72d69632011-09-12 21:09:55 -0700242
243# Start our container
244lxc-start -d -n $CONTAINER
termie73774442011-09-28 19:02:28 -0500245
246# Done creating the container, let's tail the log
247echo
248echo "============================================================="
249echo " -- YAY! --"
250echo "============================================================="
251echo
252echo "We're done creating the container, about to start tailing the"
253echo "stack.sh log. It will take a second or two to start."
254echo
255echo "Just CTRL-C at any time to stop tailing."
256
257while [ ! -e "$ROOTFS/$DEST/run.sh.log" ]; do
258 sleep 1
259done
260
Anthony Youngd1b1cdb2011-10-13 15:25:38 -0700261tail -F $ROOTFS/$DEST/run.sh.log