blob: 4b10afcddc03c1ac94e2e7dfa3943aa341428763 [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 Young60534962011-09-13 10:40:04 -070012# Create lxc configuration
13LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070014cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070015lxc.network.type = veth
16lxc.network.link = $BRIDGE
17lxc.network.flags = up
18lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070019# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070020lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070021EOF
22
Anthony Young60534962011-09-13 10:40:04 -070023# Shutdown any existing container
24lxc-stop -n $CONTAINER
25
26# This prevents zombie containers
27cgdelete -r cpu,net_cls:$CONTAINER
28
29# Warm the base image on first install
30CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
31if [ ! -d $CACHEDIR ]; then
32 # trigger the initial debootstrap
33 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
34 chroot $CACHEDIR apt-get update
35 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
36 chroot $CACHEDIR pip install `cat pips/*`
37fi
38
39# Destroy the old container
40lxc-destroy -n $CONTAINER
41
Anthony Young7c3e5ed2011-09-13 09:57:31 -070042# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070043lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070044
Anthony Youngf9998ab2011-09-13 10:43:44 -070045# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070046ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
47
Anthony Young7c3e5ed2011-09-13 09:57:31 -070048# Create a stack user that is a member of the libvirtd group so that stack
49# is able to interact with libvirt.
50chroot $ROOTFS groupadd libvirtd
51chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
52
53# a simple password - pass
54echo stack:pass | chroot $ROOTFS chpasswd
55
56# and has sudo ability (in the future this should be limited to only what
57# stack requires)
58echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
59
Anthony Youngb3c04542011-09-13 01:28:18 -070060# Copy over your ssh keys and env if desired
61if [ "$COPYENV" = "1" ]; then
Anthony Young7c3e5ed2011-09-13 09:57:31 -070062 cp -pr ~/.ssh $ROOTFS/opt/.ssh
63 cp -p ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
64 cp -pr ~/.gitconfig $ROOTFS/opt/.gitconfig
65 cp -pr ~/.vimrc $ROOTFS/opt/.vimrc
66 cp -pr ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -070067fi
68
Anthony Young10039522011-09-13 10:05:07 -070069# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -070070chroot $ROOTFS chown -R stack /opt
71
Anthony Young72d69632011-09-12 21:09:55 -070072# Configure instance network
73INTERFACES=$ROOTFS/etc/network/interfaces
74cat > $INTERFACES <<EOF
75auto lo
76iface lo inet loopback
77
78auto eth0
79iface eth0 inet static
80 address $CONTAINER_IP
81 netmask $CONTAINER_NETMASK
82 gateway $CONTAINER_GATEWAY
83EOF
84
85# Configure the first run installer
86INSTALL_SH=$ROOTFS/root/install.sh
87cat > $INSTALL_SH <<EOF
88#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -070089# Disable startup script
Anthony Youngeeba8862011-09-13 03:02:38 -070090echo \#\!/bin/sh -e > /etc/rc.local
Anthony Young7c3e5ed2011-09-13 09:57:31 -070091# Make sure dns is set up
Anthony Young72d69632011-09-12 21:09:55 -070092echo "nameserver $NAMESERVER" | resolvconf -a eth0
93sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070094
95# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -070096apt-get update
97apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngbdbe6d92011-09-13 09:43:46 -070098su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
99su -c "cd ~/nfs-stack && ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700100EOF
101
102chmod 700 $INSTALL_SH
103
104# Make installer run on boot
105RC_LOCAL=$ROOTFS/etc/rc.local
106cat > $RC_LOCAL <<EOF
107#!/bin/sh -e
108/root/install.sh
109EOF
110
Anthony Young72d69632011-09-12 21:09:55 -0700111# Configure cgroup directory
Anthony Young99003e72011-09-13 02:05:12 -0700112mkdir -p /cgroup
113mount none -t cgroup /cgroup
Anthony Young72d69632011-09-12 21:09:55 -0700114
115# Start our container
116lxc-start -d -n $CONTAINER