Run ODL CSIT on Apex from Cperf container
[releng.git] / jjb / apex / apex-snapshot-deploy.sh
1 #!/usr/bin/env bash
2 ##############################################################################
3 # Copyright (c) 2016 Tim Rozet (Red Hat) and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set -o errexit
12 set -o nounset
13 set -o pipefail
14
15 SSH_OPTIONS=(-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o LogLevel=error)
16 SNAP_CACHE=$HOME/snap_cache
17
18
19 echo "Deploying Apex snapshot..."
20 echo "--------------------------"
21 echo
22
23 echo "Cleaning server"
24 pushd ci > /dev/null
25 sudo opnfv-clean
26 popd > /dev/null
27
28 full_snap_url=http://$GS_URL/${OS_VERSION}/${TOPOLOGY}
29
30 echo "Downloading latest snapshot properties file"
31 if ! wget -O $WORKSPACE/opnfv.properties ${full_snap_url}/snapshot.properties; then
32   echo "ERROR: Unable to find snapshot.properties at ${full_snap_url}...exiting"
33   exit 1
34 fi
35
36 # find latest check sum
37 latest_snap_checksum=$(cat opnfv.properties | grep OPNFV_SNAP_SHA512SUM | awk -F "=" '{print $2}')
38 if [ -z "$latest_snap_checksum" ]; then
39   echo "ERROR: checksum of latest snapshot from snapshot.properties is null!"
40   exit 1
41 fi
42
43 local_snap_checksum=""
44 SNAP_CACHE=${SNAP_CACHE}/${OS_VERSION}/${TOPOLOGY}
45
46 # check snap cache directory exists
47 # if snapshot cache exists, find the checksum
48 if [ -d "$SNAP_CACHE" ]; then
49   latest_snap=$(ls ${SNAP_CACHE} | grep tar.gz | tail -n 1)
50   if [ -n "$latest_snap" ]; then
51     local_snap_checksum=$(sha512sum ${SNAP_CACHE}/${latest_snap} | cut -d' ' -f1)
52   fi
53 else
54   mkdir -p ${SNAP_CACHE}
55 fi
56
57 # compare check sum and download latest snap if not up to date
58 if [ "$local_snap_checksum" != "$latest_snap_checksum" ]; then
59   snap_url=$(cat opnfv.properties | grep OPNFV_SNAP_URL | awk -F "=" '{print $2}')
60   if [ -z "$snap_url" ]; then
61     echo "ERROR: Snap URL from snapshot.properties is null!"
62     exit 1
63   fi
64   echo "INFO: SHA mismatch, will download latest snapshot"
65   # wipe cache
66   rm -rf ${SNAP_CACHE}/*
67   wget --directory-prefix=${SNAP_CACHE}/ ${snap_url}
68   snap_tar=$(basename ${snap_url})
69 else
70   snap_tar=${latest_snap}
71 fi
72
73 echo "INFO: Snapshot to be used is ${snap_tar}"
74
75 # move to snap cache dir and unpack
76 pushd ${SNAP_CACHE} > /dev/null
77 tar xvf ${snap_tar}
78
79 # create each network
80 virsh_networks=$(ls *.xml | grep -v baremetal)
81
82 if [ -z "$virsh_networks" ]; then
83   echo "ERROR: no virsh networks found in snapshot unpack"
84   exit 1
85 fi
86
87 echo "Checking overcloudrc"
88 if ! stat overcloudrc; then
89   echo "ERROR: overcloudrc does not exist in snap unpack"
90   exit 1
91 fi
92
93 for network_def in ${virsh_networks}; do
94   sudo virsh net-create ${network_def}
95   network=$(echo ${network_def} | awk -F '.' '{print $1}')
96   if ! sudo virsh net-list | grep ${network}; then
97     sudo virsh net-start ${network}
98   fi
99   echo "Checking if OVS bridge is missing for network: ${network}"
100   if ! sudo ovs-vsctl show | grep "br-${network}"; then
101     sudo ovs-vsctl add-br br-${network}
102     echo "OVS Bridge created: br-${network}"
103     if [ "br-${network}" == 'br-admin' ]; then
104       echo "Configuring IP 192.0.2.99 on br-admin"
105       sudo ip addr add  192.0.2.99/24 dev br-admin
106       sudo ip link set up dev br-admin
107     elif [ "br-${network}" == 'br-external' ]; then
108       echo "Configuring IP 192.168.37.1 on br-external"
109       sudo ip addr add  192.168.37.1/24 dev br-external
110       sudo ip link set up dev br-external
111       # Routes for admin network
112       # The overcloud controller is multi-homed and will fail to respond
113       # to traffic from the functest container due to reverse-path-filtering
114       # This route allows reverse traffic, by forcing admin network destined
115       # traffic through the external network for controller IPs only.
116       # Compute nodes have no ip on external interfaces.
117       controller_ips=$(cat overcloudrc | grep -Eo "192.0.2.[0-9]+")
118       for ip in $controller_ips; do
119         sudo ip route add ${ip}/32 dev br-external
120       done
121     fi
122   fi
123 done
124
125 echo "Virsh networks up: $(sudo virsh net-list)"
126 echo "Bringing up Overcloud VMs..."
127 virsh_vm_defs=$(ls baremetal*.xml)
128
129 if [ -z "$virsh_vm_defs" ]; then
130   echo "ERROR: no virsh VMs found in snapshot unpack"
131   exit 1
132 fi
133
134 for node_def in ${virsh_vm_defs}; do
135   sed  -ri "s/machine='[^\s]+'/machine='pc'/" ${node_def}
136   sudo virsh define ${node_def}
137   node=$(echo ${node_def} | awk -F '.' '{print $1}')
138   sudo cp -f ${node}.qcow2 /var/lib/libvirt/images/
139   # FIXME (trozet) install java on each disk image as required to upgrade ODL
140   # should be added to Apex as part of the deployment. Remove this after that
141   # is complete
142   sudo LIBGUESTFS_BACKEND=direct virt-customize --install java-1.8.0-openjdk -a /var/lib/libvirt/images/${node}.qcow2
143   sudo virsh start ${node}
144   echo "Node: ${node} started"
145 done
146
147 # copy overcloudrc for functest
148 mkdir -p $HOME/cloner-info
149 cp -f overcloudrc $HOME/cloner-info/
150
151 admin_controller_ip=$(cat overcloudrc | grep -Eo -m 1 "192.0.2.[0-9]+" | head -1)
152 netvirt_url="http://${admin_controller_ip}:8081/restconf/operational/network-topology:network-topology/topology/netvirt:1"
153
154 source overcloudrc
155 counter=1
156 while [ "$counter" -le 10 ]; do
157   echo "Checking if OpenStack is up"
158   if nc -z ${admin_controller_ip} 9696 > /dev/null; then
159     echo "Overcloud Neutron is up...Checking if OpenDaylight NetVirt is up..."
160     if curl --fail --silent -u admin:admin ${netvirt_url} > /dev/null; then
161       echo "OpenDaylight is up.  Overcloud deployment complete"
162       exit 0
163     else
164       echo "OpenDaylight not yet up, try ${counter}"
165     fi
166   else
167     echo "Neutron not yet up, try ${counter}"
168   fi
169   counter=$((counter+1))
170   sleep 60
171 done
172
173 echo "ERROR: Deployment not up after 10 minutes...exiting."
174 exit 1