blob: b97f8db15aa89fe32cb0ad239fc440b7d049fb15 [file] [log] [blame]
Anthony Young4f279222011-09-13 21:51:28 -07001#!/usr/bin/env bash
Jesse Andrews18ebd862011-09-19 14:23:42 -07002
Anthony Youngd8c259a2011-09-26 14:04:13 -07003# Use stackrc.example if stackrc is missing
4if [ ! -e ./stackrc ]; then
5 read -n1 -p "No stackrc present. Copy stackrc.example to stackrc? (y/n) "
6 echo
7 [[ $REPLY = [yY] ]] && cp stackrc.example stackrc|| { echo "Aborting: Missing stackrc"; exit 1; }
8fi
9
Anthony Young2f140202011-09-26 13:02:40 -070010# Source params
11source ./stackrc
12
Anthony Young84064da2011-09-26 16:19:50 -070013# Store cwd
14CWD=`pwd`
15
Anthony Young72d69632011-09-12 21:09:55 -070016# Configurable params
17BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -070018CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -070019CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
20CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
21CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
22CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young77dbb072011-09-14 00:49:39 -070023NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070024COPYENV=${COPYENV:-1}
25
Anthony Young1c364642011-09-13 20:21:42 -070026# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
27STACKSH_PARAMS=${STACKSH_PARAMS:-}
28
Anthony Younga34b6952011-09-26 15:24:59 -070029# Option to use the version of devstack on which we are currently working
30USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
31
Anthony Young936c9282011-09-13 23:36:43 -070032# Warn users who aren't on natty
33if ! grep -q natty /etc/lsb-release; then
34 echo "WARNING: this script has only been tested on natty"
35fi
36
Anthony Young190321e2011-09-13 23:21:29 -070037# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070038apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070039
40# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070041if ! which cgdelete | grep -q cgdelete; then
Anthony Young856d09f2011-09-19 19:49:20 -070042 apt-get install -y g++ bison flex libpam0g-dev
Anthony Young190321e2011-09-13 23:21:29 -070043 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
44 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
45 cd libcgroup-0.37.1
46 ./configure
47 make install
Dean Troyer62a6deb2011-09-21 20:06:01 -050048 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070049fi
50
Anthony Young60534962011-09-13 10:40:04 -070051# Create lxc configuration
52LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070053cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070054lxc.network.type = veth
55lxc.network.link = $BRIDGE
56lxc.network.flags = up
57lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070058# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070059lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070060EOF
61
Anthony Young60534962011-09-13 10:40:04 -070062# Shutdown any existing container
63lxc-stop -n $CONTAINER
64
Anthony Young4f279222011-09-13 21:51:28 -070065# This kills zombie containers
66if [ -d /cgroup/$CONTAINER ]; then
67 cgdelete -r cpu,net_cls:$CONTAINER
68fi
Anthony Young60534962011-09-13 10:40:04 -070069
Anthony Young2f140202011-09-26 13:02:40 -070070# git clone only if directory doesn't exist already. Since ``DEST`` might not
71# be owned by the installation user, we create the directory and change the
72# ownership to the proper user.
73function git_clone {
74 if [ ! -d $2 ]; then
75 sudo mkdir $2
76 sudo chown `whoami` $2
77 git clone $1 $2
78 cd $2
79 # This checkout syntax works for both branches and tags
80 git checkout $3
81 fi
82}
Jesse Andrews18ebd862011-09-19 14:23:42 -070083
Anthony Young60534962011-09-13 10:40:04 -070084# Warm the base image on first install
85CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young01ad91a2011-09-26 13:25:11 -070086if [ ! -d $CACHEDIR ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -070087 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
88 # lazy and doesn't do anything if a container already exists)
89 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -070090 # trigger the initial debootstrap
91 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
92 chroot $CACHEDIR apt-get update
Anthony Youngbf188ef2011-09-19 20:23:42 -070093 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 -070094 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Young60534962011-09-13 10:40:04 -070095fi
96
Anthony Young303233e2011-09-26 13:12:57 -070097# Cache openstack code
98git_clone $NOVA_REPO $CACHEDIR/opt/nova $NOVA_BRANCH
99git_clone $GLANCE_REPO $CACHEDIR/opt/glance $GLANCE_BRANCH
100git_clone $KEYSTONE_REPO $CACHEDIR/opt/keystone $KEYSTONE_BRANCH
101git_clone $NOVNC_REPO $CACHEDIR/opt/novnc $NOVNC_BRANCH
102git_clone $DASH_REPO $CACHEDIR/opt/dash $DASH_BRANCH $DASH_TAG
103git_clone $NIXON_REPO $CACHEDIR/opt/nixon $NIXON_BRANCH
104git_clone $NOVACLIENT_REPO $CACHEDIR/opt/python-novaclient $NOVACLIENT_BRANCH
105git_clone $OPENSTACKX_REPO $CACHEDIR/opt/openstackx $OPENSTACKX_BRANCH
106git_clone $MUNIN_REPO $CACHEDIR/opt/openstack-munin $MUNIN_BRANCH
107
Anthony Younga34b6952011-09-26 15:24:59 -0700108# Use this version of devstack?
109if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
110 rm -rf $CACHEDIR/opt/devstack
Anthony Young84064da2011-09-26 16:19:50 -0700111 cp -pr $CWD $CACHEDIR/opt/devstack
Anthony Younga34b6952011-09-26 15:24:59 -0700112fi
113
Anthony Young60534962011-09-13 10:40:04 -0700114# Destroy the old container
115lxc-destroy -n $CONTAINER
116
Anthony Young23761c32011-09-16 14:54:20 -0700117# If this call is to TERMINATE the container then exit
118if [ "$TERMINATE" = "1" ]; then
119 exit
120fi
121
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700122# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -0700123lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -0700124
Anthony Youngf9998ab2011-09-13 10:43:44 -0700125# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -0700126ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
127
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700128# Create a stack user that is a member of the libvirtd group so that stack
129# is able to interact with libvirt.
130chroot $ROOTFS groupadd libvirtd
131chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
132
133# a simple password - pass
134echo stack:pass | chroot $ROOTFS chpasswd
135
136# and has sudo ability (in the future this should be limited to only what
137# stack requires)
138echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
139
Dean Troyer62a6deb2011-09-21 20:06:01 -0500140# Copy kernel modules
141mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
142cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
143cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
144
Anthony Young34c47022011-09-13 22:14:37 -0700145# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700146function cp_it {
147 if [ -e $1 ] || [ -d $1 ]; then
148 cp -pr $1 $2
149 fi
150}
151
Anthony Youngb3c04542011-09-13 01:28:18 -0700152# Copy over your ssh keys and env if desired
153if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700154 cp_it ~/.ssh $ROOTFS/opt/.ssh
155 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
156 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
157 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
158 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700159fi
160
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700161# Make our ip address hostnames look nice at the command prompt
162echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/opt/.bashrc
163echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
164
Anthony Young10039522011-09-13 10:05:07 -0700165# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700166chroot $ROOTFS chown -R stack /opt
167
Anthony Young72d69632011-09-12 21:09:55 -0700168# Configure instance network
169INTERFACES=$ROOTFS/etc/network/interfaces
170cat > $INTERFACES <<EOF
171auto lo
172iface lo inet loopback
173
174auto eth0
175iface eth0 inet static
176 address $CONTAINER_IP
177 netmask $CONTAINER_NETMASK
178 gateway $CONTAINER_GATEWAY
179EOF
180
Anthony Young1c364642011-09-13 20:21:42 -0700181# Configure the runner
Anthony Young56e62922011-09-14 02:54:27 -0700182RUN_SH=$ROOTFS/opt/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700183cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700184#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700185# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700186echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700187sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700188
Anthony Young927a6562011-09-14 01:58:01 -0700189# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700190killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700191
Anthony Young99003e72011-09-13 02:05:12 -0700192# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700193sudo apt-get update
194sudo apt-get -y --force-yes install git-core vim-nox sudo
Jesse Andrews47e115e2011-09-14 11:25:47 -0700195if [ ! -d "/opt/devstack" ]; then
Anthony Young550ec962011-09-15 21:05:50 -0700196 git clone git://github.com/cloudbuilders/devstack.git /opt/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700197fi
Jesse Andrews47e115e2011-09-14 11:25:47 -0700198cd /opt/devstack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700199EOF
200
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700201# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700202chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700203
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700204# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700205RC_LOCAL=$ROOTFS/etc/rc.local
206cat > $RC_LOCAL <<EOF
207#!/bin/sh -e
Anthony Young56e62922011-09-14 02:54:27 -0700208su -c "/opt/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700209EOF
210
Anthony Young72d69632011-09-12 21:09:55 -0700211# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700212if ! mount | grep -q cgroup; then
213 mkdir -p /cgroup
214 mount none -t cgroup /cgroup
215fi
Anthony Young72d69632011-09-12 21:09:55 -0700216
217# Start our container
218lxc-start -d -n $CONTAINER