blob: e2c33c66aebfbe301bbeb34bbd31709bf75a9504 [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
Dean Troyer89d1d022011-11-05 16:16:54 -0500170# Pre-load an image for testing
171UEC_NAME=$DIST_NAME-server-cloudimg-amd64
172CIVMDIR=${COPY_DIR}${DEST}/openstack-integration-tests/include/sample_vm
173if [ ! -e $CIVMDIR/$UEC_NAME.tar.gz ]; then
174 mkdir -p $CIVMDIR
175 (cd $CIVMDIR && wget -N http://uec-images.ubuntu.com/$DIST_NAME/current/$UEC_NAME.tar.gz;
176 tar xzf $UEC_NAME.tar.gz;)
177fi
178
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700179# Back to devstack
180cd $TOP_DIR
181
182# Unmount the filesystems
183unmount_images
184
185# Network configuration variables
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700186GUEST_NETWORK=${GUEST_NETWORK:-1}
Jesse Andrews1d1dda12011-11-01 19:46:17 -0700187GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700188
189GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
190GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
191GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
192GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
193GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
194GUEST_RAM=${GUEST_RAM:-1524288}
195GUEST_CORES=${GUEST_CORES:-1}
196
197# libvirt.xml configuration
198NET_XML=$VM_DIR/net.xml
199cat > $NET_XML <<EOF
200<network>
201 <name>devstack-$GUEST_NETWORK</name>
202 <bridge name="stackbr%d" />
203 <forward/>
204 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" />
205</network>
206EOF
207
Jesse Andrews1d1dda12011-11-01 19:46:17 -0700208if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
209 virsh net-destroy devstack-$GUEST_NETWORK || true
210 virsh net-create $VM_DIR/net.xml
211fi
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700212
213# libvirt.xml configuration
214LIBVIRT_XML=$VM_DIR/libvirt.xml
215cat > $LIBVIRT_XML <<EOF
216<domain type='kvm'>
217 <name>$GUEST_NAME</name>
218 <memory>$GUEST_RAM</memory>
219 <os>
220 <type>hvm</type>
221 <bootmenu enable='yes'/>
222 </os>
223 <features>
224 <acpi/>
225 </features>
226 <vcpu>$GUEST_CORES</vcpu>
227 <devices>
228 <disk type='file'>
229 <driver type='qcow2'/>
230 <source file='$VM_DIR/disk'/>
231 <target dev='vda' bus='virtio'/>
232 </disk>
233
234 <interface type='network'>
235 <source network='devstack-$GUEST_NETWORK'/>
236 </interface>
237
238 <!-- The order is significant here. File must be defined first -->
239 <serial type="file">
240 <source path='$VM_DIR/console.log'/>
241 <target port='1'/>
242 </serial>
243
244 <console type='pty' tty='/dev/pts/2'>
245 <source path='/dev/pts/2'/>
246 <target port='0'/>
247 </console>
248
249 <serial type='pty'>
250 <source path='/dev/pts/2'/>
251 <target port='0'/>
252 </serial>
253
254 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
255 </devices>
256</domain>
257EOF
258
259# Mount point for instance fs
260ROOTFS=$VM_DIR/root
261mkdir -p $ROOTFS
262
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700263# Clean up from previous runs
264umount $ROOTFS || echo 'ok'
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700265
266# Clean up old runs
267cd $VM_DIR
268rm -f $VM_DIR/disk
269
270# Create our instance fs
271qemu-img create -f qcow2 -b $VM_IMAGE disk
272
Dean Troyerdccd6b92011-11-01 15:46:14 -0500273# Finds the next available NBD device
274# Exits script if error connecting or none free
275# map_nbd image
276# returns full nbd device path
277function map_nbd {
278 for i in `seq 0 15`; do
279 if [ ! -e /sys/block/nbd$i/pid ]; then
280 NBD=/dev/nbd$i
281 # Connect to nbd and wait till it is ready
282 qemu-nbd -c $NBD $1
283 if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
284 echo "Couldn't connect $NBD"
285 exit 1
286 fi
287 break
288 fi
289 done
290 if [ -z "$NBD" ]; then
291 echo "No free NBD slots"
292 exit 1
293 fi
294 echo $NBD
295}
296
Dean Troyer7a569732011-10-31 17:34:29 -0500297# Make sure we have nbd-ness
298modprobe nbd max_part=63
299
300# Set up nbd
Dean Troyerdccd6b92011-11-01 15:46:14 -0500301NBD=`map_nbd disk`
Dean Troyer7a569732011-10-31 17:34:29 -0500302NBD_DEV=`basename $NBD`
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700303
304# Mount the instance
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700305mount ${NBD}p1 $ROOTFS
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700306
307# Configure instance network
308INTERFACES=$ROOTFS/etc/network/interfaces
309cat > $INTERFACES <<EOF
310auto lo
311iface lo inet loopback
312
313auto eth0
314iface eth0 inet static
315 address $GUEST_IP
316 netmask $GUEST_NETMASK
317 gateway $GUEST_GATEWAY
318EOF
319
320# User configuration for the instance
321chroot $ROOTFS groupadd libvirtd || true
322chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrews5f894cd2011-10-30 19:52:50 -0700323cp -pr $TOP_DIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700324echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
325echo "stack:pass" | chroot $ROOTFS chpasswd
326echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
327
328# Gracefully cp only if source file/dir exists
329function cp_it {
330 if [ -e $1 ] || [ -d $1 ]; then
331 cp -pRL $1 $2
332 fi
333}
334
335# Copy over your ssh keys and env if desired
336COPYENV=${COPYENV:-1}
337if [ "$COPYENV" = "1" ]; then
338 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
339 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
340 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
341 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
342 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
343fi
344
345# pre-cache uec images
346for image_url in ${IMAGE_URLS//,/ }; do
347 IMAGE_FNAME=`basename "$image_url"`
348 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
349 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
350 fi
351 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
352done
353
354# Configure the runner
355RUN_SH=$ROOTFS/$DEST/run.sh
356cat > $RUN_SH <<EOF
357#!/usr/bin/env bash
358
359# Kill any existing screens
360killall screen
361
362# Install and run stack.sh
363sudo apt-get update
364sudo apt-get -y --force-yes install git-core vim-nox sudo
365if [ ! -d "$DEST/devstack" ]; then
366 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
367fi
368cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
369echo >> /$DEST/run.sh.log
370echo >> /$DEST/run.sh.log
371echo "All done! Time to start clicking." >> /$DEST/run.sh.log
372cat $DEST/run.sh.log
373EOF
374chmod 755 $RUN_SH
375
376# Make runner launch on boot
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700377RC_LOCAL=$ROOTFS/etc/init.d/zlocal
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700378cat > $RC_LOCAL <<EOF
379#!/bin/sh -e
Jesse Andrewsddcc36d2011-10-30 22:41:23 -0700380# cloud-init overwrites the hostname with ubuntuhost
381echo $GUEST_NAME > /etc/hostname
382hostname $GUEST_NAME
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700383su -c "$DEST/run.sh" stack
384EOF
385chmod +x $RC_LOCAL
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700386chroot $ROOTFS sudo update-rc.d zlocal defaults 99
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700387
388# Make our ip address hostnames look nice at the command prompt
389echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
390echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
391
392# Give stack ownership over $DEST so it may do the work needed
393chroot $ROOTFS chown -R stack $DEST
394
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700395# Set the hostname
396echo $GUEST_NAME > $ROOTFS/etc/hostname
397
398# We need the hostname to resolve for rabbit to launch
399if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
400 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
401fi
402
Dean Troyer6fe687b2011-10-31 20:30:04 -0500403# GRUB 2 wants to see /dev
404mount -o bind /dev $ROOTFS/dev
405
Jesse Andrews2b7d2212011-10-30 19:37:56 -0700406# Change boot params so that we get a console log
407G_DEV_UUID=`blkid -t LABEL=cloudimg-rootfs -s UUID -o value | head -1`
408sed -e "s/GRUB_TIMEOUT=.*$/GRUB_TIMEOUT=3/" -i $ROOTFS/etc/default/grub
Jesse Andrews2c5201b2011-10-30 20:44:26 -0700409sed -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 -0700410sed -e 's/[#]*GRUB_TERMINAL=.*$/GRUB_TERMINAL="serial console"/' -i $ROOTFS/etc/default/grub
411echo 'GRUB_SERIAL_COMMAND="serial --unit=0"' >>$ROOTFS/etc/default/grub
412echo 'GRUB_DISABLE_OS_PROBER=true' >>$ROOTFS/etc/default/grub
413echo "GRUB_DEVICE_UUID=$G_DEV_UUID" >>$ROOTFS/etc/default/grub
414
415chroot $ROOTFS update-grub
416umount $ROOTFS/dev
417
418# Pre-generate ssh host keys and allow password login
419chroot $ROOTFS dpkg-reconfigure openssh-server
420sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc/ssh/sshd_config
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