blob: a052c0b06c4b6a7bceb681f4adc3957d6461c8d7 [file] [log] [blame]
Anthony Young72d69632011-09-12 21:09:55 -07001#!/bin/bash
2# Configurable params
3BRIDGE=${BRIDGE:-br0}
4CONTAINER=${CONTAINER:-TESTER}
5CONTAINER_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}
Anthony Youngb3c04542011-09-13 01:28:18 -070011WARMCACHE=${WARMCACHE:-0}
Anthony Young72d69632011-09-12 21:09:55 -070012
13# Destroy any existing container
14lxc-stop -n $CONTAINER
Anthony Youngeeba8862011-09-13 03:02:38 -070015sleep 1
16cgdelete -r cpu,net_cls:$CONTAINER
17sleep 1
Anthony Young72d69632011-09-12 21:09:55 -070018lxc-destroy -n $CONTAINER
Anthony Youngeeba8862011-09-13 03:02:38 -070019sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070020
21CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
22if [ "$WARMCACHE" = "1" ]; then
23 if [ -d $CACHEDIR ]; then
24 # Pre-cache files
25 chroot $CACHEDIR apt-get update
26 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
27 chroot $CACHEDIR pip install `cat pips/*`
28 fi
29fi
Anthony Young72d69632011-09-12 21:09:55 -070030
31# Create network configuration
32NET_CONF=/tmp/net.conf
33cat > $NET_CONF <<EOF
34lxc.network.type = veth
35lxc.network.link = $BRIDGE
36lxc.network.flags = up
37lxc.network.ipv4 = $CONTAINER_CIDR
38EOF
39
40# Configure the network
41lxc-create -n $CONTAINER -t natty -f $NET_CONF
Anthony Young99003e72011-09-13 02:05:12 -070042sleep 2
Anthony Youngb3c04542011-09-13 01:28:18 -070043
Anthony Young72d69632011-09-12 21:09:55 -070044# Where our container lives
45ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
46
Anthony Youngb3c04542011-09-13 01:28:18 -070047# Copy over your ssh keys and env if desired
48if [ "$COPYENV" = "1" ]; then
Anthony Young72d69632011-09-12 21:09:55 -070049 cp -pr ~/.ssh $ROOTFS/root/.ssh
Anthony Youngb3c04542011-09-13 01:28:18 -070050 cp -p ~/.ssh/id_rsa.pub $ROOTFS/root/.ssh/authorized_keys
Anthony Young72d69632011-09-12 21:09:55 -070051 cp -pr ~/.gitconfig $ROOTFS/root/.gitconfig
52 cp -pr ~/.vimrc $ROOTFS/root/.vimrc
53 cp -pr ~/.bashrc $ROOTFS/root/.bashrc
54fi
55
56# Configure instance network
57INTERFACES=$ROOTFS/etc/network/interfaces
58cat > $INTERFACES <<EOF
59auto lo
60iface lo inet loopback
61
62auto eth0
63iface eth0 inet static
64 address $CONTAINER_IP
65 netmask $CONTAINER_NETMASK
66 gateway $CONTAINER_GATEWAY
67EOF
68
69# Configure the first run installer
70INSTALL_SH=$ROOTFS/root/install.sh
71cat > $INSTALL_SH <<EOF
72#!/bin/bash
Anthony Youngeeba8862011-09-13 03:02:38 -070073echo \#\!/bin/sh -e > /etc/rc.local
Anthony Young72d69632011-09-12 21:09:55 -070074echo "nameserver $NAMESERVER" | resolvconf -a eth0
75sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070076# Create a stack user that is a member of the libvirtd group so that stack
77# is able to interact with libvirt.
78groupadd libvirtd
79useradd stack -s /bin/bash -d /opt -G libvirtd
80
81# a simple password - pass
82echo stack:pass | chpasswd
83
84# give stack ownership over /opt so it may do the work needed
85chown -R stack /opt
86
87# and has sudo ability (in the future this should be limited to only what
88# stack requires)
89
90echo "stack ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
91
92# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -070093apt-get update
94apt-get -y --force-yes install git-core vim-nox sudo
Anthony Young99003e72011-09-13 02:05:12 -070095su -c "git clone git://github.com/cloudbuilders/nfs-stack.git /opt/nfs-stack" stack
96su -c "cd /opt/nfs-stack && ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -070097EOF
98
99chmod 700 $INSTALL_SH
100
101# Make installer run on boot
102RC_LOCAL=$ROOTFS/etc/rc.local
103cat > $RC_LOCAL <<EOF
104#!/bin/sh -e
105/root/install.sh
106EOF
107
Anthony Young72d69632011-09-12 21:09:55 -0700108# Configure cgroup directory
Anthony Young99003e72011-09-13 02:05:12 -0700109mkdir -p /cgroup
110mount none -t cgroup /cgroup
Anthony Young72d69632011-09-12 21:09:55 -0700111
112# Start our container
113lxc-start -d -n $CONTAINER
Anthony Youngeeba8862011-09-13 03:02:38 -0700114
115cat << EOF > /bin/remove_dead_cgroup.shecho
116"Removing dead cgroup .$CONTAINER." >> /var/log/cgroup
117rmdir /cgroup/$CONTAINER >> /var/log/cgroup 2>&1
118echo "return value was $?" >> /var/log/cgroup
119EOF
120chmod 755 /bin/remove_dead_cgroup.sh
121echo /bin/remove_dead_cgroup.sh > /cgroup/release_agent
122echo 1 > /cgroup/notify_on_release