| 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 { | 
| Simon Pasquier | e76e262 | 2017-05-15 15:04:47 +0200 | [diff] [blame] | 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" | 
| Jeepyb User | dd14e0b | 2017-02-28 15:19:32 +0000 | [diff] [blame] | 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 { | 
| Simon Pasquier | e76e262 | 2017-05-15 15:04:47 +0200 | [diff] [blame] | 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 | 
| Jeepyb User | dd14e0b | 2017-02-28 15:19:32 +0000 | [diff] [blame] | 35 | nb_nodes=$(salt "$nodes" test.ping --out txt | grep -c True) | 
| Simon Pasquier | e76e262 | 2017-05-15 15:04:47 +0200 | [diff] [blame] | 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 | } | 
|  | 51 |  | 
|  | 52 | # Waits for an HTTP service to be ready | 
|  | 53 | function wait_for_http_service { | 
|  | 54 | local url=$1 | 
|  | 55 | local timeout=${2:-120} | 
|  | 56 | local expected_code=${3:-200} | 
|  | 57 |  | 
|  | 58 | start=$(date +'%s') | 
|  | 59 | while [ $(( $(date +'%s') - start )) -lt "$timeout"  ]; do | 
|  | 60 | code=$(curl -ksL -w '%{http_code}' -o /dev/null "$url") | 
|  | 61 | if [[ $code == "$expected_code" ]]; then | 
|  | 62 | echo "$url responded with $code" | 
|  | 63 | break | 
|  | 64 | fi | 
|  | 65 | sleep 1 | 
|  | 66 | done | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | # Returns the value of _param:$1 from the pillar | 
|  | 70 | function get_param_value { | 
|  | 71 | salt-call pillar.data "_param:$1" --out key | grep "_param:" | awk '{print $2}' | 
| Jeepyb User | dd14e0b | 2017-02-28 15:19:32 +0000 | [diff] [blame] | 72 | } |