Fix ssh-copy-keys script
[releng/builder.git] / jjb / opendaylight-infra-copy-ssh-keys.sh
1 #!/bin/bash
2 echo "----------> Copy ssh public keys to csit lab"
3
4 # shellcheck disable=SC1090
5 source "$WORKSPACE/.venv-openstack/bin/activate"
6
7 function copy-ssh-keys-to-slave() {
8     RETRIES=60
9     for j in $(seq 1 $RETRIES); do
10         # shellcheck disable=SC2046
11         if [ $(ssh-copy-id -i /home/jenkins/.ssh/id_rsa.pub "jenkins@${i}" > /dev/null 2>&1) -eq 0 ]; then
12             ssh "jenkins@${i}" 'echo "$(facter ipaddress_eth0) $(/bin/hostname)" | sudo tee -a /etc/hosts'
13             echo "Successfully copied public keys to slave ${i}"
14             break
15         elif [ "$j" -eq $RETRIES ]; then
16             echo "SSH not responding on ${i} after $RETIRES tries. Giving up."
17             exit 1
18         else
19             echo "SSH not responding on ${i}. Retrying in 10 seconds..."
20             sleep 10
21         fi
22
23         # ping test to see if connectivity is available
24         if ping -c1 "${i}" &> /dev/null; then
25             echo "Ping to ${i} successful."
26         else
27             echo "Ping to ${i} failed."
28         fi
29     done
30 }
31
32 # Print the Stack outputs parameters so that we can identify which IPs belong
33 # to which VM types.
34 openstack --os-cloud rackspace stack show -c outputs "$STACK_NAME"
35
36 # shellcheck disable=SC2006
37 ADDR=(`openstack --os-cloud rackspace stack show -f json -c outputs "$STACK_NAME" | \
38        jq -r '.outputs[] |
39               select(.output_key | match("^vm_[0-9]+_ips\$")) |
40               .output_value | .[]'`)
41 pids=""
42 for i in "${ADDR[@]}"; do
43     ( copy-ssh-keys-to-slave ) &
44     # Store PID of process
45     pids+=" $!"
46 done
47
48 # Detect when a process failed to copy ssh keys and fail build
49 for p in $pids; do
50     if wait "$p"; then
51         echo "Process $p successfully copied ssh keys."
52     else
53         echo "Process $p failed to copy ssh keys."
54         exit 1
55     fi
56 done
57 echo "Copying ssh keys complete."