forked from RackHD/on-build-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shareMethod.sh
75 lines (71 loc) · 1.88 KB
/
shareMethod.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
#!/bin/bash
execWithTimeout() {
set +e
# $1 command to execute
# $2 timeout
# $3 retries on timeout
if [ -z "${1}" ]; then
echo "execWithTimeout() Command not specified"
exit 2
fi
local on_build_config_dir=$(cd $(dirname "${BASH_SOURCE[0]}");pwd)
local cmd="/bin/sh -c \"$1\""
#timeout default to 90 seconds
local timeout=90
local retry=3
local result=0
if [ ! -z "${2}" ]; then
timeout=$2
fi
if [ ! -z "${3}" ]; then
retry=$3
fi
echo "execWithTimeout() retry count is $retry"
echo "execWithTimeout() timeout is set to $timeout"
dir=${on_build_config_dir}/deployment
local time_cmd="${dir}/timeout_cmd.exp"
if [ ! -x ${time_cmd} ]; then
chmod a+x ${time_cmd}
echo "chmod +x ${time_cmd}"
fi
i=1
while [[ $i -le $retry ]]
do
${time_cmd} "${timeout}" "${cmd}"
result=$?
echo "execWithTimeout() exit code $result"
if [ $result = 0 ] ; then
break
fi
((i = i + 1))
done
if [ $result = 1 ] ; then
echo "execWithTimeout() command timed out $retry times after $timeout seconds"
exit 1
elif [ $result != 0 ]; then
echo "execWithTimeout() exit final code $result"
exit $result
fi
set -e
}
####################################3
#
# To Check if a Shell Variable or Env Variable is empty or null (Jenkins Parameter but not set)
#
# $1; the variable name
#
# return 0: not empty
# return 1: empty or null
###################################
check_empty_variable(){
if [ -n "$1" ] ; then
# ${!1} will dereference the variable value
if [ "${!1}" == "" ] || [ "${!1}" == "null" ]; then
echo "[Error] Env Variable $1 is missing "
return 1
fi
else
echo "[Warning] Wrong usage of $0"
fi
return 0
}