blob: 674742c3e28aa684685d981c2603cf46213aac19 [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 Young72d69632011-09-12 21:09:55 -070013# Configurable params
14BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -070015CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -070016CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
17CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
18CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
19CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young77dbb072011-09-14 00:49:39 -070020NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070021COPYENV=${COPYENV:-1}
22
Anthony Young1c364642011-09-13 20:21:42 -070023# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
24STACKSH_PARAMS=${STACKSH_PARAMS:-}
25
Anthony Younga34b6952011-09-26 15:24:59 -070026# Option to use the version of devstack on which we are currently working
27USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
28
Anthony Young936c9282011-09-13 23:36:43 -070029# Warn users who aren't on natty
30if ! grep -q natty /etc/lsb-release; then
31 echo "WARNING: this script has only been tested on natty"
32fi
33
Anthony Young190321e2011-09-13 23:21:29 -070034# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070035apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070036
37# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070038if ! which cgdelete | grep -q cgdelete; then
Anthony Young856d09f2011-09-19 19:49:20 -070039 apt-get install -y g++ bison flex libpam0g-dev
Anthony Young190321e2011-09-13 23:21:29 -070040 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
41 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
42 cd libcgroup-0.37.1
43 ./configure
44 make install
Dean Troyer62a6deb2011-09-21 20:06:01 -050045 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070046fi
47
Anthony Young60534962011-09-13 10:40:04 -070048# Create lxc configuration
49LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070050cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070051lxc.network.type = veth
52lxc.network.link = $BRIDGE
53lxc.network.flags = up
54lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070055# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070056lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070057EOF
58
Anthony Young60534962011-09-13 10:40:04 -070059# Shutdown any existing container
60lxc-stop -n $CONTAINER
61
Anthony Young4f279222011-09-13 21:51:28 -070062# This kills zombie containers
63if [ -d /cgroup/$CONTAINER ]; then
64 cgdelete -r cpu,net_cls:$CONTAINER
65fi
Anthony Young60534962011-09-13 10:40:04 -070066
Anthony Young2f140202011-09-26 13:02:40 -070067# git clone only if directory doesn't exist already. Since ``DEST`` might not
68# be owned by the installation user, we create the directory and change the
69# ownership to the proper user.
70function git_clone {
71 if [ ! -d $2 ]; then
72 sudo mkdir $2
73 sudo chown `whoami` $2
74 git clone $1 $2
75 cd $2
76 # This checkout syntax works for both branches and tags
77 git checkout $3
78 fi
79}
Jesse Andrews18ebd862011-09-19 14:23:42 -070080
Anthony Young60534962011-09-13 10:40:04 -070081# Warm the base image on first install
82CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young01ad91a2011-09-26 13:25:11 -070083if [ ! -d $CACHEDIR ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -070084 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
85 # lazy and doesn't do anything if a container already exists)
86 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -070087 # trigger the initial debootstrap
88 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
89 chroot $CACHEDIR apt-get update
Anthony Youngbf188ef2011-09-19 20:23:42 -070090 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 -070091 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Young60534962011-09-13 10:40:04 -070092fi
93
Anthony Young303233e2011-09-26 13:12:57 -070094# Cache openstack code
95git_clone $NOVA_REPO $CACHEDIR/opt/nova $NOVA_BRANCH
96git_clone $GLANCE_REPO $CACHEDIR/opt/glance $GLANCE_BRANCH
97git_clone $KEYSTONE_REPO $CACHEDIR/opt/keystone $KEYSTONE_BRANCH
98git_clone $NOVNC_REPO $CACHEDIR/opt/novnc $NOVNC_BRANCH
99git_clone $DASH_REPO $CACHEDIR/opt/dash $DASH_BRANCH $DASH_TAG
100git_clone $NIXON_REPO $CACHEDIR/opt/nixon $NIXON_BRANCH
101git_clone $NOVACLIENT_REPO $CACHEDIR/opt/python-novaclient $NOVACLIENT_BRANCH
102git_clone $OPENSTACKX_REPO $CACHEDIR/opt/openstackx $OPENSTACKX_BRANCH
103git_clone $MUNIN_REPO $CACHEDIR/opt/openstack-munin $MUNIN_BRANCH
104
Anthony Younga34b6952011-09-26 15:24:59 -0700105# Use this version of devstack?
106if [ "$USE_CURRENT_DEVSTACK" = "1" ]; then
107 rm -rf $CACHEDIR/opt/devstack
108 cp -pr . $CACHEDIR/opt/devstack
109fi
110
Anthony Young60534962011-09-13 10:40:04 -0700111# Destroy the old container
112lxc-destroy -n $CONTAINER
113
Anthony Young23761c32011-09-16 14:54:20 -0700114# If this call is to TERMINATE the container then exit
115if [ "$TERMINATE" = "1" ]; then
116 exit
117fi
118
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700119# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -0700120lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -0700121
Anthony Youngf9998ab2011-09-13 10:43:44 -0700122# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -0700123ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
124
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700125# Create a stack user that is a member of the libvirtd group so that stack
126# is able to interact with libvirt.
127chroot $ROOTFS groupadd libvirtd
128chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
129
130# a simple password - pass
131echo stack:pass | chroot $ROOTFS chpasswd
132
133# and has sudo ability (in the future this should be limited to only what
134# stack requires)
135echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
136
Dean Troyer62a6deb2011-09-21 20:06:01 -0500137# Copy kernel modules
138mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
139cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
140cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
141
Anthony Young34c47022011-09-13 22:14:37 -0700142# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700143function cp_it {
144 if [ -e $1 ] || [ -d $1 ]; then
145 cp -pr $1 $2
146 fi
147}
148
Anthony Youngb3c04542011-09-13 01:28:18 -0700149# Copy over your ssh keys and env if desired
150if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700151 cp_it ~/.ssh $ROOTFS/opt/.ssh
152 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
153 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
154 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
155 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700156fi
157
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700158# Make our ip address hostnames look nice at the command prompt
159echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/opt/.bashrc
160echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
161
Anthony Young10039522011-09-13 10:05:07 -0700162# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700163chroot $ROOTFS chown -R stack /opt
164
Anthony Young72d69632011-09-12 21:09:55 -0700165# Configure instance network
166INTERFACES=$ROOTFS/etc/network/interfaces
167cat > $INTERFACES <<EOF
168auto lo
169iface lo inet loopback
170
171auto eth0
172iface eth0 inet static
173 address $CONTAINER_IP
174 netmask $CONTAINER_NETMASK
175 gateway $CONTAINER_GATEWAY
176EOF
177
Anthony Young1c364642011-09-13 20:21:42 -0700178# Configure the runner
Anthony Young56e62922011-09-14 02:54:27 -0700179RUN_SH=$ROOTFS/opt/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700180cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700181#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700182# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700183echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700184sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700185
Anthony Young927a6562011-09-14 01:58:01 -0700186# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700187killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700188
Anthony Young99003e72011-09-13 02:05:12 -0700189# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700190sudo apt-get update
191sudo apt-get -y --force-yes install git-core vim-nox sudo
Jesse Andrews47e115e2011-09-14 11:25:47 -0700192if [ ! -d "/opt/devstack" ]; then
Anthony Young550ec962011-09-15 21:05:50 -0700193 git clone git://github.com/cloudbuilders/devstack.git /opt/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700194fi
Jesse Andrews47e115e2011-09-14 11:25:47 -0700195cd /opt/devstack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700196EOF
197
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700198# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700199chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700200
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700201# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700202RC_LOCAL=$ROOTFS/etc/rc.local
203cat > $RC_LOCAL <<EOF
204#!/bin/sh -e
Anthony Young56e62922011-09-14 02:54:27 -0700205su -c "/opt/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700206EOF
207
Anthony Young72d69632011-09-12 21:09:55 -0700208# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700209if ! mount | grep -q cgroup; then
210 mkdir -p /cgroup
211 mount none -t cgroup /cgroup
212fi
Anthony Young72d69632011-09-12 21:09:55 -0700213
214# Start our container
215lxc-start -d -n $CONTAINER