blob: c1ddd950ba76f0f5c94e8e0c6f223b2f9ac1fdd4 [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
22apt-get install lxc debootstrap
23
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 Young190321e2011-09-13 23:21:29 -070026 apt-get install g++ bison flex libpam0g-dev
27 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
Jesse Andrews4d282182011-09-16 11:27:43 -070063 chroot $CACHEDIR apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
64 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Young3859f732011-09-14 02:33:43 -070065 git clone https://github.com/cloudbuilders/nova.git $CACHEDIR/opt/nova
66 git clone https://github.com/cloudbuilders/openstackx.git $CACHEDIR/opt/openstackx
67 git clone https://github.com/cloudbuilders/noVNC.git $CACHEDIR/opt/noVNC
68 git clone https://github.com/cloudbuilders/openstack-dashboard.git $CACHEDIR/opt/dash
69 git clone https://github.com/cloudbuilders/python-novaclient.git $CACHEDIR/opt/python-novaclient
70 git clone https://github.com/cloudbuilders/keystone.git $CACHEDIR/opt/keystone
71 git clone https://github.com/cloudbuilders/glance.git $CACHEDIR/opt/glance
Anthony Young60534962011-09-13 10:40:04 -070072fi
73
74# Destroy the old container
75lxc-destroy -n $CONTAINER
76
Anthony Young23761c32011-09-16 14:54:20 -070077# If this call is to TERMINATE the container then exit
78if [ "$TERMINATE" = "1" ]; then
79 exit
80fi
81
Anthony Young7c3e5ed2011-09-13 09:57:31 -070082# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070083lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070084
Anthony Youngf9998ab2011-09-13 10:43:44 -070085# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070086ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
87
Anthony Young7c3e5ed2011-09-13 09:57:31 -070088# Create a stack user that is a member of the libvirtd group so that stack
89# is able to interact with libvirt.
90chroot $ROOTFS groupadd libvirtd
91chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
92
93# a simple password - pass
94echo stack:pass | chroot $ROOTFS chpasswd
95
96# and has sudo ability (in the future this should be limited to only what
97# stack requires)
98echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
99
Anthony Young34c47022011-09-13 22:14:37 -0700100# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700101function cp_it {
102 if [ -e $1 ] || [ -d $1 ]; then
103 cp -pr $1 $2
104 fi
105}
106
Anthony Youngb3c04542011-09-13 01:28:18 -0700107# Copy over your ssh keys and env if desired
108if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700109 cp_it ~/.ssh $ROOTFS/opt/.ssh
110 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
111 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
112 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
113 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700114fi
115
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700116# Make our ip address hostnames look nice at the command prompt
117echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/opt/.bashrc
118echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
119
Anthony Young10039522011-09-13 10:05:07 -0700120# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700121chroot $ROOTFS chown -R stack /opt
122
Anthony Young72d69632011-09-12 21:09:55 -0700123# Configure instance network
124INTERFACES=$ROOTFS/etc/network/interfaces
125cat > $INTERFACES <<EOF
126auto lo
127iface lo inet loopback
128
129auto eth0
130iface eth0 inet static
131 address $CONTAINER_IP
132 netmask $CONTAINER_NETMASK
133 gateway $CONTAINER_GATEWAY
134EOF
135
Anthony Young1c364642011-09-13 20:21:42 -0700136# Configure the runner
Anthony Young56e62922011-09-14 02:54:27 -0700137RUN_SH=$ROOTFS/opt/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700138cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700139#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700140# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700141echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700142sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700143
Anthony Young927a6562011-09-14 01:58:01 -0700144# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700145killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700146
Anthony Young99003e72011-09-13 02:05:12 -0700147# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700148sudo apt-get update
149sudo apt-get -y --force-yes install git-core vim-nox sudo
Jesse Andrews47e115e2011-09-14 11:25:47 -0700150if [ ! -d "/opt/devstack" ]; then
Anthony Young550ec962011-09-15 21:05:50 -0700151 git clone git://github.com/cloudbuilders/devstack.git /opt/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700152fi
Jesse Andrews47e115e2011-09-14 11:25:47 -0700153cd /opt/devstack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700154EOF
155
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700156# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700157chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700158
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700159# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700160RC_LOCAL=$ROOTFS/etc/rc.local
161cat > $RC_LOCAL <<EOF
162#!/bin/sh -e
Anthony Young56e62922011-09-14 02:54:27 -0700163su -c "/opt/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700164EOF
165
Anthony Young72d69632011-09-12 21:09:55 -0700166# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700167if ! mount | grep -q cgroup; then
168 mkdir -p /cgroup
169 mount none -t cgroup /cgroup
170fi
Anthony Young72d69632011-09-12 21:09:55 -0700171
172# Start our container
173lxc-start -d -n $CONTAINER