blob: 4faa2e7f6654e81c18c4e86da696741ee01d6c53 [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}
Anthony Youngb3c04542011-09-13 01:28:18 -070011WARMCACHE=${WARMCACHE:-0}
Anthony Young72d69632011-09-12 21:09:55 -070012
Anthony Young40203cb2011-09-13 09:17:56 -070013# Shutdown any existing container
Anthony Young72d69632011-09-12 21:09:55 -070014lxc-stop -n $CONTAINER
Anthony Young7c3e5ed2011-09-13 09:57:31 -070015
Anthony Young40203cb2011-09-13 09:17:56 -070016# This prevents zombie containers
Anthony Youngeeba8862011-09-13 03:02:38 -070017cgdelete -r cpu,net_cls:$CONTAINER
Anthony Young7c3e5ed2011-09-13 09:57:31 -070018
Anthony Young40203cb2011-09-13 09:17:56 -070019# Destroy the old container
Anthony Young72d69632011-09-12 21:09:55 -070020lxc-destroy -n $CONTAINER
Anthony Young99003e72011-09-13 02:05:12 -070021
Anthony Young40203cb2011-09-13 09:17:56 -070022# Warm the base image on first run or when WARMCACHE=1
Anthony Young99003e72011-09-13 02:05:12 -070023CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
Anthony Young40203cb2011-09-13 09:17:56 -070024if [ "$WARMCACHE" = "1" ] || [ ! -d $CACHEDIR ]; then
Anthony Young99003e72011-09-13 02:05:12 -070025 if [ -d $CACHEDIR ]; then
26 # Pre-cache files
27 chroot $CACHEDIR apt-get update
28 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
29 chroot $CACHEDIR pip install `cat pips/*`
30 fi
31fi
Anthony Young72d69632011-09-12 21:09:55 -070032
33# Create network configuration
Anthony Young40203cb2011-09-13 09:17:56 -070034LXC_CONF=/tmp/net.conf
35cat > $LXC_CONF <<EOF
Anthony Young72d69632011-09-12 21:09:55 -070036lxc.network.type = veth
37lxc.network.link = $BRIDGE
38lxc.network.flags = up
39lxc.network.ipv4 = $CONTAINER_CIDR
Anthony Young40203cb2011-09-13 09:17:56 -070040# allow tap/tun devices
Anthony Youngf49d7ee2011-09-13 03:29:52 -070041lxc.cgroup.devices.allow = c 10:200 rwm
Anthony Young72d69632011-09-12 21:09:55 -070042EOF
43
Anthony Young7c3e5ed2011-09-13 09:57:31 -070044# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070045lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070046
Anthony Young7c3e5ed2011-09-13 09:57:31 -070047# Specify where our container lives
Anthony Young72d69632011-09-12 21:09:55 -070048ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
49
Anthony Young7c3e5ed2011-09-13 09:57:31 -070050# set root password to password
51echo root:pass | chroot $ROOTFS chpasswd
52
53# Create a stack user that is a member of the libvirtd group so that stack
54# is able to interact with libvirt.
55chroot $ROOTFS groupadd libvirtd
56chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
57
58# a simple password - pass
59echo stack:pass | chroot $ROOTFS chpasswd
60
61# and has sudo ability (in the future this should be limited to only what
62# stack requires)
63echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
64
Anthony Youngb3c04542011-09-13 01:28:18 -070065# Copy over your ssh keys and env if desired
66if [ "$COPYENV" = "1" ]; then
Anthony Young7c3e5ed2011-09-13 09:57:31 -070067 cp -pr ~/.ssh $ROOTFS/opt/.ssh
68 cp -p ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
69 cp -pr ~/.gitconfig $ROOTFS/opt/.gitconfig
70 cp -pr ~/.vimrc $ROOTFS/opt/.vimrc
71 cp -pr ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -070072fi
73
Anthony Young10039522011-09-13 10:05:07 -070074# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -070075chroot $ROOTFS chown -R stack /opt
76
Anthony Young72d69632011-09-12 21:09:55 -070077# Configure instance network
78INTERFACES=$ROOTFS/etc/network/interfaces
79cat > $INTERFACES <<EOF
80auto lo
81iface lo inet loopback
82
83auto eth0
84iface eth0 inet static
85 address $CONTAINER_IP
86 netmask $CONTAINER_NETMASK
87 gateway $CONTAINER_GATEWAY
88EOF
89
90# Configure the first run installer
91INSTALL_SH=$ROOTFS/root/install.sh
92cat > $INSTALL_SH <<EOF
93#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -070094# Disable startup script
Anthony Youngeeba8862011-09-13 03:02:38 -070095echo \#\!/bin/sh -e > /etc/rc.local
Anthony Young7c3e5ed2011-09-13 09:57:31 -070096# Make sure dns is set up
Anthony Young72d69632011-09-12 21:09:55 -070097echo "nameserver $NAMESERVER" | resolvconf -a eth0
98sleep 1
Anthony Young99003e72011-09-13 02:05:12 -070099
100# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -0700101apt-get update
102apt-get -y --force-yes install git-core vim-nox sudo
Anthony Youngbdbe6d92011-09-13 09:43:46 -0700103su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
104su -c "cd ~/nfs-stack && ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700105EOF
106
107chmod 700 $INSTALL_SH
108
109# Make installer run on boot
110RC_LOCAL=$ROOTFS/etc/rc.local
111cat > $RC_LOCAL <<EOF
112#!/bin/sh -e
113/root/install.sh
114EOF
115
Anthony Young72d69632011-09-12 21:09:55 -0700116# Configure cgroup directory
Anthony Young99003e72011-09-13 02:05:12 -0700117mkdir -p /cgroup
118mount none -t cgroup /cgroup
Anthony Young72d69632011-09-12 21:09:55 -0700119
120# Start our container
121lxc-start -d -n $CONTAINER