-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.sh
622 lines (547 loc) · 24.1 KB
/
common.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#!/usr/bin/env bash
####----[ ERRORS ]----------------------------------------------------------####
# Generate error codes (emulate enum)
ECODES=(OK ERROR EUNDEF EUNDEFP ENAN EINVOPT ECMD ECMDF EINVNUM EINVNUMC
EINVNUMS EINVNODES ETIMEO EUNDEFT)
for i in $(seq 0 $((${#ECODES[@]} -1 ))); do
declare -r ${ECODES[$(($i))]}=$i;
done
# Register error messages
ERRORS[$EINVOPT]='invalid option $ARG'
ERRORS[$EUNDEF]='undefined --servers or --clients option(s)'
ERRORS[$EUNDEFP]='undefined partition'
ERRORS[$ENAN]='$CMD not a number'
ERRORS[$ECMD]='$CMD not found. Please install the $PACKAGE package'
ERRORS[$ECMDF]='$CMD failed. Exit'
ERRORS[$EINVNUM]='invalid number of arguments'
ERRORS[$EINVNUMC]='invalid quantity of client nodes'
ERRORS[$EINVNUMS]='invalid quantity of server nodes'
ERRORS[$EINVNODES]='no servers nor clients were provided. Exit'
ERRORS[$ETIMEO]='$CMD received a timeout. Exit'
ERRORS[$EUNDEFT]='undefined servers type. Please use --servers-type. Exit'
####----[ GENERIC FUNCTIONS ]-----------------------------------------------####
############################################################################
# Check if argument is a number. #
# Args: #
# -$1: Argument to check. #
# Result: return 0 if a number, else 1. #
is_numeric()
{
# Check number of input arguments
if [[ "$#" -ne 1 ]]; then
print_error $EINVNUM; return $?
fi
# Extract argument
local in="$1"
# Check if a number
local regex='^[0-9]+$'
if [[ "$in" =~ $regex ]]; then
return 0
else
return 1
fi
}
############################################################################
# Replace tags with content of variables. #
# Args: #
# -$1: Input text. #
# -$2: Output variable where to store the resulting text. #
# -$3: Start of the tag. #
# -$4: End of the tag. #
# Result: store the resulting text in the variable defined by $2. #
tags_replace()
{
# Check number of input arguments
if [[ "$#" -ne 4 ]]; then
print_error $EINVNUM; return $?
fi
# Extract arguments
local in="$1"
local out="$2"
local stag="$3"
local etag="$4"
# Search list of tags to replace
local varList=$(echo "$in" | egrep -o "$stag[0-9A-Za-z_-]*$etag" |
sort -u | sed -e "s/^$stag//" -e "s/$etag$//")
local res="$in"
# Check if there are some tags to replace
if [[ -n "$varList" ]]; then
# Generate sed remplacement string
sedOpts=''
for var in $varList; do
eval "value=\${${var}}"
sedOpts="${sedOpts} -e 's#$stag${var}$etag#${value}#g'"
done
res=$(eval "echo -e \"\$in\" | sed $sedOpts")
fi
# Store resulting string in the output variable
eval "$out=\"$res\""
}
############################################################################
# Remove all tags contained in a text. #
# Args: #
# -$1: Input text. #
# -$2: Output variable where to store the resulting text. #
# -$3: Start of the tag. #
# -$4: End of the tag. #
# Result: store the resulting text in the variable defined by $2. #
tags_remove()
{
# Check number of input arguments
if [[ "$#" -ne 4 ]]; then
print_error $EINVNUM; return $?
fi
# Extract arguments
local in="$1"
local out="$2"
local stag="$3"
local etag="$4"
# Remove tags
local res="$(echo "$in" | sed -e "s#$stag[A-Za-z0-9_]*$etag##g")"
# Store resulting string in the output variable
eval "$out=\"$res\""
}
############################################################################
# Replace tags in a text with the content of variables. #
# Args: #
# -$1: Input text. #
# -$2: Output variable where to store the resulting text or output #
# the content. ($2='var_name', $2='stdout' or $2='stderr') #
# Result: store or output the resulting text. #
tags_replace_txt()
{
# Check number of input arguments
if [[ "$#" -ne 2 ]]; then
print_error $EINVNUM; return $?
fi
# Extract arguments
local in="$1"
local out="$2"
# Replace all tags defined by {{TAG_NAME}}
tags_replace "$in" "$out" '{{' '}}'
# Check if the resulting string has to be printed in stderr or stdout
case "$out" in
stdout)
eval "echo -e \"\$$out\""
;;
stderr)
eval "echo -e \"\$$out\"" 1>&2
;;
esac
}
############################################################################
# Enable colors and check color depth. #
# Args: #
# None #
# Result: set global variable if color support may be turned on. #
enable_colors()
{
ENABLE_COLORS='false'
check_cmd 'tput' 'ncurses'
# Check if tput available for colors management
if [[ "$?" -eq 0 ]]; then
# Check if launched in a terminal
if [[ -t 1 ]]; then
local color_depth=$(tput colors)
# Check color depth
if [[ "$color_depth" -ge 8 ]] 2>/dev/null; then
print_verbose 3 'color support turned on'
ENABLE_COLORS='true'
if [[ "$color_depth" -lt 256 ]] 2>/dev/null; then
print_verbose 2 'color depth less than 256 (using 8)'
fi
else
print_verbose 2 'color depth less than 8, color disabled'
fi
else # Not run in a terminal, turn off color support
print_verbose 2 'not run in a terminal, color disbaled'
fi
else # tput not vailable, turn off color support
print_verbose 2 'colors support turned off'
fi
}
############################################################################
# Print text with colors if colors are enables. #
# Args: #
# -$1: Input text. #
# -$*: Other arguments for printf function. #
# Result: print resulting string in stdout. #
print_colors()
{
# Check number of input arguments
if [[ "$#" -lt 1 ]]; then
print_error $EINVNUM; return $?
fi
# Extract argument
local in="$1<normal>"
# Shift arguments
shift
# Check if colors are enabled and prepare output string
if [[ "$ENABLE_COLORS" == "true" ]]; then
# End tags
local normal='$(tput sgr0)'
local black="$normal"
local red="$normal"
local green="$normal"
local grey1="$normal"
local grey2="$normal"
local grey3="$normal"
local yellow="$normal"
local blue="$normal"
local magenta="$normal"
local cyan="$normal"
local white="$normal"
local orange="$normal"
local b="$normal"
local i='$(tput ritm)'
local u='$(tput rmul)'
tags_replace "$in" 'OUT' '<\/' '>'
# Start tags
if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then
yellow='$(tput setaf 190)'
orange='$(tput setaf 172)'
grey1='$(tput setaf 240)'
grey2='$(tput setaf 239)'
grey3='$(tput setaf 238)'
else
yellow='$(tput setaf 3)'
orange='$(tput setaf 3)'
grey1='$(tput setaf 0)'
grey2='$(tput setaf 0)'
grey3='$(tput setaf 0)'
fi
black='$(tput setaf 0)'
red='$(tput setaf 1)'
green='$(tput setaf 2)'
blue='$(tput setaf 4)'
magenta='$(tput setaf 5)'
cyan='$(tput setaf 6)'
white='$(tput setaf 7)'
b='$(tput bold)'
i='$(tput sitm)'
u='$(tput smul)'
tags_replace "$OUT" 'OUT' '<' '>'
else
tags_remove "$in" 'OUT' '</' '>'
tags_remove "$OUT" 'OUT' '<' '>'
fi
# Print string to stdout
printf "$OUT" $*
}
############################################################################
# Print error in stderr. #
# Args: #
# -$1: Error code. #
# Result: print error and return error code. #
print_error()
{
# Extract argument
local error_code="$1"
# Check if output is not muted
if [[ -z "$SILENT" ]]; then
# Get error description
eval "msg=\"${ERRORS[${error_code}]}\""
# Print the error message
print_colors '<red><b>Error:</b> </red>' 1>&2
print_colors "<red>$msg</red>\n" 1>&2
fi
# Return the corresponding error code
return "$error_code"
}
############################################################################
# Print warning in stderr. #
# Args: #
# -$1: message to print. #
# -$*: printf arguments. #
# Result: print warning. #
print_warning()
{
# Check if output is not muted
if [[ -z "$SILENT" ]]; then
# Extract argument
local msg="$1"
# Shift arguments
shift
# Print the warning message
print_colors '<orange><b>Warning:</b> </orange>' 1>&2
print_colors "<orange>$msg</orange>\n" $* 1>&2
fi
}
############################################################################
# Print info in stdout. #
# Args: #
# -$1: message to print. #
# -$*: printf arguments. #
# Result: print info message. #
print_info()
{
# Check if output is not muted
if [[ -z "$SILENT" ]]; then
# Extract argument
local msg="$1"
# Shift arguments
shift
# Print the message
print_colors '<yellow><b>Info:</b> </yellow>'
print_colors "<yellow>$msg</yellow>\n" $*
fi
}
############################################################################
# Print verbose info in stdout. #
# Args: #
# -$1: verbosity (1, 2 or 3). #
# -$2: message to print. #
# -$*: printf arguments. #
# Result: print info in verbose mod. #
print_verbose()
{
# Check if output is not muted
if [[ -z "$SILENT" ]]; then
# Extract argument
local level="$1"
local msg="$2"
# Shift arguments
shift; shift
# Check the verbosity level currently set
if [[ "$VERBOSE_LEVEL" -ge "$level" ]]; then
# Select color
local color="white"
case "$level" in
1) color="grey1";;
2) color="grey2";;
3) color="grey3";;
esac
# Print the warning message
print_colors "<$color><b>Verbose $level:</b> </$color>"
print_colors "<$color>$msg</$color>\n" $*
fi
fi
}
############################################################################
# Fold and indent input message. #
# Args: #
# -$1: message to format. #
# Result: print message folded on 80 col. #
format_message()
{
# Extract argument
local msg="$1"
echo "${msg}" | fold -s | sed -r 's/^([^[:space:]])/ \1/'
}
############################################################################
# Print usage. #
# Args: #
# None #
# Result: print short usage message. #
usage()
{
print_colors '<b>Usage:</b> '
local tmp=$(head -n${SC_HSIZE:-99} "${0}" | grep -e "^#+" |
sed -e "s/^#+[ ]*//g" -e "s/#$//g")
tags_replace_txt "$tmp" 'stdout'
}
############################################################################
# Print information related to development. #
# Args: #
# None #
# Result: print version and contact information. #
info()
{
local tmp=$(head -n${SC_HSIZE:-99} "${0}" | grep -e "^#-" |
sed -e "s/^#-//g" -e "s/#$//g" -e "s/\[at\]/@/g")
format_message "$(tags_replace_txt "$tmp" 'stdout')"
}
############################################################################
# Print full detailled usage. #
# Args: #
# None #
# Result: print help. #
usage_full()
{
local tmp=$(head -n${SC_HSIZE:-99} "${0}" | grep -e "^#[%+]" |
sed -e "s/^#[%+-]//g" -e "s/#$//g")
format_message "$(tags_replace_txt "$tmp" 'stdout')"
info
}
############################################################################
# Check if the tool is installed and the command is working on the system. #
# Args: #
# -$1: command to check. #
# -$2: package name. #
# Result: display an error and return error code ECMD if not installed. #
check_cmd()
{
# Check number of input arguments
if [[ "$#" -ne 2 ]]; then
print_error $EINVNUM; return $?
fi
# Extract parameters
local cmd="$1"
local package="$2"
# Check if command works
command -v $cmd >/dev/null 2>&1 ||
{
# Set variables for error message
CMD=$cmd
PACKAGE=$package
# Print error message and return error code
print_error $ECMD; return $?
}
print_verbose 3 "command %s available" "$cmd"
return $OK
}
############################################################################
# Expand a triplet built with prefix, lower and upper numerical bounds #
# Args: #
# -$1: prefix #
# -$2: lower integer bound, potentially with leading zeros #
# -$3: upper integer bound, potentially with leading zeros #
# -$4: suffix #
# #
# Result: a space separated list of enumerated values #
# or $EINVNUM if some argument are missing #
# or $EINVOPT if lower or upper bounds are not integer are missing #
expand_element()
{
# Check number of input arguments
if [[ "$#" -lt 3 ]]; then
print_error $EINVNUM; return $?
fi
local prefix=$1
local lower=$2;
local upper=$3;
local suffix=$4;
is_numeric $lower
if [[ $? != 0 ]]; then
print_error $EINVOPT; return $?
fi
is_numeric $upper
if [[ $? != 0 ]]; then
print_error $EINVOPT; return $?
fi
# seq -w manage width and takes care of leading zero
for i in $(seq -w $lower $upper) ; do
printf "%s%s," $prefix$i$suffix
done
}
############################################################################
# Format the strings provided as argument to node_set #
# The difficulty to solve is related to comma #
# which can be used within pattern or as token separator #
# e.g.: #
# imenode[1],ime_head_0 imeserver{1,5,7} #
# ^_ we want to remove this comma #
# is equivalent to #
# imenode[1] ime_head_0 imeserver{1,5,7} #
# Args: #
# -$1: string containing comma as token separator #
# Result: string where commas are only used within patterns #
preprocess_node_set()
{
local string=$1
local cleaned=""
local TRIGGER=1
for (( i=0; i<${#string}; i++ )); do
char=${string:$i:1};
if [[ $char =~ [\{\[] ]]; then TRIGGER=0; fi
if [[ $char =~ [\]\}] ]]; then TRIGGER=1; fi
# if trigger is on, the replace comma by space
[[ $TRIGGER != 0 ]] && char=${char/,/ }
cleaned+="$char"
done
printf "$cleaned"
}
############################################################################
# Expand a list potentially complex of patterns. #
# 3 kinds of formats are supported: #
# [-] to indicate a numerical sequence that need to be expanded #
# [,] to indicate a numerical sequence to add to a prefix #
# {,} to indicate a numerical sequence to add to a prefix (equ. to above) #
# #
# eg. node[02-6,9-10, 15-17] #
# will return: #
# node02,node03,node04,node05,node06,node09,node10,node15,node16,node17 #
# Args: #
# -$1: string describing a list of node potentially using patterns #
# -$2: suffix to append to each node entry of the list #
# Result: a comma separated list of expanded values #
node_set()
{
local string=$1
local suffix=$2
local result=""
string=$(preprocess_node_set "$string")
for word in ${string[*]}; do
# comma separated list value {,}
if [[ "$word" =~ "{" ]] ; then
local prefix=`echo $word | cut -d'{' -f1`
local list=`echo $word | cut -d'{' -f2`
list=`echo $list | tr -d "}"`
local sequence=(${list//,/ })
for element in ${sequence[*]}; do
result+="$prefix$element$suffix,"
done
fi
# range value []
if [[ "$word" =~ "[" ]] ; then
local prefix=`echo $word | cut -d'[' -f1`
local pattern=`echo $word | cut -d'[' -f2`
pattern=`echo $pattern | tr -d "]"`
# create the vector of sequence: replace ',' by ' '
local sequence=(${pattern//,/ })
for element in ${sequence[*]}; do
if [[ "$element" =~ "-" ]]; then
bounds=(${element//-/ })
result+=$(expand_element $prefix ${bounds[0]} \
${bounds[1]} $suffix)
else
result+="$prefix$element$suffix,"
fi
done
fi
# simple word
if [[ ! "$word" =~ "{" && ! "$word" =~ "[" ]] ; then
result+="$word$suffix,"
fi
done
# remove trailing comma if any
printf "${result%,}"
}
############################################################################
# Check return code and exit if it is not equal to 0. #
# Args: #
# -$1: return code. #
# -$@: command with arguments which was executed. #
check_return()
{
local exit_code=$1
shift
CMD="$@"
if [[ "$exit_code" -ne 0 ]]; then
if [[ "$exit_code" -eq 124 ]]; then
print_error $ETIMEO
else
print_error $ECMDF
fi
if [[ "$CLEANUP" == "true" ]]; then
my_exit "$exit_code"
else
exit $exit_code
fi
fi
}
############################################################################
# Run command and check return code. #
# Args: #
# -$@: command to execute. #
run_cmd()
{
if [[ -n "$TIMEOUT" ]]; then
print_info "Running (${TIMEOUT}s timeout): $(echo "$@" | xargs)"
eval "timeout -s QUIT -k 30 --foreground $TIMEOUT $@"
else
print_info "Running: $(echo "$@" | xargs)"
eval "$@"
fi
check_return $? "$@"
}