blob: 787c4bdf859926b68480cdec4e7df04d89c25855 [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
Jesse Andrews4d282182011-09-16 11:27:43 -070058 chroot $CACHEDIR apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
59 chroot $CACHEDIR pip install `cat files/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 Young23761c32011-09-16 14:54:20 -070072# If this call is to TERMINATE the container then exit
73if [ "$TERMINATE" = "1" ]; then
74 exit
75fi
76
Anthony Young7c3e5ed2011-09-13 09:57:31 -070077# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070078lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070079
Anthony Youngf9998ab2011-09-13 10:43:44 -070080# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070081ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
82
Anthony Young7c3e5ed2011-09-13 09:57:31 -070083# Create a stack user that is a member of the libvirtd group so that stack
84# is able to interact with libvirt.
85chroot $ROOTFS groupadd libvirtd
86chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
87
88# a simple password - pass
89echo stack:pass | chroot $ROOTFS chpasswd
90
91# and has sudo ability (in the future this should be limited to only what
92# stack requires)
93echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
94
Anthony Young34c47022011-09-13 22:14:37 -070095# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -070096function cp_it {
97 if [ -e $1 ] || [ -d $1 ]; then
98 cp -pr $1 $2
99 fi
100}
101
Anthony Youngb3c04542011-09-13 01:28:18 -0700102# Copy over your ssh keys and env if desired
103if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700104 cp_it ~/.ssh $ROOTFS/opt/.ssh
105 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
106 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
107 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
108 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700109fi
110
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700111# Make our ip address hostnames look nice at the command prompt
112echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/opt/.bashrc
113echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
114
Anthony Young10039522011-09-13 10:05:07 -0700115# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700116chroot $ROOTFS chown -R stack /opt
117
Anthony Young72d69632011-09-12 21:09:55 -0700118# Configure instance network
119INTERFACES=$ROOTFS/etc/network/interfaces
120cat > $INTERFACES <<EOF
121auto lo
122iface lo inet loopback
123
124auto eth0
125iface eth0 inet static
126 address $CONTAINER_IP
127 netmask $CONTAINER_NETMASK
128 gateway $CONTAINER_GATEWAY
129EOF
130
Anthony Young1c364642011-09-13 20:21:42 -0700131# Configure the runner
Anthony Young56e62922011-09-14 02:54:27 -0700132RUN_SH=$ROOTFS/opt/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700133cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700134#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700135# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700136echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700137sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700138
Anthony Young927a6562011-09-14 01:58:01 -0700139# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700140killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700141
Anthony Young99003e72011-09-13 02:05:12 -0700142# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700143sudo apt-get update
144sudo apt-get -y --force-yes install git-core vim-nox sudo
Jesse Andrews47e115e2011-09-14 11:25:47 -0700145if [ ! -d "/opt/devstack" ]; then
Anthony Young550ec962011-09-15 21:05:50 -0700146 git clone git://github.com/cloudbuilders/devstack.git /opt/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700147fi
Jesse Andrews47e115e2011-09-14 11:25:47 -0700148cd /opt/devstack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700149EOF
150
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700151# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700152chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700153
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700154# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700155RC_LOCAL=$ROOTFS/etc/rc.local
156cat > $RC_LOCAL <<EOF
157#!/bin/sh -e
Anthony Young56e62922011-09-14 02:54:27 -0700158su -c "/opt/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700159EOF
160
Anthony Young72d69632011-09-12 21:09:55 -0700161# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700162if ! mount | grep -q cgroup; then
163 mkdir -p /cgroup
164 mount none -t cgroup /cgroup
165fi
Anthony Young72d69632011-09-12 21:09:55 -0700166
167# Start our container
168lxc-start -d -n $CONTAINER