forked from apache/incubator-hugegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.sh
executable file
·390 lines (349 loc) · 10.6 KB
/
util.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with this
# work for additional information regarding copyright ownership. The ASF
# licenses this file to You under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
function command_available() {
local cmd=$1
if [[ -x "$(command -v "$cmd")" ]]; then
return 0
else
return 1
fi
}
# read a property from .properties file
function read_property() {
# file path
file_name=$1
# replace "." to "\."
property_name=$(echo "$2" | sed 's/\./\\\./g')
cat "$file_name" | sed -n -e "s/^[ ]*//g;/^#/d;s/^$property_name=//p" | tail -1
}
function write_property() {
local file=$1
local key=$2
local value=$3
local os=$(uname)
case $os in
# Note: in mac os should use sed -i '' "xxx" to replace string,
# otherwise prompt 'command c expects \ followed by text'.
# See http://www.cnblogs.com/greedy-day/p/5952899.html
Darwin) sed -i '' "s!$key=.*!$key=$value!g" "$file" ;;
*) sed -i "s!$key=.*!$key=$value!g" "$file" ;;
esac
}
function parse_yaml() {
local file=$1
local version=$2
local module=$3
cat "$file" | tr -d '\n {}'| awk -F',+|:' '''{
pre="";
for(i=1; i<=NF; ) {
if(match($i, /version/)) {
pre=$i;
i+=1
} else {
result[pre"-"$i] = $(i+1);
i+=2
}
}
} END {for(e in result) {print e": "result[e]}}''' \
| grep "$version-$module" | awk -F':' '{print $2}' | tr -d ' ' && echo
}
function process_num() {
num=$(ps -ef | grep "$1" | grep -v grep | wc -l)
return "$num"
}
function process_id() {
pid=$(ps -ef | grep "$1" | grep -v grep | awk '{print $2}')
return "$pid"
}
# check the port of rest server is occupied
function check_port() {
local port=$(echo "$1" | awk -F':' '{print $3}')
if ! command_available "lsof"; then
echo "Required lsof but it is unavailable"
exit 1
fi
lsof -i :"$port" >/dev/null
if [ $? -eq 0 ]; then
echo "The port $port has already been used"
exit 1
fi
}
function crontab_append() {
local job="$1"
crontab -l | grep -F "$job" >/dev/null 2>&1
if [ $? -eq 0 ]; then
return 1
fi
(crontab -l ; echo "$job") | crontab -
}
function crontab_remove() {
local job="$1"
# check exist before remove
crontab -l | grep -F "$job" >/dev/null 2>&1
if [ $? -eq 1 ]; then
return 0
fi
crontab -l | grep -Fv "$job" | crontab -
# Check exist after remove
crontab -l | grep -F "$job" >/dev/null 2>&1
if [ $? -eq 0 ]; then
return 1
else
return 0
fi
}
# wait_for_startup friendly_name host port timeout_s
function wait_for_startup() {
local pid="$1"
local server_name="$2"
local server_url="$3"
local timeout_s="$4"
local now_s=$(date '+%s')
local stop_s=$((now_s + timeout_s))
local status
local error_file_name="startup_error.txt"
echo -n "Connecting to $server_name ($server_url)"
while [ "$now_s" -le $stop_s ]; do
echo -n .
process_status "$server_name" "$pid" >/dev/null
if [ $? -eq 1 ]; then
echo "Starting $server_name failed"
if [ -e "$error_file_name" ]; then
rm "$error_file_name"
fi
return 1
fi
status=$(curl -I -sS -k -w "%{http_code}" -o /dev/null "$server_url" 2> "$error_file_name")
if [[ $status -eq 200 || $status -eq 401 ]]; then
echo "OK"
echo "Started [pid $pid]"
if [ -e "$error_file_name" ]; then
rm "$error_file_name"
fi
return 0
fi
sleep 2
now_s=$(date '+%s')
done
echo ""
cat "$error_file_name"
rm "$error_file_name"
echo "The operation timed out(${timeout_s}s) when attempting to connect to $server_url" >&2
return 1
}
function free_memory() {
local free=""
local os=$(uname)
if [ "$os" == "Linux" ]; then
local mem_free=$(cat /proc/meminfo | grep -w "MemFree" | awk '{print $2}')
local mem_buffer=$(cat /proc/meminfo | grep -w "Buffers" | awk '{print $2}')
local mem_cached=$(cat /proc/meminfo | grep -w "Cached" | awk '{print $2}')
if [[ "$mem_free" == "" || "$mem_buffer" == "" || "$mem_cached" == "" ]]; then
echo "Failed to get free memory"
exit 1
fi
free=$(expr "$mem_free" + "$mem_buffer" + "$mem_cached")
free=$(expr "$free" / 1024)
elif [ "$os" == "Darwin" ]; then
local pages_free=$(vm_stat | awk '/Pages free/{print $0}' | awk -F'[:.]+' '{print $2}' | tr -d " ")
local pages_inactive=$(vm_stat | awk '/Pages inactive/{print $0}' | awk -F'[:.]+' '{print $2}' | tr -d " ")
local pages_available=$(expr "$pages_free" + "$pages_inactive")
free=$(expr "$pages_available" \* 4096 / 1024 / 1024)
else
echo "Unsupported operating system $os"
exit 1
fi
echo "$free"
}
function calc_xmx() {
local min_mem=$1
local max_mem=$2
# Get machine available memory
local free=$(free_memory)
local half_free=$((free/2))
local xmx=$min_mem
if [[ "$free" -lt "$min_mem" ]]; then
exit 1
elif [[ "$half_free" -ge "$max_mem" ]]; then
xmx=$max_mem
elif [[ "$half_free" -lt "$min_mem" ]]; then
xmx=$min_mem
else
xmx=$half_free
fi
echo $xmx
}
function remove_with_prompt() {
local path=$1
local tips=""
if [ -d "$path" ]; then
tips="Remove directory '$path' and all sub files [y/n]?"
elif [ -f "$path" ]; then
tips="Remove file '$path' [y/n]?"
else
return 0
fi
read -p "$tips " yn
case $yn in
[Yy]* ) rm -rf "$path";;
* ) ;;
esac
}
function ensure_path_writable() {
local path=$1
# Ensure input path exist
if [ ! -d "${path}" ]; then
mkdir -p "${path}"
fi
# Check for write permission
if [ ! -w "${path}" ]; then
echo "No write permission on directory ${path}"
exit 1
fi
}
function get_ip() {
local os=$(uname)
local loopback="127.0.0.1"
local ip=""
case $os in
Linux)
if command_available "ifconfig"; then
ip=$(ifconfig | grep 'inet addr:' | grep -v "$loopback" | cut -d: -f2 | awk '{ print $1}')
elif command_available "ip"; then
ip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | awk -F"/" '{print $1}')
else
ip=$loopback
fi
;;
FreeBSD|OpenBSD|Darwin)
if command_available "ifconfig"; then
ip=$(ifconfig | grep -E 'inet.[0-9]' | grep -v "$loopback" | awk '{ print $2}')
else
ip=$loopback
fi
;;
SunOS)
if command_available "ifconfig"; then
ip=$(ifconfig -a | grep inet | grep -v "$loopback" | awk '{ print $2} ')
else
ip=$loopback
fi
;;
*) ip=$loopback;;
esac
echo $ip
}
function download() {
local path=$1
local download_url=$2
if command_available "curl"; then
if [ ! -d "$path" ]; then
mkdir -p "$path" || {
echo "Failed to create directory: $path"
exit 1
}
fi
curl -L "${download_url}" -o "${path}/$(basename "${download_url}")"
elif command_available "wget"; then
wget --help | grep -q '\--show-progress' && progress_opt="-q --show-progress" || progress_opt=""
wget "${download_url}" -P "${path}" $progress_opt
else
echo "Required curl or wget but they are unavailable"
exit 1
fi
}
function ensure_package_exist() {
local path=$1
local dir=$2
local tar=$3
local link=$4
if [ ! -d "${path}"/"${dir}" ]; then
if [ ! -f "${path}"/"${tar}" ]; then
echo "Downloading the compressed package '${tar}'"
download "${path}" "${link}"
if [ $? -ne 0 ]; then
echo "Failed to download, please ensure the network is available and link is valid"
exit 1
fi
echo "[OK] Finished download"
fi
echo "Unzip the compressed package '$tar'"
tar zxvf "${path}"/"${tar}" -C "${path}" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Failed to unzip, please check the compressed package"
exit 1
fi
echo "[OK] Finished unzip"
fi
}
###########################################################################
function wait_for_shutdown() {
local process_name="$1"
local pid="$2"
local timeout_s="$3"
local now_s=$(date '+%s')
local stop_s=$((now_s + timeout_s))
echo -n "Killing $process_name(pid $pid)" >&2
while [ "$now_s" -le $stop_s ]; do
echo -n .
process_status "$process_name" "$pid" >/dev/null
if [ $? -eq 1 ]; then
echo "OK"
return 0
fi
sleep 2
now_s=$(date '+%s')
done
echo "$process_name shutdown timeout(exceeded $timeout_s seconds)" >&2
return 1
}
function process_status() {
local process_name="$1"
local pid="$2"
ps -p "$pid"
if [ $? -eq 0 ]; then
echo "$process_name is running with pid $pid"
return 0
else
echo "The process $process_name does not exist"
return 1
fi
}
function kill_process() {
local process_name="$1"
local pid="$2"
if [ -z "$pid" ]; then
echo "The process $pid does not exist"
return 0
fi
case "$(uname)" in
CYGWIN*) taskkill /F /PID "$pid" ;;
*) kill "$pid" ;;
esac
}
function kill_process_and_wait() {
local process_name="$1"
local pid="$2"
local timeout_s="$3"
kill_process "$process_name" "$pid"
wait_for_shutdown "$process_name" "$pid" "$timeout_s"
}
function exit_with_usage_help(){
echo "USAGE: $0 [-d true|false] [-g g1] [-m true|false] [-p true|false] [-s true|false] [-j java_options] [-t timeout]"
exit 1
}