blob: 19453c1233f46f61af38a1b2aa54178b9b859b3f [file] [log] [blame]
Anthony Youngb62b4ca2011-10-26 22:29:08 -07001#!/bin/bash
2
John Garbuttdaadf742012-04-27 18:28:28 +01003# This script is a level script
4# It must be run on a XenServer or XCP machine
5#
6# It creates a DomU VM that runs OpenStack services
7#
8# For more details see: README.md
9
Renuka Apte0af143b2012-04-02 15:46:53 -070010# Exit on errors
11set -o errexit
John Garbuttdaadf742012-04-27 18:28:28 +010012# Echo commands
13set -o xtrace
Renuka Apte0af143b2012-04-02 15:46:53 -070014
Anthony Youngb62b4ca2011-10-26 22:29:08 -070015# Abort if localrc is not set
16if [ ! -e ../../localrc ]; then
17 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
18 echo "See the xen README for required passwords."
19 exit 1
20fi
21
Anthony Youngaf6ed6b2011-11-02 07:50:27 -050022# This directory
23TOP_DIR=$(cd $(dirname "$0") && pwd)
24
Anthony Youngb3e2f332012-03-16 17:01:49 -070025# Source lower level functions
26. $TOP_DIR/../../functions
27
John Garbuttdaadf742012-04-27 18:28:28 +010028# Include onexit commands
29. $TOP_DIR/scripts/on_exit.sh
30
31
32#
33# Get Settings
34#
35
Anthony Youngb3e2f332012-03-16 17:01:49 -070036# Source params - override xenrc params in your localrc to suit your taste
Anthony Young11889042012-01-12 17:11:56 -080037source xenrc
Anthony Youngaf6ed6b2011-11-02 07:50:27 -050038
Renuka Apte0af143b2012-04-02 15:46:53 -070039xe_min()
40{
41 local cmd="$1"
42 shift
43 xe "$cmd" --minimal "$@"
44}
Anthony Youngb62b4ca2011-10-26 22:29:08 -070045
John Garbuttdaadf742012-04-27 18:28:28 +010046
47#
48# Prepare Dom0
49# including installing XenAPI plugins
50#
51
Renuka Apte0af143b2012-04-02 15:46:53 -070052cd $TOP_DIR
53if [ -f ./master ]
54then
55 rm -rf ./master
56 rm -rf ./nova
Anthony Youngb62b4ca2011-10-26 22:29:08 -070057fi
John Garbuttdaadf742012-04-27 18:28:28 +010058
59# get nova
John Garbutt7fc6dcd2012-07-03 12:25:21 +010060nova_zipball=$(echo $NOVA_REPO | sed "s:\.git$::;s:$:/zipball/$NOVA_BRANCH:g")
61wget $nova_zipball -O nova-zipball --no-check-certificate
62unzip -o nova-zipball -d ./nova
John Garbuttdaadf742012-04-27 18:28:28 +010063
64# install xapi plugins
65XAPI_PLUGIN_DIR=/etc/xapi.d/plugins/
66if [ ! -d $XAPI_PLUGIN_DIR ]; then
67 # the following is needed when using xcp-xapi
68 XAPI_PLUGIN_DIR=/usr/lib/xcp/plugins/
69fi
70cp -pr ./nova/*/plugins/xenserver/xenapi/etc/xapi.d/plugins/* $XAPI_PLUGIN_DIR
71chmod a+x ${XAPI_PLUGIN_DIR}*
Renuka Apte0af143b2012-04-02 15:46:53 -070072
73mkdir -p /boot/guest
74
John Garbuttdaadf742012-04-27 18:28:28 +010075
76#
77# Configure Networking
78#
Anthony Youngb62b4ca2011-10-26 22:29:08 -070079
80# Helper to create networks
rootb1153412012-01-19 13:28:21 -080081# Uses echo trickery to return network uuid
Anthony Youngb62b4ca2011-10-26 22:29:08 -070082function create_network() {
rootb1153412012-01-19 13:28:21 -080083 br=$1
84 dev=$2
85 vlan=$3
86 netname=$4
87 if [ -z $br ]
88 then
Renuka Apte0af143b2012-04-02 15:46:53 -070089 pif=$(xe_min pif-list device=$dev VLAN=$vlan)
rootb1153412012-01-19 13:28:21 -080090 if [ -z $pif ]
91 then
92 net=$(xe network-create name-label=$netname)
93 else
Renuka Apte0af143b2012-04-02 15:46:53 -070094 net=$(xe_min network-list PIF-uuids=$pif)
rootb1153412012-01-19 13:28:21 -080095 fi
96 echo $net
97 return 0
98 fi
Renuka Apte0af143b2012-04-02 15:46:53 -070099 if [ ! $(xe_min network-list params=bridge | grep -w --only-matching $br) ]
rootb1153412012-01-19 13:28:21 -0800100 then
101 echo "Specified bridge $br does not exist"
102 echo "If you wish to use defaults, please keep the bridge name empty"
103 exit 1
104 else
Renuka Apte0af143b2012-04-02 15:46:53 -0700105 net=$(xe_min network-list bridge=$br)
rootb1153412012-01-19 13:28:21 -0800106 echo $net
107 fi
108}
109
110function errorcheck() {
111 rc=$?
112 if [ $rc -ne 0 ]
113 then
114 exit $rc
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700115 fi
116}
117
John Garbuttdaadf742012-04-27 18:28:28 +0100118# Create host, vm, mgmt, pub networks on XenServer
rootb1153412012-01-19 13:28:21 -0800119VM_NET=$(create_network "$VM_BR" "$VM_DEV" "$VM_VLAN" "vmbr")
120errorcheck
121MGT_NET=$(create_network "$MGT_BR" "$MGT_DEV" "$MGT_VLAN" "mgtbr")
122errorcheck
123PUB_NET=$(create_network "$PUB_BR" "$PUB_DEV" "$PUB_VLAN" "pubbr")
124errorcheck
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700125
126# Helper to create vlans
127function create_vlan() {
rootb1153412012-01-19 13:28:21 -0800128 dev=$1
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700129 vlan=$2
130 net=$3
rootb1153412012-01-19 13:28:21 -0800131 # VLAN -1 refers to no VLAN (physical network)
132 if [ $vlan -eq -1 ]
133 then
134 return
135 fi
Renuka Apte0af143b2012-04-02 15:46:53 -0700136 if [ -z $(xe_min vlan-list tag=$vlan) ]
rootb1153412012-01-19 13:28:21 -0800137 then
Renuka Apte0af143b2012-04-02 15:46:53 -0700138 pif=$(xe_min pif-list network-uuid=$net)
rootb1153412012-01-19 13:28:21 -0800139 # We created a brand new network this time
140 if [ -z $pif ]
141 then
Renuka Apte0af143b2012-04-02 15:46:53 -0700142 pif=$(xe_min pif-list device=$dev VLAN=-1)
rootb1153412012-01-19 13:28:21 -0800143 xe vlan-create pif-uuid=$pif vlan=$vlan network-uuid=$net
144 else
145 echo "VLAN does not exist but PIF attached to this network"
146 echo "How did we reach here?"
147 exit 1
148 fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700149 fi
150}
151
152# Create vlans for vm and management
rootb1153412012-01-19 13:28:21 -0800153create_vlan $PUB_DEV $PUB_VLAN $PUB_NET
154create_vlan $VM_DEV $VM_VLAN $VM_NET
155create_vlan $MGT_DEV $MGT_VLAN $MGT_NET
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700156
John Garbuttdaadf742012-04-27 18:28:28 +0100157# Get final bridge names
158if [ -z $VM_BR ]; then
159 VM_BR=$(xe_min network-list uuid=$VM_NET params=bridge)
160fi
161if [ -z $MGT_BR ]; then
162 MGT_BR=$(xe_min network-list uuid=$MGT_NET params=bridge)
163fi
164if [ -z $PUB_BR ]; then
165 PUB_BR=$(xe_min network-list uuid=$PUB_NET params=bridge)
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700166fi
167
John Garbuttdaadf742012-04-27 18:28:28 +0100168# dom0 ip, XenAPI is assumed to be listening
169HOST_IP=${HOST_IP:-`ifconfig xenbr0 | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"`}
170
171# Set up ip forwarding, but skip on xcp-xapi
172if [ -a /etc/sysconfig/network]; then
173 if ! grep -q "FORWARD_IPV4=YES" /etc/sysconfig/network; then
174 # FIXME: This doesn't work on reboot!
175 echo "FORWARD_IPV4=YES" >> /etc/sysconfig/network
176 fi
177fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700178# Also, enable ip forwarding in rc.local, since the above trick isn't working
179if ! grep -q "echo 1 >/proc/sys/net/ipv4/ip_forward" /etc/rc.local; then
180 echo "echo 1 >/proc/sys/net/ipv4/ip_forward" >> /etc/rc.local
181fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700182# Enable ip forwarding at runtime as well
183echo 1 > /proc/sys/net/ipv4/ip_forward
184
John Garbuttdaadf742012-04-27 18:28:28 +0100185
186#
Anthony Young346e4912011-11-05 00:22:47 -0500187# Shutdown previous runs
John Garbuttdaadf742012-04-27 18:28:28 +0100188#
189
Anthony Young346e4912011-11-05 00:22:47 -0500190DO_SHUTDOWN=${DO_SHUTDOWN:-1}
John Garbuttdaadf742012-04-27 18:28:28 +0100191CLEAN_TEMPLATES=${CLEAN_TEMPLATES:-false}
Anthony Young346e4912011-11-05 00:22:47 -0500192if [ "$DO_SHUTDOWN" = "1" ]; then
Anthony Young40b57372011-11-05 00:30:07 -0500193 # Shutdown all domU's that created previously
John Garbuttdaadf742012-04-27 18:28:28 +0100194 clean_templates_arg=""
195 if $CLEAN_TEMPLATES; then
196 clean_templates_arg="--remove-templates"
197 fi
198 ./scripts/uninstall-os-vpx.sh $clean_templates_arg
Anthony Young346e4912011-11-05 00:22:47 -0500199
200 # Destroy any instances that were launched
201 for uuid in `xe vm-list | grep -1 instance | grep uuid | sed "s/.*\: //g"`; do
202 echo "Shutting down nova instance $uuid"
203 xe vm-unpause uuid=$uuid || true
Armando Migliaccio7a5f7f22012-04-20 22:58:00 +0100204 xe vm-shutdown uuid=$uuid || true
Anthony Young346e4912011-11-05 00:22:47 -0500205 xe vm-destroy uuid=$uuid
206 done
Anthony Youngfa4ecc62011-11-11 10:23:22 -0800207
208 # Destroy orphaned vdis
209 for uuid in `xe vdi-list | grep -1 Glance | grep uuid | sed "s/.*\: //g"`; do
210 xe vdi-destroy uuid=$uuid
211 done
Anthony Young346e4912011-11-05 00:22:47 -0500212fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700213
Renuka Apte0af143b2012-04-02 15:46:53 -0700214
John Garbuttdaadf742012-04-27 18:28:28 +0100215#
216# Create Ubuntu VM template
217# and/or create VM from template
218#
Renuka Apte0af143b2012-04-02 15:46:53 -0700219
John Garbuttdaadf742012-04-27 18:28:28 +0100220GUEST_NAME=${GUEST_NAME:-"DevStackOSDomU"}
221TNAME="devstack_template_folsom_11.10"
222SNAME_PREPARED="template_prepared"
223SNAME_FIRST_BOOT="before_first_boot"
224
225function wait_for_VM_to_halt() {
Renuka Apte0af143b2012-04-02 15:46:53 -0700226 while true
227 do
228 state=$(xe_min vm-list name-label="$GUEST_NAME" power-state=halted)
229 if [ -n "$state" ]
230 then
231 break
232 else
233 echo "Waiting for "$GUEST_NAME" to finish installation..."
John Garbuttdaadf742012-04-27 18:28:28 +0100234 sleep 20
Renuka Apte0af143b2012-04-02 15:46:53 -0700235 fi
236 done
John Garbuttdaadf742012-04-27 18:28:28 +0100237}
Renuka Apte0af143b2012-04-02 15:46:53 -0700238
John Garbuttdaadf742012-04-27 18:28:28 +0100239templateuuid=$(xe template-list name-label="$TNAME")
240if [ -z "$templateuuid" ]; then
241 #
242 # Install Ubuntu over network
243 #
244
245 # try to find ubuntu template
246 ubuntu_template_name="Ubuntu 11.10 for DevStack (64-bit)"
247 ubuntu_template=$(xe_min template-list name-label="$ubuntu_template_name")
248
249 # remove template, if we are in CLEAN_TEMPLATE mode
250 if [ -n "$ubuntu_template" ]; then
251 if $CLEAN_TEMPLATES; then
252 xe template-param-clear param-name=other-config uuid=$ubuntu_template
253 xe template-uninstall template-uuid=$ubuntu_template force=true
254 ubuntu_template=""
255 fi
256 fi
257
258 # always update the preseed file, incase we have a newer one
259 PRESEED_URL=${PRESEED_URL:-""}
260 if [ -z "$PRESEED_URL" ]; then
261 PRESEED_URL="${HOST_IP}/devstackubuntupreseed.cfg"
262 HTTP_SERVER_LOCATION="/opt/xensource/www"
263 if [ ! -e $HTTP_SERVER_LOCATION ]; then
264 HTTP_SERVER_LOCATION="/var/www/html"
265 mkdir -p $HTTP_SERVER_LOCATION
266 fi
267 cp -f $TOP_DIR/devstackubuntupreseed.cfg $HTTP_SERVER_LOCATION
268 MIRROR=${MIRROR:-""}
269 if [ -n "$MIRROR" ]; then
270 sed -e "s,d-i mirror/http/hostname string .*,d-i mirror/http/hostname string $MIRROR," \
271 -i "${HTTP_SERVER_LOCATION}/devstackubuntupreseed.cfg"
272 fi
273 fi
274
275 if [ -z "$ubuntu_template" ]; then
276 $TOP_DIR/scripts/xenoneirictemplate.sh $PRESEED_URL
277 fi
278
279 # create a new VM with the given template
280 # creating the correct VIFs and metadata
281 $TOP_DIR/scripts/install-os-vpx.sh -t "$ubuntu_template_name" -v $VM_BR -m $MGT_BR -p $PUB_BR -l $GUEST_NAME -r $OSDOMU_MEM_MB -k "flat_network_bridge=${VM_BR}"
282
283 # wait for install to finish
284 wait_for_VM_to_halt
285
286 # set VM to restart after a reboot
Renuka Apte0af143b2012-04-02 15:46:53 -0700287 vm_uuid=$(xe_min vm-list name-label="$GUEST_NAME")
288 xe vm-param-set actions-after-reboot=Restart uuid="$vm_uuid"
289
John Garbuttdaadf742012-04-27 18:28:28 +0100290 #
291 # Prepare VM for DevStack
292 #
293
294 # Install XenServer tools, and other such things
295 $TOP_DIR/prepare_guest_template.sh "$GUEST_NAME"
296
297 # start the VM to run the prepare steps
298 xe vm-start vm="$GUEST_NAME"
299
300 # Wait for prep script to finish and shutdown system
301 wait_for_VM_to_halt
302
Renuka Apte0af143b2012-04-02 15:46:53 -0700303 # Make template from VM
John Garbuttdaadf742012-04-27 18:28:28 +0100304 snuuid=$(xe vm-snapshot vm="$GUEST_NAME" new-name-label="$SNAME_PREPARED")
305 xe snapshot-clone uuid=$snuuid new-name-label="$TNAME"
306else
307 #
308 # Template already installed, create VM from template
309 #
310 vm_uuid=$(xe vm-install template="$TNAME" new-name-label="$GUEST_NAME")
Renuka Apte0af143b2012-04-02 15:46:53 -0700311fi
312
John Garbuttdaadf742012-04-27 18:28:28 +0100313
314#
315# Inject DevStack inside VM disk
316#
Renuka Apte0af143b2012-04-02 15:46:53 -0700317$TOP_DIR/build_xva.sh "$GUEST_NAME"
318
John Garbuttdaadf742012-04-27 18:28:28 +0100319# create a snapshot before the first boot
320# to allow a quick re-run with the same settings
321xe vm-snapshot vm="$GUEST_NAME" new-name-label="$SNAME_FIRST_BOOT"
322
323
324#
325# Run DevStack VM
326#
Renuka Apte0af143b2012-04-02 15:46:53 -0700327xe vm-start vm="$GUEST_NAME"
Anthony Young3eb8f592011-10-26 23:11:52 -0700328
John Garbuttdaadf742012-04-27 18:28:28 +0100329
330#
331# Find IP and optionally wait for stack.sh to complete
332#
333
334function find_ip_by_name() {
335 local guest_name="$1"
336 local interface="$2"
337 local period=10
338 max_tries=10
339 i=0
340 while true
341 do
342 if [ $i -ge $max_tries ]; then
343 echo "Timed out waiting for devstack ip address"
344 exit 11
345 fi
346
347 devstackip=$(xe vm-list --minimal \
348 name-label=$guest_name \
349 params=networks | sed -ne "s,^.*${interface}/ip: \([0-9.]*\).*\$,\1,p")
350 if [ -z "$devstackip" ]
351 then
352 sleep $period
353 ((i++))
354 else
355 echo $devstackip
356 break
357 fi
358 done
359}
360
361function ssh_no_check() {
362 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$@"
363}
364
365# Note the XenServer needs to be on the chosen
366# network, so XenServer can access Glance API
367if [ $HOST_IP_IFACE == "eth2" ]; then
368 DOMU_IP=$MGT_IP
369 if [ $MGT_IP == "dhcp" ]; then
370 DOMU_IP=$(find_ip_by_name $GUEST_NAME 2)
371 fi
372else
373 DOMU_IP=$PUB_IP
374 if [ $PUB_IP == "dhcp" ]; then
375 DOMU_IP=$(find_ip_by_name $GUEST_NAME 3)
376 fi
Renuka Aptec56885a2012-02-29 16:09:26 -0800377fi
378
Anthony Young1de18c62011-11-01 14:19:18 -0500379# If we have copied our ssh credentials, use ssh to monitor while the installation runs
380WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
John Garbuttdaadf742012-04-27 18:28:28 +0100381COPYENV=${COPYENV:-1}
Anthony Young1de18c62011-11-01 14:19:18 -0500382if [ "$WAIT_TILL_LAUNCH" = "1" ] && [ -e ~/.ssh/id_rsa.pub ] && [ "$COPYENV" = "1" ]; then
Anthony Young1de18c62011-11-01 14:19:18 -0500383 echo "We're done launching the vm, about to start tailing the"
384 echo "stack.sh log. It will take a second or two to start."
385 echo
386 echo "Just CTRL-C at any time to stop tailing."
387
John Garbuttdaadf742012-04-27 18:28:28 +0100388 # wait for log to appear
389 while ! ssh_no_check -q stack@$DOMU_IP "[ -e run.sh.log ]"; do
390 sleep 10
Anthony Young1de18c62011-11-01 14:19:18 -0500391 done
392
John Garbuttdaadf742012-04-27 18:28:28 +0100393 # output the run.sh.log
394 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no stack@$DOMU_IP 'tail -f run.sh.log' &
Anthony Young1de18c62011-11-01 14:19:18 -0500395 TAIL_PID=$!
396
397 function kill_tail() {
John Garbuttdaadf742012-04-27 18:28:28 +0100398 kill -9 $TAIL_PID
Anthony Young1de18c62011-11-01 14:19:18 -0500399 exit 1
400 }
Anthony Young1de18c62011-11-01 14:19:18 -0500401 # Let Ctrl-c kill tail and exit
402 trap kill_tail SIGINT
403
John Garbuttdaadf742012-04-27 18:28:28 +0100404 # ensure we kill off the tail if we exit the script early
405 # for other reasons
406 add_on_exit "kill -9 $TAIL_PID || true"
407
408 # wait silently until stack.sh has finished
409 set +o xtrace
410 while ! ssh_no_check -q stack@$DOMU_IP "tail run.sh.log | grep -q 'stack.sh completed in'"; do
411 sleep 10
Anthony Young1de18c62011-11-01 14:19:18 -0500412 done
John Garbuttdaadf742012-04-27 18:28:28 +0100413 set -o xtrace
Anthony Young1de18c62011-11-01 14:19:18 -0500414
John Garbuttdaadf742012-04-27 18:28:28 +0100415 # kill the tail process now stack.sh has finished
416 kill -9 $TAIL_PID
Anthony Young1de18c62011-11-01 14:19:18 -0500417
John Garbuttdaadf742012-04-27 18:28:28 +0100418 # check for a failure
419 if ssh_no_check -q stack@$DOMU_IP "grep -q 'stack.sh failed' run.sh.log"; then
Anthony Young1de18c62011-11-01 14:19:18 -0500420 exit 1
421 fi
John Garbuttdaadf742012-04-27 18:28:28 +0100422 echo "################################################################################"
Anthony Young1de18c62011-11-01 14:19:18 -0500423 echo ""
John Garbuttdaadf742012-04-27 18:28:28 +0100424 echo "All Finished!"
425 echo "You can visit the OpenStack Dashboard"
426 echo "at http://$DOMU_IP, and contact other services at the usual ports."
Anthony Young1de18c62011-11-01 14:19:18 -0500427else
428 echo "################################################################################"
429 echo ""
430 echo "All Finished!"
431 echo "Now, you can monitor the progress of the stack.sh installation by "
432 echo "tailing /opt/stack/run.sh.log from within your domU."
433 echo ""
John Garbuttdaadf742012-04-27 18:28:28 +0100434 echo "ssh into your domU now: 'ssh stack@$DOMU_IP' using your password"
Anthony Young1de18c62011-11-01 14:19:18 -0500435 echo "and then do: 'tail -f /opt/stack/run.sh.log'"
436 echo ""
437 echo "When the script completes, you can then visit the OpenStack Dashboard"
John Garbuttdaadf742012-04-27 18:28:28 +0100438 echo "at http://$DOMU_IP, and contact other services at the usual ports."
Anthony Young1de18c62011-11-01 14:19:18 -0500439fi