blob: e34e1db91273610e848ecbe0cba37fa2b368e02d [file] [log] [blame]
Jeepyb Userdd14e0b2017-02-28 15:19:32 +00001#!/bin/bash
2
3# Function used to return list of node names according
4# to given string parameter match criteria
5function get_nodes_names {
Simon Pasquiere76e2622017-05-15 15:04:47 +02006 # 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 Userdd14e0b2017-02-28 15:19:32 +000013}
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)
20function wait_for {
Simon Pasquiere76e2622017-05-15 15:04:47 +020021 # 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 Userdd14e0b2017-02-28 15:19:32 +000035 nb_nodes=$(salt "$nodes" test.ping --out txt | grep -c True)
Simon Pasquiere76e2622017-05-15 15:04:47 +020036 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
53function 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
70function get_param_value {
71 salt-call pillar.data "_param:$1" --out key | grep "_param:" | awk '{print $2}'
Jeepyb Userdd14e0b2017-02-28 15:19:32 +000072}