blob: e8612e6989e3525aa5a501d3ecfb5b451c3b811a [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
Dean Troyer62a6deb2011-09-21 20:06:01 -050032 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070033fi
34
Anthony Young60534962011-09-13 10:40:04 -070035# Create lxc configuration
36LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070037cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070038lxc.network.type = veth
39lxc.network.link = $BRIDGE
40lxc.network.flags = up
41lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070042# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070043lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070044EOF
45
Anthony Young60534962011-09-13 10:40:04 -070046# Shutdown any existing container
47lxc-stop -n $CONTAINER
48
Anthony Young4f279222011-09-13 21:51:28 -070049# This kills zombie containers
50if [ -d /cgroup/$CONTAINER ]; then
51 cgdelete -r cpu,net_cls:$CONTAINER
52fi
Anthony Young60534962011-09-13 10:40:04 -070053
Jesse Andrews18ebd862011-09-19 14:23:42 -070054
Anthony Young60534962011-09-13 10:40:04 -070055# Warm the base image on first install
56CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young40f8d162011-09-14 10:20:14 -070057if [ ! -d $CACHEDIR ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -070058 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
59 # lazy and doesn't do anything if a container already exists)
60 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -070061 # trigger the initial debootstrap
62 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
63 chroot $CACHEDIR apt-get update
Anthony Youngbf188ef2011-09-19 20:23:42 -070064 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 -070065 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Youngdb8f7f72011-09-20 02:12:46 -070066 # FIXME (anthony) - provide ability to vary source locations
67 #git clone https://github.com/cloudbuilders/nova.git $CACHEDIR/opt/nova
68 bzr clone lp:~hudson-openstack/nova/milestone-proposed/ $CACHEDIR/opt/nova
Anthony Young3859f732011-09-14 02:33:43 -070069 git clone https://github.com/cloudbuilders/openstackx.git $CACHEDIR/opt/openstackx
70 git clone https://github.com/cloudbuilders/noVNC.git $CACHEDIR/opt/noVNC
71 git clone https://github.com/cloudbuilders/openstack-dashboard.git $CACHEDIR/opt/dash
72 git clone https://github.com/cloudbuilders/python-novaclient.git $CACHEDIR/opt/python-novaclient
73 git clone https://github.com/cloudbuilders/keystone.git $CACHEDIR/opt/keystone
74 git clone https://github.com/cloudbuilders/glance.git $CACHEDIR/opt/glance
Anthony Young60534962011-09-13 10:40:04 -070075fi
76
77# Destroy the old container
78lxc-destroy -n $CONTAINER
79
Anthony Young23761c32011-09-16 14:54:20 -070080# If this call is to TERMINATE the container then exit
81if [ "$TERMINATE" = "1" ]; then
82 exit
83fi
84
Anthony Young7c3e5ed2011-09-13 09:57:31 -070085# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070086lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070087
Anthony Youngf9998ab2011-09-13 10:43:44 -070088# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070089ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
90
Anthony Young7c3e5ed2011-09-13 09:57:31 -070091# Create a stack user that is a member of the libvirtd group so that stack
92# is able to interact with libvirt.
93chroot $ROOTFS groupadd libvirtd
94chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
95
96# a simple password - pass
97echo stack:pass | chroot $ROOTFS chpasswd
98
99# and has sudo ability (in the future this should be limited to only what
100# stack requires)
101echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
102
Dean Troyer62a6deb2011-09-21 20:06:01 -0500103# Copy kernel modules
104mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
105cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
106cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
107
Anthony Young34c47022011-09-13 22:14:37 -0700108# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700109function cp_it {
110 if [ -e $1 ] || [ -d $1 ]; then
111 cp -pr $1 $2
112 fi
113}
114
Anthony Youngb3c04542011-09-13 01:28:18 -0700115# Copy over your ssh keys and env if desired
116if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700117 cp_it ~/.ssh $ROOTFS/opt/.ssh
118 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
119 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
120 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
121 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700122fi
123
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700124# Make our ip address hostnames look nice at the command prompt
125echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/opt/.bashrc
126echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
127
Anthony Young10039522011-09-13 10:05:07 -0700128# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700129chroot $ROOTFS chown -R stack /opt
130
Anthony Young72d69632011-09-12 21:09:55 -0700131# Configure instance network
132INTERFACES=$ROOTFS/etc/network/interfaces
133cat > $INTERFACES <<EOF
134auto lo
135iface lo inet loopback
136
137auto eth0
138iface eth0 inet static
139 address $CONTAINER_IP
140 netmask $CONTAINER_NETMASK
141 gateway $CONTAINER_GATEWAY
142EOF
143
Anthony Young1c364642011-09-13 20:21:42 -0700144# Configure the runner
Anthony Young56e62922011-09-14 02:54:27 -0700145RUN_SH=$ROOTFS/opt/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700146cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700147#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700148# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700149echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700150sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700151
Anthony Young927a6562011-09-14 01:58:01 -0700152# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700153killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700154
Anthony Young99003e72011-09-13 02:05:12 -0700155# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700156sudo apt-get update
157sudo apt-get -y --force-yes install git-core vim-nox sudo
Jesse Andrews47e115e2011-09-14 11:25:47 -0700158if [ ! -d "/opt/devstack" ]; then
Anthony Young550ec962011-09-15 21:05:50 -0700159 git clone git://github.com/cloudbuilders/devstack.git /opt/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700160fi
Jesse Andrews47e115e2011-09-14 11:25:47 -0700161cd /opt/devstack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700162EOF
163
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700164# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700165chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700166
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700167# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700168RC_LOCAL=$ROOTFS/etc/rc.local
169cat > $RC_LOCAL <<EOF
170#!/bin/sh -e
Anthony Young56e62922011-09-14 02:54:27 -0700171su -c "/opt/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700172EOF
173
Anthony Young72d69632011-09-12 21:09:55 -0700174# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700175if ! mount | grep -q cgroup; then
176 mkdir -p /cgroup
177 mount none -t cgroup /cgroup
178fi
Anthony Young72d69632011-09-12 21:09:55 -0700179
180# Start our container
181lxc-start -d -n $CONTAINER