-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry.txt
executable file
·131 lines (118 loc) · 3.56 KB
/
retry.txt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
SCRIPT_NAME="${0##*/}"
info() {
echo "${SCRIPT_NAME}: ${1}"
}
warn() {
info "[WARNING]: ${1}" >&2
}
function finish_checked() {
if [[ $count -eq 3 ]]; then
echo "ERROR: cloud dns record-sets transaction execute failure, Please rerun your deploy"
exit 1
else
echo "success add cloud dns record"
fi
}
#######
# retry takes an integer N as the first argument, and a list of arguments
# representing a command afterwards. It will retry the given command up to N
# times before returning 1. If the command is ifconfig, it will try to
# re-get credentials in case something caused the k8s IP to change.
retry() {
local MAX_TRIES; MAX_TRIES="${1}";
shift 1
for i in $(seq 0 "${MAX_TRIES}"); do
if [[ "${i}" -eq "${MAX_TRIES}" ]]; then
break
fi
{ "${@}" && return 0; } || true
warn "Failed, retrying...($((i+1)) of ${MAX_TRIES})"
sleep 2
done
local CMD="'$*'"
warn "Command $CMD failed."
false
}
retry 4 netstat -an | grep -i "listen"|grep www
#retry 4 netstat -an | grep -i "listen"
if ! retry 4 netstat -an | grep -i "listen"|grep www; then
#https://github.com/koalaman/shellcheck/wiki/SC2181
#if [ $? -ne 0 ]; then
echo "ERROR: www is not listen"
count=0
while [ $count -lt 3 ]; do
echo "5 seconds sleep and try again"
info "5 seconds sleep and try again"
sleep 5
echo "re-add new record"
rm -fr ./transaction.yaml
echo "first delete transaction.yaml"
warn "first delete transaction.yaml"
#gcloud dns record-sets transaction start -z "${ZoneName?
#gcloud dns record-sets transaction add "${externalIP)"-name="${DomainserviceName}" -ttl="${TTL}" -type="${RecordType}" --zone="${ZoneName}"
#gcloud dns record-sets transaction execute - "${ZoneName]" --verbosity debug
if [ $? = 0 ]; then
echo "add record ok" && break
else
count=$(($count+1))
fi
done
finish_checked
fi
awk 'BEGIN{while (a++<50) s=s "-"; print s,"splite line",s}'
function check_metadata_is_available() {
local count=0
local flag=$(curl -I -m 10 -o /dev/null -s -w %{http_code} http://www.baidu.com)
if [ -z "$flag" ]; then
while [ $count -lt 3 ]; do
echo "30 seconds sleep and try again ..."
sleep 30
flag=$(curl -I -m 10 -o /dev/null -s -w %{http_code} http://www.baidu.com)
[ $flag ] && break
count=$(($count+1))
done
fi
echo "The number of attempts to run check_metadata_is_available function: $count"
echo "The flag: $flag"
[ $flag ] && echo "The metadata is available" || echo "The metadata is unavailable"
}
check_metadata_is_available
#https://stackoverflow.com/questions/7449772/how-to-retry-a-command-in-bash
awk 'BEGIN{while (a++<50) s=s "-"; print s,"splite line",s}'
finish() {
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
echo "ERROR: failed your command."
fi
}
retry3() {
local -r -i max_attempts="$1"; shift
local -i attempt_num=1
until "$@"
do
if ((attempt_num==max_attempts))
then
echo "Attempt $attempt_num failed and there are no more attempts left!"
return 1
else
echo "Attempt $attempt_num failed! Trying again in $attempt_num seconds..."
sleep $((attempt_num++))
fi
done
}
function curl_www() {
curl -I -m 10 -o /dev/null -s -w %{http_code} http://www.baidu.com
}
#retry3 5 curl -I https://gitee.comt
retry3 5 curl_www
finish
function finish_curl_checked() {
if [[ $max_attempts -eq 5 ]]; then
echo "ERROR: curl 5 time and failed"
exit 1
else
echo "success curl you url"
fi
}
#finish_curl_checked