blob: 97dd594ef39d5452da9bbed5f13334842cd580b6 [file] [log] [blame]
Anthony Young72d69632011-09-12 21:09:55 -07001#!/bin/bash
2# 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 Young60534962011-09-13 10:40:04 -070015# Create lxc configuration
16LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070017cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070018lxc.network.type = veth
19lxc.network.link = $BRIDGE
20lxc.network.flags = up
21lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070022# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070023lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070024EOF
25
Anthony Young60534962011-09-13 10:40:04 -070026# Shutdown any existing container
27lxc-stop -n $CONTAINER
28
29# This prevents zombie containers
30cgdelete -r cpu,net_cls:$CONTAINER
31
32# Warm the base image on first install
33CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
34if [ ! -d $CACHEDIR ]; then
35 # trigger the initial debootstrap
36 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
37 chroot $CACHEDIR apt-get update
38 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
39 chroot $CACHEDIR pip install `cat pips/*`
40fi
41
42# Destroy the old container
43lxc-destroy -n $CONTAINER
44
Anthony Young7c3e5ed2011-09-13 09:57:31 -070045# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070046lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070047
Anthony Youngf9998ab2011-09-13 10:43:44 -070048# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070049ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
50
Anthony Young7c3e5ed2011-09-13 09:57:31 -070051# Create a stack user that is a member of the libvirtd group so that stack
52# is able to interact with libvirt.
53chroot $ROOTFS groupadd libvirtd
54chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
55
56# a simple password - pass
57echo stack:pass | chroot $ROOTFS chpasswd
58
59# and has sudo ability (in the future this should be limited to only what
60# stack requires)
61echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
62
Anthony Youngb3c04542011-09-13 01:28:18 -070063# Copy over your ssh keys and env if desired
64if [ "$COPYENV" = "1" ]; then
Anthony Young7c3e5ed2011-09-13 09:57:31 -070065 cp -pr ~/.ssh $ROOTFS/opt/.ssh
66 cp -p ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
67 cp -pr ~/.gitconfig $ROOTFS/opt/.gitconfig
68 cp -pr ~/.vimrc $ROOTFS/opt/.vimrc
69 cp -pr ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -070070fi
71
Anthony Young10039522011-09-13 10:05:07 -070072# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -070073chroot $ROOTFS chown -R stack /opt
74
Anthony Young72d69632011-09-12 21:09:55 -070075# Configure instance network
76INTERFACES=$ROOTFS/etc/network/interfaces
77cat > $INTERFACES <<EOF
78auto lo
79iface lo inet loopback
80
81auto eth0
82iface eth0 inet static
83 address $CONTAINER_IP
84 netmask $CONTAINER_NETMASK
85 gateway $CONTAINER_GATEWAY
86EOF
87
Anthony Young1c364642011-09-13 20:21:42 -070088# Configure the runner
Anthony Young72d69632011-09-12 21:09:55 -070089INSTALL_SH=$ROOTFS/root/install.sh
90cat > $INSTALL_SH <<EOF
91#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -070092# Make sure dns is set up
Anthony Young72d69632011-09-12 21:09:55 -070093echo "nameserver $NAMESERVER" | resolvconf -a eth0
94sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070095
96# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -070097apt-get update
98apt-get -y --force-yes install git-core vim-nox sudo
Anthony Young1c364642011-09-13 20:21:42 -070099if [ ! -d "~/nfs-stack" ]
100 su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
101fi
102su -c "cd ~/nfs-stack && $STACKSH_PARAMS ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700103EOF
104
Anthony Young76d5dc72011-09-13 17:00:00 -0700105# Make the install.sh executable
Anthony Young72d69632011-09-12 21:09:55 -0700106chmod 700 $INSTALL_SH
107
108# Make installer run on boot
109RC_LOCAL=$ROOTFS/etc/rc.local
110cat > $RC_LOCAL <<EOF
111#!/bin/sh -e
112/root/install.sh
113EOF
114
Anthony Young72d69632011-09-12 21:09:55 -0700115# Configure cgroup directory
Anthony Young99003e72011-09-13 02:05:12 -0700116mkdir -p /cgroup
117mount none -t cgroup /cgroup
Anthony Young72d69632011-09-12 21:09:55 -0700118
119# Start our container
120lxc-start -d -n $CONTAINER