Jeepyb User | dd14e0b | 2017-02-28 15:19:32 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Function used to return list of node names according |
| 4 | # to given string parameter match criteria |
| 5 | function get_nodes_names { |
| 6 | # Enforce 1st parameter availability |
| 7 | if [ -z "$1" ]; then |
| 8 | match="[0-9]" |
| 9 | else |
| 10 | match="$1" |
| 11 | fi |
| 12 | salt-call pillar.get linux:network:host --out key | sed 's/:.*//' | grep "$match" |
| 13 | } |
| 14 | |
| 15 | # Function used to wait for node availability |
| 16 | # (aka answering to salt pings) |
| 17 | # 1st parameter (mandatory) is number of nodes to wait for |
| 18 | # 2nd parameter (optional) is nodes names to wait for |
| 19 | # (* = all nodes is default) |
| 20 | function wait_for { |
| 21 | # Enforce 1st parameter availability |
| 22 | if [ -z "$1" ]; then |
| 23 | echo "wait_for function requires at least 1 parameter" |
| 24 | return 1 |
| 25 | fi |
| 26 | if [ "$1" -lt "1" ]; then |
| 27 | echo "wait_for function requires 1st parameter to be number greater than 0 ($1 invalid)" |
| 28 | return 1 |
| 29 | fi |
| 30 | wanted=$1 |
| 31 | nodes=${2:-"*"} |
| 32 | # Default max waiting time is 5mn |
| 33 | MAX_WAIT=${MAX_WAIT:-300} |
| 34 | while true; do |
| 35 | nb_nodes=$(salt "$nodes" test.ping --out txt | grep -c True) |
| 36 | if [ -n "$nb_nodes" ] && [ "$nb_nodes" -eq "$wanted" ]; then |
| 37 | echo "All nodes are now answering to salt pings" |
| 38 | break |
| 39 | fi |
| 40 | MAX_WAIT=$(( MAX_WAIT - 15 )) |
| 41 | if [ $MAX_WAIT -le 0 ]; then |
| 42 | echo "Only $nb_nodes answering to salt pings out of $wanted after maximum timeout" |
| 43 | return 2 |
| 44 | fi |
| 45 | echo -n "Only $nb_nodes answering to salt pings out of $wanted. Waiting a bit longer ..." |
| 46 | sleep 15 |
| 47 | echo |
| 48 | done |
| 49 | return 0 |
| 50 | } |