blob: e1d64471a2a5cd5cc62e9d15a417d228d39dec9c [file] [log] [blame]
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07001#!/usr/bin/env bash
2
Jesse Andrews228f2462011-11-05 17:36:14 -07003# Ubuntu distro to install
4DIST_NAME=${DIST_NAME:-oneiric}
5
6# Make sure that we have the proper version of ubuntu (only works on natty/oneiric)
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07007UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
8if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then
9 if [ ! "natty" = "$UBUNTU_VERSION" ]; then
10 echo "This script only works with oneiric and natty"
11 exit 1
12 fi
13fi
14
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070015# Keep track of the current directory
16TOOLS_DIR=$(cd $(dirname "$0") && pwd)
17TOP_DIR=`cd $TOOLS_DIR/..; pwd`
18
Jesse Andrews228f2462011-11-05 17:36:14 -070019# exit on error to stop unexpected errors
20set -o errexit
21set -o xtrace
22
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070023# Abort if localrc is not set
24if [ ! -e $TOP_DIR/localrc ]; then
25 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
26 echo "See stack.sh for required passwords."
27 exit 1
28fi
29
30# Install deps if needed
31dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
32
33# Where to store files and instances
34WORK_DIR=${WORK_DIR:-/opt/kvmstack}
35
36# Where to store images
Jesse Andrews228f2462011-11-05 17:36:14 -070037image_dir=$WORK_DIR/images/$DIST_NAME
38mkdir -p $image_dir
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070039
40# Original version of built image
Jesse Andrews228f2462011-11-05 17:36:14 -070041uec_url=http://uec-images.ubuntu.com/$DIST_NAME/current/$DIST_NAME-server-cloudimg-amd64.tar.gz
Jesse Andrews96dffbc2011-11-05 17:37:33 -070042tarball=$image_dir/$(basename $uec_url)
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070043
44# download the base uec image if we haven't already
Jesse Andrews228f2462011-11-05 17:36:14 -070045if [ ! -f $tarball ]; then
46 curl $uec_url -o $tarball
Jesse Andrews3b768582011-11-05 17:40:20 -070047 (cd $image_dir && tar -Sxvzf $tarball)
Jesse Andrews9102d452011-11-05 23:49:08 -070048 resize-part-image $image_dir/*.img 10G $image_dir/disk
Jesse Andrews228f2462011-11-05 17:36:14 -070049 cp $image_dir/*-vmlinuz-virtual $image_dir/kernel
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070050fi
51
52cd $TOP_DIR
53
54# Source params
55source ./stackrc
56
57# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
58ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
59
60# Name of our instance, used by libvirt
61GUEST_NAME=${GUEST_NAME:-devstack}
62
63# Mop up after previous runs
64virsh destroy $GUEST_NAME || true
65
66# Where this vm is stored
Jesse Andrews228f2462011-11-05 17:36:14 -070067vm_dir=$WORK_DIR/instances/$GUEST_NAME
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070068
69# Create vm dir and remove old disk
Jesse Andrews228f2462011-11-05 17:36:14 -070070mkdir -p $vm_dir
71rm -f $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070072
73# Create a copy of the base image
Jesse Andrewsf5a76912011-11-05 17:47:50 -070074qemu-img create -f qcow2 -b $image_dir/disk $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070075
76# Back to devstack
77cd $TOP_DIR
78
79GUEST_NETWORK=${GUEST_NETWORK:-1}
80GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
81GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
82GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
83GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
84GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
85GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
86GUEST_RAM=${GUEST_RAM:-1524288}
87GUEST_CORES=${GUEST_CORES:-1}
88
89# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -070090NET_XML=$vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070091cat > $NET_XML <<EOF
92<network>
93 <name>devstack-$GUEST_NETWORK</name>
94 <bridge name="stackbr%d" />
95 <forward/>
Jesse Andrewsa6282622011-11-05 18:39:33 -070096 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK">
97 <dhcp>
Jesse Andrews63fa7ab2011-11-05 18:49:36 -070098 <range start='192.168.$GUEST_NETWORK.100' end='192.168.$GUEST_NETWORK.120' />
Jesse Andrewsa6282622011-11-05 18:39:33 -070099 </dhcp>
100 </ip>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700101</network>
102EOF
103
104if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
105 virsh net-destroy devstack-$GUEST_NETWORK || true
Jesse Andrews228f2462011-11-05 17:36:14 -0700106 virsh net-create $vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700107fi
108
109# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -0700110LIBVIRT_XML=$vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700111cat > $LIBVIRT_XML <<EOF
112<domain type='kvm'>
113 <name>$GUEST_NAME</name>
114 <memory>$GUEST_RAM</memory>
115 <os>
Jesse Andrews228f2462011-11-05 17:36:14 -0700116 <type>hvm</type>
Jesse Andrewsf5a76912011-11-05 17:47:50 -0700117 <kernel>$image_dir/kernel</kernel>
Jesse Andrews438ea572011-11-05 22:33:49 -0700118 <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 Andrews8b3eb5f2011-11-05 16:05:14 -0700119 </os>
120 <features>
121 <acpi/>
122 </features>
123 <clock offset='utc'/>
124 <vcpu>$GUEST_CORES</vcpu>
125 <devices>
126 <disk type='file'>
127 <driver type='qcow2'/>
Jesse Andrews228f2462011-11-05 17:36:14 -0700128 <source file='$vm_dir/disk'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700129 <target dev='vda' bus='virtio'/>
130 </disk>
131
132 <interface type='network'>
133 <source network='devstack-$GUEST_NETWORK'/>
134 </interface>
135
136 <!-- The order is significant here. File must be defined first -->
137 <serial type="file">
Jesse Andrews228f2462011-11-05 17:36:14 -0700138 <source path='$vm_dir/console.log'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700139 <target port='1'/>
140 </serial>
141
142 <console type='pty' tty='/dev/pts/2'>
143 <source path='/dev/pts/2'/>
144 <target port='0'/>
145 </console>
146
147 <serial type='pty'>
148 <source path='/dev/pts/2'/>
149 <target port='0'/>
150 </serial>
151
152 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
153 </devices>
154</domain>
155EOF
156
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700157
Jesse Andrewse49f7512011-11-05 22:34:45 -0700158rm -rf $vm_dir/uec
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700159cp -r $TOOLS_DIR/uec $vm_dir/uec
160
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700161# set metadata
162cat > $vm_dir/uec/meta-data<<EOF
163hostname: $GUEST_NAME
164instance-id: i-87018aed
165instance-type: m1.large
Jesse Andrews7306f3b2011-11-05 23:13:34 -0700166local-hostname: $GUEST_NAME.local
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700167EOF
168
Jesse Andrews63cb9232011-11-05 23:16:53 -0700169# set metadata
170cat > $vm_dir/uec/user-data<<EOF
Jesse Andrews446a3302011-11-05 23:36:29 -0700171#!/bin/bash
172apt-get update
173apt-get install git -y
174git clone https://github.com/cloudbuilders/devstack.git
175cd devstack
176echo DASH_BRANCH=instance-overview > localrc
177echo ADMIN_PASSWORD=golfing >> localrc
178echo MYSQL_PASSWORD=golfing >> localrc
179echo RABBIT_PASSWORD=golfing >> localrc
180echo SERVICE_TOKEN=123124123124 >> localrc
181echo FLAT_INTERFACE=br100 >> localrc
182./stack.sh
Jesse Andrews63cb9232011-11-05 23:16:53 -0700183EOF
184
Jesse Andrewsee34f622011-11-05 22:41:57 -0700185# (re)start a metadata service
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700186(
187 pid=`lsof -iTCP@192.168.$GUEST_NETWORK.1:4567 -n | awk '{print $2}' | tail -1`
Jesse Andrews9645b0c2011-11-05 23:05:33 -0700188 [ -z "$pid" ] || kill -9 $pid
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700189)
Jesse Andrewsf504e282011-11-05 22:29:35 -0700190cd $vm_dir/uec
191python meta.py 192.168.$GUEST_NETWORK.1:4567 &
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700192
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700193# Create the instance
Jesse Andrews63fa7ab2011-11-05 18:49:36 -0700194virsh create $vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700195
196# Tail the console log till we are done
197WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
198if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
Jesse Andrews228f2462011-11-05 17:36:14 -0700199 set +o xtrace
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700200 # Done creating the container, let's tail the log
201 echo
202 echo "============================================================="
203 echo " -- YAY! --"
204 echo "============================================================="
205 echo
206 echo "We're done launching the vm, about to start tailing the"
207 echo "stack.sh log. It will take a second or two to start."
208 echo
209 echo "Just CTRL-C at any time to stop tailing."
210
Jesse Andrews228f2462011-11-05 17:36:14 -0700211 while [ ! -e "$vm_dir/console.log" ]; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700212 sleep 1
213 done
214
Jesse Andrews228f2462011-11-05 17:36:14 -0700215 tail -F $vm_dir/console.log &
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700216
217 TAIL_PID=$!
218
219 function kill_tail() {
220 kill $TAIL_PID
221 exit 1
222 }
223
224 # Let Ctrl-c kill tail and exit
225 trap kill_tail SIGINT
226
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700227 echo "Waiting stack.sh to finish..."
Jesse Andrews228f2462011-11-05 17:36:14 -0700228 while ! cat $vm_dir/console.log | grep -q 'All done' ; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700229 sleep 1
230 done
231
232 set -o xtrace
233
234 kill $TAIL_PID
235
Jesse Andrews228f2462011-11-05 17:36:14 -0700236 if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700237 exit 1
238 fi
239 echo ""
240 echo "Finished - Zip-a-dee Doo-dah!"
241fi