Dump server console logs on failure
[releng/builder.git] / jjb / opendaylight-infra-copy-ssh-keys.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11
12 echo "----------> Copy ssh public keys to csit lab"
13
14 # shellcheck source=/tmp/v/openstack/bin/activate disable=SC1091
15 source "/tmp/v/openstack/bin/activate"
16
17 function copy-ssh-keys-to-slave() {
18     RETRIES=60
19     for j in $(seq 1 $RETRIES); do
20         # shellcheck disable=SC2006,SC2092
21         if `ssh-copy-id -i /home/jenkins/.ssh/id_rsa.pub "jenkins@${i}" > /dev/null 2>&1`; then
22             ssh "jenkins@${i}" 'echo "$(facter ipaddress_eth0) $(/bin/hostname)" | sudo tee -a /etc/hosts'
23             echo "Successfully copied public keys to slave ${i}"
24             break
25         elif [ "$j" -eq $RETRIES ]; then
26             echo "SSH not responding on ${i} after $RETIRES tries. Giving up."
27
28             server=$(openstack port list -f value -c device_id --fixed-ip ip-address="${i}")
29             echo "Dumping console logs for $server ${i}"
30             openstack console log show "$server"
31
32             exit 1
33         else
34             echo "SSH not responding on ${i}. Retrying in 10 seconds..."
35             sleep 10
36         fi
37
38         # ping test to see if connectivity is available
39         if ping -c1 "${i}" &> /dev/null; then
40             echo "Ping to ${i} successful."
41         else
42             echo "Ping to ${i} failed."
43         fi
44     done
45 }
46
47 # Print the Stack outputs parameters so that we can identify which IPs belong
48 # to which VM types.
49 openstack stack show -c outputs "$STACK_NAME"
50
51 # shellcheck disable=SC2006,SC2207
52 ADDR=(`openstack stack show -f json -c outputs "$STACK_NAME" | \
53        jq -r '.outputs[] | \
54               select(.output_key | match("^vm_[0-9]+_ips\$")) | \
55               .output_value | .[]'`)
56 pids=""
57 for i in "${ADDR[@]}"; do
58     ( copy-ssh-keys-to-slave ) &
59     # Store PID of process
60     pids+=" $!"
61 done
62
63 # Detect when a process failed to copy ssh keys and fail build
64 for p in $pids; do
65     if wait "$p"; then
66         echo "Process $p successfully copied ssh keys."
67     else
68         echo "Process $p failed to copy ssh keys."
69         exit 1
70     fi
71 done
72 echo "Copying ssh keys complete."