blob: fbdfcd343dfc42a835ba752790073cbf4707746b [file] [log] [blame]
Anthony Young4f279222011-09-13 21:51:28 -07001#!/usr/bin/env bash
Jesse Andrews18ebd862011-09-19 14:23:42 -07002
Anthony Young72d69632011-09-12 21:09:55 -07003# Configurable params
4BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -07005CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -07006CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
7CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
8CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
9CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young77dbb072011-09-14 00:49:39 -070010NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070011COPYENV=${COPYENV:-1}
12
Anthony Young1c364642011-09-13 20:21:42 -070013# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
14STACKSH_PARAMS=${STACKSH_PARAMS:-}
15
Anthony Young936c9282011-09-13 23:36:43 -070016# Warn users who aren't on natty
17if ! grep -q natty /etc/lsb-release; then
18 echo "WARNING: this script has only been tested on natty"
19fi
20
Anthony Young190321e2011-09-13 23:21:29 -070021# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070022apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070023
24# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070025if ! which cgdelete | grep -q cgdelete; then
Anthony Young856d09f2011-09-19 19:49:20 -070026 apt-get install -y g++ bison flex libpam0g-dev
Anthony Young190321e2011-09-13 23:21:29 -070027 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
28 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
29 cd libcgroup-0.37.1
30 ./configure
31 make install
Anthony Young4f279222011-09-13 21:51:28 -070032fi
33
Anthony Young60534962011-09-13 10:40:04 -070034# Create lxc configuration
35LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070036cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070037lxc.network.type = veth
38lxc.network.link = $BRIDGE
39lxc.network.flags = up
40lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070041# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070042lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070043EOF
44
Anthony Young60534962011-09-13 10:40:04 -070045# Shutdown any existing container
46lxc-stop -n $CONTAINER
47
Anthony Young4f279222011-09-13 21:51:28 -070048# This kills zombie containers
49if [ -d /cgroup/$CONTAINER ]; then
50 cgdelete -r cpu,net_cls:$CONTAINER
51fi
Anthony Young60534962011-09-13 10:40:04 -070052
Jesse Andrews18ebd862011-09-19 14:23:42 -070053
Anthony Young60534962011-09-13 10:40:04 -070054# Warm the base image on first install
55CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young40f8d162011-09-14 10:20:14 -070056if [ ! -d $CACHEDIR ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -070057 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
58 # lazy and doesn't do anything if a container already exists)
59 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -070060 # trigger the initial debootstrap
61 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
62 chroot $CACHEDIR apt-get update
Anthony Youngbf188ef2011-09-19 20:23:42 -070063 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 -070064 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Youngdb8f7f72011-09-20 02:12:46 -070065 # FIXME (anthony) - provide ability to vary source locations
66 #git clone https://github.com/cloudbuilders/nova.git $CACHEDIR/opt/nova
67 bzr clone lp:~hudson-openstack/nova/milestone-proposed/ $CACHEDIR/opt/nova
Anthony Young3859f732011-09-14 02:33:43 -070068 git clone https://github.com/cloudbuilders/openstackx.git $CACHEDIR/opt/openstackx
69 git clone https://github.com/cloudbuilders/noVNC.git $CACHEDIR/opt/noVNC
70 git clone https://github.com/cloudbuilders/openstack-dashboard.git $CACHEDIR/opt/dash
71 git clone https://github.com/cloudbuilders/python-novaclient.git $CACHEDIR/opt/python-novaclient
72 git clone https://github.com/cloudbuilders/keystone.git $CACHEDIR/opt/keystone
73 git clone https://github.com/cloudbuilders/glance.git $CACHEDIR/opt/glance
Anthony Young60534962011-09-13 10:40:04 -070074fi
75
76# Destroy the old container
77lxc-destroy -n $CONTAINER
78
Anthony Young23761c32011-09-16 14:54:20 -070079# If this call is to TERMINATE the container then exit
80if [ "$TERMINATE" = "1" ]; then
81 exit
82fi
83
Anthony Young7c3e5ed2011-09-13 09:57:31 -070084# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070085lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070086
Anthony Youngf9998ab2011-09-13 10:43:44 -070087# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070088ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
89
Anthony Young7c3e5ed2011-09-13 09:57:31 -070090# Create a stack user that is a member of the libvirtd group so that stack
91# is able to interact with libvirt.
92chroot $ROOTFS groupadd libvirtd
93chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
94
95# a simple password - pass
96echo stack:pass | chroot $ROOTFS chpasswd
97
98# and has sudo ability (in the future this should be limited to only what
99# stack requires)
100echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
101
Anthony Young34c47022011-09-13 22:14:37 -0700102# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700103function cp_it {
104 if [ -e $1 ] || [ -d $1 ]; then
105 cp -pr $1 $2
106 fi
107}
108
Anthony Youngb3c04542011-09-13 01:28:18 -0700109# Copy over your ssh keys and env if desired
110if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700111 cp_it ~/.ssh $ROOTFS/opt/.ssh
112 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
113 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
114 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
115 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700116fi
117
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700118# Make our ip address hostnames look nice at the command prompt
119echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/opt/.bashrc
120echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
121
Anthony Young10039522011-09-13 10:05:07 -0700122# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700123chroot $ROOTFS chown -R stack /opt
124
Anthony Young72d69632011-09-12 21:09:55 -0700125# Configure instance network
126INTERFACES=$ROOTFS/etc/network/interfaces
127cat > $INTERFACES <<EOF
128auto lo
129iface lo inet loopback
130
131auto eth0
132iface eth0 inet static
133 address $CONTAINER_IP
134 netmask $CONTAINER_NETMASK
135 gateway $CONTAINER_GATEWAY
136EOF
137
Anthony Young1c364642011-09-13 20:21:42 -0700138# Configure the runner
Anthony Young56e62922011-09-14 02:54:27 -0700139RUN_SH=$ROOTFS/opt/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700140cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700141#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700142# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700143echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700144sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700145
Anthony Young927a6562011-09-14 01:58:01 -0700146# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700147killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700148
Anthony Young99003e72011-09-13 02:05:12 -0700149# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700150sudo apt-get update
151sudo apt-get -y --force-yes install git-core vim-nox sudo
Jesse Andrews47e115e2011-09-14 11:25:47 -0700152if [ ! -d "/opt/devstack" ]; then
Anthony Young550ec962011-09-15 21:05:50 -0700153 git clone git://github.com/cloudbuilders/devstack.git /opt/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700154fi
Jesse Andrews47e115e2011-09-14 11:25:47 -0700155cd /opt/devstack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700156EOF
157
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700158# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700159chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700160
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700161# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700162RC_LOCAL=$ROOTFS/etc/rc.local
163cat > $RC_LOCAL <<EOF
164#!/bin/sh -e
Anthony Young56e62922011-09-14 02:54:27 -0700165su -c "/opt/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700166EOF
167
Anthony Young72d69632011-09-12 21:09:55 -0700168# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700169if ! mount | grep -q cgroup; then
170 mkdir -p /cgroup
171 mount none -t cgroup /cgroup
172fi
Anthony Young72d69632011-09-12 21:09:55 -0700173
174# Start our container
175lxc-start -d -n $CONTAINER