| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 1 | #!/bin/bash | 
 | 2 |  | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 3 | set -e | 
 | 4 |  | 
 | 5 | declare -a on_exit_hooks | 
 | 6 |  | 
 | 7 | on_exit() | 
 | 8 | { | 
 | 9 |     for i in $(seq $((${#on_exit_hooks[*]} - 1)) -1 0) | 
 | 10 |     do | 
 | 11 |         eval "${on_exit_hooks[$i]}" | 
 | 12 |     done | 
 | 13 | } | 
 | 14 |  | 
 | 15 | add_on_exit() | 
 | 16 | { | 
 | 17 |     local n=${#on_exit_hooks[*]} | 
 | 18 |     on_exit_hooks[$n]="$*" | 
 | 19 |     if [[ $n -eq 0 ]] | 
 | 20 |     then | 
 | 21 |         trap on_exit EXIT | 
 | 22 |     fi | 
 | 23 | } | 
 | 24 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 25 | # Abort if localrc is not set | 
 | 26 | if [ ! -e ../../localrc ]; then | 
 | 27 |     echo "You must have a localrc with ALL necessary passwords defined before proceeding." | 
 | 28 |     echo "See the xen README for required passwords." | 
 | 29 |     exit 1 | 
 | 30 | fi | 
 | 31 |  | 
 | 32 | # This directory | 
 | 33 | TOP_DIR=$(cd $(dirname "$0") && pwd) | 
 | 34 |  | 
 | 35 | # Source params - override xenrc params in your localrc to suite your taste | 
 | 36 | source xenrc | 
 | 37 |  | 
 | 38 | # Echo commands | 
 | 39 | set -o xtrace | 
 | 40 |  | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 41 | GUEST_NAME="$1" | 
 | 42 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 43 | # Directory where we stage the build | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 44 | STAGING_DIR=$($TOP_DIR/scripts/manage-vdi open $GUEST_NAME 0 1 | grep -o "/tmp/tmp.[[:alnum:]]*") | 
 | 45 | add_on_exit "$TOP_DIR/scripts/manage-vdi close $GUEST_NAME 0 1" | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 46 |  | 
 | 47 | # Make sure we have a stage | 
 | 48 | if [ ! -d $STAGING_DIR/etc ]; then | 
 | 49 |     echo "Stage is not properly set up!" | 
 | 50 |     exit 1 | 
 | 51 | fi | 
 | 52 |  | 
 | 53 | # Directory where our conf files are stored | 
 | 54 | FILES_DIR=$TOP_DIR/files | 
 | 55 | TEMPLATES_DIR=$TOP_DIR/templates | 
 | 56 |  | 
 | 57 | # Directory for supporting script files | 
 | 58 | SCRIPT_DIR=$TOP_DIR/scripts | 
 | 59 |  | 
 | 60 | # Version of ubuntu with which we are working | 
 | 61 | UBUNTU_VERSION=`cat $STAGING_DIR/etc/lsb-release | grep "DISTRIB_CODENAME=" | sed "s/DISTRIB_CODENAME=//"` | 
 | 62 | KERNEL_VERSION=`ls $STAGING_DIR/boot/vmlinuz* | head -1 | sed "s/.*vmlinuz-//"` | 
 | 63 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 64 | # Configure dns (use same dns as dom0) | 
 | 65 | cp /etc/resolv.conf $STAGING_DIR/etc/resolv.conf | 
 | 66 |  | 
 | 67 | # Copy over devstack | 
 | 68 | rm -f /tmp/devstack.tar | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 69 | cd $TOP_DIR/../../ | 
 | 70 | tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar . | 
 | 71 | mkdir -p $STAGING_DIR/opt/stack/devstack | 
 | 72 | tar xf /tmp/devstack.tar -C $STAGING_DIR/opt/stack/devstack | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 73 | cd $TOP_DIR | 
 | 74 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 75 | # Run devstack on launch | 
 | 76 | cat <<EOF >$STAGING_DIR/etc/rc.local | 
| root | b115341 | 2012-01-19 13:28:21 -0800 | [diff] [blame] | 77 | # network restart required for getting the right gateway | 
 | 78 | /etc/init.d/networking restart | 
| Renuka Apte | ce59d64 | 2012-02-02 16:09:23 -0800 | [diff] [blame] | 79 | GUEST_PASSWORD=$GUEST_PASSWORD STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh > /opt/stack/prepare_guest.log 2>&1 | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 80 | su -c "/opt/stack/run.sh > /opt/stack/run.sh.log 2>&1" stack | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 81 | exit 0 | 
 | 82 | EOF | 
 | 83 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 84 | # Configure the hostname | 
 | 85 | echo $GUEST_NAME > $STAGING_DIR/etc/hostname | 
 | 86 |  | 
 | 87 | # Hostname must resolve for rabbit | 
 | 88 | cat <<EOF >$STAGING_DIR/etc/hosts | 
 | 89 | $MGT_IP $GUEST_NAME | 
 | 90 | 127.0.0.1 localhost localhost.localdomain | 
 | 91 | EOF | 
 | 92 |  | 
 | 93 | # Configure the network | 
 | 94 | INTERFACES=$STAGING_DIR/etc/network/interfaces | 
 | 95 | cp $TEMPLATES_DIR/interfaces.in  $INTERFACES | 
| root | b115341 | 2012-01-19 13:28:21 -0800 | [diff] [blame] | 96 | if [ $VM_IP == "dhcp" ]; then | 
 | 97 |     echo 'eth1 on dhcp' | 
 | 98 |     sed -e "s,iface eth1 inet static,iface eth1 inet dhcp,g" -i $INTERFACES | 
 | 99 |     sed -e '/@ETH1_/d' -i $INTERFACES | 
 | 100 | else | 
 | 101 |     sed -e "s,@ETH1_IP@,$VM_IP,g" -i $INTERFACES | 
 | 102 |     sed -e "s,@ETH1_NETMASK@,$VM_NETMASK,g" -i $INTERFACES | 
 | 103 | fi | 
 | 104 |  | 
 | 105 | if [ $MGT_IP == "dhcp" ]; then | 
 | 106 |     echo 'eth2 on dhcp' | 
 | 107 |     sed -e "s,iface eth2 inet static,iface eth2 inet dhcp,g" -i $INTERFACES | 
 | 108 |     sed -e '/@ETH2_/d' -i $INTERFACES | 
 | 109 | else | 
 | 110 |     sed -e "s,@ETH2_IP@,$MGT_IP,g" -i $INTERFACES | 
 | 111 |     sed -e "s,@ETH2_NETMASK@,$MGT_NETMASK,g" -i $INTERFACES | 
 | 112 | fi | 
 | 113 |  | 
 | 114 | if [ $PUB_IP == "dhcp" ]; then | 
 | 115 |     echo 'eth3 on dhcp' | 
 | 116 |     sed -e "s,iface eth3 inet static,iface eth3 inet dhcp,g" -i $INTERFACES | 
 | 117 |     sed -e '/@ETH3_/d' -i $INTERFACES | 
 | 118 | else | 
 | 119 |     sed -e "s,@ETH3_IP@,$PUB_IP,g" -i $INTERFACES | 
 | 120 |     sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES | 
 | 121 | fi | 
 | 122 |  | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 123 | # Gracefully cp only if source file/dir exists | 
 | 124 | function cp_it { | 
 | 125 |     if [ -e $1 ] || [ -d $1 ]; then | 
 | 126 |         cp -pRL $1 $2 | 
 | 127 |     fi | 
 | 128 | } | 
 | 129 |  | 
 | 130 | # Copy over your ssh keys and env if desired | 
 | 131 | COPYENV=${COPYENV:-1} | 
 | 132 | if [ "$COPYENV" = "1" ]; then | 
 | 133 |     cp_it ~/.ssh $STAGING_DIR/opt/stack/.ssh | 
 | 134 |     cp_it ~/.ssh/id_rsa.pub $STAGING_DIR/opt/stack/.ssh/authorized_keys | 
 | 135 |     cp_it ~/.gitconfig $STAGING_DIR/opt/stack/.gitconfig | 
 | 136 |     cp_it ~/.vimrc $STAGING_DIR/opt/stack/.vimrc | 
 | 137 |     cp_it ~/.bashrc $STAGING_DIR/opt/stack/.bashrc | 
 | 138 | fi | 
 | 139 |  | 
 | 140 | # Configure run.sh | 
 | 141 | cat <<EOF >$STAGING_DIR/opt/stack/run.sh | 
 | 142 | #!/bin/bash | 
 | 143 | cd /opt/stack/devstack | 
 | 144 | killall screen | 
| Armando Migliaccio | 4f27a72 | 2012-04-13 12:33:49 +0100 | [diff] [blame] | 145 | UPLOAD_LEGACY_TTY=yes HOST_IP=$PUB_IP VIRT_DRIVER=xenserver FORCE=yes MULTI_HOST=$MULTI_HOST HOST_IP_IFACE=$HOST_IP_IFACE $STACKSH_PARAMS ./stack.sh | 
| Anthony Young | 1188904 | 2012-01-12 17:11:56 -0800 | [diff] [blame] | 146 | EOF | 
 | 147 | chmod 755 $STAGING_DIR/opt/stack/run.sh | 
 | 148 |  | 
| Renuka Apte | 0af143b | 2012-04-02 15:46:53 -0700 | [diff] [blame] | 149 | echo "Done" |