forked from vesoft-inc/nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
162 lines (124 loc) · 3.42 KB
/
utils.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
#
# Copyright (c) 2020 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License.
#
# Some color code definitions
RED=
GREEN=
YELLOW=
BLUE=
BLINK=
NC=
# To detect if stdout or stderr is being attached to terminal
function is_tty {
[[ -t 1 ]] && [[ -t 2 ]]
return $?
}
# Enable colorful output only if no terminal attached
is_tty && RED=$(echo -e "\001\033[1;31m\002")
is_tty && GREEN=$(echo -e "\001\033[1;32m\002")
is_tty && YELLOW=$(echo -e "\001\033[1;33m\002")
is_tty && BLUE=$(echo -e "\001\033[1;34m\002")
is_tty && BLINK=$(echo -e "\001\033[5m\002")
is_tty && NC=$(echo -e "\001\033[0m\002")
function INFO {
echo "${GREEN}[INFO]${NC}" $@
}
function WARN {
echo "${YELLOW}[WARN]${NC}" $@
}
function ERROR {
echo "${RED}[ERROR]${NC}" $@ >&2
}
function ERROR_AND_EXIT {
ERROR $@
exit 1
}
# Tell if a path is an absolute one
# args: <path>
function is_absolute_path {
[[ ${1} = /* ]]
return $?
}
# Tell if a path is an existing directory
# args: <path>
function is_existing_dir {
[[ -d ${1} ]]
return $?
}
# Tell if a process is running, via a file hosting its pid
# args: <path to pid file>
function is_process_running {
local pid_file=${1}
[[ -f ${pid_file} ]] || return 1
local pid=$(cat ${pid_file})
[[ -z ${pid} ]] && return 1
ps -p ${pid} &>/dev/null
return $?
}
# Tell if some process is listening on the target port
# args: <port number>
function is_port_listened_on {
local port=${1}
if which ss &>/dev/null; then
ss -lnt | grep ${port} &>/dev/null && return 0
fi
}
# To wait for a process to exit
# args: <path to pid file> <seconds to wait>
function wait_for_exit {
local pid_file=${1}
local seconds=${2}
is_process_running ${pid_file} || return 0
while [[ ${seconds} > 0 ]]; do
sleep 0.1
is_process_running ${pid_file} || return 0
seconds=$(echo "${seconds} - 0.1" | bc -l)
done
}
# To read a config item's value from the config file
# args: <config file> <config item name>
function get_item_from_config {
sed -n -e "s/^--${2}=\(.*\)$/\1/p" ${1} 2>/dev/null | sed -n "1p"
}
# To read the path to the pid file from the config file
# args: <config file>
function get_pid_file_from_config {
get_item_from_config ${1} "pid_file"
}
# To read to listen port number from the config file
# args: <config file>
function get_port_from_config {
get_item_from_config ${1} "port"
}
# To read logging directory from the config file
# args: <config file>
function get_log_dir_from_config {
get_item_from_config ${1} "log_dir"
}
# To create the logging directory if absent
# args: <path to directory>
function make_log_dir_if_absent {
[[ -d ${1} ]] || mkdir -p ${1} >/dev/null
}
# Perform some environment checking
function env_check {
local nfile=$(ulimit -n 2>/dev/null)
local core=$(ulimit -c 2>/dev/null)
local cputime=$(ulimit -t 2>/dev/null)
if [[ ${nfile} -le 1024 ]]; then
WARN "The maximum files allowed to open might be too few: ${nfile}"
fi
if [[ ${core} -eq 0 ]]; then
ulimit -c unlimited &>/dev/null
fi
if [[ ${cputime} != "unlimited" ]]; then
WARN "The CPU time a process can consume is restricted to ${cputime}"
fi
}
# Get the daemon Git commit version information
function daemon_version {
local version=$(${1} --version | head -n 1)
echo ${version} | sed 's/.*Git: \([[:alnum:]]*\).*/\1/g'
}