blob: 35a4d6dbbad79d810818a124484bd4e77d5e481e [file] [log] [blame]
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07001#!/usr/bin/env bash
2
Anthony Younge28f7752011-11-09 11:48:09 -08003# Make sure that we have the proper version of ubuntu (only works on oneiric)
4if ! egrep -q "oneiric" /etc/lsb-release; then
Anthony Young593e9aa2011-11-09 12:42:08 -08005 echo "This script only works with ubuntu oneiric."
Jesse Andrewse3c47a32011-11-07 10:44:43 -08006 exit 1
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07007fi
8
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -07009# Keep track of the current directory
10TOOLS_DIR=$(cd $(dirname "$0") && pwd)
Dean Troyer7f9aa712012-01-31 12:11:56 -060011TOP_DIR=$(cd $TOOLS_DIR/..; pwd)
12
13# Import common functions
14. $TOP_DIR/functions
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070015
Jesse Andrews53d75332011-11-06 07:54:11 -080016cd $TOP_DIR
17
18# Source params
19source ./stackrc
20
21# Ubuntu distro to install
22DIST_NAME=${DIST_NAME:-oneiric}
23
Jesse Andrewse3c47a32011-11-07 10:44:43 -080024# Configure how large the VM should be
25GUEST_SIZE=${GUEST_SIZE:-10G}
26
Jesse Andrews228f2462011-11-05 17:36:14 -070027# exit on error to stop unexpected errors
28set -o errexit
29set -o xtrace
30
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070031# 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
38# Install deps if needed
Jesse Andrews26793032011-11-11 13:56:29 -080039DEPS="kvm libvirt-bin kpartx cloud-utils curl"
Dean Troyer7f9aa712012-01-31 12:11:56 -060040apt_get install -y --force-yes $DEPS || true # allow this to fail gracefully for concurrent builds
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070041
42# Where to store files and instances
Jesse Andrewsd7326d22011-11-20 10:02:26 -080043WORK_DIR=${WORK_DIR:-/opt/uecstack}
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070044
45# Where to store images
Jesse Andrews228f2462011-11-05 17:36:14 -070046image_dir=$WORK_DIR/images/$DIST_NAME
47mkdir -p $image_dir
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070048
Jesse Andrewsd7326d22011-11-20 10:02:26 -080049# Start over with a clean base image, if desired
50if [ $CLEAN_BASE ]; then
51 rm -f $image_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070052fi
53
Jesse Andrewsd7326d22011-11-20 10:02:26 -080054# Get the base image if it does not yet exist
55if [ ! -e $image_dir/disk ]; then
56 $TOOLS_DIR/get_uec_image.sh -r $GUEST_SIZE $DIST_NAME $image_dir/disk $image_dir/kernel
57fi
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070058
Jesse Andrewsd7326d22011-11-20 10:02:26 -080059# Copy over dev environment if COPY_ENV is set.
60# This will also copy over your current devstack.
61if [ $COPY_ENV ]; then
62 cd $TOOLS_DIR
63 ./copy_dev_environment_to_uec.sh $image_dir/disk
64fi
65
66# Option to warm the base image with software requirements.
67if [ $WARM_CACHE ]; then
68 cd $TOOLS_DIR
69 ./warm_apts_and_pips_for_uec.sh $image_dir/disk
70fi
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070071
72# Name of our instance, used by libvirt
73GUEST_NAME=${GUEST_NAME:-devstack}
74
75# Mop up after previous runs
76virsh destroy $GUEST_NAME || true
77
78# Where this vm is stored
Jesse Andrews228f2462011-11-05 17:36:14 -070079vm_dir=$WORK_DIR/instances/$GUEST_NAME
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070080
81# Create vm dir and remove old disk
Jesse Andrews228f2462011-11-05 17:36:14 -070082mkdir -p $vm_dir
83rm -f $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070084
85# Create a copy of the base image
Jesse Andrewsf5a76912011-11-05 17:47:50 -070086qemu-img create -f qcow2 -b $image_dir/disk $vm_dir/disk
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -070087
88# Back to devstack
89cd $TOP_DIR
90
91GUEST_NETWORK=${GUEST_NETWORK:-1}
92GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
93GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
94GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
95GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
96GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.$GUEST_NETWORK.1}
97GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $GUEST_NETWORK`"}
98GUEST_RAM=${GUEST_RAM:-1524288}
99GUEST_CORES=${GUEST_CORES:-1}
100
101# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -0700102NET_XML=$vm_dir/net.xml
Anthony Young8b47cdf2011-11-09 23:36:18 -0800103NET_NAME=${NET_NAME:-devstack-$GUEST_NETWORK}
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700104cat > $NET_XML <<EOF
105<network>
Anthony Young8b47cdf2011-11-09 23:36:18 -0800106 <name>$NET_NAME</name>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700107 <bridge name="stackbr%d" />
108 <forward/>
Jesse Andrewsa6282622011-11-05 18:39:33 -0700109 <ip address="$GUEST_GATEWAY" netmask="$GUEST_NETMASK">
110 <dhcp>
Jesse Andrews02cc96c2011-11-06 10:29:10 -0800111 <range start='192.168.$GUEST_NETWORK.2' end='192.168.$GUEST_NETWORK.127' />
Jesse Andrewsa6282622011-11-05 18:39:33 -0700112 </dhcp>
113 </ip>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700114</network>
115EOF
116
117if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
Anthony Young8b47cdf2011-11-09 23:36:18 -0800118 virsh net-destroy $NET_NAME || true
Jesse Andrewsdca89002011-11-06 10:33:33 -0800119 # destroying the network isn't enough to delete the leases
Anthony Young8b47cdf2011-11-09 23:36:18 -0800120 rm -f /var/lib/libvirt/dnsmasq/$NET_NAME.leases
Jesse Andrews228f2462011-11-05 17:36:14 -0700121 virsh net-create $vm_dir/net.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700122fi
123
124# libvirt.xml configuration
Jesse Andrews228f2462011-11-05 17:36:14 -0700125LIBVIRT_XML=$vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700126cat > $LIBVIRT_XML <<EOF
127<domain type='kvm'>
128 <name>$GUEST_NAME</name>
129 <memory>$GUEST_RAM</memory>
130 <os>
Jesse Andrews228f2462011-11-05 17:36:14 -0700131 <type>hvm</type>
Jesse Andrewsf5a76912011-11-05 17:47:50 -0700132 <kernel>$image_dir/kernel</kernel>
Jesse Andrews438ea572011-11-05 22:33:49 -0700133 <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 -0700134 </os>
135 <features>
136 <acpi/>
137 </features>
138 <clock offset='utc'/>
139 <vcpu>$GUEST_CORES</vcpu>
140 <devices>
141 <disk type='file'>
142 <driver type='qcow2'/>
Jesse Andrews228f2462011-11-05 17:36:14 -0700143 <source file='$vm_dir/disk'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700144 <target dev='vda' bus='virtio'/>
145 </disk>
146
147 <interface type='network'>
Anthony Young72eab222011-11-09 23:38:18 -0800148 <source network='$NET_NAME'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700149 </interface>
Hengqing Hu3b719e52012-03-09 16:03:00 +0800150
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700151 <!-- The order is significant here. File must be defined first -->
152 <serial type="file">
Jesse Andrews228f2462011-11-05 17:36:14 -0700153 <source path='$vm_dir/console.log'/>
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700154 <target port='1'/>
155 </serial>
156
157 <console type='pty' tty='/dev/pts/2'>
158 <source path='/dev/pts/2'/>
159 <target port='0'/>
160 </console>
161
162 <serial type='pty'>
163 <source path='/dev/pts/2'/>
164 <target port='0'/>
165 </serial>
166
167 <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
168 </devices>
169</domain>
170EOF
171
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700172
Jesse Andrewse49f7512011-11-05 22:34:45 -0700173rm -rf $vm_dir/uec
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700174cp -r $TOOLS_DIR/uec $vm_dir/uec
175
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700176# set metadata
177cat > $vm_dir/uec/meta-data<<EOF
178hostname: $GUEST_NAME
Jesse Andrewse3c47a32011-11-07 10:44:43 -0800179instance-id: i-hop
180instance-type: m1.ignore
Jesse Andrews7306f3b2011-11-05 23:13:34 -0700181local-hostname: $GUEST_NAME.local
Jesse Andrewsd7ce7af2011-11-05 22:47:28 -0700182EOF
183
Anthony Young2838f122011-11-10 13:04:40 -0800184# set user-data
Jesse Andrews63cb9232011-11-05 23:16:53 -0700185cat > $vm_dir/uec/user-data<<EOF
Jesse Andrews446a3302011-11-05 23:36:29 -0700186#!/bin/bash
Jesse Andrewsb17c4f32011-11-06 09:25:55 -0800187# hostname needs to resolve for rabbit
Jesse Andrews6e3a4c52011-11-06 09:35:13 -0800188sed -i "s/127.0.0.1/127.0.0.1 \`hostname\`/" /etc/hosts
Jesse Andrews446a3302011-11-05 23:36:29 -0700189apt-get update
Jesse Andrewsc7f72ad2011-11-06 08:00:28 -0800190apt-get install git sudo -y
Anthony Young760ddde2011-11-10 13:46:52 -0800191# Disable byobu
Jesse Andrewsd7326d22011-11-20 10:02:26 -0800192sudo apt-get remove -y byobu
Anthony Youngb2256822011-11-10 12:57:59 -0800193EOF
194
195# Setup stack user with our key
Jesse Andrewsd7326d22011-11-20 10:02:26 -0800196if [[ -e ~/.ssh/id_rsa.pub ]]; then
Anthony Youngb7661282011-11-10 13:09:25 -0800197 PUB_KEY=`cat ~/.ssh/id_rsa.pub`
Anthony Young2838f122011-11-10 13:04:40 -0800198 cat >> $vm_dir/uec/user-data<<EOF
Anthony Youngb2256822011-11-10 12:57:59 -0800199mkdir -p /opt/stack
Anthony Young331ae292011-12-21 11:55:35 -0800200if [ ! -d /opt/stack/devstack ]; then
201 git clone https://github.com/cloudbuilders/devstack.git /opt/stack/devstack
202 cd /opt/stack/devstack
203 cat > localrc <<LOCAL_EOF
204ROOTSLEEP=0
205`cat $TOP_DIR/localrc`
206LOCAL_EOF
207fi
Anthony Youngff7771e2011-11-10 13:33:31 -0800208useradd -U -G sudo -s /bin/bash -d /opt/stack -m stack
Anthony Youngb2256822011-11-10 12:57:59 -0800209echo stack:pass | chpasswd
210mkdir -p /opt/stack/.ssh
Anthony Youngb7661282011-11-10 13:09:25 -0800211echo "$PUB_KEY" > /opt/stack/.ssh/authorized_keys
Anthony Youngb2256822011-11-10 12:57:59 -0800212chown -R stack /opt/stack
213chmod 700 /opt/stack/.ssh
214chmod 600 /opt/stack/.ssh/authorized_keys
215
216grep -q "^#includedir.*/etc/sudoers.d" /etc/sudoers ||
217 echo "#includedir /etc/sudoers.d" >> /etc/sudoers
218( umask 226 && echo "stack ALL=(ALL) NOPASSWD:ALL" \
219 > /etc/sudoers.d/50_stack_sh )
220EOF
221fi
222
223# Run stack.sh
Anthony Young2838f122011-11-10 13:04:40 -0800224cat >> $vm_dir/uec/user-data<<EOF
Anthony Young331ae292011-12-21 11:55:35 -0800225su -c "cd /opt/stack/devstack && ./stack.sh" stack
Jesse Andrews63cb9232011-11-05 23:16:53 -0700226EOF
227
Jesse Andrewsee34f622011-11-05 22:41:57 -0700228# (re)start a metadata service
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700229(
230 pid=`lsof -iTCP@192.168.$GUEST_NETWORK.1:4567 -n | awk '{print $2}' | tail -1`
Jesse Andrews9645b0c2011-11-05 23:05:33 -0700231 [ -z "$pid" ] || kill -9 $pid
Jesse Andrews3ce79aa2011-11-05 22:52:20 -0700232)
Jesse Andrewsf504e282011-11-05 22:29:35 -0700233cd $vm_dir/uec
234python meta.py 192.168.$GUEST_NETWORK.1:4567 &
Jesse Andrews9ed6bbd2011-11-05 22:28:46 -0700235
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700236# Create the instance
Jesse Andrews63fa7ab2011-11-05 18:49:36 -0700237virsh create $vm_dir/libvirt.xml
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700238
239# Tail the console log till we are done
240WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
241if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
Jesse Andrews228f2462011-11-05 17:36:14 -0700242 set +o xtrace
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700243 # Done creating the container, let's tail the log
244 echo
245 echo "============================================================="
246 echo " -- YAY! --"
247 echo "============================================================="
248 echo
249 echo "We're done launching the vm, about to start tailing the"
250 echo "stack.sh log. It will take a second or two to start."
251 echo
252 echo "Just CTRL-C at any time to stop tailing."
Jesse Andrewsd7326d22011-11-20 10:02:26 -0800253 echo
254
255 if ! timeout 60 sh -c "while [ ! -s /var/lib/libvirt/dnsmasq/$NET_NAME.leases ]; do sleep 1; done"; then
256 echo "Your instance failed to acquire an IP address"
257 exit 1
258 fi
259
260 ip=`cat /var/lib/libvirt/dnsmasq/$NET_NAME.leases | cut -d " " -f3`
261 echo "#############################################################"
262 echo " -- This is your instance's IP: --"
263 echo " $ip"
264 echo "#############################################################"
265
266 sleep 2
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700267
Jesse Andrews228f2462011-11-05 17:36:14 -0700268 while [ ! -e "$vm_dir/console.log" ]; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700269 sleep 1
270 done
271
Jesse Andrews228f2462011-11-05 17:36:14 -0700272 tail -F $vm_dir/console.log &
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700273
274 TAIL_PID=$!
275
276 function kill_tail() {
277 kill $TAIL_PID
278 exit 1
279 }
280
281 # Let Ctrl-c kill tail and exit
282 trap kill_tail SIGINT
283
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700284 echo "Waiting stack.sh to finish..."
Jesse Andrewsd55a5152011-11-06 08:16:42 -0800285 while ! egrep -q '^stack.sh (completed|failed)' $vm_dir/console.log ; do
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700286 sleep 1
287 done
288
289 set -o xtrace
290
291 kill $TAIL_PID
292
Jesse Andrews228f2462011-11-05 17:36:14 -0700293 if ! grep -q "^stack.sh completed in" $vm_dir/console.log; then
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700294 exit 1
295 fi
Jesse Andrewsd7326d22011-11-20 10:02:26 -0800296
297 set +o xtrace
Jesse Andrews8b3eb5f2011-11-05 16:05:14 -0700298 echo ""
299 echo "Finished - Zip-a-dee Doo-dah!"
300fi