blob: deaf7e5e677aa2d6ebae1078ecbd23e98a61d51a [file] [log] [blame]
Anthony Youngb62b4ca2011-10-26 22:29:08 -07001#!/bin/bash
2
Mate Lakat0b3804b2013-05-07 16:58:17 +01003# This script must be run on a XenServer or XCP machine
John Garbuttdaadf742012-04-27 18:28:28 +01004#
5# It creates a DomU VM that runs OpenStack services
6#
7# For more details see: README.md
8
Renuka Apte0af143b2012-04-02 15:46:53 -07009set -o errexit
Mate Lakat0b3804b2013-05-07 16:58:17 +010010set -o nounset
John Garbuttdaadf742012-04-27 18:28:28 +010011set -o xtrace
Renuka Apte0af143b2012-04-02 15:46:53 -070012
Anthony Youngb62b4ca2011-10-26 22:29:08 -070013# Abort if localrc is not set
14if [ ! -e ../../localrc ]; then
15 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
16 echo "See the xen README for required passwords."
17 exit 1
18fi
19
Anthony Youngaf6ed6b2011-11-02 07:50:27 -050020# This directory
Mate Lakata8bf0f22013-03-07 18:37:31 +000021THIS_DIR=$(cd $(dirname "$0") && pwd)
Anthony Youngaf6ed6b2011-11-02 07:50:27 -050022
Anthony Youngb3e2f332012-03-16 17:01:49 -070023# Source lower level functions
Mate Lakata8bf0f22013-03-07 18:37:31 +000024. $THIS_DIR/../../functions
Anthony Youngb3e2f332012-03-16 17:01:49 -070025
John Garbuttdaadf742012-04-27 18:28:28 +010026# Include onexit commands
Mate Lakata8bf0f22013-03-07 18:37:31 +000027. $THIS_DIR/scripts/on_exit.sh
John Garbuttdaadf742012-04-27 18:28:28 +010028
Mate Lakat57e3da92013-03-22 16:34:05 +000029# xapi functions
30. $THIS_DIR/functions
31
John Garbuttdaadf742012-04-27 18:28:28 +010032#
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
Mate Lakat0b3804b2013-05-07 16:58:17 +010037source $THIS_DIR/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# Prepare Dom0
48# including installing XenAPI plugins
49#
50
Mate Lakata8bf0f22013-03-07 18:37:31 +000051cd $THIS_DIR
John Garbuttdaadf742012-04-27 18:28:28 +010052
Mate Lakat57e3da92013-03-22 16:34:05 +000053# Install plugins
John Garbuttdaadf742012-04-27 18:28:28 +010054
Mate Lakat57e3da92013-03-22 16:34:05 +000055## Nova plugins
56NOVA_ZIPBALL_URL=${NOVA_ZIPBALL_URL:-$(zip_snapshot_location $NOVA_REPO $NOVA_BRANCH)}
57install_xapi_plugins_from_zipball $NOVA_ZIPBALL_URL
Maru Newby2298ca42012-10-25 23:46:42 +000058
Mate Lakat57e3da92013-03-22 16:34:05 +000059## Install the netwrap xapi plugin to support agent control of dom0 networking
Maru Newby2298ca42012-10-25 23:46:42 +000060if [[ "$ENABLED_SERVICES" =~ "q-agt" && "$Q_PLUGIN" = "openvswitch" ]]; then
Mate Lakat57e3da92013-03-22 16:34:05 +000061 QUANTUM_ZIPBALL_URL=${QUANTUM_ZIPBALL_URL:-$(zip_snapshot_location $QUANTUM_REPO $QUANTUM_BRANCH)}
62 install_xapi_plugins_from_zipball $QUANTUM_ZIPBALL_URL
Maru Newby2298ca42012-10-25 23:46:42 +000063fi
64
Mate Lakat57e3da92013-03-22 16:34:05 +000065create_directory_for_kernels
Bob Ball39aeda22013-06-17 12:51:33 +010066create_directory_for_images
John Garbuttdaadf742012-04-27 18:28:28 +010067
68#
69# Configure Networking
70#
Mate Lakat9e326772013-05-08 16:42:22 +010071setup_network "$VM_BRIDGE_OR_NET_NAME"
72setup_network "$MGT_BRIDGE_OR_NET_NAME"
73setup_network "$PUB_BRIDGE_OR_NET_NAME"
Anthony Youngb62b4ca2011-10-26 22:29:08 -070074
Mate Lakatf652e0f2013-05-21 18:12:48 +010075# With quantum, one more network is required, which is internal to the
76# hypervisor, and used by the VMs
77if is_service_enabled quantum; then
78 setup_network "$XEN_INT_BRIDGE_OR_NET_NAME"
79fi
80
Mate Lakat9e326772013-05-08 16:42:22 +010081if parameter_is_specified "FLAT_NETWORK_BRIDGE"; then
82 cat >&2 << EOF
83ERROR: FLAT_NETWORK_BRIDGE is specified in localrc file
84This is considered as an error, as its value will be derived from the
85VM_BRIDGE_OR_NET_NAME variable's value.
86EOF
87 exit 1
Anthony Youngb62b4ca2011-10-26 22:29:08 -070088fi
89
Mate Lakat9e326772013-05-08 16:42:22 +010090if ! xenapi_is_listening_on "$MGT_BRIDGE_OR_NET_NAME"; then
91 cat >&2 << EOF
92ERROR: XenAPI does not have an assigned IP address on the management network.
93please review your XenServer network configuration / localrc file.
94EOF
95 exit 1
96fi
97
98HOST_IP=$(xenapi_ip_on "$MGT_BRIDGE_OR_NET_NAME")
John Garbuttdaadf742012-04-27 18:28:28 +010099
100# Set up ip forwarding, but skip on xcp-xapi
John Garbuttd8f1a872012-06-26 11:16:38 +0100101if [ -a /etc/sysconfig/network ]; then
John Garbuttdaadf742012-04-27 18:28:28 +0100102 if ! grep -q "FORWARD_IPV4=YES" /etc/sysconfig/network; then
103 # FIXME: This doesn't work on reboot!
104 echo "FORWARD_IPV4=YES" >> /etc/sysconfig/network
105 fi
106fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700107# Also, enable ip forwarding in rc.local, since the above trick isn't working
108if ! grep -q "echo 1 >/proc/sys/net/ipv4/ip_forward" /etc/rc.local; then
109 echo "echo 1 >/proc/sys/net/ipv4/ip_forward" >> /etc/rc.local
110fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700111# Enable ip forwarding at runtime as well
112echo 1 > /proc/sys/net/ipv4/ip_forward
113
John Garbuttdaadf742012-04-27 18:28:28 +0100114
115#
Anthony Young346e4912011-11-05 00:22:47 -0500116# Shutdown previous runs
John Garbuttdaadf742012-04-27 18:28:28 +0100117#
118
Anthony Young346e4912011-11-05 00:22:47 -0500119DO_SHUTDOWN=${DO_SHUTDOWN:-1}
John Garbuttdaadf742012-04-27 18:28:28 +0100120CLEAN_TEMPLATES=${CLEAN_TEMPLATES:-false}
Anthony Young346e4912011-11-05 00:22:47 -0500121if [ "$DO_SHUTDOWN" = "1" ]; then
Anthony Young40b57372011-11-05 00:30:07 -0500122 # Shutdown all domU's that created previously
John Garbuttdaadf742012-04-27 18:28:28 +0100123 clean_templates_arg=""
124 if $CLEAN_TEMPLATES; then
125 clean_templates_arg="--remove-templates"
126 fi
127 ./scripts/uninstall-os-vpx.sh $clean_templates_arg
Anthony Young346e4912011-11-05 00:22:47 -0500128
129 # Destroy any instances that were launched
130 for uuid in `xe vm-list | grep -1 instance | grep uuid | sed "s/.*\: //g"`; do
131 echo "Shutting down nova instance $uuid"
132 xe vm-unpause uuid=$uuid || true
Armando Migliaccio7a5f7f22012-04-20 22:58:00 +0100133 xe vm-shutdown uuid=$uuid || true
Anthony Young346e4912011-11-05 00:22:47 -0500134 xe vm-destroy uuid=$uuid
135 done
Anthony Youngfa4ecc62011-11-11 10:23:22 -0800136
137 # Destroy orphaned vdis
138 for uuid in `xe vdi-list | grep -1 Glance | grep uuid | sed "s/.*\: //g"`; do
139 xe vdi-destroy uuid=$uuid
140 done
Anthony Young346e4912011-11-05 00:22:47 -0500141fi
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700142
Renuka Apte0af143b2012-04-02 15:46:53 -0700143
John Garbuttdaadf742012-04-27 18:28:28 +0100144#
145# Create Ubuntu VM template
146# and/or create VM from template
147#
Renuka Apte0af143b2012-04-02 15:46:53 -0700148
John Garbuttdaadf742012-04-27 18:28:28 +0100149GUEST_NAME=${GUEST_NAME:-"DevStackOSDomU"}
John Garbuttd8f1a872012-06-26 11:16:38 +0100150TNAME="devstack_template"
John Garbuttdaadf742012-04-27 18:28:28 +0100151SNAME_PREPARED="template_prepared"
152SNAME_FIRST_BOOT="before_first_boot"
153
154function wait_for_VM_to_halt() {
Bob Ball63c6c2b2013-01-24 13:13:51 +0000155 set +x
156 echo "Waiting for the VM to halt. Progress in-VM can be checked with vncviewer:"
157 mgmt_ip=$(echo $XENAPI_CONNECTION_URL | tr -d -c '1234567890.')
158 domid=$(xe vm-list name-label="$GUEST_NAME" params=dom-id minimal=true)
159 port=$(xenstore-read /local/domain/$domid/console/vnc-port)
160 echo "vncviewer -via $mgmt_ip localhost:${port:2}"
Renuka Apte0af143b2012-04-02 15:46:53 -0700161 while true
162 do
163 state=$(xe_min vm-list name-label="$GUEST_NAME" power-state=halted)
164 if [ -n "$state" ]
165 then
166 break
167 else
Bob Ball63c6c2b2013-01-24 13:13:51 +0000168 echo -n "."
John Garbuttdaadf742012-04-27 18:28:28 +0100169 sleep 20
Renuka Apte0af143b2012-04-02 15:46:53 -0700170 fi
171 done
Bob Ball63c6c2b2013-01-24 13:13:51 +0000172 set -x
John Garbuttdaadf742012-04-27 18:28:28 +0100173}
Renuka Apte0af143b2012-04-02 15:46:53 -0700174
John Garbuttdaadf742012-04-27 18:28:28 +0100175templateuuid=$(xe template-list name-label="$TNAME")
176if [ -z "$templateuuid" ]; then
177 #
178 # Install Ubuntu over network
179 #
180
John Garbuttdaadf742012-04-27 18:28:28 +0100181 # always update the preseed file, incase we have a newer one
182 PRESEED_URL=${PRESEED_URL:-""}
183 if [ -z "$PRESEED_URL" ]; then
184 PRESEED_URL="${HOST_IP}/devstackubuntupreseed.cfg"
185 HTTP_SERVER_LOCATION="/opt/xensource/www"
186 if [ ! -e $HTTP_SERVER_LOCATION ]; then
187 HTTP_SERVER_LOCATION="/var/www/html"
188 mkdir -p $HTTP_SERVER_LOCATION
189 fi
Mate Lakata8bf0f22013-03-07 18:37:31 +0000190 cp -f $THIS_DIR/devstackubuntupreseed.cfg $HTTP_SERVER_LOCATION
Mate Lakatd3740f72013-05-09 15:02:21 +0100191
192 sed \
193 -e "s,\(d-i mirror/http/hostname string\).*,\1 $UBUNTU_INST_HTTP_HOSTNAME,g" \
194 -e "s,\(d-i mirror/http/directory string\).*,\1 $UBUNTU_INST_HTTP_DIRECTORY,g" \
195 -e "s,\(d-i mirror/http/proxy string\).*,\1 $UBUNTU_INST_HTTP_PROXY,g" \
196 -i "${HTTP_SERVER_LOCATION}/devstackubuntupreseed.cfg"
John Garbuttdaadf742012-04-27 18:28:28 +0100197 fi
198
John Garbuttd8f1a872012-06-26 11:16:38 +0100199 # Update the template
Mate Lakata8bf0f22013-03-07 18:37:31 +0000200 $THIS_DIR/scripts/install_ubuntu_template.sh $PRESEED_URL
John Garbuttdaadf742012-04-27 18:28:28 +0100201
202 # create a new VM with the given template
203 # creating the correct VIFs and metadata
Mate Lakat9e326772013-05-08 16:42:22 +0100204 $THIS_DIR/scripts/install-os-vpx.sh \
205 -t "$UBUNTU_INST_TEMPLATE_NAME" \
206 -v "$VM_BRIDGE_OR_NET_NAME" \
207 -m "$MGT_BRIDGE_OR_NET_NAME" \
208 -p "$PUB_BRIDGE_OR_NET_NAME" \
209 -l "$GUEST_NAME" \
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100210 -r "$OSDOMU_MEM_MB"
John Garbuttdaadf742012-04-27 18:28:28 +0100211
212 # wait for install to finish
213 wait_for_VM_to_halt
214
215 # set VM to restart after a reboot
Renuka Apte0af143b2012-04-02 15:46:53 -0700216 vm_uuid=$(xe_min vm-list name-label="$GUEST_NAME")
217 xe vm-param-set actions-after-reboot=Restart uuid="$vm_uuid"
218
John Garbuttdaadf742012-04-27 18:28:28 +0100219 #
220 # Prepare VM for DevStack
221 #
222
223 # Install XenServer tools, and other such things
Mate Lakata8bf0f22013-03-07 18:37:31 +0000224 $THIS_DIR/prepare_guest_template.sh "$GUEST_NAME"
John Garbuttdaadf742012-04-27 18:28:28 +0100225
226 # start the VM to run the prepare steps
227 xe vm-start vm="$GUEST_NAME"
228
229 # Wait for prep script to finish and shutdown system
230 wait_for_VM_to_halt
231
Renuka Apte0af143b2012-04-02 15:46:53 -0700232 # Make template from VM
John Garbuttdaadf742012-04-27 18:28:28 +0100233 snuuid=$(xe vm-snapshot vm="$GUEST_NAME" new-name-label="$SNAME_PREPARED")
234 xe snapshot-clone uuid=$snuuid new-name-label="$TNAME"
235else
236 #
237 # Template already installed, create VM from template
238 #
239 vm_uuid=$(xe vm-install template="$TNAME" new-name-label="$GUEST_NAME")
Renuka Apte0af143b2012-04-02 15:46:53 -0700240fi
241
John Garbuttdaadf742012-04-27 18:28:28 +0100242
243#
244# Inject DevStack inside VM disk
245#
Mate Lakata8bf0f22013-03-07 18:37:31 +0000246$THIS_DIR/build_xva.sh "$GUEST_NAME"
Renuka Apte0af143b2012-04-02 15:46:53 -0700247
Mate Lakatf652e0f2013-05-21 18:12:48 +0100248# Attach a network interface for the integration network (so that the bridge
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100249# is created by XenServer). This is required for Quantum. Also pass that as a
250# kernel parameter for DomU
Mate Lakatf652e0f2013-05-21 18:12:48 +0100251if is_service_enabled quantum; then
252 add_interface "$GUEST_NAME" "$XEN_INT_BRIDGE_OR_NET_NAME" "4"
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100253
254 XEN_INTEGRATION_BRIDGE=$(bridge_for "$XEN_INT_BRIDGE_OR_NET_NAME")
255 append_kernel_cmdline \
256 "$GUEST_NAME" \
257 "xen_integration_bridge=${XEN_INTEGRATION_BRIDGE}"
Mate Lakatf652e0f2013-05-21 18:12:48 +0100258fi
259
Mate Lakat8ff33ce2013-05-30 13:26:58 +0100260FLAT_NETWORK_BRIDGE=$(bridge_for "$VM_BRIDGE_OR_NET_NAME")
261append_kernel_cmdline "$GUEST_NAME" "flat_network_bridge=${FLAT_NETWORK_BRIDGE}"
262
John Garbuttdaadf742012-04-27 18:28:28 +0100263# create a snapshot before the first boot
264# to allow a quick re-run with the same settings
265xe vm-snapshot vm="$GUEST_NAME" new-name-label="$SNAME_FIRST_BOOT"
266
John Garbuttdaadf742012-04-27 18:28:28 +0100267#
268# Run DevStack VM
269#
Renuka Apte0af143b2012-04-02 15:46:53 -0700270xe vm-start vm="$GUEST_NAME"
Anthony Young3eb8f592011-10-26 23:11:52 -0700271
John Garbuttdaadf742012-04-27 18:28:28 +0100272function ssh_no_check() {
273 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$@"
274}
275
Mate Lakat86446762013-05-12 18:34:29 +0100276# Get hold of the Management IP of OpenStack VM
277OS_VM_MANAGEMENT_ADDRESS=$MGT_IP
278if [ $OS_VM_MANAGEMENT_ADDRESS == "dhcp" ]; then
279 OS_VM_MANAGEMENT_ADDRESS=$(find_ip_by_name $GUEST_NAME 2)
280fi
281
282# Get hold of the Service IP of OpenStack VM
John Garbuttdaadf742012-04-27 18:28:28 +0100283if [ $HOST_IP_IFACE == "eth2" ]; then
Mate Lakat86446762013-05-12 18:34:29 +0100284 OS_VM_SERVICES_ADDRESS=$MGT_IP
John Garbuttdaadf742012-04-27 18:28:28 +0100285 if [ $MGT_IP == "dhcp" ]; then
Mate Lakat86446762013-05-12 18:34:29 +0100286 OS_VM_SERVICES_ADDRESS=$(find_ip_by_name $GUEST_NAME 2)
John Garbuttdaadf742012-04-27 18:28:28 +0100287 fi
288else
Mate Lakat86446762013-05-12 18:34:29 +0100289 OS_VM_SERVICES_ADDRESS=$PUB_IP
John Garbuttdaadf742012-04-27 18:28:28 +0100290 if [ $PUB_IP == "dhcp" ]; then
Mate Lakat86446762013-05-12 18:34:29 +0100291 OS_VM_SERVICES_ADDRESS=$(find_ip_by_name $GUEST_NAME 3)
John Garbuttdaadf742012-04-27 18:28:28 +0100292 fi
Renuka Aptec56885a2012-02-29 16:09:26 -0800293fi
294
Anthony Young1de18c62011-11-01 14:19:18 -0500295# If we have copied our ssh credentials, use ssh to monitor while the installation runs
296WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
John Garbuttdaadf742012-04-27 18:28:28 +0100297COPYENV=${COPYENV:-1}
Anthony Young1de18c62011-11-01 14:19:18 -0500298if [ "$WAIT_TILL_LAUNCH" = "1" ] && [ -e ~/.ssh/id_rsa.pub ] && [ "$COPYENV" = "1" ]; then
Bob Ball7fcc1572013-02-20 15:56:25 +0000299 set +x
Anthony Young1de18c62011-11-01 14:19:18 -0500300
Bob Ball7fcc1572013-02-20 15:56:25 +0000301 echo "VM Launched - Waiting for startup script"
John Garbuttdaadf742012-04-27 18:28:28 +0100302 # wait for log to appear
Mate Lakat86446762013-05-12 18:34:29 +0100303 while ! ssh_no_check -q stack@$OS_VM_MANAGEMENT_ADDRESS "[ -e run.sh.log ]"; do
John Garbuttdaadf742012-04-27 18:28:28 +0100304 sleep 10
Anthony Young1de18c62011-11-01 14:19:18 -0500305 done
Bob Ball7fcc1572013-02-20 15:56:25 +0000306 echo -n "Running"
Mate Lakat86446762013-05-12 18:34:29 +0100307 while [ `ssh_no_check -q stack@$OS_VM_MANAGEMENT_ADDRESS pgrep -c run.sh` -ge 1 ]
Mate Lakat9efcf602012-12-19 10:23:06 +0000308 do
John Garbuttdaadf742012-04-27 18:28:28 +0100309 sleep 10
Mate Lakat9efcf602012-12-19 10:23:06 +0000310 echo -n "."
Anthony Young1de18c62011-11-01 14:19:18 -0500311 done
Mate Lakat9efcf602012-12-19 10:23:06 +0000312 echo "done!"
313 set -x
Anthony Young1de18c62011-11-01 14:19:18 -0500314
Mate Lakat9efcf602012-12-19 10:23:06 +0000315 # output the run.sh.log
Mate Lakat86446762013-05-12 18:34:29 +0100316 ssh_no_check -q stack@$OS_VM_MANAGEMENT_ADDRESS 'cat run.sh.log'
Anthony Young1de18c62011-11-01 14:19:18 -0500317
Mate Lakat9efcf602012-12-19 10:23:06 +0000318 # Fail if the expected text is not found
Mate Lakat86446762013-05-12 18:34:29 +0100319 ssh_no_check -q stack@$OS_VM_MANAGEMENT_ADDRESS 'cat run.sh.log' | grep -q 'stack.sh completed in'
Mate Lakat9efcf602012-12-19 10:23:06 +0000320
Bob Ball63c6c2b2013-01-24 13:13:51 +0000321 set +x
John Garbuttdaadf742012-04-27 18:28:28 +0100322 echo "################################################################################"
Anthony Young1de18c62011-11-01 14:19:18 -0500323 echo ""
John Garbuttdaadf742012-04-27 18:28:28 +0100324 echo "All Finished!"
325 echo "You can visit the OpenStack Dashboard"
Mate Lakat86446762013-05-12 18:34:29 +0100326 echo "at http://$OS_VM_SERVICES_ADDRESS, and contact other services at the usual ports."
Anthony Young1de18c62011-11-01 14:19:18 -0500327else
Bob Ball63c6c2b2013-01-24 13:13:51 +0000328 set +x
Anthony Young1de18c62011-11-01 14:19:18 -0500329 echo "################################################################################"
330 echo ""
331 echo "All Finished!"
332 echo "Now, you can monitor the progress of the stack.sh installation by "
333 echo "tailing /opt/stack/run.sh.log from within your domU."
334 echo ""
Mate Lakat86446762013-05-12 18:34:29 +0100335 echo "ssh into your domU now: 'ssh stack@$OS_VM_MANAGEMENT_ADDRESS' using your password"
Anthony Young1de18c62011-11-01 14:19:18 -0500336 echo "and then do: 'tail -f /opt/stack/run.sh.log'"
337 echo ""
338 echo "When the script completes, you can then visit the OpenStack Dashboard"
Mate Lakat86446762013-05-12 18:34:29 +0100339 echo "at http://$OS_VM_SERVICES_ADDRESS, and contact other services at the usual ports."
Anthony Young1de18c62011-11-01 14:19:18 -0500340fi