Merge "Fix wait_for
function in kolla-common.sh"
This commit is contained in:
commit
f84c2b1705
@ -19,16 +19,37 @@ check_required_vars() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# The usage of the wait_for function looks like the following
|
||||||
|
# wait_for LOOPS_NUMBER SLEEP_TIME ARGS
|
||||||
|
#
|
||||||
|
# The ARGS are read and concatenated together into a single command and the
|
||||||
|
# command is executed in a loop until it succeeds or reaches the max number of
|
||||||
|
# attempts (LOOPS_NUMBER).
|
||||||
|
#
|
||||||
|
# Optional variables SUCCESSFUL_MATCH_OUTPUT and FAIL_MATCH_OUTPUT variable may
|
||||||
|
# also be set to control if the loop exits early if the commands stdout/stderr
|
||||||
|
# matches the supplied regex string. Consider using the `wait_for_output` and
|
||||||
|
# `wait_for_output_unless` functions in case there is a need to check for the
|
||||||
|
# command output.
|
||||||
|
#
|
||||||
|
# The script exits on failure, either when output contains string identified as
|
||||||
|
# failure, or when it reaches a timeout.
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# wait_for 30 10 ping -c 1 192.0.2.2
|
||||||
|
# wait_for 10 1 ls file_we_are_waiting_for
|
||||||
|
# wait_for 10 3 date \| grep 8
|
||||||
wait_for() {
|
wait_for() {
|
||||||
local loops=${1:-""}
|
local loops=${1:-""}
|
||||||
local sleeptime=${2:-""}
|
local sleeptime=${2:-""}
|
||||||
local fail_match_output=${fail_match_output:-""}
|
FAIL_MATCH_OUTPUT=${FAIL_MATCH_OUTPUT:-""}
|
||||||
local successful_match_output=${successful_match_output:-""}
|
SUCCESSFUL_MATCH_OUTPUT=${SUCCESSFUL_MATCH_OUTPUT:-""}
|
||||||
shift 2 || true
|
shift 2 || true
|
||||||
local command="$@"
|
local command="$@"
|
||||||
|
|
||||||
if [ -z "$loops" -o -z "$sleeptime" -o -z "$command" ]; then
|
if [ -z "$loops" -o -z "$sleeptime" -o -z "$command" ]; then
|
||||||
echo "Incorrect call of wait_for. Refer to docs/wait-for.md for help"
|
echo "wait_for is missing a required parameter"
|
||||||
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local i=0
|
local i=0
|
||||||
@ -36,15 +57,15 @@ wait_for() {
|
|||||||
i=$((i + 1))
|
i=$((i + 1))
|
||||||
local status=0
|
local status=0
|
||||||
local output=$(eval $command 2>&1) || status=$?
|
local output=$(eval $command 2>&1) || status=$?
|
||||||
if [[ -n "$successful_match_output" ]] \
|
if [[ -n "$SUCCESSFUL_MATCH_OUTPUT" ]] \
|
||||||
&& [[ $output =~ $successful_match_output ]]; then
|
&& [[ $output =~ $SUCCESSFUL_MATCH_OUTPUT ]]; then
|
||||||
break
|
return 0
|
||||||
elif [[ -n "$fail_match_output" ]] \
|
elif [[ -n "$FAIL_MATCH_OUTPUT" ]] \
|
||||||
&& [[ $output =~ $fail_match_output ]]; then
|
&& [[ $output =~ $FAIL_MATCH_OUTPUT ]]; then
|
||||||
echo "Command output matched '$fail_match_output'."
|
echo "Command output matched '$FAIL_MATCH_OUTPUT'."
|
||||||
continue
|
exit 1
|
||||||
elif [[ -z "$successful_match_output" ]] && [[ $status -eq 0 ]]; then
|
elif [[ -z "$SUCCESSFUL_MATCH_OUTPUT" ]] && [[ $status -eq 0 ]]; then
|
||||||
break
|
return 0
|
||||||
fi
|
fi
|
||||||
sleep $sleeptime
|
sleep $sleeptime
|
||||||
done
|
done
|
||||||
@ -54,6 +75,34 @@ wait_for() {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Helper function to `wait_for` that only succeeds when the given regex is
|
||||||
|
# matching the command's output. Exit early with a failure when the second
|
||||||
|
# supplied regexp is matching the output.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
# wait_for_output_unless CREATE_COMPLETE CREATE_FAIL 30 10 heat stack-show undercloud
|
||||||
|
wait_for_output_unless() {
|
||||||
|
SUCCESSFUL_MATCH_OUTPUT=$1
|
||||||
|
FAIL_MATCH_OUTPUT=$2
|
||||||
|
shift 2
|
||||||
|
wait_for $@
|
||||||
|
local status=$?
|
||||||
|
unset SUCCESSFUL_MATCH_OUTPUT
|
||||||
|
unset FAIL_MATCH_OUTPUT
|
||||||
|
return $status
|
||||||
|
}
|
||||||
|
|
||||||
|
# Helper function to `wait_for` that only succeeds when the given regex is
|
||||||
|
# matching the command's output.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
# wait_for_output CREATE_COMPLETE 30 10 heat stack-show undercloud
|
||||||
|
wait_for_output() {
|
||||||
|
local expected_output=$1
|
||||||
|
shift
|
||||||
|
wait_for_output_unless $expected_output '' $@
|
||||||
|
}
|
||||||
|
|
||||||
# Exit unless we receive a successful response from corresponding OpenStack
|
# Exit unless we receive a successful response from corresponding OpenStack
|
||||||
# service.
|
# service.
|
||||||
check_for_os_service() {
|
check_for_os_service() {
|
||||||
|
@ -1,18 +0,0 @@
|
|||||||
# Wait-for function
|
|
||||||
|
|
||||||
The usage of the wait_for function looks like the following
|
|
||||||
$ SCRIPT_NAME LOOPS_NUMBER SLEEP_TIME ARGS
|
|
||||||
|
|
||||||
The ARGS are read and concatenated together into a single command
|
|
||||||
and the command is executed in a loop until it succeeds or reaches
|
|
||||||
the max number of attempts (LOOPS_NUMBER).
|
|
||||||
|
|
||||||
An optional FAIL_MATCH_OUTPUT variable may also be set to control
|
|
||||||
if the loop exits early if the commands stdout/stderr matches the
|
|
||||||
supplied regex string.
|
|
||||||
Examples:
|
|
||||||
$ wait_for 30 10 ping -c 1 192.0.2.2
|
|
||||||
$ wait_for 10 1 ls file_we_are_waiting_for
|
|
||||||
$ wait_for 10 3 date \| grep 8
|
|
||||||
$ FAIL_MATCH_OUTPUT=CREATE_FAILED wait_for 30 10 heat stack-show undercloud
|
|
||||||
$ SUCCESSFUL_MATCH_OUTPUT=CREATE_COMPLETE wait_for 30 10 heat stack-show undercloud
|
|
Loading…
Reference in New Issue
Block a user