blob: 07fd10242546976f4e8b5fdd3bc6c9841eb3f629 [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
15# Echo commands
16set -o xtrace
17
18# Keep track of the current directory
19TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Jesse Andrews31989742011-10-30 18:56:05 -070020TOP_DIR=`cd $TOOLS_DIR/..; pwd`
Jesse Andrewse97a2e72011-10-30 18:37:49 -070021
22# Where to store files and instances
23WORK_DIR=${WORK_DIR:-$TOP_DIR/work}
24
25# Where to store images
26IMAGES_DIR=$WORK_DIR/images
27
28# Create images dir
29mkdir -p $IMAGES_DIR
30
31# Abort if localrc is not set
32if [ ! -e $TOP_DIR/localrc ]; then
33 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
34 echo "See stack.sh for required passwords."
35 exit 1
36fi
37
Jesse Andrews7fa56132011-10-30 18:43:54 -070038cd $TOP_DIR
39
Jesse Andrewse97a2e72011-10-30 18:37:49 -070040# Source params
41source ./stackrc
42
43# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
44ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
45
Jesse Andrewse97a2e72011-10-30 18:37:49 -070046# Base image (natty by default)
47DIST_NAME=${DIST_NAME:-natty}
48IMAGE_FNAME=$DIST_NAME.raw
49
50# Name of our instance, used by libvirt
51GUEST_NAME=${GUEST_NAME:-devstack}
52
53# Original version of built image
54BASE_IMAGE=$WORK_DIR/images/$DIST_NAME.raw
55
56# Copy of base image, which we pre-install with tasty treats
57VM_IMAGE=$IMAGES_DIR/$DIST_NAME.$GUEST_NAME.raw
58
59# Mop up after previous runs
60virsh destroy $GUEST_NAME || true
61
62# Where this vm is stored
63VM_DIR=$WORK_DIR/instances/$GUEST_NAME
64
65# Create vm dir
66mkdir -p $VM_DIR
67
68# Mount point into copied base image
69COPY_DIR=$VM_DIR/copy
70mkdir -p $COPY_DIR
71
72# Create the base image if it does not yet exist
73if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
74 cd $TOOLS_DIR
75 ./make_image.sh -m -r 5000 $DIST_NAME raw
76 mv $DIST_NAME.raw $BASE_IMAGE
77 cd $TOP_DIR
78fi
79
80# Create a copy of the base image
81if [ ! -e $VM_IMAGE ]; then
82 cp -p $BASE_IMAGE $VM_IMAGE
83fi
84
85# Unmount the copied base image
86function unmount_images() {
87 # unmount the filesystem
88 while df | grep -q $COPY_DIR; do
89 umount $COPY_DIR || echo 'ok'
90 sleep 1
91 done
92}
93
94# Unmount from failed runs
95unmount_images
96
97# Ctrl-c catcher
98function kill_unmount() {
99 unmount_images
100 exit 1
101}
102
103# Install deps if needed
104dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
105
106# Let Ctrl-c kill tail and exit
107trap kill_unmount SIGINT
108
109# Where Openstack code will live in image
110DEST=${DEST:-/opt/stack}
111
112# Mount the file system
113mount -o loop,offset=32256 $VM_IMAGE $COPY_DIR
114
115# git clone only if directory doesn't exist already. Since ``DEST`` might not
116# be owned by the installation user, we create the directory and change the
117# ownership to the proper user.
118function git_clone {
119 if [ ! -d $2 ]; then
120 sudo mkdir $2
121 sudo chown `whoami` $2
122 git clone $1 $2
123 cd $2
124 # This checkout syntax works for both branches and tags
125 git checkout $3
126 fi
127}
128
129# Make sure that base requirements are installed
130cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf
131chroot $COPY_DIR apt-get update
132chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
133chroot $COPY_DIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
134chroot $COPY_DIR pip install `cat files/pips/*`
135
136# Clean out code repos if directed to do so
137if [ "$CLEAN" = "1" ]; then
138 rm -rf $COPY_DIR/$DEST
139fi
140
141# Cache openstack code
142mkdir -p $COPY_DIR/$DEST
143git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
144git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
145git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
146git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
147git_clone $HORIZON_REPO $COPY_DIR/$DEST/horizon $HORIZON_BRANCH $HORIZON_TAG
148git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
149git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
150git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
151git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
152
153# Back to devstack
154cd $TOP_DIR
155
156# Unmount the filesystems
157unmount_images
158
159# Network configuration variables
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700160GUEST_NETWORK=${GUEST_NETWORK:-1}
161
162GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
163GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
164GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
165GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
166GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
167GUEST_RAM=${GUEST_RAM:-1524288}
168GUEST_CORES=${GUEST_CORES:-1}
169
170# libvirt.xml configuration
171NET_XML=$VM_DIR/net.xml
172cat > $NET_XML <<EOF
173<network>
174 <name>devstack-$GUEST_NETWORK</name>
175 <bridge name="stackbr%d" />
176 <forward/>
177 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK" />
178</network>
179EOF
180
181virsh net-destroy devstack-$GUEST_NETWORK || true
182virsh net-create $VM_DIR/net.xml
183
184# libvirt.xml configuration
185LIBVIRT_XML=$VM_DIR/libvirt.xml
186cat > $LIBVIRT_XML <<EOF
187<domain type='kvm'>
188 <name>$GUEST_NAME</name>
189 <memory>$GUEST_RAM</memory>
190 <os>
191 <type>hvm</type>
192 <bootmenu enable='yes'/>
193 </os>
194 <features>
195 <acpi/>
196 </features>
197 <vcpu>$GUEST_CORES</vcpu>
198 <devices>
199 <disk type='file'>
200 <driver type='qcow2'/>
201 <source file='$VM_DIR/disk'/>
202 <target dev='vda' bus='virtio'/>
203 </disk>
204
205 <interface type='network'>
206 <source network='devstack-$GUEST_NETWORK'/>
207 </interface>
208
209 <!-- The order is significant here. File must be defined first -->
210 <serial type="file">
211 <source path='$VM_DIR/console.log'/>
212 <target port='1'/>
213 </serial>
214
215 <console type='pty' tty='/dev/pts/2'>
216 <source path='/dev/pts/2'/>
217 <target port='0'/>
218 </console>
219
220 <serial type='pty'>
221 <source path='/dev/pts/2'/>
222 <target port='0'/>
223 </serial>
224
225 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
226 </devices>
227</domain>
228EOF
229
230# Mount point for instance fs
231ROOTFS=$VM_DIR/root
232mkdir -p $ROOTFS
233
234# Make sure we have nbd-ness
235modprobe nbd max_part=63
236
237# Which NBD device to use?
238NBD=${NBD:-/dev/nbd5}
239
240# Clean up from previous runs
241umount $ROOTFS || echo 'ok'
242qemu-nbd -d $NBD || echo 'ok'
243
244# Clean up old runs
245cd $VM_DIR
246rm -f $VM_DIR/disk
247
248# Create our instance fs
249qemu-img create -f qcow2 -b $VM_IMAGE disk
250
251# Connect our nbd and wait till it is mountable
252qemu-nbd -c $NBD disk
253NBD_DEV=`basename $NBD`
254if ! timeout 60 sh -c "while ! [ -e /sys/block/$NBD_DEV/pid ]; do sleep 1; done"; then
255 echo "Couldn't connect $NBD"
256 exit 1
257fi
258
259# Mount the instance
260mount $NBD $ROOTFS -o offset=32256 -t ext4
261
262# Configure instance network
263INTERFACES=$ROOTFS/etc/network/interfaces
264cat > $INTERFACES <<EOF
265auto lo
266iface lo inet loopback
267
268auto eth0
269iface eth0 inet static
270 address $GUEST_IP
271 netmask $GUEST_NETMASK
272 gateway $GUEST_GATEWAY
273EOF
274
275# User configuration for the instance
276chroot $ROOTFS groupadd libvirtd || true
277chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
Jesse Andrews19d62872011-10-30 19:26:28 -0700278rsync -av --exclude=work $TOP_DIR $ROOTFS/$DEST/devstack
Jesse Andrewse97a2e72011-10-30 18:37:49 -0700279echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
280echo "stack:pass" | chroot $ROOTFS chpasswd
281echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
282
283# Gracefully cp only if source file/dir exists
284function cp_it {
285 if [ -e $1 ] || [ -d $1 ]; then
286 cp -pRL $1 $2
287 fi
288}
289
290# Copy over your ssh keys and env if desired
291COPYENV=${COPYENV:-1}
292if [ "$COPYENV" = "1" ]; then
293 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
294 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
295 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
296 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
297 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
298fi
299
300# pre-cache uec images
301for image_url in ${IMAGE_URLS//,/ }; do
302 IMAGE_FNAME=`basename "$image_url"`
303 if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
304 wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
305 fi
306 cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
307done
308
309# Configure the runner
310RUN_SH=$ROOTFS/$DEST/run.sh
311cat > $RUN_SH <<EOF
312#!/usr/bin/env bash
313
314# Kill any existing screens
315killall screen
316
317# Install and run stack.sh
318sudo apt-get update
319sudo apt-get -y --force-yes install git-core vim-nox sudo
320if [ ! -d "$DEST/devstack" ]; then
321 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
322fi
323cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
324echo >> /$DEST/run.sh.log
325echo >> /$DEST/run.sh.log
326echo "All done! Time to start clicking." >> /$DEST/run.sh.log
327cat $DEST/run.sh.log
328EOF
329chmod 755 $RUN_SH
330
331# Make runner launch on boot
332RC_LOCAL=$ROOTFS/etc/init.d/local
333cat > $RC_LOCAL <<EOF
334#!/bin/sh -e
335# Reboot if this is our first run to enable console log on $DIST_NAME :(
336if [ ! -e /root/firstlaunch ]; then
337 touch /root/firstlaunch
338 reboot -f
339 exit 0
340fi
341su -c "$DEST/run.sh" stack
342EOF
343chmod +x $RC_LOCAL
344chroot $ROOTFS sudo update-rc.d local defaults 80
345
346# Make our ip address hostnames look nice at the command prompt
347echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
348echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
349
350# Give stack ownership over $DEST so it may do the work needed
351chroot $ROOTFS chown -R stack $DEST
352
353# Change boot params so that we get a console log
354sudo sed -e "s/quiet splash/splash console=ttyS0 console=ttyS1,19200n8/g" -i $ROOTFS/boot/grub/menu.lst
355sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst
356
357# Set the hostname
358echo $GUEST_NAME > $ROOTFS/etc/hostname
359
360# We need the hostname to resolve for rabbit to launch
361if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
362 echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
363fi
364
365# Unmount
366umount $ROOTFS || echo 'ok'
367qemu-nbd -d $NBD
368
369# Create the instance
370cd $VM_DIR && virsh create libvirt.xml
371
372# Tail the console log till we are done
373WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
374if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
375 # Done creating the container, let's tail the log
376 echo
377 echo "============================================================="
378 echo " -- YAY! --"
379 echo "============================================================="
380 echo
381 echo "We're done launching the vm, about to start tailing the"
382 echo "stack.sh log. It will take a second or two to start."
383 echo
384 echo "Just CTRL-C at any time to stop tailing."
385
386 while [ ! -e "$VM_DIR/console.log" ]; do
387 sleep 1
388 done
389
390 tail -F $VM_DIR/console.log &
391
392 TAIL_PID=$!
393
394 function kill_tail() {
395 kill $TAIL_PID
396 exit 1
397 }
398
399 # Let Ctrl-c kill tail and exit
400 trap kill_tail SIGINT
401
402 echo "Waiting stack.sh to finish..."
403 while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
404 sleep 5
405 done
406
407 kill $TAIL_PID
408
409 if grep -q "stack.sh failed" $VM_DIR/console.log; then
410 exit 1
411 fi
412 echo ""
413 echo "Finished - Zip-a-dee Doo-dah!"
414fi