blob: a81aa2dd7c646e9a4741bba562293bec5521eb7d [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 Youngb3c04542011-09-13 01:28:18 -070070# Copy over your ssh keys and env if desired
71if [ "$COPYENV" = "1" ]; then
Anthony Young7c3e5ed2011-09-13 09:57:31 -070072 cp -pr ~/.ssh $ROOTFS/opt/.ssh
73 cp -p ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
74 cp -pr ~/.gitconfig $ROOTFS/opt/.gitconfig
75 cp -pr ~/.vimrc $ROOTFS/opt/.vimrc
76 cp -pr ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -070077fi
78
Anthony Young10039522011-09-13 10:05:07 -070079# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -070080chroot $ROOTFS chown -R stack /opt
81
Anthony Young72d69632011-09-12 21:09:55 -070082# Configure instance network
83INTERFACES=$ROOTFS/etc/network/interfaces
84cat > $INTERFACES <<EOF
85auto lo
86iface lo inet loopback
87
88auto eth0
89iface eth0 inet static
90 address $CONTAINER_IP
91 netmask $CONTAINER_NETMASK
92 gateway $CONTAINER_GATEWAY
93EOF
94
Anthony Young1c364642011-09-13 20:21:42 -070095# Configure the runner
Anthony Young72d69632011-09-12 21:09:55 -070096INSTALL_SH=$ROOTFS/root/install.sh
97cat > $INSTALL_SH <<EOF
98#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -070099# Make sure dns is set up
Anthony Young72d69632011-09-12 21:09:55 -0700100echo "nameserver $NAMESERVER" | resolvconf -a eth0
101sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700102
103# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -0700104apt-get update
105apt-get -y --force-yes install git-core vim-nox sudo
Anthony Young1c364642011-09-13 20:21:42 -0700106if [ ! -d "~/nfs-stack" ]
107 su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
108fi
109su -c "cd ~/nfs-stack && $STACKSH_PARAMS ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700110EOF
111
Anthony Young76d5dc72011-09-13 17:00:00 -0700112# Make the install.sh executable
Anthony Young72d69632011-09-12 21:09:55 -0700113chmod 700 $INSTALL_SH
114
115# Make installer run on boot
116RC_LOCAL=$ROOTFS/etc/rc.local
117cat > $RC_LOCAL <<EOF
118#!/bin/sh -e
119/root/install.sh
120EOF
121
Anthony Young72d69632011-09-12 21:09:55 -0700122# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700123if ! mount | grep -q cgroup; then
124 mkdir -p /cgroup
125 mount none -t cgroup /cgroup
126fi
Anthony Young72d69632011-09-12 21:09:55 -0700127
128# Start our container
129lxc-start -d -n $CONTAINER