Fix OS_CLOUD export for image validation
[releng/global-jjb.git] / shell / openstack-stack-copy-ssh-keys.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017, 2018 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 echo "---> Copy SSH public keys to CSIT lab"
12
13 os_cloud="${OS_CLOUD:-vex}"
14 stack_name="${OS_STACK_NAME}"
15
16 function copy_ssh_keys() {
17     if [ -z "$1" ]; then
18         >&2 echo "ERROR: Missing required arguments."
19         >&2 echo "Usage: copy_ssh_keys IP_ADDRESS"
20         exit 1
21     fi
22
23     local ip_address="$1"
24     RETRIES=60
25
26     for i in $(seq 1 $RETRIES); do
27         if ssh-copy-id -i /home/jenkins/.ssh/id_rsa.pub "jenkins@${ip_address}" > /dev/null 2>&1; then
28             ssh "jenkins@${ip_address}" 'echo "$(facter ipaddress_eth0) $(/bin/hostname)" | sudo tee -a /etc/hosts'
29             echo "Successfully copied public keys to slave ${ip_address}"
30             break
31         elif [ "$i" -eq $RETRIES ]; then
32             echo "SSH not responding on ${ip_address} after $RETIRES tries. Giving up."
33
34             server=$(openstack port list -f value -c device_id --fixed-ip ip-address="${ip_address}")
35             echo "Dumping console logs for $server ${ip_address}"
36             openstack --os-cloud "$os_cloud" console log show "$server"
37
38             exit 1
39         else
40             echo "SSH not responding on ${ip_address}. Retrying in 10 seconds..."
41             sleep 10
42         fi
43
44         # ping test to see if connectivity is available
45         if ping -c1 "${ip_address}" &> /dev/null; then
46             echo "Ping to ${ip_address} successful."
47         else
48             echo "Ping to ${ip_address} failed."
49         fi
50     done
51 }
52
53 # IP Addresses are returned as a space separated list so word splitting is ok
54 # shellcheck disable=SC2207
55 ip_addresses=($(openstack --os-cloud "$os_cloud" stack show -f json -c outputs "$stack_name" |
56        jq -r '.outputs[] |
57               select(.output_key | match("^vm_[0-9]+_ips$")) |
58               .output_value | .[]'))
59 pids=""
60 for ip in "${ip_addresses[@]}"; do
61     ( copy_ssh_keys "$ip" ) &
62     # Store PID of process
63     pids+=" $!"
64 done
65 for p in $pids; do
66     if wait "$p"; then
67         echo "Process $p ready."
68     else
69         echo "Process $p timed out waiting for SSH."
70         exit 1
71     fi
72 done
73 echo "SSH ready on all stack servers."