-
Notifications
You must be signed in to change notification settings - Fork 3
/
cloneRepo.sh
executable file
·322 lines (263 loc) · 10.9 KB
/
cloneRepo.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/bin/bash
#export PS4='+ $LINENO: ' ## single quotes prevent $LINENO being expanded immediately
#set -ux
declare -f -F WriteLog> /dev/null
if [ $? -ne 0 ]
then
[ -f ./timestampLogger.sh ] && . ./timestampLogger.sh || . ~/build/bin/timestampLogger.sh
# . ~/build/bin/timestampLogger.sh
fi
# Git branch settings
. ~/build/bin/settings.sh
KillWatchDogAndWaitToDie()
{
DELAY=10
ps -p $1 > /dev/null
[ "$?" -eq 0 ] && sudo kill -TERM $1
while (true)
do
stillRunning=$( ps aux | grep -c -i '[w]atchdog.py' )
if [[ ${stillRunning} -eq 0 ]]
then
WriteLog "WatchDog ($1) finished." "$2"
break
fi
WriteLog "WatchDog ($1) is still running. Wait ${DELAY} sec and try again." "$2"
sleep ${DELAY}
sudo kill -KILL $1
done
rm ./WatchDog.pid
}
CloneRepo()
{
LONG_DATE=$(date "+%Y-%m-%d_%H-%M-%S")
CLONE_LOG_FILE=$OBT_BIN_DIR/CloneRepo-${LONG_DATE}.log
WATCHDOG_LOG_FILE=${OBT_LOG_DIR}/WatchDog-$(date "+%Y-%m-%d_%H-%M-%S").log
if [ -f "$OBT_BIN_DIR/WatchDog.py" ]
then
watchDogStartCmd="$OBT_BIN_DIR/WatchDog.py -p 'git-*' -t 900 -r 3600 -v"
WriteLog "Start WatchDog: ${watchDogStartCmd}" "${CLONE_LOG_FILE}"
WriteLog "Watchdog logfile: ${WATCHDOG_LOG_FILE}" "${CLONE_LOG_FILE}"
${watchDogStartCmd} >> ${WATCHDOG_LOG_FILE} 2>&1 &
echo $! > ./WatchDog.pid
WriteLog "WatchDog pid: $( cat ./WatchDog.pid )." "$CLONE_LOG_FILE"
fi
NO_ERROR=0
RECOVERABLE_ERROR=1
UNRECOVERABLE_ERROR=2
DESTINATION_EXISTS_ERROR=3
HTTP_REQUEST_ERROR=4
tryCount=20
tryDelay=2m
repo=$1
target=$2
WriteLog "Clone $1 (into '$target')" "${CLONE_LOG_FILE}"
WriteLog "[$( caller )] $*" "${CLONE_LOG_FILE}"
while true
do
WriteLog "Try count: ${tryCount}" "${CLONE_LOG_FILE}"
res=0
err=0
while read line
do
WriteLog "${line}" "${CLONE_LOG_FILE}" > /dev/null
res=$( echo "${line}" | egrep -i '^fatal' | egrep -c -i 'not an empty directory' )
if [ $res -ne 0 ]
then
err=$DESTINATION_EXISTS_ERROR
break
fi
res=$( echo "${line}" | egrep -i '^fatal' | egrep -c -i 'HTTP request failed' )
if [ $res -ne 0 ]
then
err=$HTTP_REQUEST_ERROR
break
fi
res=$( echo "${line}" | egrep -i '^fatal' | egrep -c -i 'unable to access' )
if [ $res -ne 0 ]
then
err=$HTTP_REQUEST_ERROR
break
fi;
res=$( echo "${line}" | egrep -c -i '^fatal' )
if [ $res -ne 0 ]
then
err=$UNRECOVERABLE_ERROR
break
fi;
res=$( echo "${line}" | egrep -c -i '^error' )
if [ $res -ne 0 ]
then
err=$RECOVERABLE_ERROR
break
fi
done < <( git clone $repo $target 2>&1 )
if [ $err -eq 0 ]
then
WriteLog "Cloned ! " "${CLONE_LOG_FILE}"
break
else
WriteLog "Error:${err}" "${CLONE_LOG_FILE}"
case $err in
$RECOVERABLE_ERROR ) WriteLog "res:$err -> 'RECOVERABLE_ERROR'" "${CLONE_LOG_FILE}"
# Check/set MTU with 'ip link set eth0 mtu 1400'
ipResetCMD="ip link set eth0 mtu 1400"
WriteLog "Reset link: ${ipResetCMD}" "${CLONE_LOG_FILE}"
${ipResetCMD} >> ${CLONE_LOG_FILE} 2>&1 &
;;
$UNRECOVERABLE_ERROR ) WriteLog "res:$err -> 'UNRECOVERABLE_ERROR'" "${CLONE_LOG_FILE}"
# return 1
rmt=$( git ls-remote ${repo} | wc -l )
WriteLog "ls-remote returns with ${rmt} results." "${CLONE_LOG_FILE}"
gitproc=$( pgrep -f 'git-' )
WriteLog "git process(es): ${gitproc} ." "${CLONE_LOG_FILE}"
[ -n "$gitproc" ] && sudo pkill -KILL ${gitproc}
;;
$DESTINATION_EXISTS_ERROR ) WriteLog "res:$err -> 'DESTINATION_EXISTS_ERROR'." "${CLONE_LOG_FILE}"
WriteLog "Remove target directory and try again." "${CLONE_LOG_FILE}"
rm -rf $target
#mkdir build
;;
$HTTP_REQUEST_ERROR ) WriteLog "res:$err -> 'HTTP_REQUEST_ERROR'" "${CLONE_LOG_FILE}"
WriteLog "Try it with SSH: 'git@github.com:hpcc-systems/HPCC-Platform.git'" "${CLONE_LOG_FILE}"
repo='git@github.com:hpcc-systems/HPCC-Platform.git'
;;
esac
tryCount=$(( $tryCount-1 ))
if [[ $tryCount -ne 0 ]]
then
WriteLog "Wait for ${tryDelay} to try again." "${CLONE_LOG_FILE}"
sleep ${tryDelay}
continue
else
break;
fi
fi
done
wdPid=$( cat ./WatchDog.pid )
WriteLog "Kill WatchDog (${wdPid})." "${CLONE_LOG_FILE}"
KillWatchDogAndWaitToDie "${wdPid}" "${CLONE_LOG_FILE}"
if [[ $tryCount -eq 0 ]]
then
WriteLog "Give up ! " "${CLONE_LOG_FILE}"
# send email to Agyi
return 1
fi
WriteLog "End." "${CLONE_LOG_FILE}"
return 0
}
SubmoduleUpdate()
{
LONG_DATE=$(date "+%Y-%m-%d_%H-%M-%S")
SUBMODULE_LOG_FILE=$OBT_BIN_DIR/SubmoduleUpdate-${LONG_DATE}.log
WATCHDOG_LOG_FILE=${OBT_LOG_DIR}/WatchDog-$(date "+%Y-%m-%d_%H-%M-%S").log
if [ -f "$OBT_BIN_DIR/WatchDog.py" ]
then
watchDogStartCmd="$OBT_BIN_DIR/WatchDog.py -p 'git-*' -t 360 -r 3600 -v"
WriteLog "Start WatchDog: ${watchDogStartCmd}" "${SUBMODULE_LOG_FILE}"
WriteLog "Watchdog logfile: ${WATCHDOG_LOG_FILE}" "${SUBMODULE_LOG_FILE}"
${watchDogStartCmd} >> ${WATCHDOG_LOG_FILE} 2>&1 &
echo $! > ./WatchDog.pid
WriteLog "WatchDog pid: $( cat ./WatchDog.pid )." "${SUBMODULE_LOG_FILE}"
fi
tryCount=20
tryDelay=3m
export GIT_CURL_VERBOSE=1
export GIT_CURL_RETRY=10
export CURLOPT_RETRY=10
export CURL_RETRY=10
NO_ERROR=0
SINGLE_REVISION_NEEDED_ERROR=1
RECOVERABLE_ERROR=2
NOT_GIT_REPO_ERROR=3
WriteLog "git submodule update $1" "${SUBMODULE_LOG_FILE}"
WriteLog "[$( caller )] $*" "${SUBMODULE_LOG_FILE}"
while true
do
WriteLog "Try count: ${tryCount}" "${SUBMODULE_LOG_FILE}"
res=''
err=$NO_ERROR
while read line
do
WriteLog "${line}" "${SUBMODULE_LOG_FILE}" # >> "${SUBMODULE_LOG_FILE}"
if [ $err -ne $NO_ERROR ]
then
continue
fi
res=$( echo "${line}" | egrep -i '^fatal' | egrep -c -i 'Needed a single revision' )
if [ $res -ne 0 ]
then
err=$SINGLE_REVISION_NEEDED_ERROR
break
fi
res=$( echo "${line}" | egrep -i '^fatal' | egrep -c -i 'Not a git repository' )
if [ $res -ne 0 ]
then
err=$NOT_GIT_REPO_ERROR
break
fi
res=$( echo "${line}" | egrep -c -i '^fatal|^error|Killed|failed' )
if [ $res -ne 0 ]
then
err=$RECOVERABLE_ERROR
break
fi
done < <( git submodule update $1 2>&1 )
if [ $err -eq $NO_ERROR ]
then
WriteLog "Submodule update finished ! " "${SUBMODULE_LOG_FILE}"
break
else
set -x
WriteLog "Error: $err" "${SUBMODULE_LOG_FILE}"
case $err in
$SINGLE_REVISION_NEEDED_ERROR )
WriteLog "res: $err -> 'SINGLE_REVISION_NEEDED_ERROR'" "${SUBMODULE_LOG_FILE}"
while read module
do
WriteLog "module: ${module}" "${SUBMODULE_LOG_FILE}"
cleanUpCmd="rm -rfv ${module}"
WriteLog "cmd: ${cleanUpCmd}" "${SUBMODULE_LOG_FILE}"
${cleanUpCmd} >> ${SUBMODULE_LOG_FILE} 2>&1
WriteLog "retcode:$?" "${SUBMODULE_LOG_FILE}"
done < <( git config --file .gitmodules --get-regexp path | awk '{ print $2 }' )
;;
$RECOVERABLE_ERROR ) WriteLog "res: $err -> 'RECOVERABLE_ERROR'" "${SUBMODULE_LOG_FILE}"
while read submodule
do
WriteLog "submodule: ${submodule}" "${SUBMODULE_LOG_FILE}"
cleanUpCmd2="rm -rfv ${submodule}"
WriteLog "cmd: ${cleanUpCmd2}" "${SUBMODULE_LOG_FILE}"
${cleanUpCmd2} >> ${SUBMODULE_LOG_FILE} 2>&1
WriteLog "retcode:$?" "${SUBMODULE_LOG_FILE}"
done < <( git config --file .gitmodules --get-regexp path | awk '{ print $2 }' )
;;
$NOT_GIT_REPO_ERROR ) WriteLog "res: $err -> 'NOT_GIT_REPO_ERROR'" "${SUBMODULE_LOG_FILE}"
tryCount=0
break
;;
esac
set +x
tryCount=$(( $tryCount-1 ))
if [[ $tryCount -ne 0 ]]
then
WriteLog "Wait for ${tryDelay} to try again." "${SUBMODULE_LOG_FILE}"
sleep ${tryDelay}
continue
else
break;
fi
fi
done
wdPid=$( cat ./WatchDog.pid )
WriteLog "Kill WatchDog (${wdPid})." "${SUBMODULE_LOG_FILE}"
KillWatchDogAndWaitToDie "${wdPid}" "${SUBMODULE_LOG_FILE}"
if [[ $tryCount -eq 0 ]]
then
WriteLog "Give up ! " "${SUBMODULE_LOG_FILE}"
# send email to Agyi
return 1
fi
WriteLog "End." "${SUBMODULE_LOG_FILE}"
return 0
}