|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Make sure that we have the proper version of ubuntu (only works on natty/oneiric) |
| 4 | +if ! egrep -q "oneiric|natty" /etc/lsb-release; then |
| 5 | + echo "This script only works with ubuntu oneiric and natty" |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +# Keep track of the current directory |
| 10 | +TOOLS_DIR=$(cd $(dirname "$0") && pwd) |
| 11 | +TOP_DIR=`cd $TOOLS_DIR/..; pwd` |
| 12 | + |
| 13 | +cd $TOP_DIR |
| 14 | + |
| 15 | +# Source params |
| 16 | +source ./stackrc |
| 17 | + |
| 18 | +# Ubuntu distro to install |
| 19 | +DIST_NAME=${DIST_NAME:-oneiric} |
| 20 | + |
| 21 | +# Configure how large the VM should be |
| 22 | +GUEST_SIZE=${GUEST_SIZE:-10G} |
| 23 | + |
| 24 | +# exit on error to stop unexpected errors |
| 25 | +set -o errexit |
| 26 | +set -o xtrace |
| 27 | + |
| 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 | +DEPS="kvm libvirt-bin kpartx" |
| 37 | +dpkg -l $DEPS || apt-get install -y --force-yes $DEPS |
| 38 | + |
| 39 | +# Where to store files and instances |
| 40 | +WORK_DIR=${WORK_DIR:-/opt/kvmstack} |
| 41 | + |
| 42 | +# Where to store images |
| 43 | +image_dir=$WORK_DIR/images/$DIST_NAME |
| 44 | +mkdir -p $image_dir |
| 45 | + |
| 46 | +# Original version of built image |
| 47 | +uec_url=http://uec-images.ubuntu.com/$DIST_NAME/current/$DIST_NAME-server-cloudimg-amd64.tar.gz |
| 48 | +tarball=$image_dir/$(basename $uec_url) |
| 49 | + |
| 50 | +# download the base uec image if we haven't already |
| 51 | +if [ ! -f $tarball ]; then |
| 52 | + curl $uec_url -o $tarball |
| 53 | + (cd $image_dir && tar -Sxvzf $tarball) |
| 54 | + resize-part-image $image_dir/*.img $GUEST_SIZE $image_dir/disk |
| 55 | + cp $image_dir/*-vmlinuz-virtual $image_dir/kernel |
| 56 | +fi |
| 57 | + |
| 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 |
| 69 | +vm_dir=$WORK_DIR/instances/$GUEST_NAME |
| 70 | + |
| 71 | +# Create vm dir and remove old disk |
| 72 | +mkdir -p $vm_dir |
| 73 | +rm -f $vm_dir/disk |
| 74 | + |
| 75 | +# Create a copy of the base image |
| 76 | +qemu-img create -f qcow2 -b $image_dir/disk $vm_dir/disk |
| 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 |
| 92 | +NET_XML=$vm_dir/net.xml |
| 93 | +cat > $NET_XML <<EOF |
| 94 | +<network> |
| 95 | + <name>devstack-$GUEST_NETWORK</name> |
| 96 | + <bridge name="stackbr%d" /> |
| 97 | + <forward/> |
| 98 | + <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK"> |
| 99 | + <dhcp> |
| 100 | + <range start='192.168.$GUEST_NETWORK.2' end='192.168.$GUEST_NETWORK.127' /> |
| 101 | + </dhcp> |
| 102 | + </ip> |
| 103 | +</network> |
| 104 | +EOF |
| 105 | + |
| 106 | +if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then |
| 107 | + virsh net-destroy devstack-$GUEST_NETWORK || true |
| 108 | + # destroying the network isn't enough to delete the leases |
| 109 | + rm -f /var/lib/libvirt/dnsmasq/devstack-$GUEST_NETWORK.leases |
| 110 | + virsh net-create $vm_dir/net.xml |
| 111 | +fi |
| 112 | + |
| 113 | +# libvirt.xml configuration |
| 114 | +LIBVIRT_XML=$vm_dir/libvirt.xml |
| 115 | +cat > $LIBVIRT_XML <<EOF |
| 116 | +<domain type='kvm'> |
| 117 | + <name>$GUEST_NAME</name> |
| 118 | + <memory>$GUEST_RAM</memory> |
| 119 | + <os> |
| 120 | + <type>hvm</type> |
| 121 | + <kernel>$image_dir/kernel</kernel> |
| 122 | + <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> |
| 123 | + </os> |
| 124 | + <features> |
| 125 | + <acpi/> |
| 126 | + </features> |
| 127 | + <clock offset='utc'/> |
| 128 | + <vcpu>$GUEST_CORES</vcpu> |
| 129 | + <devices> |
| 130 | + <disk type='file'> |
| 131 | + <driver type='qcow2'/> |
| 132 | + <source file='$vm_dir/disk'/> |
| 133 | + <target dev='vda' bus='virtio'/> |
| 134 | + </disk> |
| 135 | +
|
| 136 | + <interface type='network'> |
| 137 | + <source network='devstack-$GUEST_NETWORK'/> |
| 138 | + </interface> |
| 139 | + |
| 140 | + <!-- The order is significant here. File must be defined first --> |
| 141 | + <serial type="file"> |
| 142 | + <source path='$vm_dir/console.log'/> |
| 143 | + <target port='1'/> |
| 144 | + </serial> |
| 145 | +
|
| 146 | + <console type='pty' tty='/dev/pts/2'> |
| 147 | + <source path='/dev/pts/2'/> |
| 148 | + <target port='0'/> |
| 149 | + </console> |
| 150 | +
|
| 151 | + <serial type='pty'> |
| 152 | + <source path='/dev/pts/2'/> |
| 153 | + <target port='0'/> |
| 154 | + </serial> |
| 155 | +
|
| 156 | + <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/> |
| 157 | + </devices> |
| 158 | +</domain> |
| 159 | +EOF |
| 160 | + |
| 161 | + |
| 162 | +rm -rf $vm_dir/uec |
| 163 | +cp -r $TOOLS_DIR/uec $vm_dir/uec |
| 164 | + |
| 165 | +# set metadata |
| 166 | +cat > $vm_dir/uec/meta-data<<EOF |
| 167 | +hostname: $GUEST_NAME |
| 168 | +instance-id: i-hop |
| 169 | +instance-type: m1.ignore |
| 170 | +local-hostname: $GUEST_NAME.local |
| 171 | +EOF |
| 172 | + |
| 173 | +# set metadata |
| 174 | +cat > $vm_dir/uec/user-data<<EOF |
| 175 | +#!/bin/bash |
| 176 | +# hostname needs to resolve for rabbit |
| 177 | +sed -i "s/127.0.0.1/127.0.0.1 \`hostname\`/" /etc/hosts |
| 178 | +apt-get update |
| 179 | +apt-get install git sudo -y |
| 180 | +git clone https://github.com/cloudbuilders/devstack.git |
| 181 | +cd devstack |
| 182 | +git remote set-url origin `cd $TOP_DIR; git remote show origin | grep Fetch | awk '{print $3}'` |
| 183 | +git fetch |
| 184 | +git checkout `git rev-parse HEAD` |
| 185 | +cat > localrc <<LOCAL_EOF |
| 186 | +ROOTSLEEP=0 |
| 187 | +`cat $TOP_DIR/localrc` |
| 188 | +LOCAL_EOF |
| 189 | +./stack.sh |
| 190 | +EOF |
| 191 | + |
| 192 | +# (re)start a metadata service |
| 193 | +( |
| 194 | + pid=`lsof -iTCP@192.168.$GUEST_NETWORK.1:4567 -n | awk '{print $2}' | tail -1` |
| 195 | + [ -z "$pid" ] || kill -9 $pid |
| 196 | +) |
| 197 | +cd $vm_dir/uec |
| 198 | +python meta.py 192.168.$GUEST_NETWORK.1:4567 & |
| 199 | + |
| 200 | +# Create the instance |
| 201 | +virsh create $vm_dir/libvirt.xml |
| 202 | + |
| 203 | +# Tail the console log till we are done |
| 204 | +WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1} |
| 205 | +if [ "$WAIT_TILL_LAUNCH" = "1" ]; then |
| 206 | + set +o xtrace |
| 207 | + # Done creating the container, let's tail the log |
| 208 | + echo |
| 209 | + echo "=============================================================" |
| 210 | + echo " -- YAY! --" |
| 211 | + echo "=============================================================" |
| 212 | + echo |
| 213 | + echo "We're done launching the vm, about to start tailing the" |
| 214 | + echo "stack.sh log. It will take a second or two to start." |
| 215 | + echo |
| 216 | + echo "Just CTRL-C at any time to stop tailing." |
| 217 | + |
| 218 | + while [ ! -e "$vm_dir/console.log" ]; do |
| 219 | + sleep 1 |
| 220 | + done |
| 221 | + |
| 222 | + tail -F $vm_dir/console.log & |
| 223 | + |
| 224 | + TAIL_PID=$! |
| 225 | + |
| 226 | + function kill_tail() { |
| 227 | + kill $TAIL_PID |
| 228 | + exit 1 |
| 229 | + } |
| 230 | + |
| 231 | + # Let Ctrl-c kill tail and exit |
| 232 | + trap kill_tail SIGINT |
| 233 | + |
| 234 | + echo "Waiting stack.sh to finish..." |
| 235 | + while ! egrep -q '^stack.sh (completed|failed)' $vm_dir/console.log ; do |
| 236 | + sleep 1 |
| 237 | + done |
| 238 | + |
| 239 | + set -o xtrace |
| 240 | + |
| 241 | + kill $TAIL_PID |
| 242 | + |
| 243 | + if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then |
| 244 | + exit 1 |
| 245 | + fi |
| 246 | + echo "" |
| 247 | + echo "Finished - Zip-a-dee Doo-dah!" |
| 248 | +fi |
0 commit comments