Fix ShellCheck issues in jjb/*.sh
[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 ADDR=$(openstack --os-cloud rackspace stack show -f json -c outputs "$STACK_NAME" | \
36        jq -r '.outputs[] |
37               select(.output_key | match("^vm_[0-9]+_ips\$")) |
38               .output_value | .[]')
39 pids=""
40 for i in "${ADDR[@]}"; do
41     ( copy-ssh-keys-to-slave ) &
42     # Store PID of process
43     pids+=" $!"
44 done
45
46 # Detect when a process failed to copy ssh keys and fail build
47 for p in $pids; do
48     if wait "$p"; then
49         echo "Process $p successfully copied ssh keys."
50     else
51         echo "Process $p failed to copy ssh keys."
52         exit 1
53     fi
54 done
55 echo "Copying ssh keys complete."