Apex: Allow for multi-version snapshots
[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 echo "Detecting requested OpenStack branch and topology type in gerrit comment"
29 parsed_comment=$(echo $GERRIT_EVENT_COMMENT_TEXT | sed -n 's/^opnfv-check\s*//p')
30 parsed_comment=$(echo $parsed_comment | sed -n 's/\s*$//p')
31 if [ ! -z "$parsed_comment" ]; then
32   if echo $parsed_comment | grep -E '^[a-z]+-(no)?ha'; then
33     IFS='-' read -r -a array <<< "$parsed_comment"
34     os_version=${array[0]}
35     topo=${array[1]}
36     echo "OS version detected in gerrit comment: ${os_version}"
37     echo "Topology type detected in gerrit comment: ${topo}"
38   else
39     echo "Invalid format given for scenario in gerrit comment: ${parsed_comment}...aborting"
40     exit 1
41   fi
42 else
43   echo "No scenario given in gerrit comment, will use default (master OpenStack, noha)"
44   os_version='master'
45   topo='noha'
46 fi
47
48 full_snap_url=http://$GS_URL/${os_version}/${topo}
49
50 echo "Downloading latest snapshot properties file"
51 if ! wget -O $WORKSPACE/opnfv.properties ${full_snap_url}/snapshot.properties; then
52   echo "ERROR: Unable to find snapshot.properties at ${full_snap_url}...exiting"
53   exit 1
54 fi
55
56 # find latest check sum
57 latest_snap_checksum=$(cat opnfv.properties | grep OPNFV_SNAP_SHA512SUM | awk -F "=" '{print $2}')
58 if [ -z "$latest_snap_checksum" ]; then
59   echo "ERROR: checksum of latest snapshot from snapshot.properties is null!"
60   exit 1
61 fi
62
63 local_snap_checksum=""
64 SNAP_CACHE=${SNAP_CACHE}/${os_version}/${topo}
65
66 # check snap cache directory exists
67 # if snapshot cache exists, find the checksum
68 if [ -d "$SNAP_CACHE" ]; then
69   latest_snap=$(ls ${SNAP_CACHE} | grep tar.gz | tail -n 1)
70   if [ -n "$latest_snap" ]; then
71     local_snap_checksum=$(sha512sum ${SNAP_CACHE}/${latest_snap} | cut -d' ' -f1)
72   fi
73 else
74   mkdir -p ${SNAP_CACHE}
75 fi
76
77 # compare check sum and download latest snap if not up to date
78 if [ "$local_snap_checksum" != "$latest_snap_checksum" ]; then
79   snap_url=$(cat opnfv.properties | grep OPNFV_SNAP_URL | awk -F "=" '{print $2}')
80   if [ -z "$snap_url" ]; then
81     echo "ERROR: Snap URL from snapshot.properties is null!"
82     exit 1
83   fi
84   echo "INFO: SHA mismatch, will download latest snapshot"
85   # wipe cache
86   rm -rf ${SNAP_CACHE}/*
87   wget --directory-prefix=${SNAP_CACHE}/ ${snap_url}
88   snap_tar=$(basename ${snap_url})
89 else
90   snap_tar=${latest_snap}
91 fi
92
93 echo "INFO: Snapshot to be used is ${snap_tar}"
94
95 # move to snap cache dir and unpack
96 pushd ${SNAP_CACHE} > /dev/null
97 tar xvf ${snap_tar}
98
99 # create each network
100 virsh_networks=$(ls *.xml | grep -v baremetal)
101
102 if [ -z "$virsh_networks" ]; then
103   echo "ERROR: no virsh networks found in snapshot unpack"
104   exit 1
105 fi
106
107 echo "Checking overcloudrc"
108 if ! stat overcloudrc; then
109   echo "ERROR: overcloudrc does not exist in snap unpack"
110   exit 1
111 fi
112
113 for network_def in ${virsh_networks}; do
114   sudo virsh net-create ${network_def}
115   network=$(echo ${network_def} | awk -F '.' '{print $1}')
116   if ! sudo virsh net-list | grep ${network}; then
117     sudo virsh net-start ${network}
118   fi
119   echo "Checking if OVS bridge is missing for network: ${network}"
120   if ! sudo ovs-vsctl show | grep "br-${network}"; then
121     sudo ovs-vsctl add-br br-${network}
122     echo "OVS Bridge created: br-${network}"
123     if [ "br-${network}" == 'br-admin' ]; then
124       echo "Configuring IP 192.0.2.99 on br-admin"
125       sudo ip addr add  192.0.2.99/24 dev br-admin
126       sudo ip link set up dev br-admin
127     elif [ "br-${network}" == 'br-external' ]; then
128       echo "Configuring IP 192.168.37.1 on br-external"
129       sudo ip addr add  192.168.37.1/24 dev br-external
130       sudo ip link set up dev br-external
131       # Routes for admin network
132       # The overcloud controller is multi-homed and will fail to respond
133       # to traffic from the functest container due to reverse-path-filtering
134       # This route allows reverse traffic, by forcing admin network destined
135       # traffic through the external network for controller IPs only.
136       # Compute nodes have no ip on external interfaces.
137       controller_ips=$(cat overcloudrc | grep -Eo "192.0.2.[0-9]+")
138       for ip in $controller_ips; do
139         sudo ip route add ${ip}/32 dev br-external
140       done
141     fi
142   fi
143 done
144
145 echo "Virsh networks up: $(sudo virsh net-list)"
146 echo "Bringing up Overcloud VMs..."
147 virsh_vm_defs=$(ls baremetal*.xml)
148
149 if [ -z "$virsh_vm_defs" ]; then
150   echo "ERROR: no virsh VMs found in snapshot unpack"
151   exit 1
152 fi
153
154 for node_def in ${virsh_vm_defs}; do
155   sed  -ri "s/machine='[^\s]+'/machine='pc'/" ${node_def}
156   sudo virsh define ${node_def}
157   node=$(echo ${node_def} | awk -F '.' '{print $1}')
158   sudo cp -f ${node}.qcow2 /var/lib/libvirt/images/
159   # FIXME (trozet) install java on each disk image as required to upgrade ODL
160   # should be added to Apex as part of the deployment. Remove this after that
161   # is complete
162   sudo LIBGUESTFS_BACKEND=direct virt-customize --install java-1.8.0-openjdk -a /var/lib/libvirt/images/${node}.qcow2
163   sudo virsh start ${node}
164   echo "Node: ${node} started"
165 done
166
167 # copy overcloudrc for functest
168 mkdir -p $HOME/cloner-info
169 cp -f overcloudrc $HOME/cloner-info/
170
171 admin_controller_ip=$(cat overcloudrc | grep -Eo -m 1 "192.0.2.[0-9]+" | head -1)
172 netvirt_url="http://${admin_controller_ip}:8081/restconf/operational/network-topology:network-topology/topology/netvirt:1"
173
174 source overcloudrc
175 counter=1
176 while [ "$counter" -le 10 ]; do
177   echo "Checking if OpenStack is up"
178   if nc -z ${admin_controller_ip} 9696 > /dev/null; then
179     echo "Overcloud Neutron is up...Checking if OpenDaylight NetVirt is up..."
180     if curl --fail --silent -u admin:admin ${netvirt_url} > /dev/null; then
181       echo "OpenDaylight is up.  Overcloud deployment complete"
182       exit 0
183     else
184       echo "OpenDaylight not yet up, try ${counter}"
185     fi
186   else
187     echo "Neutron not yet up, try ${counter}"
188   fi
189   counter=$((counter+1))
190   sleep 60
191 done
192
193 echo "ERROR: Deployment not up after 10 minutes...exiting."
194 exit 1