blob: f7536d418d4f62a39c1b3a34d404345aa72320de [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 Young4f279222011-09-13 21:51:28 -070015# Install cgroup-bin if we don't have it yet
16if ! which cgdelete | grep -q cgdelete; then
17 apt-get install cgroup-bin
18fi
19
Anthony Young60534962011-09-13 10:40:04 -070020# Create lxc configuration
21LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070022cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070023lxc.network.type = veth
24lxc.network.link = $BRIDGE
25lxc.network.flags = up
26lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070027# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070028lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070029EOF
30
Anthony Young60534962011-09-13 10:40:04 -070031# Shutdown any existing container
32lxc-stop -n $CONTAINER
33
Anthony Young4f279222011-09-13 21:51:28 -070034# This kills zombie containers
35if [ -d /cgroup/$CONTAINER ]; then
36 cgdelete -r cpu,net_cls:$CONTAINER
37fi
Anthony Young60534962011-09-13 10:40:04 -070038
39# Warm the base image on first install
40CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
41if [ ! -d $CACHEDIR ]; then
42 # trigger the initial debootstrap
43 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
44 chroot $CACHEDIR apt-get update
45 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
46 chroot $CACHEDIR pip install `cat pips/*`
47fi
48
49# Destroy the old container
50lxc-destroy -n $CONTAINER
51
Anthony Young7c3e5ed2011-09-13 09:57:31 -070052# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070053lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070054
Anthony Youngf9998ab2011-09-13 10:43:44 -070055# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070056ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
57
Anthony Young7c3e5ed2011-09-13 09:57:31 -070058# Create a stack user that is a member of the libvirtd group so that stack
59# is able to interact with libvirt.
60chroot $ROOTFS groupadd libvirtd
61chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
62
63# a simple password - pass
64echo stack:pass | chroot $ROOTFS chpasswd
65
66# and has sudo ability (in the future this should be limited to only what
67# stack requires)
68echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
69
Anthony Youngfe1d95a2011-09-13 22:09:36 -070070function cp_it {
71 if [ -e $1 ] || [ -d $1 ]; then
72 cp -pr $1 $2
73 fi
74}
75
Anthony Youngb3c04542011-09-13 01:28:18 -070076# Copy over your ssh keys and env if desired
77if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -070078 cp_it ~/.ssh $ROOTFS/opt/.ssh
79 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
80 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
81 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
82 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -070083fi
84
Anthony Young10039522011-09-13 10:05:07 -070085# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -070086chroot $ROOTFS chown -R stack /opt
87
Anthony Young72d69632011-09-12 21:09:55 -070088# Configure instance network
89INTERFACES=$ROOTFS/etc/network/interfaces
90cat > $INTERFACES <<EOF
91auto lo
92iface lo inet loopback
93
94auto eth0
95iface eth0 inet static
96 address $CONTAINER_IP
97 netmask $CONTAINER_NETMASK
98 gateway $CONTAINER_GATEWAY
99EOF
100
Anthony Young1c364642011-09-13 20:21:42 -0700101# Configure the runner
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700102RUN_SH=$ROOTFS/root/run.sh
103cat > $RUN_SH <<EOF
Anthony Young72d69632011-09-12 21:09:55 -0700104#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700105# Make sure dns is set up
Anthony Young72d69632011-09-12 21:09:55 -0700106echo "nameserver $NAMESERVER" | resolvconf -a eth0
107sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700108
109# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -0700110apt-get update
111apt-get -y --force-yes install git-core vim-nox sudo
Anthony Young1c364642011-09-13 20:21:42 -0700112if [ ! -d "~/nfs-stack" ]
113 su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
114fi
115su -c "cd ~/nfs-stack && $STACKSH_PARAMS ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700116EOF
117
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700118# Make the run.sh executable
119chmod 700 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700120
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700121# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700122RC_LOCAL=$ROOTFS/etc/rc.local
123cat > $RC_LOCAL <<EOF
124#!/bin/sh -e
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700125/root/run.sh
Anthony Young72d69632011-09-12 21:09:55 -0700126EOF
127
Anthony Young72d69632011-09-12 21:09:55 -0700128# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700129if ! mount | grep -q cgroup; then
130 mkdir -p /cgroup
131 mount none -t cgroup /cgroup
132fi
Anthony Young72d69632011-09-12 21:09:55 -0700133
134# Start our container
135lxc-start -d -n $CONTAINER