blob: b5de23266cc213cb7d1210e9ea7b60bc0bc5e23e [file] [log] [blame]
Anthony Young4f279222011-09-13 21:51:28 -07001#!/usr/bin/env bash
Anthony Young72d69632011-09-12 21:09:55 -07002# Configurable params
3BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -07004CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -07005CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
6CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
7CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
8CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young77dbb072011-09-14 00:49:39 -07009NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070010COPYENV=${COPYENV:-1}
11
Anthony Young1c364642011-09-13 20:21:42 -070012# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
13STACKSH_PARAMS=${STACKSH_PARAMS:-}
14
Anthony Young936c9282011-09-13 23:36:43 -070015# Warn users who aren't on natty
16if ! grep -q natty /etc/lsb-release; then
17 echo "WARNING: this script has only been tested on natty"
18fi
19
Anthony Young190321e2011-09-13 23:21:29 -070020# Install deps
21apt-get install lxc debootstrap
22
23# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070024if ! which cgdelete | grep -q cgdelete; then
Anthony Young190321e2011-09-13 23:21:29 -070025 apt-get install g++ bison flex libpam0g-dev
26 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
27 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
28 cd libcgroup-0.37.1
29 ./configure
30 make install
Anthony Young4f279222011-09-13 21:51:28 -070031fi
32
Anthony Young60534962011-09-13 10:40:04 -070033# Create lxc configuration
34LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070035cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070036lxc.network.type = veth
37lxc.network.link = $BRIDGE
38lxc.network.flags = up
39lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070040# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070041lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070042EOF
43
Anthony Young60534962011-09-13 10:40:04 -070044# Shutdown any existing container
45lxc-stop -n $CONTAINER
46
Anthony Young4f279222011-09-13 21:51:28 -070047# This kills zombie containers
48if [ -d /cgroup/$CONTAINER ]; then
49 cgdelete -r cpu,net_cls:$CONTAINER
50fi
Anthony Young60534962011-09-13 10:40:04 -070051
52# Warm the base image on first install
53CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young40f8d162011-09-14 10:20:14 -070054if [ ! -d $CACHEDIR ]; then
Anthony Young60534962011-09-13 10:40:04 -070055 # trigger the initial debootstrap
56 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
57 chroot $CACHEDIR apt-get update
58 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
59 chroot $CACHEDIR pip install `cat pips/*`
Anthony Young3859f732011-09-14 02:33:43 -070060 git clone https://github.com/cloudbuilders/nova.git $CACHEDIR/opt/nova
61 git clone https://github.com/cloudbuilders/openstackx.git $CACHEDIR/opt/openstackx
62 git clone https://github.com/cloudbuilders/noVNC.git $CACHEDIR/opt/noVNC
63 git clone https://github.com/cloudbuilders/openstack-dashboard.git $CACHEDIR/opt/dash
64 git clone https://github.com/cloudbuilders/python-novaclient.git $CACHEDIR/opt/python-novaclient
65 git clone https://github.com/cloudbuilders/keystone.git $CACHEDIR/opt/keystone
66 git clone https://github.com/cloudbuilders/glance.git $CACHEDIR/opt/glance
Anthony Young60534962011-09-13 10:40:04 -070067fi
68
69# Destroy the old container
70lxc-destroy -n $CONTAINER
71
Anthony Young7c3e5ed2011-09-13 09:57:31 -070072# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070073lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070074
Anthony Youngf9998ab2011-09-13 10:43:44 -070075# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070076ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
77
Anthony Young7c3e5ed2011-09-13 09:57:31 -070078# Create a stack user that is a member of the libvirtd group so that stack
79# is able to interact with libvirt.
80chroot $ROOTFS groupadd libvirtd
81chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
82
83# a simple password - pass
84echo stack:pass | chroot $ROOTFS chpasswd
85
86# and has sudo ability (in the future this should be limited to only what
87# stack requires)
88echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
89
Anthony Young34c47022011-09-13 22:14:37 -070090# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -070091function cp_it {
92 if [ -e $1 ] || [ -d $1 ]; then
93 cp -pr $1 $2
94 fi
95}
96
Anthony Youngb3c04542011-09-13 01:28:18 -070097# Copy over your ssh keys and env if desired
98if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -070099 cp_it ~/.ssh $ROOTFS/opt/.ssh
100 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
101 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
102 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
103 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700104fi
105
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700106# Make our ip address hostnames look nice at the command prompt
107echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/opt/.bashrc
108echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
109
Anthony Young10039522011-09-13 10:05:07 -0700110# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700111chroot $ROOTFS chown -R stack /opt
112
Anthony Young72d69632011-09-12 21:09:55 -0700113# Configure instance network
114INTERFACES=$ROOTFS/etc/network/interfaces
115cat > $INTERFACES <<EOF
116auto lo
117iface lo inet loopback
118
119auto eth0
120iface eth0 inet static
121 address $CONTAINER_IP
122 netmask $CONTAINER_NETMASK
123 gateway $CONTAINER_GATEWAY
124EOF
125
Anthony Young1c364642011-09-13 20:21:42 -0700126# Configure the runner
Anthony Young56e62922011-09-14 02:54:27 -0700127RUN_SH=$ROOTFS/opt/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700128cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700129#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700130# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700131echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700132sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700133
Anthony Young927a6562011-09-14 01:58:01 -0700134# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700135killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700136
Anthony Young99003e72011-09-13 02:05:12 -0700137# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700138sudo apt-get update
139sudo apt-get -y --force-yes install git-core vim-nox sudo
Jesse Andrews47e115e2011-09-14 11:25:47 -0700140if [ ! -d "/opt/devstack" ]; then
Anthony Young550ec962011-09-15 21:05:50 -0700141 git clone git://github.com/cloudbuilders/devstack.git /opt/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700142fi
Jesse Andrews47e115e2011-09-14 11:25:47 -0700143cd /opt/devstack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700144EOF
145
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700146# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700147chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700148
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700149# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700150RC_LOCAL=$ROOTFS/etc/rc.local
151cat > $RC_LOCAL <<EOF
152#!/bin/sh -e
Anthony Young56e62922011-09-14 02:54:27 -0700153su -c "/opt/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700154EOF
155
Anthony Young72d69632011-09-12 21:09:55 -0700156# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700157if ! mount | grep -q cgroup; then
158 mkdir -p /cgroup
159 mount none -t cgroup /cgroup
160fi
Anthony Young72d69632011-09-12 21:09:55 -0700161
162# Start our container
163lxc-start -d -n $CONTAINER