blob: f82399dae9e83de26ec0a33a4c507873f602b609 [file] [log] [blame]
Jesse Andrewse97a2e72011-10-30 18:37:49 -07001#!/usr/bin/env bash
2
3# exit on error to stop unexpected errors
4set -o errexit
5
6# Make sure that we have the proper version of ubuntu
7UBUNTU_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
Dean Troyer55c02732011-11-01 17:44:03 -050015# Clean up any resources that may be in use
16cleanup() {
17 set +o errexit
18 unmount_images
19
20 if [ -n "$ROOTFS" ]; then
21 umount $ROOTFS/dev
22 umount $ROOTFS
23 fi
24
25 # Release NBD devices
26 if [ -n "$NBD" ]; then
27 qemu-nbd -d $NBD
28 fi
29
30 # Kill ourselves to signal any calling process
31 trap 2; kill -2 $$
32}
33
Dean Troyer264aba52011-11-04 13:22:09 -050034trap cleanup SIGHUP SIGINT SIGTERM SIGQUIT
Dean Troyer55c02732011-11-01 17:44:03 -050035
Jesse Andrewse97a2e72011-10-30 18:37:49 -070036# Echo commands
37set -o xtrace
38
39# Keep track of the current directory
40TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Jesse Andrews31989742011-10-30 18:56:05 -070041TOP_DIR=`cd $TOOLS_DIR/..; pwd`
Jesse Andrewse97a2e72011-10-30 18:37:49 -070042
43# Where to store files and instances
Jesse Andrewsb0559b22011-10-30 19:46:54 -070044WORK_DIR=${WORK_DIR:-/opt/kvmstack}
Jesse Andrewse97a2e72011-10-30 18:37:49 -070045
46# Where to store images
47IMAGES_DIR=$WORK_DIR/images
48
49# Create images dir
50mkdir -p $IMAGES_DIR
51
52# Abort if localrc is not set
53if [ ! -e $TOP_DIR/localrc ]; then
54 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
55 echo "See stack.sh for required passwords."
56 exit 1
57fi
58
Jesse Andrews7fa56132011-10-30 18:43:54 -070059cd $TOP_DIR
60
Jesse Andrewse97a2e72011-10-30 18:37:49 -070061# Source params
62source ./stackrc
63
64# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
65ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
66
Jesse Andrewse97a2e72011-10-30 18:37:49 -070067# Base image (natty by default)
68DIST_NAME=${DIST_NAME:-natty}
69IMAGE_FNAME=$DIST_NAME.raw
70
71# Name of our instance, used by libvirt
72GUEST_NAME=${GUEST_NAME:-devstack}
73
74# Original version of built image
Jesse Andrews2b7d2212011-10-30 19:37:56 -070075BASE_IMAGE=$IMAGES_DIR/$DIST_NAME.raw
Jesse Andrewse97a2e72011-10-30 18:37:49 -070076
77# Copy of base image, which we pre-install with tasty treats
78VM_IMAGE=$IMAGES_DIR/$DIST_NAME.$GUEST_NAME.raw
79
80# Mop up after previous runs
81virsh destroy $GUEST_NAME || true
82
83# Where this vm is stored
84VM_DIR=$WORK_DIR/instances/$GUEST_NAME
85
86# Create vm dir
87mkdir -p $VM_DIR
88
89# Mount point into copied base image
90COPY_DIR=$VM_DIR/copy
91mkdir -p $COPY_DIR
92
Jesse Andrews2b7d2212011-10-30 19:37:56 -070093# Get the base image if it does not yet exist
94if [ ! -e $BASE_IMAGE ]; then
95 $TOOLS_DIR/get_uec_image.sh -f raw -r 5000 $DIST_NAME $BASE_IMAGE
Jesse Andrewse97a2e72011-10-30 18:37:49 -070096fi
97
98# Create a copy of the base image
99if [ ! -e $VM_IMAGE ]; then
100 cp -p $BASE_IMAGE $VM_IMAGE
101fi
102
103# Unmount the copied base image
104function unmount_images() {
105 # unmount the filesystem
106 while df | grep -q $COPY_DIR; do
107 umount $COPY_DIR || echo 'ok'
108 sleep 1
109 done
110}
111
112# Unmount from failed runs
113unmount_images
114
115# Ctrl-c catcher
116function kill_unmount() {
117 unmount_images
118 exit 1
119}
120
121# Install deps if needed
122dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
123
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700124# Where Openstack code will live in image
125DEST=${DEST:-/opt/stack}
126
127# Mount the file system
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700128# For some reason, UEC-based images want 255 heads * 63 sectors * 512 byte sectors = 8225280
129mount -t ext4 -o loop,offset=8225280 $VM_IMAGE $COPY_DIR
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700130
131# git clone only if directory doesn't exist already. Since ``DEST`` might not
132# be owned by the installation user, we create the directory and change the
133# ownership to the proper user.
134function git_clone {
135 if [ ! -d $2 ]; then
136 sudo mkdir $2
137 sudo chown `whoami` $2
138 git clone $1 $2
139 cd $2
140 # This checkout syntax works for both branches and tags
141 git checkout $3
142 fi
143}
144
145# Make sure that base requirements are installed
146cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf
147chroot $COPY_DIR apt-get update
Anthony Youngca2c0472011-11-03 16:29:32 -0700148chroot $COPY_DIR apt-get install -y --download-only `cat files/apts/* | grep NOPRIME | cut -d\# -f1`
149chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | grep -v NOPRIME | cut -d\# -f1`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700150chroot $COPY_DIR pip install `cat files/pips/*`
151
152# Clean out code repos if directed to do so
153if [ "$CLEAN" = "1" ]; then
154 rm -rf $COPY_DIR/$DEST
155fi
156
157# Cache openstack code
158mkdir -p $COPY_DIR/$DEST
159git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
160git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
161git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
162git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
163git_clone $HORIZON_REPO $COPY_DIR/$DEST/horizon $HORIZON_BRANCH $HORIZON_TAG
164git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
165git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
166git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
167git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
Dean Troyer264aba52011-11-04 13:22:09 -0500168git_clone $CITEST_REPO $COPY_DIR/$DEST/openstack-integration-tests $CITEST_BRANCH
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700169
170# Back to devstack
171cd $TOP_DIR
172
173# Unmount the filesystems
174unmount_images
175
176# Network configuration variables
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700177GUEST_NETWORK=${GUEST_NETWORK:-1}
Jesse Andrews1d1dda12011-11-01 19:46:17 -0700178GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700179
180GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
181GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
182GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
183GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
184GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
185GUEST_RAM=${GUEST_RAM:-1524288}
186GUEST_CORES=${GUEST_CORES:-1}
187
188# libvirt.xml configuration
189NET_XML=$VM_DIR/net.xml
190cat > $NET_XML <<EOF
191<network>
192 <name>devstack-$GUEST_NETWORK</name>
193 <bridge name="stackbr%d" />
194 <forward/>
195 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" />
196</network>
197EOF
198
Jesse Andrews1d1dda12011-11-01 19:46:17 -0700199if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
200 virsh net-destroy devstack-$GUEST_NETWORK || true
201 virsh net-create $VM_DIR/net.xml
202fi
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700203
204# libvirt.xml configuration
205LIBVIRT_XML=$VM_DIR/libvirt.xml
206cat > $LIBVIRT_XML <<EOF
207<domain type='kvm'>
208 <name>$GUEST_NAME</name>
209 <memory>$GUEST_RAM</memory>
210 <os>
211 <type>hvm</type>
212 <bootmenu enable='yes'/>
213 </os>
214 <features>
215 <acpi/>
216 </features>
217 <vcpu>$GUEST_CORES</vcpu>
218 <devices>
219 <disk type='file'>
220 <driver type='qcow2'/>
221 <source file='$VM_DIR/disk'/>
222 <target dev='vda' bus='virtio'/>
223 </disk>
224
225 <interface type='network'>
226 <source network='devstack-$GUEST_NETWORK'/>
227 </interface>
228
229 <!-- The order is significant here. File must be defined first -->
230 <serial type="file">
231 <source path='$VM_DIR/console.log'/>
232 <target port='1'/>
233 </serial>
234
235 <console type='pty' tty='/dev/pts/2'>
236 <source path='/dev/pts/2'/>
237 <target port='0'/>
238 </console>
239
240 <serial type='pty'>
241 <source path='/dev/pts/2'/>
242 <target port='0'/>
243 </serial>
244
245 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
246 </devices>
247</domain>
248EOF
249
250# Mount point for instance fs
251ROOTFS=$VM_DIR/root
252mkdir -p $ROOTFS
253
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700254# Clean up from previous runs
255umount $ROOTFS || echo 'ok'
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700256
257# Clean up old runs
258cd $VM_DIR
259rm -f $VM_DIR/disk
260
261# Create our instance fs
262qemu-img create -f qcow2 -b $VM_IMAGE disk
263
Dean Troyerdccd6b92011-11-01 15:46:14 -0500264# Finds the next available NBD device
265# Exits script if error connecting or none free
266# map_nbd image
267# returns full nbd device path
268function map_nbd {
269 for i in `seq 0 15`; do
270 if [ ! -e /sys/block/nbd$i/pid ]; then
271 NBD=/dev/nbd$i
272 # Connect to nbd and wait till it is ready
273 qemu-nbd -c $NBD $1
274 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
275 echo "Couldn't connect $NBD"
276 exit 1
277 fi
278 break
279 fi
280 done
281 if [ -z "$NBD" ]; then
282 echo "No free NBD slots"
283 exit 1
284 fi
285 echo $NBD
286}
287
Dean Troyer7a569732011-10-31 17:34:29 -0500288# Make sure we have nbd-ness
289modprobe nbd max_part=63
290
291# Set up nbd
Dean Troyerdccd6b92011-11-01 15:46:14 -0500292NBD=`map_nbd disk`
Dean Troyer7a569732011-10-31 17:34:29 -0500293NBD_DEV=`basename $NBD`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700294
295# Mount the instance
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700296mount ${NBD}p1 $ROOTFS
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700297
298# Configure instance network
299INTERFACES=$ROOTFS/etc/network/interfaces
300cat > $INTERFACES <<EOF
301auto lo
302iface lo inet loopback
303
304auto eth0
305iface eth0 inet static
306 address $GUEST_IP
307 netmask $GUEST_NETMASK
308 gateway $GUEST_GATEWAY
309EOF
310
311# User configuration for the instance
312chroot $ROOTFS groupadd libvirtd || true
313chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrews5f894cd2011-10-30 19:52:50 -0700314cp -pr $TOP_DIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700315echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
316echo "stack:pass" | chroot $ROOTFS chpasswd
317echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
318
319# Gracefully cp only if source file/dir exists
320function cp_it {
321 if [ -e $1 ] || [ -d $1 ]; then
322 cp -pRL $1 $2
323 fi
324}
325
326# Copy over your ssh keys and env if desired
327COPYENV=${COPYENV:-1}
328if [ "$COPYENV" = "1" ]; then
329 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
330 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
331 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
332 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
333 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
334fi
335
336# pre-cache uec images
337for image_url in ${IMAGE_URLS//,/ }; do
338 IMAGE_FNAME=`basename "$image_url"`
339 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
340 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
341 fi
342 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
343done
344
345# Configure the runner
346RUN_SH=$ROOTFS/$DEST/run.sh
347cat > $RUN_SH <<EOF
348#!/usr/bin/env bash
349
350# Kill any existing screens
351killall screen
352
353# Install and run stack.sh
354sudo apt-get update
355sudo apt-get -y --force-yes install git-core vim-nox sudo
356if [ ! -d "$DEST/devstack" ]; then
357 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
358fi
359cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
360echo >> /$DEST/run.sh.log
361echo >> /$DEST/run.sh.log
362echo "All done! Time to start clicking." >> /$DEST/run.sh.log
363cat $DEST/run.sh.log
364EOF
365chmod 755 $RUN_SH
366
367# Make runner launch on boot
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700368RC_LOCAL=$ROOTFS/etc/init.d/zlocal
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700369cat > $RC_LOCAL <<EOF
370#!/bin/sh -e
Jesse Andrewsddcc36d2011-10-30 22:41:23 -0700371# cloud-init overwrites the hostname with ubuntuhost
372echo $GUEST_NAME > /etc/hostname
373hostname $GUEST_NAME
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700374su -c "$DEST/run.sh" stack
375EOF
376chmod +x $RC_LOCAL
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700377chroot $ROOTFS sudo update-rc.d zlocal defaults 99
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700378
379# Make our ip address hostnames look nice at the command prompt
380echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
381echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
382
383# Give stack ownership over $DEST so it may do the work needed
384chroot $ROOTFS chown -R stack $DEST
385
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700386# Set the hostname
387echo $GUEST_NAME > $ROOTFS/etc/hostname
388
389# We need the hostname to resolve for rabbit to launch
390if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
391 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
392fi
393
Dean Troyer6fe687b2011-10-31 20:30:04 -0500394# GRUB 2 wants to see /dev
395mount -o bind /dev $ROOTFS/dev
396
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700397# Change boot params so that we get a console log
398G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
399sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub
Jesse Andrews2c5201b2011-10-30 20:44:26 -0700400sed -e "s,GRUB_CMDLINE_LINUX_DEFAULT=.*$,GRUB_CMDLINE_LINUX_DEFAULT=\"console=ttyS0 console=tty0 ds=nocloud ubuntu-pass=pass\",g" -i $ROOTFS/etc/default/grub
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700401sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub
402echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub
403echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub
404echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub
405
406chroot $ROOTFS update-grub
407umount $ROOTFS/dev
408
409# Pre-generate ssh host keys and allow password login
410chroot $ROOTFS dpkg-reconfigure openssh-server
411sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config
412
Dean Troyer264aba52011-11-04 13:22:09 -0500413# Pre-load an image for testing
414UEC_NAME=$DIST_NAME-server-cloudimg-amd64
415CIVMDIR=${ROOTFS}${DEST}/openstack-integration-tests/include/sample_vm
416if [ ! -e $CIVMDIR/$UEC_NAME.tar.gz ]; then
417 mkdir -p $CIVMDIR
418 (cd $CIVMDIR && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME.tar.gz;
419 tar xzf $UEC_NAME.tar.gz;)
420fi
421
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700422# Unmount
423umount $ROOTFS || echo 'ok'
Dean Troyer55c02732011-11-01 17:44:03 -0500424ROOTFS=""
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700425qemu-nbd -d $NBD
Dean Troyer55c02732011-11-01 17:44:03 -0500426NBD=""
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700427
428# Create the instance
429cd $VM_DIR && virsh create libvirt.xml
430
431# Tail the console log till we are done
432WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
433if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
434 # Done creating the container, let's tail the log
435 echo
436 echo "============================================================="
437 echo " -- YAY! --"
438 echo "============================================================="
439 echo
440 echo "We're done launching the vm, about to start tailing the"
441 echo "stack.sh log. It will take a second or two to start."
442 echo
443 echo "Just CTRL-C at any time to stop tailing."
444
445 while [ ! -e "$VM_DIR/console.log" ]; do
446 sleep 1
447 done
448
Jesse Andrews16341962011-10-31 00:21:56 -0700449 tail -F $VM_DIR/console.log &
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700450
451 TAIL_PID=$!
452
453 function kill_tail() {
454 kill $TAIL_PID
455 exit 1
456 }
457
458 # Let Ctrl-c kill tail and exit
459 trap kill_tail SIGINT
460
Jesse Andrews16341962011-10-31 00:21:56 -0700461 set +o xtrace
462
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700463 echo "Waiting stack.sh to finish..."
464 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
Jesse Andrews16341962011-10-31 00:21:56 -0700465 sleep 1
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700466 done
467
Jesse Andrews16341962011-10-31 00:21:56 -0700468 set -o xtrace
469
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700470 kill $TAIL_PID
471
Jesse Andrewsa06ac1c2011-10-31 22:29:23 -0700472 if ! grep -q "^stack.sh completed in" $VM_DIR/console.log; then
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700473 exit 1
474 fi
475 echo ""
476 echo "Finished - Zip-a-dee Doo-dah!"
477fi