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