blob: ade5115acb2efd2b8c54a393433d09a2cb7347e4 [file] [log] [blame]
Anthony Young1b7a42e2011-10-19 02:34:06 -07001#!/usr/bin/env bash
2
3# Echo commands
4set -o xtrace
5
6# Keep track of the current directory
7TOOLS_DIR=$(cd $(dirname "$0") && pwd)
8
9ROOT_PASSWORD=${ROOT_PASSWORD:password}
10PERSIST_DIR=${PERSIST_DIR:-/opt/kvmstack}
11IMAGES_DIR=$PERSIST_DIR/images
12mkdir -p $UEC_DIR
13
14# Move to top devstack dir
15cd ..
16
17# Abort if localrc is not set
18if [ ! -e ./localrc ]; then
19 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
20 echo "See stack.sh for required passwords."
21 exit 1
22fi
23
24# Source params
25source ./stackrc
26
27# Base image (oneiric by default)
28IMAGE_FNAME=natty.raw
29IMAGE_NAME=natty
30
31BASE_IMAGE=$PERSIST_DIR/images/natty.raw
32BASE_IMAGE_COPY=$IMAGES_DIR/$IMAGE_NAME.raw.copy
33
34VM_NAME=${VM_NAME:-kvmstack}
35virsh shutdown $VM_NAME
36virsh destroy $VM_NAME
37
38VM_DIR=$PERSIST_DIR/instances/$VM_NAME
39
40mkdir -p $VM_DIR
41
42# Where to mount
43COPY_DIR=$VM_DIR/copy
44mkdir -p $COPY_DIR
45
46
47if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
48 cd $TOOLS_DIR
49 ./make_image.sh -m -r 5000 natty raw
50 mv natty.raw $BASE_IMAGE
51 cd $TOP_DIR
52fi
53
54function unmount_images() {
55 # unmount the filesystem
56 while df | grep -q $COPY_DIR; do
57 umount $COPY_DIR || echo 'ok'
58 sleep 1
59 done
60}
61
62# unmount from failed runs
63unmount_images
64
65function kill_tail() {
66 unmount_images
67 exit 1
68}
69
70if [ ! -e $BASE_IMAGE_COPY ]; then
71 cp -p $BASE_IMAGE $BASE_IMAGE_COPY
72fi
73
74# Install deps
75apt-get install -y kvm libvirt-bin kpartx
76
77# Let Ctrl-c kill tail and exit
78trap kill_tail SIGINT
79
80# Where code will live in image
81DEST=${DEST:-/opt/stack}
82
83# Mount the file system
84mount -o loop,offset=32256 $BASE_IMAGE_COPY $COPY_DIR
85
86# git clone only if directory doesn't exist already. Since ``DEST`` might not
87# be owned by the installation user, we create the directory and change the
88# ownership to the proper user.
89function git_clone {
90 if [ ! -d $2 ]; then
91 sudo mkdir $2
92 sudo chown `whoami` $2
93 git clone $1 $2
94 cd $2
95 # This checkout syntax works for both branches and tags
96 git checkout $3
97 fi
98}
99
100# Make sure that base requirements are installed
101cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf
102chroot $COPY_DIR apt-get update
103chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
104chroot $COPY_DIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
105chroot $COPY_DIR pip install `cat files/pips/*`
106
107# Clean out code repos if directed to do so
108if [ "$CLEAN" = "1" ]; then
109 rm -rf $COPY_DIR/$DEST
110fi
111
112# Cache openstack code
113mkdir -p $COPY_DIR/$DEST
114git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
115git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
116git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
117git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
118git_clone $DASH_REPO $COPY_DIR/$DEST/dash $DASH_BRANCH $DASH_TAG
119git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
120git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
121git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
122git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
123
124# unmount the filesystems
125unmount_images
126
127rm -f $VM_DIR/kernel
128rm -f $VM_DIR/disk
129
130cd $VM_DIR
131qemu-img create -f qcow2 -b $BASE_IMAGE_COPY disk
132
133BRIDGE=${BRIDGE:-br0}
134CONTAINER=${CONTAINER:-STACK}
135CONTAINER_IP=${CONTAINER_IP:-192.168.1.50}
136CONTAINER_CIDR=${CONTAINER_CIDR:-$CONTAINER_IP/24}
137CONTAINER_NETMASK=${CONTAINER_NETMASK:-255.255.255.0}
138CONTAINER_GATEWAY=${CONTAINER_GATEWAY:-192.168.1.1}
139CONTAINER_MAC=${CONTAINER_MAC:-02:16:3e:07:70:d7}
140
141# Create configuration
142LIBVIRT_XML=libvirt.xml
143cat > $LIBVIRT_XML <<EOF
144<domain type='kvm'>
145 <name>$VM_NAME</name>
146 <memory>1524288</memory>
147 <os>
148 <type>hvm</type>
149<!--
150 <kernel>$VM_DIR/kernel</kernel>
151 <cmdline>root=/dev/vda console=ttyS0</cmdline>
152-->
153 </os>
154 <features>
155 <acpi/>
156 </features>
157 <vcpu>1</vcpu>
158 <devices>
159 <disk type='file'>
160 <driver type='qcow2'/>
161 <source file='$VM_DIR/disk'/>
162 <target dev='vda' bus='virtio'/>
163 </disk>
164
165 <interface type='bridge'>
166 <source bridge='$BRIDGE'/>
167 <mac address='$CONTAINER_MAC'/>
168 </interface>
169
170 <!-- The order is significant here. File must be defined first -->
171 <serial type="file">
172 <source path='$VM_DIR/console.log'/>
173 <target port='1'/>
174 </serial>
175
176 <console type='pty' tty='/dev/pts/2'>
177 <source path='/dev/pts/2'/>
178 <target port='0'/>
179 </console>
180
181 <serial type='pty'>
182 <source path='/dev/pts/2'/>
183 <target port='0'/>
184 </serial>
185
186 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
187 </devices>
188</domain>
189EOF
190
191ROOTFS=$VM_DIR/root
192mkdir -p $ROOTFS
193
194modprobe nbd max_part=63
195
196umount $ROOTFS || echo 'ok'
197qemu-nbd -d /dev/nbd5 || echo 'ok'
198
199qemu-nbd -c /dev/nbd5 disk
200mount /dev/nbd5 $ROOTFS -o offset=32256 -t ext4
201
202# Configure instance network
203INTERFACES=$ROOTFS/etc/network/interfaces
204cat > $INTERFACES <<EOF
205auto lo
206iface lo inet loopback
207
208auto eth0
209iface eth0 inet static
210 address $CONTAINER_IP
211 netmask $CONTAINER_NETMASK
212 gateway $CONTAINER_GATEWAY
213EOF
214
215chroot $ROOTFS groupadd libvirtd
216chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
217cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
218echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
219
220# a simple password - pass
221echo "stack:pass" | chroot $ROOTFS chpasswd
222
223# stack requires)
224echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
225
226# Gracefully cp only if source file/dir exists
227function cp_it {
228 if [ -e $1 ] || [ -d $1 ]; then
229 cp -pRL $1 $2
230 fi
231}
232
233# Copy over your ssh keys and env if desired
234COPYENV=${COPYENV:-1}
235if [ "$COPYENV" = "1" ]; then
236 cp_it ~/.ssh $ROOTFS/$DEST/.ssh
237 cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
238 cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
239 cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
240 cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
241fi
242
243# Configure the runner
244RUN_SH=$ROOTFS/$DEST/run.sh
245cat > $RUN_SH <<EOF
246#!/usr/bin/env bash
247sleep 1
248
249# Kill any existing screens
250killall screen
251
252# Install and run stack.sh
253sudo apt-get update
254sudo apt-get -y --force-yes install git-core vim-nox sudo
255if [ ! -d "$DEST/devstack" ]; then
256 git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
257fi
258cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
259echo >> /$DEST/run.sh.log
260echo >> /$DEST/run.sh.log
261echo "All done! Time to start clicking." >> /$DEST/run.sh.log
262EOF
263
264# Make the run.sh executable
265chmod 755 $RUN_SH
266
267# Make runner launch on boot
268RC_LOCAL=$ROOTFS/etc/init.d/local
269cat > $RC_LOCAL <<EOF
270#!/bin/sh -e
271su -c "$DEST/run.sh" stack
272EOF
273
274# Make our ip address hostnames look nice at the command prompt
275echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
276echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
277
278# Give stack ownership over $DEST so it may do the work needed
279chroot $ROOTFS chown -R stack $DEST
280
281chmod +x $RC_LOCAL
282chroot $ROOTFS sudo update-rc.d local defaults 80
283
284umount $ROOTFS
285qemu-nbd -d /dev/nbd5
286
287cd $VM_DIR
288virsh create libvirt.xml