blob: c7bb3d1730e3aeb8bea76b195971f410aa9d1b86 [file] [log] [blame]
Anthony Youngb62b4ca2011-10-26 22:29:08 -07001#!/bin/bash
2
3# Abort if localrc is not set
4if [ ! -e ../../localrc ]; then
5 echo "You must have a localrc with ALL necessary passwords defined before proceeding."
6 echo "See the xen README for required passwords."
7 exit 1
8fi
9
10# Echo commands
11set -o xtrace
12
13# Name of this guest
14GUEST_NAME=${GUEST_NAME:-ALLINONE}
15
16# dom0 ip
17HOST_IP=${HOST_IP:-`ifconfig xenbr0 | grep "inet addr" | cut -d ":" -f2 | sed "s/ .*//"`}
18
19# Our nova host's network info
Anthony Youngf6ef5692011-10-26 23:40:46 -070020VM_IP=${VM_IP:-10.255.255.255} # A host-only ip that let's the interface come up, otherwise unused
21MGT_IP=${MGT_IP:-172.16.100.55}
Anthony Youngb62b4ca2011-10-26 22:29:08 -070022PUB_IP=${PUB_IP:-192.168.1.55}
23
24# Public network
25PUB_BR=${PUB_BR:-xenbr0}
26PUB_NETMASK=${PUB_NETMASK:-255.255.255.0}
27
28# VM network params
29VM_NETMASK=${VM_NETMASK:-255.255.255.0}
Anthony Youngea1290a2011-10-27 12:53:30 -070030VM_BR=${VM_BR:-xenbr1}
Anthony Youngb62b4ca2011-10-26 22:29:08 -070031VM_VLAN=${VM_VLAN:-100}
32
33# MGMT network params
34MGT_NETMASK=${MGT_NETMASK:-255.255.255.0}
Anthony Youngea1290a2011-10-27 12:53:30 -070035MGT_BR=${MGT_BR:-xenbr2}
Anthony Youngb62b4ca2011-10-26 22:29:08 -070036MGT_VLAN=${MGT_VLAN:-101}
37
38# VM Password
Anthony Younga6936552011-10-26 23:29:59 -070039GUEST_PASSWORD=${GUEST_PASSWORD:-secrete}
Anthony Youngb62b4ca2011-10-26 22:29:08 -070040
41# Size of image
42VDI_MB=${VDI_MB:-2500}
43
44# This directory
45TOP_DIR=$(cd $(dirname "$0") && pwd)
46
47# Make sure we have git
48if ! which git; then
49 GITDIR=/tmp/git-1.7.7
50 cd /tmp
51 rm -rf $GITDIR*
52 wget http://git-core.googlecode.com/files/git-1.7.7.tar.gz
53 tar xfv git-1.7.7.tar.gz
54 cd $GITDIR
55 ./configure
56 make install
57 cd $TOP_DIR
58fi
59
60# Helper to create networks
61function create_network() {
62 if ! xe network-list | grep bridge | grep -q $1; then
63 echo "Creating bridge $1"
64 xe network-create name-label=$1
65 fi
66}
67
68# Create host, vm, mgmt, pub networks
69create_network xapi0
70create_network $VM_BR
71create_network $MGT_BR
72create_network $PUB_BR
73
74# Get the uuid for our physical (public) interface
75PIF=`xe pif-list --minimal device=eth0`
76
77# Create networks/bridges for vm and management
78VM_NET=`xe network-list --minimal bridge=$VM_BR`
79MGT_NET=`xe network-list --minimal bridge=$MGT_BR`
80
81# Helper to create vlans
82function create_vlan() {
83 pif=$1
84 vlan=$2
85 net=$3
86 if ! xe vlan-list | grep tag | grep -q $vlan; then
87 xe vlan-create pif-uuid=$pif vlan=$vlan network-uuid=$net
88 fi
89}
90
91# Create vlans for vm and management
92create_vlan $PIF $VM_VLAN $VM_NET
93create_vlan $PIF $MGT_VLAN $MGT_NET
94
95# Setup host-only nat rules
96HOST_NET=169.254.0.0/16
97if ! iptables -L -v -t nat | grep -q $HOST_NET; then
98 iptables -t nat -A POSTROUTING -s $HOST_NET -j SNAT --to-source $HOST_IP
99 iptables -I FORWARD 1 -s $HOST_NET -j ACCEPT
100 /etc/init.d/iptables save
101fi
102
103# Set up ip forwarding
104if ! grep -q "FORWARD_IPV4=YES" /etc/sysconfig/network; then
105 # FIXME: This doesn't work on reboot!
106 echo "FORWARD_IPV4=YES" >> /etc/sysconfig/network
107fi
108
109# Also, enable ip forwarding in rc.local, since the above trick isn't working
110if ! grep -q "echo 1 >/proc/sys/net/ipv4/ip_forward" /etc/rc.local; then
111 echo "echo 1 >/proc/sys/net/ipv4/ip_forward" >> /etc/rc.local
112fi
113
114# Enable ip forwarding at runtime as well
115echo 1 > /proc/sys/net/ipv4/ip_forward
116
117# Directory where we stage the build
118STAGING_DIR=$TOP_DIR/stage
119
120# Option to clean out old stuff
121CLEAN=${CLEAN:-0}
122if [ "$CLEAN" = "1" ]; then
123 rm -rf $STAGING_DIR
124fi
125
126# Download our base image. This image is made using prepare_guest.sh
127BASE_IMAGE_URL=${BASE_IMAGE_URL:-http://images.ansolabs.com/xen/stage.tgz}
128if [ ! -e $STAGING_DIR ]; then
129 if [ ! -e /tmp/stage.tgz ]; then
130 wget $BASE_IMAGE_URL -O /tmp/stage.tgz
131 fi
132 tar xfz /tmp/stage.tgz
133 cd $TOP_DIR
134fi
135
136# Free up precious disk space
137rm -f /tmp/stage.tgz
138
139# Make sure we have a stage
140if [ ! -d $STAGING_DIR/etc ]; then
141 echo "Stage is not properly set up!"
142 exit 1
143fi
144
145# Directory where our conf files are stored
146FILES_DIR=$TOP_DIR/files
Anthony Young3eb8f592011-10-26 23:11:52 -0700147TEMPLATES_DIR=$TOP_DIR/templates
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700148
149# Directory for supporting script files
150SCRIPT_DIR=$TOP_DIR/scripts
151
152# Version of ubuntu with which we are working
153UBUNTU_VERSION=`cat $STAGING_DIR/etc/lsb-release | grep "DISTRIB_CODENAME=" | sed "s/DISTRIB_CODENAME=//"`
154KERNEL_VERSION=`ls $STAGING_DIR/boot/vmlinuz* | head -1 | sed "s/.*vmlinuz-//"`
155
156# Setup fake grub
157rm -rf $STAGING_DIR/boot/grub/
158mkdir -p $STAGING_DIR/boot/grub/
Anthony Young3eb8f592011-10-26 23:11:52 -0700159cp $TEMPLATES_DIR/menu.lst.in $STAGING_DIR/boot/grub/menu.lst
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700160sed -e "s,@KERNEL_VERSION@,$KERNEL_VERSION,g" -i $STAGING_DIR/boot/grub/menu.lst
161
162# Setup fstab, tty, and other system stuff
163cp $FILES_DIR/fstab $STAGING_DIR/etc/fstab
164cp $FILES_DIR/hvc0.conf $STAGING_DIR/etc/init/
165
166# Put the VPX into UTC.
167rm -f $STAGING_DIR/etc/localtime
168
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700169# Configure dns (use same dns as dom0)
170cp /etc/resolv.conf $STAGING_DIR/etc/resolv.conf
171
172# Copy over devstack
173rm -f /tmp/devstack.tar
174tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar $TOP_DIR/../../../devstack
175cd $STAGING_DIR/opt/stack/
176tar xf /tmp/devstack.tar
177cd $TOP_DIR
178
179# Configure OVA
180VDI_SIZE=$(($VDI_MB*1024*1024))
181PRODUCT_BRAND=${PRODUCT_BRAND:-openstack}
182PRODUCT_VERSION=${PRODUCT_VERSION:-001}
183BUILD_NUMBER=${BUILD_NUMBER:-001}
184LABEL="$PRODUCT_BRAND $PRODUCT_VERSION-$BUILD_NUMBER"
185OVA=$STAGING_DIR/tmp/ova.xml
Anthony Young3eb8f592011-10-26 23:11:52 -0700186cp $TEMPLATES_DIR/ova.xml.in $OVA
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700187sed -e "s,@VDI_SIZE@,$VDI_SIZE,g" -i $OVA
188sed -e "s,@PRODUCT_BRAND@,$PRODUCT_BRAND,g" -i $OVA
189sed -e "s,@PRODUCT_VERSION@,$PRODUCT_VERSION,g" -i $OVA
190sed -e "s,@BUILD_NUMBER@,$BUILD_NUMBER,g" -i $OVA
191
192# Directory for xvas
193XVA_DIR=$TOP_DIR/xvas
194
195# Create xva dir
196mkdir -p $XVA_DIR
197
198# Clean nova if desired
199if [ "$CLEAN" = "1" ]; then
200 rm -rf $TOP_DIR/nova
201fi
202
203# Checkout nova
204if [ ! -d $TOP_DIR/nova ]; then
205 git clone git://github.com/cloudbuilders/nova.git
206 git checkout diablo
207fi
208
209# Run devstack on launch
210cat <<EOF >$STAGING_DIR/etc/rc.local
Anthony Youngf54bc062011-10-27 00:39:30 -0700211GUEST_PASSWORD=$GUEST_PASSWORD STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700212su -c "/opt/stack/run.sh > /opt/stack/run.sh.log" stack
213exit 0
214EOF
215
216# Install plugins
217cp -pr $TOP_DIR/nova/plugins/xenserver/xenapi/etc/xapi.d /etc/
218chmod a+x /etc/xapi.d/plugins/*
219yum --enablerepo=base install -y parted
220mkdir -p /boot/guest
221
222# Set local storage il8n
223SR_UUID=`xe sr-list --minimal name-label="Local storage"`
224xe sr-param-set uuid=$SR_UUID other-config:i18n-key=local-storage
225
226# Uninstall previous runs
227xe vm-list --minimal name-label="$LABEL" | xargs ./scripts/uninstall-os-vpx.sh
228
229# Destroy any instances that were launched
230for uuid in `xe vm-list | grep -1 instance | grep uuid | sed "s/.*\: //g"`; do
231 echo "Shutting down nova instance $uuid"
Anthony Young2cec3dc2011-11-02 07:03:38 -0500232 xe vm-unpause uuid=$uuid || true
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700233 xe vm-shutdown uuid=$uuid
234 xe vm-destroy uuid=$uuid
235done
236
237# Path to head xva. By default keep overwriting the same one to save space
238USE_SEPARATE_XVAS=${USE_SEPARATE_XVAS:-0}
239if [ "$USE_SEPARATE_XVAS" = "0" ]; then
240 XVA=$XVA_DIR/$UBUNTU_VERSION.xva
241else
242 XVA=$XVA_DIR/$UBUNTU_VERSION.$GUEST_NAME.xva
243fi
244
245# Clean old xva. In the future may not do this every time.
246rm -f $XVA
247
Anthony Young138b2832011-10-27 13:06:39 -0700248# Configure the hostname
249echo $GUEST_NAME > $STAGING_DIR/etc/hostname
250
251# Hostname must resolve for rabbit
252cat <<EOF >$STAGING_DIR/etc/hosts
253$MGT_IP $GUEST_NAME
254127.0.0.1 localhost localhost.localdomain
255EOF
256
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700257# Configure the network
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700258INTERFACES=$STAGING_DIR/etc/network/interfaces
Anthony Young3eb8f592011-10-26 23:11:52 -0700259cp $TEMPLATES_DIR/interfaces.in $INTERFACES
Anthony Youngf6ef5692011-10-26 23:40:46 -0700260sed -e "s,@ETH1_IP@,$VM_IP,g" -i $INTERFACES
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700261sed -e "s,@ETH1_NETMASK@,$VM_NETMASK,g" -i $INTERFACES
262sed -e "s,@ETH2_IP@,$MGT_IP,g" -i $INTERFACES
263sed -e "s,@ETH2_NETMASK@,$MGT_NETMASK,g" -i $INTERFACES
264sed -e "s,@ETH3_IP@,$PUB_IP,g" -i $INTERFACES
265sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES
266
Anthony Younga1a90772011-10-31 15:32:38 -0700267# Gracefully cp only if source file/dir exists
268function cp_it {
269 if [ -e $1 ] || [ -d $1 ]; then
270 cp -pRL $1 $2
271 fi
272}
273
274# Copy over your ssh keys and env if desired
275COPYENV=${COPYENV:-1}
276if [ "$COPYENV" = "1" ]; then
277 cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh
278 cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys
279 cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig
280 cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc
281 cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc
282fi
283
Anthony Youngb62b4ca2011-10-26 22:29:08 -0700284# Configure run.sh
285cat <<EOF >$STAGING_DIR/opt/stack/run.sh
286#!/bin/bash
287cd /opt/stack/devstack
288killall screen
289UPLOAD_LEGACY_TTY=yes HOST_IP=$PUB_IP VIRT_DRIVER=xenserver FORCE=yes MULTI_HOST=1 $STACKSH_PARAMS ./stack.sh
290EOF
291chmod 755 $STAGING_DIR/opt/stack/run.sh
292
293# Create xva
294if [ ! -e $XVA ]; then
295 rm -rf /tmp/mkxva*
296 UID=0 $SCRIPT_DIR/mkxva -o $XVA -t xva -x $OVA $STAGING_DIR $VDI_MB /tmp/
297fi
298
299# Start guest
300$TOP_DIR/scripts/install-os-vpx.sh -f $XVA -v $VM_BR -m $MGT_BR -p $PUB_BR
Anthony Young3eb8f592011-10-26 23:11:52 -0700301
Anthony Young1de18c62011-11-01 14:19:18 -0500302# If we have copied our ssh credentials, use ssh to monitor while the installation runs
303WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
304if [ "$WAIT_TILL_LAUNCH" = "1" ] && [ -e ~/.ssh/id_rsa.pub ] && [ "$COPYENV" = "1" ]; then
305 # Done creating the container, let's tail the log
306 echo
307 echo "============================================================="
308 echo " -- YAY! --"
309 echo "============================================================="
310 echo
311 echo "We're done launching the vm, about to start tailing the"
312 echo "stack.sh log. It will take a second or two to start."
313 echo
314 echo "Just CTRL-C at any time to stop tailing."
315
316 set +o xtrace
317
318 while ! ssh -q stack@$PUB_IP "[ -e run.sh.log ]"; do
319 sleep 1
320 done
321
322 ssh stack@$PUB_IP 'tail -f run.sh.log' &
323
324 TAIL_PID=$!
325
326 function kill_tail() {
327 kill $TAIL_PID
328 exit 1
329 }
330
331 # Let Ctrl-c kill tail and exit
332 trap kill_tail SIGINT
333
334 echo "Waiting stack.sh to finish..."
Anthony Youngf0dca552011-11-01 14:23:14 -0700335 while ! ssh -q stack@$PUB_IP "grep -q 'stack.sh completed in' run.sh.log"; do
Anthony Young1de18c62011-11-01 14:19:18 -0500336 sleep 1
337 done
338
339 kill $TAIL_PID
340
Anthony Youngf0dca552011-11-01 14:23:14 -0700341 if ssh -q stack@$PUB_IP "grep -q 'stack.sh failed' run.sh.log"; then
Anthony Young1de18c62011-11-01 14:19:18 -0500342 exit 1
343 fi
344 echo ""
345 echo "Finished - Zip-a-dee Doo-dah!"
346 echo "You can then visit the OpenStack Dashboard"
347 echo "at http://$PUB_IP, and contact other services at the usual ports."
348else
349 echo "################################################################################"
350 echo ""
351 echo "All Finished!"
352 echo "Now, you can monitor the progress of the stack.sh installation by "
353 echo "tailing /opt/stack/run.sh.log from within your domU."
354 echo ""
355 echo "ssh into your domU now: 'ssh stack@$PUB_IP' using your password"
356 echo "and then do: 'tail -f /opt/stack/run.sh.log'"
357 echo ""
358 echo "When the script completes, you can then visit the OpenStack Dashboard"
359 echo "at http://$PUB_IP, and contact other services at the usual ports."
360
361fi