Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 3 | # Make sure that we have the proper version of ubuntu (only works on natty/oneiric) |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 4 | UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'` |
| 5 | if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then |
| 6 | if [ ! "natty" = "$UBUNTU_VERSION" ]; then |
| 7 | echo "This script only works with oneiric and natty" |
| 8 | exit 1 |
| 9 | fi |
| 10 | fi |
| 11 | |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 12 | # Keep track of the current directory |
| 13 | TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
| 14 | TOP_DIR=`cd $TOOLS_DIR/..; pwd` |
| 15 | |
Jesse Andrews | 53d7533 | 2011-11-06 07:54:11 -0800 | [diff] [blame] | 16 | cd $TOP_DIR |
| 17 | |
| 18 | # Source params |
| 19 | source ./stackrc |
| 20 | |
| 21 | # Ubuntu distro to install |
| 22 | DIST_NAME=${DIST_NAME:-oneiric} |
| 23 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 24 | # exit on error to stop unexpected errors |
| 25 | set -o errexit |
| 26 | set -o xtrace |
| 27 | |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 28 | # Abort if localrc is not set |
| 29 | if [ ! -e $TOP_DIR/localrc ]; then |
| 30 | echo "You must have a localrc with ALL necessary passwords defined before proceeding." |
| 31 | echo "See stack.sh for required passwords." |
| 32 | exit 1 |
| 33 | fi |
| 34 | |
| 35 | # Install deps if needed |
| 36 | dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx |
| 37 | |
| 38 | # Where to store files and instances |
| 39 | WORK_DIR=${WORK_DIR:-/opt/kvmstack} |
| 40 | |
| 41 | # Where to store images |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 42 | image_dir=$WORK_DIR/images/$DIST_NAME |
| 43 | mkdir -p $image_dir |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 44 | |
| 45 | # Original version of built image |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 46 | uec_url=http://uec-images.ubuntu.com/$DIST_NAME/current/$DIST_NAME-server-cloudimg-amd64.tar.gz |
Jesse Andrews | 96dffbc | 2011-11-05 17:37:33 -0700 | [diff] [blame] | 47 | tarball=$image_dir/$(basename $uec_url) |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 48 | |
| 49 | # download the base uec image if we haven't already |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 50 | if [ ! -f $tarball ]; then |
| 51 | curl $uec_url -o $tarball |
Jesse Andrews | 3b76858 | 2011-11-05 17:40:20 -0700 | [diff] [blame] | 52 | (cd $image_dir && tar -Sxvzf $tarball) |
Jesse Andrews | 9102d45 | 2011-11-05 23:49:08 -0700 | [diff] [blame] | 53 | resize-part-image $image_dir/*.img 10G $image_dir/disk |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 54 | cp $image_dir/*-vmlinuz-virtual $image_dir/kernel |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 55 | fi |
| 56 | |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 57 | |
| 58 | # Configure the root password of the vm to be the same as ``ADMIN_PASSWORD`` |
| 59 | ROOT_PASSWORD=${ADMIN_PASSWORD:-password} |
| 60 | |
| 61 | # Name of our instance, used by libvirt |
| 62 | GUEST_NAME=${GUEST_NAME:-devstack} |
| 63 | |
| 64 | # Mop up after previous runs |
| 65 | virsh destroy $GUEST_NAME || true |
| 66 | |
| 67 | # Where this vm is stored |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 68 | vm_dir=$WORK_DIR/instances/$GUEST_NAME |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 69 | |
| 70 | # Create vm dir and remove old disk |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 71 | mkdir -p $vm_dir |
| 72 | rm -f $vm_dir/disk |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 73 | |
| 74 | # Create a copy of the base image |
Jesse Andrews | f5a7691 | 2011-11-05 17:47:50 -0700 | [diff] [blame] | 75 | qemu-img create -f qcow2 -b $image_dir/disk $vm_dir/disk |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 76 | |
| 77 | # Back to devstack |
| 78 | cd $TOP_DIR |
| 79 | |
| 80 | GUEST_NETWORK=${GUEST_NETWORK:-1} |
| 81 | GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes} |
| 82 | GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50} |
| 83 | GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24} |
| 84 | GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0} |
| 85 | GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1} |
| 86 | GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"} |
| 87 | GUEST_RAM=${GUEST_RAM:-1524288} |
| 88 | GUEST_CORES=${GUEST_CORES:-1} |
| 89 | |
| 90 | # libvirt.xml configuration |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 91 | NET_XML=$vm_dir/net.xml |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 92 | cat > $NET_XML <<EOF |
| 93 | <network> |
| 94 | <name>devstack-$GUEST_NETWORK</name> |
| 95 | <bridge name="stackbr%d" /> |
| 96 | <forward/> |
Jesse Andrews | a628262 | 2011-11-05 18:39:33 -0700 | [diff] [blame] | 97 | <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK"> |
| 98 | <dhcp> |
Jesse Andrews | 63fa7ab | 2011-11-05 18:49:36 -0700 | [diff] [blame] | 99 | <range start='192.168.$GUEST_NETWORK.100' end='192.168.$GUEST_NETWORK.120' /> |
Jesse Andrews | a628262 | 2011-11-05 18:39:33 -0700 | [diff] [blame] | 100 | </dhcp> |
| 101 | </ip> |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 102 | </network> |
| 103 | EOF |
| 104 | |
| 105 | if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then |
| 106 | virsh net-destroy devstack-$GUEST_NETWORK || true |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 107 | virsh net-create $vm_dir/net.xml |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 108 | fi |
| 109 | |
| 110 | # libvirt.xml configuration |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 111 | LIBVIRT_XML=$vm_dir/libvirt.xml |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 112 | cat > $LIBVIRT_XML <<EOF |
| 113 | <domain type='kvm'> |
| 114 | <name>$GUEST_NAME</name> |
| 115 | <memory>$GUEST_RAM</memory> |
| 116 | <os> |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 117 | <type>hvm</type> |
Jesse Andrews | f5a7691 | 2011-11-05 17:47:50 -0700 | [diff] [blame] | 118 | <kernel>$image_dir/kernel</kernel> |
Jesse Andrews | 438ea57 | 2011-11-05 22:33:49 -0700 | [diff] [blame] | 119 | <cmdline>root=/dev/vda ro console=ttyS0 init=/usr/lib/cloud-init/uncloud-init ds=nocloud-net;s=http://192.168.$GUEST_NETWORK.1:4567/ ubuntu-pass=ubuntu</cmdline> |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 120 | </os> |
| 121 | <features> |
| 122 | <acpi/> |
| 123 | </features> |
| 124 | <clock offset='utc'/> |
| 125 | <vcpu>$GUEST_CORES</vcpu> |
| 126 | <devices> |
| 127 | <disk type='file'> |
| 128 | <driver type='qcow2'/> |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 129 | <source file='$vm_dir/disk'/> |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 130 | <target dev='vda' bus='virtio'/> |
| 131 | </disk> |
| 132 | |
| 133 | <interface type='network'> |
| 134 | <source network='devstack-$GUEST_NETWORK'/> |
| 135 | </interface> |
| 136 | |
| 137 | <!-- The order is significant here. File must be defined first --> |
| 138 | <serial type="file"> |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 139 | <source path='$vm_dir/console.log'/> |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 140 | <target port='1'/> |
| 141 | </serial> |
| 142 | |
| 143 | <console type='pty' tty='/dev/pts/2'> |
| 144 | <source path='/dev/pts/2'/> |
| 145 | <target port='0'/> |
| 146 | </console> |
| 147 | |
| 148 | <serial type='pty'> |
| 149 | <source path='/dev/pts/2'/> |
| 150 | <target port='0'/> |
| 151 | </serial> |
| 152 | |
| 153 | <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/> |
| 154 | </devices> |
| 155 | </domain> |
| 156 | EOF |
| 157 | |
Jesse Andrews | d7ce7af | 2011-11-05 22:47:28 -0700 | [diff] [blame] | 158 | |
Jesse Andrews | e49f751 | 2011-11-05 22:34:45 -0700 | [diff] [blame] | 159 | rm -rf $vm_dir/uec |
Jesse Andrews | 9ed6bbd | 2011-11-05 22:28:46 -0700 | [diff] [blame] | 160 | cp -r $TOOLS_DIR/uec $vm_dir/uec |
| 161 | |
Jesse Andrews | d7ce7af | 2011-11-05 22:47:28 -0700 | [diff] [blame] | 162 | # set metadata |
| 163 | cat > $vm_dir/uec/meta-data<<EOF |
| 164 | hostname: $GUEST_NAME |
| 165 | instance-id: i-87018aed |
| 166 | instance-type: m1.large |
Jesse Andrews | 7306f3b | 2011-11-05 23:13:34 -0700 | [diff] [blame] | 167 | local-hostname: $GUEST_NAME.local |
Jesse Andrews | d7ce7af | 2011-11-05 22:47:28 -0700 | [diff] [blame] | 168 | EOF |
| 169 | |
Jesse Andrews | 63cb923 | 2011-11-05 23:16:53 -0700 | [diff] [blame] | 170 | # set metadata |
| 171 | cat > $vm_dir/uec/user-data<<EOF |
Jesse Andrews | 446a330 | 2011-11-05 23:36:29 -0700 | [diff] [blame] | 172 | #!/bin/bash |
| 173 | apt-get update |
Jesse Andrews | c7f72ad | 2011-11-06 08:00:28 -0800 | [diff] [blame] | 174 | apt-get install git sudo -y |
Jesse Andrews | 446a330 | 2011-11-05 23:36:29 -0700 | [diff] [blame] | 175 | git clone https://github.com/cloudbuilders/devstack.git |
| 176 | cd devstack |
Jesse Andrews | 81881ec | 2011-11-06 00:22:41 -0700 | [diff] [blame] | 177 | git remote set-url origin `cd $TOP_DIR; git remote show origin | grep Fetch | awk '{print $3}'` |
| 178 | git fetch |
Jesse Andrews | 11416bf | 2011-11-06 00:32:21 -0700 | [diff] [blame] | 179 | git checkout `git rev-parse HEAD` |
Jesse Andrews | 0c00895 | 2011-11-06 00:26:29 -0700 | [diff] [blame] | 180 | cat > localrc <<LOCAL_EOF |
Jesse Andrews | c7f72ad | 2011-11-06 08:00:28 -0800 | [diff] [blame] | 181 | ROOTSLEEP=0 |
Jesse Andrews | 6b1c26e | 2011-11-06 00:13:30 -0700 | [diff] [blame] | 182 | `cat $TOP_DIR/localrc` |
Jesse Andrews | 0c00895 | 2011-11-06 00:26:29 -0700 | [diff] [blame] | 183 | LOCAL_EOF |
Jesse Andrews | 446a330 | 2011-11-05 23:36:29 -0700 | [diff] [blame] | 184 | ./stack.sh |
Jesse Andrews | 63cb923 | 2011-11-05 23:16:53 -0700 | [diff] [blame] | 185 | EOF |
| 186 | |
Jesse Andrews | ee34f62 | 2011-11-05 22:41:57 -0700 | [diff] [blame] | 187 | # (re)start a metadata service |
Jesse Andrews | 3ce79aa | 2011-11-05 22:52:20 -0700 | [diff] [blame] | 188 | ( |
| 189 | pid=`lsof -iTCP@192.168.$GUEST_NETWORK.1:4567 -n | awk '{print $2}' | tail -1` |
Jesse Andrews | 9645b0c | 2011-11-05 23:05:33 -0700 | [diff] [blame] | 190 | [ -z "$pid" ] || kill -9 $pid |
Jesse Andrews | 3ce79aa | 2011-11-05 22:52:20 -0700 | [diff] [blame] | 191 | ) |
Jesse Andrews | f504e28 | 2011-11-05 22:29:35 -0700 | [diff] [blame] | 192 | cd $vm_dir/uec |
| 193 | python meta.py 192.168.$GUEST_NETWORK.1:4567 & |
Jesse Andrews | 9ed6bbd | 2011-11-05 22:28:46 -0700 | [diff] [blame] | 194 | |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 195 | # Create the instance |
Jesse Andrews | 63fa7ab | 2011-11-05 18:49:36 -0700 | [diff] [blame] | 196 | virsh create $vm_dir/libvirt.xml |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 197 | |
| 198 | # Tail the console log till we are done |
| 199 | WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1} |
| 200 | if [ "$WAIT_TILL_LAUNCH" = "1" ]; then |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 201 | set +o xtrace |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 202 | # Done creating the container, let's tail the log |
| 203 | echo |
| 204 | echo "=============================================================" |
| 205 | echo " -- YAY! --" |
| 206 | echo "=============================================================" |
| 207 | echo |
| 208 | echo "We're done launching the vm, about to start tailing the" |
| 209 | echo "stack.sh log. It will take a second or two to start." |
| 210 | echo |
| 211 | echo "Just CTRL-C at any time to stop tailing." |
| 212 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 213 | while [ ! -e "$vm_dir/console.log" ]; do |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 214 | sleep 1 |
| 215 | done |
| 216 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 217 | tail -F $vm_dir/console.log & |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 218 | |
| 219 | TAIL_PID=$! |
| 220 | |
| 221 | function kill_tail() { |
| 222 | kill $TAIL_PID |
| 223 | exit 1 |
| 224 | } |
| 225 | |
| 226 | # Let Ctrl-c kill tail and exit |
| 227 | trap kill_tail SIGINT |
| 228 | |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 229 | echo "Waiting stack.sh to finish..." |
Jesse Andrews | d55a515 | 2011-11-06 08:16:42 -0800 | [diff] [blame^] | 230 | while ! egrep -q '^stack.sh (completed|failed)' $vm_dir/console.log ; do |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 231 | sleep 1 |
| 232 | done |
| 233 | |
| 234 | set -o xtrace |
| 235 | |
| 236 | kill $TAIL_PID |
| 237 | |
Jesse Andrews | 228f246 | 2011-11-05 17:36:14 -0700 | [diff] [blame] | 238 | if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then |
Jesse Andrews | 8b3eb5f | 2011-11-05 16:05:14 -0700 | [diff] [blame] | 239 | exit 1 |
| 240 | fi |
| 241 | echo "" |
| 242 | echo "Finished - Zip-a-dee Doo-dah!" |
| 243 | fi |