Anthony Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 2 | # Configurable params |
| 3 | BRIDGE=${BRIDGE:-br0} |
Anthony Young | b748e69 | 2011-09-13 10:16:13 -0700 | [diff] [blame] | 4 | CONTAINER=${CONTAINER:-STACK} |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 5 | CONTAINER_IP=${CONTAINER_IP:-192.168.1.50} |
| 6 | CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24} |
| 7 | CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0} |
| 8 | CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1} |
| 9 | NAMESERVER=${NAMESERVER:-192.168.1.1} |
| 10 | COPYENV=${COPYENV:-1} |
| 11 | |
Anthony Young | 1c36464 | 2011-09-13 20:21:42 -0700 | [diff] [blame] | 12 | # Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova" |
| 13 | STACKSH_PARAMS=${STACKSH_PARAMS:-} |
| 14 | |
Anthony Young | 936c928 | 2011-09-13 23:36:43 -0700 | [diff] [blame^] | 15 | # Warn users who aren't on natty |
| 16 | if ! grep -q natty /etc/lsb-release; then |
| 17 | echo "WARNING: this script has only been tested on natty" |
| 18 | fi |
| 19 | |
Anthony Young | 190321e | 2011-09-13 23:21:29 -0700 | [diff] [blame] | 20 | # Install deps |
| 21 | apt-get install lxc debootstrap |
| 22 | |
| 23 | # Install cgroup-bin from source, since the packaging is buggy and possibly incompatible with our setup |
Anthony Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 24 | if ! which cgdelete | grep -q cgdelete; then |
Anthony Young | 190321e | 2011-09-13 23:21:29 -0700 | [diff] [blame] | 25 | 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 Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 31 | fi |
| 32 | |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 33 | # Create lxc configuration |
| 34 | LXC_CONF=/tmp/$CONTAINER.conf |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 35 | cat > $LXC_CONF <<EOF |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 36 | lxc.network.type = veth |
| 37 | lxc.network.link = $BRIDGE |
| 38 | lxc.network.flags = up |
| 39 | lxc.network.ipv4 = $CONTAINER_CIDR |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 40 | # allow tap/tun devices |
Anthony Young | f49d7ee | 2011-09-13 03:29:52 -0700 | [diff] [blame] | 41 | lxc.cgroup.devices.allow = c 10:200 rwm |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 42 | EOF |
| 43 | |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 44 | # Shutdown any existing container |
| 45 | lxc-stop -n $CONTAINER |
| 46 | |
Anthony Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 47 | # This kills zombie containers |
| 48 | if [ -d /cgroup/$CONTAINER ]; then |
| 49 | cgdelete -r cpu,net_cls:$CONTAINER |
| 50 | fi |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 51 | |
| 52 | # Warm the base image on first install |
| 53 | CACHEDIR=/var/cache/lxc/natty/rootfs-amd64 |
| 54 | if [ ! -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/*` |
| 60 | fi |
| 61 | |
| 62 | # Destroy the old container |
| 63 | lxc-destroy -n $CONTAINER |
| 64 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 65 | # Create the container |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 66 | lxc-create -n $CONTAINER -t natty -f $LXC_CONF |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 67 | |
Anthony Young | f9998ab | 2011-09-13 10:43:44 -0700 | [diff] [blame] | 68 | # Specify where our container rootfs lives |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 69 | ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/ |
| 70 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 71 | # Create a stack user that is a member of the libvirtd group so that stack |
| 72 | # is able to interact with libvirt. |
| 73 | chroot $ROOTFS groupadd libvirtd |
| 74 | chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd |
| 75 | |
| 76 | # a simple password - pass |
| 77 | echo stack:pass | chroot $ROOTFS chpasswd |
| 78 | |
| 79 | # and has sudo ability (in the future this should be limited to only what |
| 80 | # stack requires) |
| 81 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers |
| 82 | |
Anthony Young | 34c4702 | 2011-09-13 22:14:37 -0700 | [diff] [blame] | 83 | # Gracefully cp only if source file/dir exists |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 84 | function cp_it { |
| 85 | if [ -e $1 ] || [ -d $1 ]; then |
| 86 | cp -pr $1 $2 |
| 87 | fi |
| 88 | } |
| 89 | |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 90 | # Copy over your ssh keys and env if desired |
| 91 | if [ "$COPYENV" = "1" ]; then |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 92 | 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 Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 97 | fi |
| 98 | |
Anthony Young | 1003952 | 2011-09-13 10:05:07 -0700 | [diff] [blame] | 99 | # Give stack ownership over /opt so it may do the work needed |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 100 | chroot $ROOTFS chown -R stack /opt |
| 101 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 102 | # Configure instance network |
| 103 | INTERFACES=$ROOTFS/etc/network/interfaces |
| 104 | cat > $INTERFACES <<EOF |
| 105 | auto lo |
| 106 | iface lo inet loopback |
| 107 | |
| 108 | auto eth0 |
| 109 | iface eth0 inet static |
| 110 | address $CONTAINER_IP |
| 111 | netmask $CONTAINER_NETMASK |
| 112 | gateway $CONTAINER_GATEWAY |
| 113 | EOF |
| 114 | |
Anthony Young | 1c36464 | 2011-09-13 20:21:42 -0700 | [diff] [blame] | 115 | # Configure the runner |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 116 | RUN_SH=$ROOTFS/root/run.sh |
| 117 | cat > $RUN_SH <<EOF |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 118 | #!/bin/bash |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 119 | # Make sure dns is set up |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 120 | echo "nameserver $NAMESERVER" | resolvconf -a eth0 |
| 121 | sleep 1 |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 122 | |
| 123 | # Install and run stack.sh |
Anthony Young | e2c3a37 | 2011-09-12 23:25:37 -0700 | [diff] [blame] | 124 | apt-get update |
| 125 | apt-get -y --force-yes install git-core vim-nox sudo |
Anthony Young | 1c36464 | 2011-09-13 20:21:42 -0700 | [diff] [blame] | 126 | if [ ! -d "~/nfs-stack" ] |
| 127 | su -c "git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack" stack |
| 128 | fi |
| 129 | su -c "cd ~/nfs-stack && $STACKSH_PARAMS ./stack.sh" stack |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 130 | EOF |
| 131 | |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 132 | # Make the run.sh executable |
| 133 | chmod 700 $RUN_SH |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 134 | |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 135 | # Make runner launch on boot |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 136 | RC_LOCAL=$ROOTFS/etc/rc.local |
| 137 | cat > $RC_LOCAL <<EOF |
| 138 | #!/bin/sh -e |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 139 | /root/run.sh |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 140 | EOF |
| 141 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 142 | # Configure cgroup directory |
Anthony Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 143 | if ! mount | grep -q cgroup; then |
| 144 | mkdir -p /cgroup |
| 145 | mount none -t cgroup /cgroup |
| 146 | fi |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 147 | |
| 148 | # Start our container |
| 149 | lxc-start -d -n $CONTAINER |