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