blob: c24539eecc06a151ff814a1527d6ecb5eb82b2af [file] [log] [blame]
Anthony Young4f279222011-09-13 21:51:28 -07001#!/usr/bin/env bash
Jesse Andrews18ebd862011-09-19 14:23:42 -07002
Anthony Young2f140202011-09-26 13:02:40 -07003# Source params
4source ./stackrc
5
Anthony Young72d69632011-09-12 21:09:55 -07006# Configurable params
7BRIDGE=${BRIDGE:-br0}
Anthony Youngb748e692011-09-13 10:16:13 -07008CONTAINER=${CONTAINER:-STACK}
Anthony Young72d69632011-09-12 21:09:55 -07009CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
10CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
11CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
12CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
Anthony Young77dbb072011-09-14 00:49:39 -070013NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY}
Anthony Young72d69632011-09-12 21:09:55 -070014COPYENV=${COPYENV:-1}
15
Anthony Young1c364642011-09-13 20:21:42 -070016# Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
17STACKSH_PARAMS=${STACKSH_PARAMS:-}
18
Anthony Young936c9282011-09-13 23:36:43 -070019# Warn users who aren't on natty
20if ! grep -q natty /etc/lsb-release; then
21 echo "WARNING: this script has only been tested on natty"
22fi
23
Anthony Young190321e2011-09-13 23:21:29 -070024# Install deps
Anthony Young856d09f2011-09-19 19:49:20 -070025apt-get install -y lxc debootstrap
Anthony Young190321e2011-09-13 23:21:29 -070026
27# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070028if ! which cgdelete | grep -q cgdelete; then
Anthony Young856d09f2011-09-19 19:49:20 -070029 apt-get install -y g++ bison flex libpam0g-dev
Anthony Young190321e2011-09-13 23:21:29 -070030 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
31 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
32 cd libcgroup-0.37.1
33 ./configure
34 make install
Dean Troyer62a6deb2011-09-21 20:06:01 -050035 ldconfig
Anthony Young4f279222011-09-13 21:51:28 -070036fi
37
Anthony Young60534962011-09-13 10:40:04 -070038# Create lxc configuration
39LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070040cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070041lxc.network.type = veth
42lxc.network.link = $BRIDGE
43lxc.network.flags = up
44lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070045# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070046lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070047EOF
48
Anthony Young60534962011-09-13 10:40:04 -070049# Shutdown any existing container
50lxc-stop -n $CONTAINER
51
Anthony Young4f279222011-09-13 21:51:28 -070052# This kills zombie containers
53if [ -d /cgroup/$CONTAINER ]; then
54 cgdelete -r cpu,net_cls:$CONTAINER
55fi
Anthony Young60534962011-09-13 10:40:04 -070056
Anthony Young2f140202011-09-26 13:02:40 -070057# git clone only if directory doesn't exist already. Since ``DEST`` might not
58# be owned by the installation user, we create the directory and change the
59# ownership to the proper user.
60function git_clone {
61 if [ ! -d $2 ]; then
62 sudo mkdir $2
63 sudo chown `whoami` $2
64 git clone $1 $2
65 cd $2
66 # This checkout syntax works for both branches and tags
67 git checkout $3
68 fi
69}
Jesse Andrews18ebd862011-09-19 14:23:42 -070070
Anthony Young60534962011-09-13 10:40:04 -070071# Warm the base image on first install
72CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young303233e2011-09-26 13:12:57 -070073if [ ! -d $CACHEDIR/ ]; then
Jesse Andrews18ebd862011-09-19 14:23:42 -070074 # by deleting the container, we force lxc-create to re-bootstrap (lxc is
75 # lazy and doesn't do anything if a container already exists)
76 lxc-destroy -n $CONTAINER
Anthony Young60534962011-09-13 10:40:04 -070077 # trigger the initial debootstrap
78 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
79 chroot $CACHEDIR apt-get update
Anthony Youngbf188ef2011-09-19 20:23:42 -070080 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 -070081 chroot $CACHEDIR pip install `cat files/pips/*`
Anthony Young60534962011-09-13 10:40:04 -070082fi
83
Anthony Young303233e2011-09-26 13:12:57 -070084# Cache openstack code
85git_clone $NOVA_REPO $CACHEDIR/opt/nova $NOVA_BRANCH
86git_clone $GLANCE_REPO $CACHEDIR/opt/glance $GLANCE_BRANCH
87git_clone $KEYSTONE_REPO $CACHEDIR/opt/keystone $KEYSTONE_BRANCH
88git_clone $NOVNC_REPO $CACHEDIR/opt/novnc $NOVNC_BRANCH
89git_clone $DASH_REPO $CACHEDIR/opt/dash $DASH_BRANCH $DASH_TAG
90git_clone $NIXON_REPO $CACHEDIR/opt/nixon $NIXON_BRANCH
91git_clone $NOVACLIENT_REPO $CACHEDIR/opt/python-novaclient $NOVACLIENT_BRANCH
92git_clone $OPENSTACKX_REPO $CACHEDIR/opt/openstackx $OPENSTACKX_BRANCH
93git_clone $MUNIN_REPO $CACHEDIR/opt/openstack-munin $MUNIN_BRANCH
94
Anthony Young60534962011-09-13 10:40:04 -070095# Destroy the old container
96lxc-destroy -n $CONTAINER
97
Anthony Young23761c32011-09-16 14:54:20 -070098# If this call is to TERMINATE the container then exit
99if [ "$TERMINATE" = "1" ]; then
100 exit
101fi
102
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700103# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -0700104lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -0700105
Anthony Youngf9998ab2011-09-13 10:43:44 -0700106# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -0700107ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
108
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700109# Create a stack user that is a member of the libvirtd group so that stack
110# is able to interact with libvirt.
111chroot $ROOTFS groupadd libvirtd
112chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
113
114# a simple password - pass
115echo stack:pass | chroot $ROOTFS chpasswd
116
117# and has sudo ability (in the future this should be limited to only what
118# stack requires)
119echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
120
Dean Troyer62a6deb2011-09-21 20:06:01 -0500121# Copy kernel modules
122mkdir -p $ROOTFS/lib/modules/`uname -r`/kernel
123cp -p /lib/modules/`uname -r`/modules.dep $ROOTFS/lib/modules/`uname -r`/
124cp -pR /lib/modules/`uname -r`/kernel/net $ROOTFS/lib/modules/`uname -r`/kernel/
125
Anthony Young34c47022011-09-13 22:14:37 -0700126# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700127function cp_it {
128 if [ -e $1 ] || [ -d $1 ]; then
129 cp -pr $1 $2
130 fi
131}
132
Anthony Youngb3c04542011-09-13 01:28:18 -0700133# Copy over your ssh keys and env if desired
134if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700135 cp_it ~/.ssh $ROOTFS/opt/.ssh
136 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
137 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
138 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
139 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -0700140fi
141
Anthony Youngfde5a1c2011-09-15 23:49:02 -0700142# Make our ip address hostnames look nice at the command prompt
143echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/opt/.bashrc
144echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
145
Anthony Young10039522011-09-13 10:05:07 -0700146# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700147chroot $ROOTFS chown -R stack /opt
148
Anthony Young72d69632011-09-12 21:09:55 -0700149# Configure instance network
150INTERFACES=$ROOTFS/etc/network/interfaces
151cat > $INTERFACES <<EOF
152auto lo
153iface lo inet loopback
154
155auto eth0
156iface eth0 inet static
157 address $CONTAINER_IP
158 netmask $CONTAINER_NETMASK
159 gateway $CONTAINER_GATEWAY
160EOF
161
Anthony Young1c364642011-09-13 20:21:42 -0700162# Configure the runner
Anthony Young56e62922011-09-14 02:54:27 -0700163RUN_SH=$ROOTFS/opt/run.sh
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700164cat > $RUN_SH <<EOF
Anthony Young10791a12011-09-14 09:40:58 -0700165#!/usr/bin/env bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700166# Make sure dns is set up
Anthony Young56e62922011-09-14 02:54:27 -0700167echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0
Anthony Young72d69632011-09-12 21:09:55 -0700168sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700169
Anthony Young927a6562011-09-14 01:58:01 -0700170# Kill any existing screens
Anthony Young92e31ab2011-09-14 02:56:41 -0700171killall screen
Anthony Young927a6562011-09-14 01:58:01 -0700172
Anthony Young99003e72011-09-13 02:05:12 -0700173# Install and run stack.sh
Anthony Young56e62922011-09-14 02:54:27 -0700174sudo apt-get update
175sudo apt-get -y --force-yes install git-core vim-nox sudo
Jesse Andrews47e115e2011-09-14 11:25:47 -0700176if [ ! -d "/opt/devstack" ]; then
Anthony Young550ec962011-09-15 21:05:50 -0700177 git clone git://github.com/cloudbuilders/devstack.git /opt/devstack
Anthony Young1c364642011-09-13 20:21:42 -0700178fi
Jesse Andrews47e115e2011-09-14 11:25:47 -0700179cd /opt/devstack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log
Anthony Young72d69632011-09-12 21:09:55 -0700180EOF
181
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700182# Make the run.sh executable
Anthony Young56e62922011-09-14 02:54:27 -0700183chmod 755 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700184
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700185# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700186RC_LOCAL=$ROOTFS/etc/rc.local
187cat > $RC_LOCAL <<EOF
188#!/bin/sh -e
Anthony Young56e62922011-09-14 02:54:27 -0700189su -c "/opt/run.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700190EOF
191
Anthony Young72d69632011-09-12 21:09:55 -0700192# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700193if ! mount | grep -q cgroup; then
194 mkdir -p /cgroup
195 mount none -t cgroup /cgroup
196fi
Anthony Young72d69632011-09-12 21:09:55 -0700197
198# Start our container
199lxc-start -d -n $CONTAINER