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