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