dcae0c14d587c7a959b5fd2ead69caba558f5d30
[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 CONFIG=../build/ LIB=../lib ./clean.sh
26 popd > /dev/null
27
28 echo "Downloading latest snapshot properties file"
29 if ! wget -O $WORKSPACE/opnfv.properties http://$GS_URL/snapshot.properties; then
30   echo "ERROR: Unable to find snapshot.properties at ${GS_URL}...exiting"
31   exit 1
32 fi
33
34 # find latest check sum
35 latest_snap_checksum=$(cat opnfv.properties | grep OPNFV_SNAP_SHA512SUM | awk -F "=" '{print $2}')
36 if [ -z "$latest_snap_checksum" ]; then
37   echo "ERROR: checksum of latest snapshot from snapshot.properties is null!"
38   exit 1
39 fi
40
41 local_snap_checksum=""
42
43 # check snap cache directory exists
44 # if snapshot cache exists, find the checksum
45 if [ -d "$SNAP_CACHE" ]; then
46   latest_snap=$(ls ${SNAP_CACHE} | grep tar.gz | tail -n 1)
47   if [ -n "$latest_snap" ]; then
48     local_snap_checksum=$(sha512sum ${SNAP_CACHE}/${latest_snap} | cut -d' ' -f1)
49   fi
50 else
51   mkdir -p ${SNAP_CACHE}
52 fi
53
54 # compare check sum and download latest snap if not up to date
55 if [ "$local_snap_checksum" != "$latest_snap_checksum" ]; then
56   snap_url=$(cat opnfv.properties | grep OPNFV_SNAP_URL | awk -F "=" '{print $2}')
57   if [ -z "$snap_url" ]; then
58     echo "ERROR: Snap URL from snapshot.properties is null!"
59     exit 1
60   fi
61   echo "INFO: SHA mismatch, will download latest snapshot"
62   # wipe cache
63   rm -rf ${SNAP_CACHE}/*
64   wget --directory-prefix=${SNAP_CACHE}/ ${snap_url}
65   snap_tar=$(basename ${snap_url})
66 else
67   snap_tar=${latest_snap}
68 fi
69
70 echo "INFO: Snapshot to be used is ${snap_tar}"
71
72 # move to snap cache dir and unpack
73 pushd ${SNAP_CACHE} > /dev/null
74 tar xvf ${snap_tar}
75
76 # create each network
77 virsh_networks=$(ls *.xml | grep -v baremetal)
78
79 if [ -z "$virsh_networks" ]; then
80   echo "ERROR: no virsh networks found in snapshot unpack"
81   exit 1
82 fi
83
84 for network_def in ${virsh_networks}; do
85   sudo virsh net-create ${network_def}
86   network=$(echo ${network_def} | awk -F '.' '{print $1}')
87   if ! sudo virsh net-list | grep ${network}; then
88     sudo virsh net-start ${network}
89   fi
90   echo "Checking if OVS bridge is missing for network: ${network}"
91   if ! sudo ovs-vsctl show | grep "br-${network}"; then
92     sudo ovs-vsctl add-br br-${network}
93     echo "OVS Bridge created: br-${network}"
94     if [ "br-${network}" == 'br-admin' ]; then
95       echo "Configuring IP 192.0.2.99 on br-admin"
96       sudo ip addr add  192.0.2.99/24 dev br-admin
97       sudo ip link set up dev br-admin
98     elif [ "br-${network}" == 'br-external' ]; then
99       echo "Configuring IP 192.168.37.1 on br-external"
100       sudo ip addr add  192.168.37.1/24 dev br-external
101       sudo ip link set up dev br-external
102       # Route for admin network
103       # The overcloud controller is multi-homed and will fail to respond
104       # to traffic from the functest container due to reverse-path-filtering
105       # This route allows reverse traffic, by forcing admin network destined
106       # traffic through the external network
107       sudo ip route add 192.0.2.0/25 dev br-external
108     fi
109   fi
110 done
111
112 echo "Virsh networks up: $(sudo virsh net-list)"
113 echo "Bringing up Overcloud VMs..."
114 virsh_vm_defs=$(ls baremetal*.xml)
115
116 if [ -z "$virsh_vm_defs" ]; then
117   echo "ERROR: no virsh VMs found in snapshot unpack"
118   exit 1
119 fi
120
121 for node_def in ${virsh_vm_defs}; do
122   sudo virsh define ${node_def}
123   node=$(echo ${node_def} | awk -F '.' '{print $1}')
124   sudo cp -f ${node}.qcow2 /var/lib/libvirt/images/
125   sudo virsh start ${node}
126   echo "Node: ${node} started"
127 done
128
129 echo "Checking overcloudrc"
130 if ! stat overcloudrc; then
131   echo "ERROR: overcloudrc does not exist in snap unpack"
132   exit 1
133 fi
134
135 # copy overcloudrc for functest
136 mkdir -p $HOME/cloner-info
137 cp -f overcloudrc $HOME/cloner-info/
138
139 admin_controller_ip=$(cat overcloudrc | grep -Eo "192.0.2.[0-9]+")
140 netvirt_url="http://${admin_controller_ip}:8081/restconf/operational/network-topology:network-topology/topology/netvirt:1"
141
142 source overcloudrc
143 counter=1
144 while [ "$counter" -le 10 ]; do
145   if curl --fail --silent ${admin_controller_ip}:80 > /dev/null; then
146     echo "Overcloud Horizon is up...Checking if OpenDaylight NetVirt is up..."
147     if curl --fail --silent -u admin:admin ${netvirt_url} > /dev/null; then
148       echo "OpenDaylight is up.  Overcloud deployment complete"
149       exit 0
150     else
151       echo "OpenDaylight not yet up, try ${counter}"
152     fi
153   else
154     echo "Horizon/Apache not yet up, try ${counter}"
155   fi
156   counter=$((counter+1))
157   sleep 60
158 done
159
160 echo "ERROR: Deployment not up after 10 minutes...exiting."
161 exit 1