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} |
Anthony Young | 77dbb07 | 2011-09-14 00:49:39 -0700 | [diff] [blame] | 9 | NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY} |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 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 |
Anthony Young | 3859f73 | 2011-09-14 02:33:43 -0700 | [diff] [blame] | 54 | if [ -d $CACHEDIR ]; then |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 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/*` |
Anthony Young | 3859f73 | 2011-09-14 02:33:43 -0700 | [diff] [blame] | 60 | git clone https://github.com/cloudbuilders/nova.git $CACHEDIR/opt/nova |
| 61 | git clone https://github.com/cloudbuilders/openstackx.git $CACHEDIR/opt/openstackx |
| 62 | git clone https://github.com/cloudbuilders/noVNC.git $CACHEDIR/opt/noVNC |
| 63 | git clone https://github.com/cloudbuilders/openstack-dashboard.git $CACHEDIR/opt/dash |
| 64 | git clone https://github.com/cloudbuilders/python-novaclient.git $CACHEDIR/opt/python-novaclient |
| 65 | git clone https://github.com/cloudbuilders/keystone.git $CACHEDIR/opt/keystone |
| 66 | git clone https://github.com/cloudbuilders/glance.git $CACHEDIR/opt/glance |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 67 | fi |
| 68 | |
| 69 | # Destroy the old container |
| 70 | lxc-destroy -n $CONTAINER |
| 71 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 72 | # Create the container |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 73 | lxc-create -n $CONTAINER -t natty -f $LXC_CONF |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 74 | |
Anthony Young | f9998ab | 2011-09-13 10:43:44 -0700 | [diff] [blame] | 75 | # Specify where our container rootfs lives |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 76 | ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/ |
| 77 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 78 | # Create a stack user that is a member of the libvirtd group so that stack |
| 79 | # is able to interact with libvirt. |
| 80 | chroot $ROOTFS groupadd libvirtd |
| 81 | chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd |
| 82 | |
| 83 | # a simple password - pass |
| 84 | echo stack:pass | chroot $ROOTFS chpasswd |
| 85 | |
| 86 | # and has sudo ability (in the future this should be limited to only what |
| 87 | # stack requires) |
| 88 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers |
| 89 | |
Anthony Young | 34c4702 | 2011-09-13 22:14:37 -0700 | [diff] [blame] | 90 | # Gracefully cp only if source file/dir exists |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 91 | function cp_it { |
| 92 | if [ -e $1 ] || [ -d $1 ]; then |
| 93 | cp -pr $1 $2 |
| 94 | fi |
| 95 | } |
| 96 | |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 97 | # Copy over your ssh keys and env if desired |
| 98 | if [ "$COPYENV" = "1" ]; then |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 99 | cp_it ~/.ssh $ROOTFS/opt/.ssh |
| 100 | cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys |
| 101 | cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig |
| 102 | cp_it ~/.vimrc $ROOTFS/opt/.vimrc |
| 103 | cp_it ~/.bashrc $ROOTFS/opt/.bashrc |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 104 | fi |
| 105 | |
Anthony Young | 1003952 | 2011-09-13 10:05:07 -0700 | [diff] [blame] | 106 | # Give stack ownership over /opt so it may do the work needed |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 107 | chroot $ROOTFS chown -R stack /opt |
| 108 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 109 | # Configure instance network |
| 110 | INTERFACES=$ROOTFS/etc/network/interfaces |
| 111 | cat > $INTERFACES <<EOF |
| 112 | auto lo |
| 113 | iface lo inet loopback |
| 114 | |
| 115 | auto eth0 |
| 116 | iface eth0 inet static |
| 117 | address $CONTAINER_IP |
| 118 | netmask $CONTAINER_NETMASK |
| 119 | gateway $CONTAINER_GATEWAY |
| 120 | EOF |
| 121 | |
Anthony Young | 1c36464 | 2011-09-13 20:21:42 -0700 | [diff] [blame] | 122 | # Configure the runner |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame^] | 123 | RUN_SH=$ROOTFS/opt/run.sh |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 124 | cat > $RUN_SH <<EOF |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 125 | #!/bin/bash |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 126 | # Make sure dns is set up |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame^] | 127 | echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0 |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 128 | sleep 1 |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 129 | |
Anthony Young | 927a656 | 2011-09-14 01:58:01 -0700 | [diff] [blame] | 130 | # Kill any existing screens |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame^] | 131 | su -c "killall screen" stack |
Anthony Young | 927a656 | 2011-09-14 01:58:01 -0700 | [diff] [blame] | 132 | |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 133 | # Install and run stack.sh |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame^] | 134 | sudo apt-get update |
| 135 | sudo apt-get -y --force-yes install git-core vim-nox sudo |
Anthony Young | d74d681 | 2011-09-14 01:35:54 -0700 | [diff] [blame] | 136 | if [ ! -d "/opt/nfs-stack" ]; then |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame^] | 137 | git clone git://github.com/cloudbuilders/nfs-stack.git ~/nfs-stack |
Anthony Young | 1c36464 | 2011-09-13 20:21:42 -0700 | [diff] [blame] | 138 | fi |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame^] | 139 | cd /opt/nfs-stack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 140 | EOF |
| 141 | |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 142 | # Make the run.sh executable |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame^] | 143 | chmod 755 $RUN_SH |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 144 | |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 145 | # Make runner launch on boot |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 146 | RC_LOCAL=$ROOTFS/etc/rc.local |
| 147 | cat > $RC_LOCAL <<EOF |
| 148 | #!/bin/sh -e |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame^] | 149 | su -c "/opt/run.sh" stack |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 150 | EOF |
| 151 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 152 | # Configure cgroup directory |
Anthony Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 153 | if ! mount | grep -q cgroup; then |
| 154 | mkdir -p /cgroup |
| 155 | mount none -t cgroup /cgroup |
| 156 | fi |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 157 | |
| 158 | # Start our container |
| 159 | lxc-start -d -n $CONTAINER |