-
Notifications
You must be signed in to change notification settings - Fork 3
/
actions_functions.sh
65 lines (55 loc) · 1.38 KB
/
actions_functions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
#
# Common functions
#
# Author: ttionya <[email protected]>
#################### Function ####################
########################################
# Print colorful message.
# Arguments:
# color
# message
# Outputs:
# colorful message
########################################
function color() {
case $1 in
red) echo -e "\033[31m$2\033[0m" ;;
green) echo -e "\033[32m$2\033[0m" ;;
yellow) echo -e "\033[33m$2\033[0m" ;;
blue) echo -e "\033[34m$2\033[0m" ;;
none) echo "$2" ;;
esac
}
export -f color
########################################
# Command with retry.
# Arguments:
# command options
# Returns:
# command return code
# Outputs:
# command output
########################################
function retry() {
local RETRY_COUNT=3
local RETRY_DELAY=2
local COUNT=0
local RETURN_CODE=0
while [[ "${COUNT}" -lt "${RETRY_COUNT}" ]]; do
$*
RETURN_CODE=$?
if [[ "${RETURN_CODE}" -eq 0 ]]; then
break
fi
((COUNT++))
if [[ "${COUNT}" -eq "${RETRY_COUNT}" ]]; then
color yellow "retried ${RETRY_COUNT} times, no more retries left" 1>&2
else
color yellow "retry after ${RETRY_DELAY} seconds" 1>&2
sleep "${RETRY_DELAY}"
fi
done
return "${RETURN_CODE}"
}
export -f retry