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