blob: 88a7443bb8b31817f3d98265cfb4af56c48c8dc0 [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 Young3859f732011-09-14 02:33:43 -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 Young10039522011-09-13 10:05:07 -0700106# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700107chroot $ROOTFS chown -R stack /opt
108
Anthony Young72d69632011-09-12 21:09:55 -0700109# Configure instance network
110INTERFACES=$ROOTFS/etc/network/interfaces
111cat > $INTERFACES <<EOF
112auto lo
113iface lo inet loopback
114
115auto eth0
116iface eth0 inet static
117 address $CONTAINER_IP
118 netmask $CONTAINER_NETMASK
119 gateway $CONTAINER_GATEWAY
120EOF
121
Anthony Young1c364642011-09-13 20:21:42 -0700122# Configure the runner
Anthony Young56e62922011-09-14 02:54:27 -0700123RUN_SH=$ROOTFS/opt/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700124cat > $RUN_SH <<EOF
Anthony Young72d69632011-09-12 21:09:55 -0700125#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700126# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700127echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700128sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700129
Anthony Young927a6562011-09-14 01:58:01 -0700130# Kill any existing screens
Anthony Young56e62922011-09-14 02:54:27 -0700131su -c "killall screen" stack
Anthony Young927a6562011-09-14 01:58:01 -0700132
Anthony Young99003e72011-09-13 02:05:12 -0700133# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700134sudo apt-get update
135sudo apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngd74d6812011-09-14 01:35:54 -0700136if [ ! -d "/opt/nfs-stack" ]; then
Anthony Young56e62922011-09-14 02:54:27 -0700137 git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack
Anthony Young1c364642011-09-13 20:21:42 -0700138fi
Anthony Young56e62922011-09-14 02:54:27 -0700139cd /opt/nfs-stack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700140EOF
141
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700142# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700143chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700144
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700145# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700146RC_LOCAL=$ROOTFS/etc/rc.local
147cat > $RC_LOCAL <<EOF
148#!/bin/sh -e
Anthony Young56e62922011-09-14 02:54:27 -0700149su -c "/opt/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700150EOF
151
Anthony Young72d69632011-09-12 21:09:55 -0700152# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700153if ! mount | grep -q cgroup; then
154 mkdir -p /cgroup
155 mount none -t cgroup /cgroup
156fi
Anthony Young72d69632011-09-12 21:09:55 -0700157
158# Start our container
159lxc-start -d -n $CONTAINER