Anthony Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Jesse Andrews | 18ebd86 | 2011-09-19 14:23:42 -0700 | [diff] [blame] | 2 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 3 | # Configurable params |
| 4 | BRIDGE=${BRIDGE:-br0} |
Anthony Young | b748e69 | 2011-09-13 10:16:13 -0700 | [diff] [blame] | 5 | CONTAINER=${CONTAINER:-STACK} |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 6 | CONTAINER_IP=${CONTAINER_IP:-192.168.1.50} |
| 7 | CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24} |
| 8 | CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0} |
| 9 | CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1} |
Anthony Young | 77dbb07 | 2011-09-14 00:49:39 -0700 | [diff] [blame] | 10 | NAMESERVER=${NAMESERVER:-$CONTAINER_GATEWAY} |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 11 | COPYENV=${COPYENV:-1} |
| 12 | |
Anthony Young | 1c36464 | 2011-09-13 20:21:42 -0700 | [diff] [blame] | 13 | # Param string to pass to stack.sh. Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova" |
| 14 | STACKSH_PARAMS=${STACKSH_PARAMS:-} |
| 15 | |
Anthony Young | 936c928 | 2011-09-13 23:36:43 -0700 | [diff] [blame] | 16 | # Warn users who aren't on natty |
| 17 | if ! grep -q natty /etc/lsb-release; then |
| 18 | echo "WARNING: this script has only been tested on natty" |
| 19 | fi |
| 20 | |
Anthony Young | 190321e | 2011-09-13 23:21:29 -0700 | [diff] [blame] | 21 | # Install deps |
Anthony Young | 856d09f | 2011-09-19 19:49:20 -0700 | [diff] [blame] | 22 | apt-get install -y lxc debootstrap |
Anthony Young | 190321e | 2011-09-13 23:21:29 -0700 | [diff] [blame] | 23 | |
| 24 | # 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] | 25 | if ! which cgdelete | grep -q cgdelete; then |
Anthony Young | 856d09f | 2011-09-19 19:49:20 -0700 | [diff] [blame] | 26 | apt-get install -y g++ bison flex libpam0g-dev |
Anthony Young | 190321e | 2011-09-13 23:21:29 -0700 | [diff] [blame] | 27 | 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 |
| 28 | cd /tmp && bunzip2 libcgroup-0.37.1.tar.bz2 && tar xfv libcgroup-0.37.1.tar |
| 29 | cd libcgroup-0.37.1 |
| 30 | ./configure |
| 31 | make install |
Anthony Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 32 | fi |
| 33 | |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 34 | # Create lxc configuration |
| 35 | LXC_CONF=/tmp/$CONTAINER.conf |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 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 | |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 45 | # Shutdown any existing container |
| 46 | lxc-stop -n $CONTAINER |
| 47 | |
Anthony Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 48 | # This kills zombie containers |
| 49 | if [ -d /cgroup/$CONTAINER ]; then |
| 50 | cgdelete -r cpu,net_cls:$CONTAINER |
| 51 | fi |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 52 | |
Jesse Andrews | 18ebd86 | 2011-09-19 14:23:42 -0700 | [diff] [blame] | 53 | |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 54 | # Warm the base image on first install |
| 55 | CACHEDIR=/var/cache/lxc/natty/rootfs-amd64 |
Anthony Young | 40f8d16 | 2011-09-14 10:20:14 -0700 | [diff] [blame] | 56 | if [ ! -d $CACHEDIR ]; then |
Jesse Andrews | 18ebd86 | 2011-09-19 14:23:42 -0700 | [diff] [blame] | 57 | # by deleting the container, we force lxc-create to re-bootstrap (lxc is |
| 58 | # lazy and doesn't do anything if a container already exists) |
| 59 | lxc-destroy -n $CONTAINER |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 60 | # trigger the initial debootstrap |
| 61 | lxc-create -n $CONTAINER -t natty -f $LXC_CONF |
| 62 | chroot $CACHEDIR apt-get update |
Anthony Young | bf188ef | 2011-09-19 20:23:42 -0700 | [diff] [blame] | 63 | chroot $CACHEDIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"` |
Jesse Andrews | 4d28218 | 2011-09-16 11:27:43 -0700 | [diff] [blame] | 64 | chroot $CACHEDIR pip install `cat files/pips/*` |
Anthony Young | db8f7f7 | 2011-09-20 02:12:46 -0700 | [diff] [blame] | 65 | # FIXME (anthony) - provide ability to vary source locations |
| 66 | #git clone https://github.com/cloudbuilders/nova.git $CACHEDIR/opt/nova |
| 67 | bzr clone lp:~hudson-openstack/nova/milestone-proposed/ $CACHEDIR/opt/nova |
Anthony Young | 3859f73 | 2011-09-14 02:33:43 -0700 | [diff] [blame] | 68 | git clone https://github.com/cloudbuilders/openstackx.git $CACHEDIR/opt/openstackx |
| 69 | git clone https://github.com/cloudbuilders/noVNC.git $CACHEDIR/opt/noVNC |
| 70 | git clone https://github.com/cloudbuilders/openstack-dashboard.git $CACHEDIR/opt/dash |
| 71 | git clone https://github.com/cloudbuilders/python-novaclient.git $CACHEDIR/opt/python-novaclient |
| 72 | git clone https://github.com/cloudbuilders/keystone.git $CACHEDIR/opt/keystone |
| 73 | git clone https://github.com/cloudbuilders/glance.git $CACHEDIR/opt/glance |
Anthony Young | 6053496 | 2011-09-13 10:40:04 -0700 | [diff] [blame] | 74 | fi |
| 75 | |
| 76 | # Destroy the old container |
| 77 | lxc-destroy -n $CONTAINER |
| 78 | |
Anthony Young | 23761c3 | 2011-09-16 14:54:20 -0700 | [diff] [blame] | 79 | # If this call is to TERMINATE the container then exit |
| 80 | if [ "$TERMINATE" = "1" ]; then |
| 81 | exit |
| 82 | fi |
| 83 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 84 | # Create the container |
Anthony Young | 40203cb | 2011-09-13 09:17:56 -0700 | [diff] [blame] | 85 | lxc-create -n $CONTAINER -t natty -f $LXC_CONF |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 86 | |
Anthony Young | f9998ab | 2011-09-13 10:43:44 -0700 | [diff] [blame] | 87 | # Specify where our container rootfs lives |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 88 | ROOTFS=/var/lib/lxc/$CONTAINER/rootfs/ |
| 89 | |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 90 | # Create a stack user that is a member of the libvirtd group so that stack |
| 91 | # is able to interact with libvirt. |
| 92 | chroot $ROOTFS groupadd libvirtd |
| 93 | chroot $ROOTFS useradd stack -s /bin/bash -d /opt -G libvirtd |
| 94 | |
| 95 | # a simple password - pass |
| 96 | echo stack:pass | chroot $ROOTFS chpasswd |
| 97 | |
| 98 | # and has sudo ability (in the future this should be limited to only what |
| 99 | # stack requires) |
| 100 | echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers |
| 101 | |
Anthony Young | 34c4702 | 2011-09-13 22:14:37 -0700 | [diff] [blame] | 102 | # Gracefully cp only if source file/dir exists |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 103 | function cp_it { |
| 104 | if [ -e $1 ] || [ -d $1 ]; then |
| 105 | cp -pr $1 $2 |
| 106 | fi |
| 107 | } |
| 108 | |
Anthony Young | b3c0454 | 2011-09-13 01:28:18 -0700 | [diff] [blame] | 109 | # Copy over your ssh keys and env if desired |
| 110 | if [ "$COPYENV" = "1" ]; then |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 111 | cp_it ~/.ssh $ROOTFS/opt/.ssh |
| 112 | cp_it ~/.ssh/id_rsa.pub $ROOTFS/opt/.ssh/authorized_keys |
| 113 | cp_it ~/.gitconfig $ROOTFS/opt/.gitconfig |
| 114 | cp_it ~/.vimrc $ROOTFS/opt/.vimrc |
| 115 | cp_it ~/.bashrc $ROOTFS/opt/.bashrc |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 116 | fi |
| 117 | |
Anthony Young | fde5a1c | 2011-09-15 23:49:02 -0700 | [diff] [blame] | 118 | # Make our ip address hostnames look nice at the command prompt |
| 119 | echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/opt/.bashrc |
| 120 | echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile |
| 121 | |
Anthony Young | 1003952 | 2011-09-13 10:05:07 -0700 | [diff] [blame] | 122 | # Give stack ownership over /opt so it may do the work needed |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 123 | chroot $ROOTFS chown -R stack /opt |
| 124 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 125 | # Configure instance network |
| 126 | INTERFACES=$ROOTFS/etc/network/interfaces |
| 127 | cat > $INTERFACES <<EOF |
| 128 | auto lo |
| 129 | iface lo inet loopback |
| 130 | |
| 131 | auto eth0 |
| 132 | iface eth0 inet static |
| 133 | address $CONTAINER_IP |
| 134 | netmask $CONTAINER_NETMASK |
| 135 | gateway $CONTAINER_GATEWAY |
| 136 | EOF |
| 137 | |
Anthony Young | 1c36464 | 2011-09-13 20:21:42 -0700 | [diff] [blame] | 138 | # Configure the runner |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame] | 139 | RUN_SH=$ROOTFS/opt/run.sh |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 140 | cat > $RUN_SH <<EOF |
Anthony Young | 10791a1 | 2011-09-14 09:40:58 -0700 | [diff] [blame] | 141 | #!/usr/bin/env bash |
Anthony Young | 7c3e5ed | 2011-09-13 09:57:31 -0700 | [diff] [blame] | 142 | # Make sure dns is set up |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame] | 143 | echo "nameserver $NAMESERVER" | sudo resolvconf -a eth0 |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 144 | sleep 1 |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 145 | |
Anthony Young | 927a656 | 2011-09-14 01:58:01 -0700 | [diff] [blame] | 146 | # Kill any existing screens |
Anthony Young | 92e31ab | 2011-09-14 02:56:41 -0700 | [diff] [blame] | 147 | killall screen |
Anthony Young | 927a656 | 2011-09-14 01:58:01 -0700 | [diff] [blame] | 148 | |
Anthony Young | 99003e7 | 2011-09-13 02:05:12 -0700 | [diff] [blame] | 149 | # Install and run stack.sh |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame] | 150 | sudo apt-get update |
| 151 | sudo apt-get -y --force-yes install git-core vim-nox sudo |
Jesse Andrews | 47e115e | 2011-09-14 11:25:47 -0700 | [diff] [blame] | 152 | if [ ! -d "/opt/devstack" ]; then |
Anthony Young | 550ec96 | 2011-09-15 21:05:50 -0700 | [diff] [blame] | 153 | git clone git://github.com/cloudbuilders/devstack.git /opt/devstack |
Anthony Young | 1c36464 | 2011-09-13 20:21:42 -0700 | [diff] [blame] | 154 | fi |
Jesse Andrews | 47e115e | 2011-09-14 11:25:47 -0700 | [diff] [blame] | 155 | cd /opt/devstack && $STACKSH_PARAMS ./stack.sh > /opt/run.sh.log |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 156 | EOF |
| 157 | |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 158 | # Make the run.sh executable |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame] | 159 | chmod 755 $RUN_SH |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 160 | |
Anthony Young | fe1d95a | 2011-09-13 22:09:36 -0700 | [diff] [blame] | 161 | # Make runner launch on boot |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 162 | RC_LOCAL=$ROOTFS/etc/rc.local |
| 163 | cat > $RC_LOCAL <<EOF |
| 164 | #!/bin/sh -e |
Anthony Young | 56e6292 | 2011-09-14 02:54:27 -0700 | [diff] [blame] | 165 | su -c "/opt/run.sh" stack |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 166 | EOF |
| 167 | |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 168 | # Configure cgroup directory |
Anthony Young | 4f27922 | 2011-09-13 21:51:28 -0700 | [diff] [blame] | 169 | if ! mount | grep -q cgroup; then |
| 170 | mkdir -p /cgroup |
| 171 | mount none -t cgroup /cgroup |
| 172 | fi |
Anthony Young | 72d6963 | 2011-09-12 21:09:55 -0700 | [diff] [blame] | 173 | |
| 174 | # Start our container |
| 175 | lxc-start -d -n $CONTAINER |