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