blob: 54ee375b866faaee4fa6b18112ea7a5dbaf58151 [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 Young936c9282011-09-13 23:36:43 -070015# Warn users who aren't on natty
16if ! grep -q natty /etc/lsb-release; then
17 echo "WARNING: this script has only been tested on natty"
18fi
19
Anthony Young190321e2011-09-13 23:21:29 -070020# Install deps
21apt-get install lxc debootstrap
22
23# Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup
Anthony Young4f279222011-09-13 21:51:28 -070024if ! which cgdelete | grep -q cgdelete; then
Anthony Young190321e2011-09-13 23:21:29 -070025 apt-get install g++ bison flex libpam0g-dev
26 wget http://sourceforge.net/projects/libcg/files/libcgroup/v0.37.1/libcgroup-0.37.1.tar.bz2/download -O /tmp/libcgroup-0.37.1.tar.bz2
27 cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar
28 cd libcgroup-0.37.1
29 ./configure
30 make install
Anthony Young4f279222011-09-13 21:51:28 -070031fi
32
Anthony Young60534962011-09-13 10:40:04 -070033# Create lxc configuration
34LXC_CONF=/tmp/$CONTAINER.conf
Anthony Young40203cb2011-09-13 09:17:56 -070035cat > $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 Young60534962011-09-13 10:40:04 -070044# Shutdown any existing container
45lxc-stop -n $CONTAINER
46
Anthony Young4f279222011-09-13 21:51:28 -070047# This kills zombie containers
48if [ -d /cgroup/$CONTAINER ]; then
49 cgdelete -r cpu,net_cls:$CONTAINER
50fi
Anthony Young60534962011-09-13 10:40:04 -070051
52# Warm the base image on first install
53CACHEDIR=/var/cache/lxc/natty/rootfs-amd64
54if [ ! -d $CACHEDIR ]; then
55 # trigger the initial debootstrap
56 lxc-create -n $CONTAINER -t natty -f $LXC_CONF
57 chroot $CACHEDIR apt-get update
58 chroot $CACHEDIR apt-get install -y `cat apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
59 chroot $CACHEDIR pip install `cat pips/*`
60fi
61
62# Destroy the old container
63lxc-destroy -n $CONTAINER
64
Anthony Young7c3e5ed2011-09-13 09:57:31 -070065# Create the container
Anthony Young40203cb2011-09-13 09:17:56 -070066lxc-create -n $CONTAINER -t natty -f $LXC_CONF
Anthony Youngb3c04542011-09-13 01:28:18 -070067
Anthony Youngf9998ab2011-09-13 10:43:44 -070068# Specify where our container rootfs lives
Anthony Young72d69632011-09-12 21:09:55 -070069ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/
70
Anthony Young7c3e5ed2011-09-13 09:57:31 -070071# Create a stack user that is a member of the libvirtd group so that stack
72# is able to interact with libvirt.
73chroot $ROOTFS groupadd libvirtd
74chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd
75
76# a simple password - pass
77echo stack:pass | chroot $ROOTFS chpasswd
78
79# and has sudo ability (in the future this should be limited to only what
80# stack requires)
81echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
82
Anthony Young34c47022011-09-13 22:14:37 -070083# Gracefully cp only if source file/dir exists
Anthony Youngfe1d95a2011-09-13 22:09:36 -070084function cp_it {
85 if [ -e $1 ] || [ -d $1 ]; then
86 cp -pr $1 $2
87 fi
88}
89
Anthony Youngb3c04542011-09-13 01:28:18 -070090# Copy over your ssh keys and env if desired
91if [ "$COPYENV" = "1" ]; then
Anthony Youngfe1d95a2011-09-13 22:09:36 -070092 cp_it ~/.ssh $ROOTFS/opt/.ssh
93 cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys
94 cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig
95 cp_it ~/.vimrc $ROOTFS/opt/.vimrc
96 cp_it ~/.bashrc $ROOTFS/opt/.bashrc
Anthony Young72d69632011-09-12 21:09:55 -070097fi
98
Anthony Young10039522011-09-13 10:05:07 -070099# Give stack ownership over /opt so it may do the work needed
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700100chroot $ROOTFS chown -R stack /opt
101
Anthony Young72d69632011-09-12 21:09:55 -0700102# Configure instance network
103INTERFACES=$ROOTFS/etc/network/interfaces
104cat > $INTERFACES <<EOF
105auto lo
106iface lo inet loopback
107
108auto eth0
109iface eth0 inet static
110 address $CONTAINER_IP
111 netmask $CONTAINER_NETMASK
112 gateway $CONTAINER_GATEWAY
113EOF
114
Anthony Young1c364642011-09-13 20:21:42 -0700115# Configure the runner
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700116RUN_SH=$ROOTFS/root/run.sh
117cat > $RUN_SH <<EOF
Anthony Young72d69632011-09-12 21:09:55 -0700118#!/bin/bash
Anthony Young7c3e5ed2011-09-13 09:57:31 -0700119# Make sure dns is set up
Anthony Young72d69632011-09-12 21:09:55 -0700120echo "nameserver $NAMESERVER" | resolvconf -a eth0
121sleep 1
Anthony Young99003e72011-09-13 02:05:12 -0700122
123# Install and run stack.sh
Anthony Younge2c3a372011-09-12 23:25:37 -0700124apt-get update
125apt-get -y --force-yes install git-core vim-nox sudo
Anthony Young1c364642011-09-13 20:21:42 -0700126if [ ! -d "~/nfs-stack" ]
127 su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack
128fi
129su -c "cd ~/nfs-stack && $STACKSH_PARAMS ./stack.sh" stack
Anthony Young72d69632011-09-12 21:09:55 -0700130EOF
131
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700132# Make the run.sh executable
133chmod 700 $RUN_SH
Anthony Young72d69632011-09-12 21:09:55 -0700134
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700135# Make runner launch on boot
Anthony Young72d69632011-09-12 21:09:55 -0700136RC_LOCAL=$ROOTFS/etc/rc.local
137cat > $RC_LOCAL <<EOF
138#!/bin/sh -e
Anthony Youngfe1d95a2011-09-13 22:09:36 -0700139/root/run.sh
Anthony Young72d69632011-09-12 21:09:55 -0700140EOF
141
Anthony Young72d69632011-09-12 21:09:55 -0700142# Configure cgroup directory
Anthony Young4f279222011-09-13 21:51:28 -0700143if ! mount | grep -q cgroup; then
144 mkdir -p /cgroup
145 mount none -t cgroup /cgroup
146fi
Anthony Young72d69632011-09-12 21:09:55 -0700147
148# Start our container
149lxc-start -d -n $CONTAINER