Merge "Fix: Use lf-activate-venv to install openstack dep"
[releng/builder.git] / jjb / opendaylight-infra-copy-ssh-keys.sh
1 #!/bin/bash -l
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 function copy-ssh-keys-to-slave() {
15     RETRIES=60
16     for j in $(seq 1 $RETRIES); do
17         # shellcheck disable=SC2006,SC2092
18         if `ssh-copy-id -i /home/jenkins/.ssh/id_rsa.pub "jenkins@${i}" > /dev/null 2>&1`; then
19             ssh "jenkins@${i}" 'echo "$(facter ipaddress_eth0) $(/bin/hostname)" | sudo tee -a /etc/hosts'
20             echo "Successfully copied public keys to slave ${i}"
21             break
22         elif [ "$j" -eq $RETRIES ]; then
23             echo "SSH not responding on ${i} after $RETIRES tries. Giving up."
24
25             server=$(openstack port list -f value -c device_id --fixed-ip ip-address="${i}")
26             echo "Dumping console logs for $server ${i}"
27             openstack console log show "$server"
28
29             exit 1
30         else
31             echo "SSH not responding on ${i}. Retrying in 10 seconds..."
32             sleep 10
33         fi
34
35         # ping test to see if connectivity is available
36         if ping -c1 "${i}" &> /dev/null; then
37             echo "Ping to ${i} successful."
38         else
39             echo "Ping to ${i} failed."
40         fi
41     done
42 }
43
44 # shellcheck disable=SC1090
45 . ~/lf-env.sh
46
47
48 # Check if openstack venv was previously created
49 if [ -f "/tmp/.os_lf_venv" ]; then
50     os_lf_venv=$(cat "/tmp/.os_lf_venv")
51 fi
52
53 if [ -d "${os_lf_venv}" ] && [ -f "${os_lf_venv}/bin/openstack" ]; then
54     echo "Re-use existing venv: ${os_lf_venv}"
55     PATH=$os_lf_venv/bin:$PATH
56 else
57     lf-activate-venv --python python3 \
58         python-heatclient \
59         python-openstackclient \
60         yq
61 fi
62
63 # Print the Stack outputs parameters so that we can identify which IPs belong
64 # to which VM types.
65 openstack stack show -c outputs "$STACK_NAME"
66
67 # shellcheck disable=SC2006,SC2207
68 ADDR=(`openstack stack show -f json -c outputs "$STACK_NAME" | \
69        jq -r '.outputs[] | \
70               select(.output_key | match("^vm_[0-9]+_ips\$")) | \
71               .output_value | .[]'`)
72 pids=""
73 for i in "${ADDR[@]}"; do
74     ( copy-ssh-keys-to-slave ) &
75     # Store PID of process
76     pids+=" $!"
77 done
78
79 # Detect when a process failed to copy ssh keys and fail build
80 for p in $pids; do
81     if wait "$p"; then
82         echo "Process $p successfully copied ssh keys."
83     else
84         echo "Process $p failed to copy ssh keys."
85         exit 1
86     fi
87 done
88 echo "Copying ssh keys complete."