Chore: Upgrade Jenkins-job-builder to 6.3.0
[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 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 # shellcheck disable=SC1090
54 source ~/lf-env.sh
55
56 lf-activate-venv --python python3 "lftools[openstack]" \
57     kubernetes \
58     python-heatclient \
59     python-openstackclient
60
61 # IP Addresses are returned as a space separated list so word splitting is ok
62 # shellcheck disable=SC2207
63 ip_addresses=($(openstack --os-cloud "$os_cloud" stack show -f json -c outputs "$stack_name" |
64         jq -r '.outputs[] |
65                 select(.output_key | match("^vm_[0-9]+_ips$")) |
66                 .output_value | .[]'))
67 pids=""
68 for ip in "${ip_addresses[@]}"; do
69     ( copy_ssh_keys "$ip" ) &
70     # Store PID of process
71     pids+=" $!"
72 done
73 for p in $pids; do
74     if wait "$p"; then
75         echo "Process $p ready."
76     else
77         echo "Process $p timed out waiting for SSH."
78         exit 1
79     fi
80 done
81 echo "SSH ready on all stack servers."