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