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