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