b739314706f306d169d56a067d9ac41596f17e39
[releng.git] / jjb / apex / apex-snapshot-create.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
17 if [ -z "$SNAP_TYPE" ]; then
18   echo "ERROR: SNAP_TYPE not provided...exiting"
19   exit 1
20 fi
21
22 echo "Creating Apex snapshot..."
23 echo "-------------------------"
24 echo
25
26 # create tmp directory
27 tmp_dir=$(pwd)/.tmp
28 mkdir -p ${tmp_dir}
29
30 # TODO(trozet) remove this after fix goes in for tripleo_inspector to copy these
31 pushd ${tmp_dir} > /dev/null
32 echo "Copying overcloudrc and ssh key from Undercloud..."
33 # Store overcloudrc
34 UNDERCLOUD=$(sudo virsh domifaddr undercloud | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+')
35 sudo scp ${SSH_OPTIONS[@]} stack@${UNDERCLOUD}:overcloudrc ./
36 # Copy out ssh key of stack from undercloud
37 sudo scp ${SSH_OPTIONS[@]} stack@${UNDERCLOUD}:.ssh/id_rsa ./
38 popd > /dev/null
39
40 echo "Gathering introspection information"
41 git clone https://gerrit.opnfv.org/gerrit/sdnvpn.git
42 pushd sdnvpn/odl-pipeline/lib > /dev/null
43 sudo ./tripleo_introspector.sh --out-file ${tmp_dir}/node.yaml
44 popd > /dev/null
45 sudo rm -rf sdnvpn
46
47 echo "Shutting down nodes"
48 # Shut down nodes
49 nodes=$(sudo virsh list | grep -Eo "baremetal[0-9]")
50 for node in $nodes; do
51   sudo virsh shutdown ${node} --mode acpi
52 done
53
54 for node in $nodes; do
55   count=0
56   while [ "$count" -lt 10 ]; do
57     sleep 10
58     if sudo virsh list | grep ${node}; then
59        echo "Waiting for $node to shutdown, try $count"
60     else
61        break
62     fi
63     count=$((count+1))
64   done
65
66   if [ "$count" -ge 10 ]; then
67     echo "Node $node failed to shutdown"
68     exit 1
69   fi
70 done
71
72 pushd ${tmp_dir} > /dev/null
73 echo "Gathering virsh definitions"
74 # copy qcow2s, virsh definitions
75 for node in $nodes; do
76   sudo cp -f /var/lib/libvirt/images/${node}.qcow2 ./
77   sudo virsh dumpxml ${node} > ${node}.xml
78 done
79
80 # copy virsh net definitions
81 sudo virsh net-dumpxml admin > admin.xml
82
83 sudo chown jenkins-ci:jenkins-ci *
84
85 # tar up artifacts
86 DATE=`date +%Y-%m-%d`
87 tar czf ../apex-${SNAP_TYPE}-snap-${DATE}.tar.gz .
88 popd > /dev/null
89 sudo rm -rf ${tmp_dir}
90 echo "Snapshot saved as apex-${SNAP_TYPE}-snap-${DATE}.tar.gz"
91
92 # update opnfv properties file
93 if [ "$SNAP_TYPE" == 'csit' ]; then
94   snap_sha=$(sha512sum apex-csit-snap-${DATE}.tar.gz | cut -d' ' -f1)
95   if curl --fail -O -L http://$GS_URL/snapshot.properties; then
96     sed -i '/^OPNFV_SNAP_URL=/{h;s#=.*#='${GS_URL}'/apex-csit-snap-'${DATE}'.tar.gz#};${x;/^$/{s##OPNFV_SNAP_URL='${GS_URL}'/apex-csit-snap-'${DATE}'.tar.gz#;H};x}' snapshot.properties
97     sed -i '/^OPNFV_SNAP_SHA512SUM=/{h;s/=.*/='${snap_sha}'/};${x;/^$/{s//OPNFV_SNAP_SHA512SUM='${snap_sha}'/;H};x}' snapshot.properties
98   else
99     cat << EOF > snapshot.properties
100 OPNFV_SNAP_URL=${GS_URL}/apex-csit-snap-${DATE}.tar.gz
101 OPNFV_SNAP_SHA512SUM=${snap_sha}
102 EOF
103   fi
104   echo "OPNFV_SNAP_URL=$GS_URL/apex-csit-snap-${DATE}.tar.gz"
105   echo "OPNFV_SNAP_SHA512SUM=$(sha512sum apex-csit-snap-${DATE}.tar.gz | cut -d' ' -f1)"
106   echo "Updated properties file: "
107   cat snapshot.properties
108 fi