blob: c019d7e99e6c4ee4947a5346e870c178abb2120b [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}
9NAMESERVER=${NAMESERVER:-192.168.1.1}
10COPYENV=${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 Young190321e2011-09-13 23:21:29 -070015# Install deps
16apt-get install lxc debootstrap
17
18# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070019if ! which cgdelete | grep -q cgdelete; then
Anthony Young190321e2011-09-13 23:21:29 -070020 apt-get install g++ bison flex libpam0g-dev
21 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
22 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
23 cd libcgroup-0.37.1
24 ./configure
25 make install
Anthony Young4f279222011-09-13 21:51:28 -070026fi
27
Anthony Young60534962011-09-13 10:40:04 -070028# Create lxc configuration
29LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070030cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070031lxc.network.type = veth
32lxc.network.link = $BRIDGE
33lxc.network.flags = up
34lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070035# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070036lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070037EOF
38
Anthony Young60534962011-09-13 10:40:04 -070039# Shutdown any existing container
40lxc-stop -n $CONTAINER
41
Anthony Young4f279222011-09-13 21:51:28 -070042# This kills zombie containers
43if [ -d /cgroup/$CONTAINER ]; then
44 cgdelete -r cpu,net_cls:$CONTAINER
45fi
Anthony Young60534962011-09-13 10:40:04 -070046
47# Warm the base image on first install
48CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
49if [ ! -d $CACHEDIR ]; then
50 # trigger the initial debootstrap
51 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
52 chroot $CACHEDIR apt-get update
53 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
54 chroot $CACHEDIR pip install `cat pips/*`
55fi
56
57# Destroy the old container
58lxc-destroy -n $CONTAINER
59
Anthony Young7c3e5ed2011-09-13 09:57:31 -070060# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070061lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070062
Anthony Youngf9998ab2011-09-13 10:43:44 -070063# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070064ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
65
Anthony Young7c3e5ed2011-09-13 09:57:31 -070066# Create a stack user that is a member of the libvirtd group so that stack
67# is able to interact with libvirt.
68chroot $ROOTFS groupadd libvirtd
69chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
70
71# a simple password - pass
72echo stack:pass | chroot $ROOTFS chpasswd
73
74# and has sudo ability (in the future this should be limited to only what
75# stack requires)
76echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
77
Anthony Young34c47022011-09-13 22:14:37 -070078# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -070079function cp_it {
80 if [ -e $1 ] || [ -d $1 ]; then
81 cp -pr $1 $2
82 fi
83}
84
Anthony Youngb3c04542011-09-13 01:28:18 -070085# Copy over your ssh keys and env if desired
86if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -070087 cp_it ~/.ssh $ROOTFS/opt/.ssh
88 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
89 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
90 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
91 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -070092fi
93
Anthony Young10039522011-09-13 10:05:07 -070094# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -070095chroot $ROOTFS chown -R stack /opt
96
Anthony Young72d69632011-09-12 21:09:55 -070097# Configure instance network
98INTERFACES=$ROOTFS/etc/network/interfaces
99cat > $INTERFACES <<EOF
100auto lo
101iface lo inet loopback
102
103auto eth0
104iface eth0 inet static
105 address $CONTAINER_IP
106 netmask $CONTAINER_NETMASK
107 gateway $CONTAINER_GATEWAY
108EOF
109
Anthony Young1c364642011-09-13 20:21:42 -0700110# Configure the runner
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700111RUN_SH=$ROOTFS/root/run.sh
112cat > $RUN_SH <<EOF
Anthony Young72d69632011-09-12 21:09:55 -0700113#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700114# Make sure dns is set up
Anthony Young72d69632011-09-12 21:09:55 -0700115echo "nameserver $NAMESERVER" | resolvconf -a eth0
116sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700117
118# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -0700119apt-get update
120apt-get -y --force-yes install git-core vim-nox sudo
Anthony Young1c364642011-09-13 20:21:42 -0700121if [ ! -d "~/nfs-stack" ]
122 su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
123fi
124su -c "cd ~/nfs-stack && $STACKSH_PARAMS ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700125EOF
126
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700127# Make the run.sh executable
128chmod 700 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700129
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700130# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700131RC_LOCAL=$ROOTFS/etc/rc.local
132cat > $RC_LOCAL <<EOF
133#!/bin/sh -e
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700134/root/run.sh
Anthony Young72d69632011-09-12 21:09:55 -0700135EOF
136
Anthony Young72d69632011-09-12 21:09:55 -0700137# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700138if ! mount | grep -q cgroup; then
139 mkdir -p /cgroup
140 mount none -t cgroup /cgroup
141fi
Anthony Young72d69632011-09-12 21:09:55 -0700142
143# Start our container
144lxc-start -d -n $CONTAINER