blob: ba26f4ff940b000755d867bb306aaa4f4e03bb93 [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 Young7c3e5ed2011-09-13 09:57:31 -070045# Specify where our container lives
Anthony Young72d69632011-09-12 21:09:55 -070046ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
47
Anthony Young7c3e5ed2011-09-13 09:57:31 -070048# set root password to password
49echo root:pass | chroot $ROOTFS chpasswd
50
51# 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
88# Configure the first run installer
89INSTALL_SH=$ROOTFS/root/install.sh
90cat > $INSTALL_SH <<EOF
91#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -070092# Disable startup script
Anthony Youngeeba8862011-09-13 03:02:38 -070093echo \#\!/bin/sh -e > /etc/rc.local
Anthony Young7c3e5ed2011-09-13 09:57:31 -070094# Make sure dns is set up
Anthony Young72d69632011-09-12 21:09:55 -070095echo "nameserver $NAMESERVER" | resolvconf -a eth0
96sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070097
98# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -070099apt-get update
100apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngbdbe6d92011-09-13 09:43:46 -0700101su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
102su -c "cd ~/nfs-stack && ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700103EOF
104
105chmod 700 $INSTALL_SH
106
107# Make installer run on boot
108RC_LOCAL=$ROOTFS/etc/rc.local
109cat > $RC_LOCAL <<EOF
110#!/bin/sh -e
111/root/install.sh
112EOF
113
Anthony Young72d69632011-09-12 21:09:55 -0700114# Configure cgroup directory
Anthony Young99003e72011-09-13 02:05:12 -0700115mkdir -p /cgroup
116mount none -t cgroup /cgroup
Anthony Young72d69632011-09-12 21:09:55 -0700117
118# Start our container
119lxc-start -d -n $CONTAINER