-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry1.txt
279 lines (247 loc) · 6.79 KB
/
retry1.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/local/bin/bash
if [ "$1" == "-h" ]; then
cat <<EOF
*****************************************************************
* Initializing system, please try later!
* Power by Lex. Let's go! and be happy!
* version 1.0
echo "Examples:"
echo " $0 -e asia-es1 -c update or delete or adddns"
*****************************************************************
EOF
exit 0
fi
function usage() {
echo "Usage: $0 [-e <asia-es1 asia-es2 eu-a eu-b>]" 1>&2
echo "Create a new zone name and A recored in specified environment."
echo
echo " -e env and apiRegion required"
echo
echo "Examples:"
echo " $0 -e asia-es1 -c upgrade"
exit 1
}
SCRIPT_NAME="${0##*/}"
info() {
echo "${SCRIPT_NAME}: ${1}"
}
warn() {
info "[WARNING]: ${1}" >&2
}
fatal() {
warn "ERROR: ${1}"
exit 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() {
url_stat=$(curl -I -m 10 -o /dev/null -s -w %{http_code} http://www.baidu.com)
echo "$url_stat"
}
#retry3 5 curl -I https://gitee.comt
retry3 5 curl_www
echo "The number of attempts to run retry3 function: $count"
finish
awk 'BEGIN{while (a++<50) s=s "-"; print s,"splite line",s}'
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
usage_short() {
cat << EOF
${SCRIPT_NAME} $(version_message)
usage: ${SCRIPT_NAME} [OPTION]...
Set up, validate, and install ASM in a Google Cloud environment.
Use -h|--help with -v|--verbose to show detailed descriptions.
EOF
}
#$* 这个程式的所有参数
if [[ "${*}" = '' ]]; then
usage_short >&2
echo "Please input you parameters"
exit 2
fi
fatal() {
warn "ERROR: ${1}"
exit 2
}
if [ "$1" == "-h" ] ; then
cat << EOF
*****************************************************************
* Initializing system, please try later!
* Power by Lex. Let's go! and be happy!
* version 1.0
echo "Examples:"
echo " $0 -e asia-es1 -c upgrade or delete or adddns"
*****************************************************************
EOF
exit 0
fi
env_arr=(asia-es1 asia-es2 eu-a eu-b pdev-cn prd-cn)
module_arr=(install migrate upgrade)
while getopts ":e:c:h:" o; do
case $o in
e) e=${OPTARG}
[[ " ${env_arr[*]} " == *" $e "* ]] || usage
;;
c) c=${OPTARG}
[[ " ${module_arr[*]} " == *" $c "* ]] || usage
;;
h) echo "a script to "
echo "Usage:"
echo "Source:"
;;
*) esac
done
case $e in
"asia-es1")
project=china-010115-ddd-dev
;;
"asia-es2")
project=china-0290910-pp-dev
;;
"eu-a"|"eu-b")
project=china-0290910-aaa-dev
;;
"prd-cn") project=china-apichina-prod;;
*) echo "Error input space and region!"
exit 1
esac
if [ ! "$e" ]; then
echo "please enter env and api region" >&2
exit 1
fi
if [ ! "$c" ]; then
echo "please enter mode" >&2
exit 1
fi
# shellcheck disable=SC2064
case "${c}" in
install | migrate | upgrade);;
*) fatal "MODE must be one of 'install', 'migrate', 'upgrade'";;
esac
#./retry1.sh -e asia-es1 -c migrate
#使用"-t"选项,在每次读取一行后就去掉该行的换行符
mapfile -d " " -t myarr < /Users/lex/shell/ip.txt
echo "${myarr[@]}"
IFS=' '
while read -r ip user password port; do
echo "ip: $ip"
echo "user: $user"
echo "password: $password"
echo "port: $port"
echo "-----------------------------"
echo "$ip $user"
done < /Users/lex/shell/ip.txt
function upgrade()
{
info "upgrade"
echo "need a new zone name"
echo "need a new A record"
}
if [ "${c}" == "upgrade" ];
then
echo "upgrade"
echo $project
upgrade
fi
#./retry1.sh -e asia-es1 -c upgrade