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