-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonlib.sh
493 lines (404 loc) · 11.7 KB
/
commonlib.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
#!/bin/bash
## run only with srvctl
[[ $SRVCTL ]] || exit 10
## optional - help execution main parameter - to allow optional arguments
readonly HEMP='## @@@'
## mandatory - the singe hint string
readonly HINT='## @en'
## mandatory - the multistring help
readonly HELP='## &en'
# No Color
#readonly CLEAR='\e[0m'
## functions common to all areas of srvctl
function hint {
local cmd hint file
cmd="$1"
hint="$2"
file="$3"
## print formatted hint
if $DEBUG && [[ $CMD == help ]]
then
printf "${BLUE}%-48s${CLEAR}" " $file"
echo ''
fi
printf "${GREEN}%-40s${CLEAR}" " $cmd"
printf "${GREEN}%-48s${CLEAR}" " $hint"
## newline
echo ''
}
function title {
echo ''
printf "${GREEN}"%-40s"${CLEAR}" "$1"
echo ''
echo ''
}
function load_libs {
local tvll module
for dir in $SC_MODULES
do
module="${dir##*/}"
tvll="SC_USE_${module^^}"
if [[ ${!tvll} == true ]]
then
for sourcefile in $dir/libs/*
do
debug "@lib ${dir##*/} ${sourcefile##*/}"
## dynamic source
# shellcheck disable=SC1090
[[ -f $sourcefile ]] && source "$sourcefile"
done
fi
done
}
## outdated - modules shall be taken from SC_MODULES
#function run_module_hook { ## module hook
# local dir hook
# dir="$SC_INSTALL_DIR/modules/$1"
# hook="$2"
#
# if [[ -f $dir/hooks/$hook.sh ]]
# then
#
# debug "@hook ${dir##*/} $hook"
# ## dynamic source
# # shellcheck disable=SC1090
# source "$dir/hooks/$hook.sh"
# exif "$dir hook '$hook' failed"
# fi
#}
function run_hook {
local hook tvrh module
hook="$1"
for dir in $SC_MODULES
do
module="${dir##*/}"
tvrh="SC_USE_${module^^}"
if [[ ${!tvrh} == true ]]
then
## find and call hooks
if [[ -f $dir/hooks/$hook.sh ]]
then
debug "@hook ${dir##*/} $hook"
## dynamic source
# shellcheck disable=SC1090
source "$dir/hooks/$hook.sh"
exif "$dir hook '$hook' failed"
fi
fi
done
}
function run_hooks {
run_hook "pre-$1"
run_hook "$1"
run_hook "post-$1"
}
function run_command {
[[ $CMD ]] || return 54
local tvrc module
## call a srvctl function
if [[ $UID == 0 ]] && [[ $OPAS ]] && [[ $CMD == 'exec-function' ]]
then
$OPAS
exif "failed to exec '$OPAS'"
return
fi
## call a srvctl data function
if [[ $UID == 0 ]] && [[ $OPAS ]] && [[ $CMD == 'new' ]] || [[ $CMD == 'get' ]] || [[ $CMD == 'put' ]] || [[ $CMD == 'out' ]] || [[ $CMD == 'cfg' ]] || [[ $CMD == 'del' ]] || [[ $CMD == 'fix' ]]
then
# shellcheck disable=SC2086
$CMD $OPAS
exif "failed to exec '$CMD $OPAS'"
return
fi
## permissions will determine the visibility of these commands
if [[ -f /root/srvctl-includes/$CMD.sh ]]
then
## dynamic source
# shellcheck disable=SC1090
source "/root/srvctl-includes/$CMD.sh"
exif "'$CMD' failed"
return
fi
## command from a module
for dir in $SC_MODULES
do
module="${dir##*/}"
tvrc="SC_USE_${module^^}"
if [[ ${!tvrc} == true ]]
then
## find and run commands
if [[ -f $dir/commands/$CMD.sh ]]
then
## dynamic source
# shellcheck disable=SC1090
source "$dir/commands/$CMD.sh"
exif "'$CMD' failed ($dir)"
return
fi
fi
done
## custom by user
if [[ -f $SC_HOME/srvctl-includes/$CMD.sh ]] && [[ $SC_HOME != /root ]]
then
## dynamic source
# shellcheck disable=SC1090
source "$SC_HOME/srvctl-includes/$CMD.sh"
exif "'$CMD' failed"
return
fi
debug "@default-command"
for dir in $SC_MODULES
do
module="${dir##*/}"
tvrc="SC_USE_${module^^}"
if [[ ${!tvrc} == true ]]
then
## try to find and run default command
if [[ -f $dir/command.sh ]]
then
debug "@command.sh ${dir##*/}"
## dynamic source
# shellcheck disable=SC1090
source "$dir/command.sh"
fi
fi
done
return 250
}
## by default complicate takes no action, but this functuion can be re-defined
function complicate() {
echo "$1" > /dev/null
}
function hint_on_file {
[[ -f $1 ]] || return 132
## root_only: if not root, and file marked as root_only skip this item
! $SC_ROOT && head "$1" | grep -q 'root_only' && return 133
## if not on a containerfarm host
! [[ $SC_HOSTNET ]] && head "$1" | grep -q 'hs_only' && return 134
## is user is not a reseller
! $SC_ROOT && ! [[ "${#SC_USER}" == 1 ]] && head "$1" | grep -q 'reseller_only' && return 134
#! $SC_ON_VE && head "$1" | grep -q 've_only' && return 135
local hintstr command hintcmd
hintstr="$(head "$1" | grep -m 1 "$HINT")"
command="$(basename "$1")"
hintcmd="$(head "$1" | grep -m 1 "$HEMP" "$1")"
if [[ -z $hintcmd ]]
then
hint "${command:0: -3}" "${hintstr:7}" "$1"
complicate "${command:0: -3}"
else
hint "${hintcmd:7}" "${hintstr:7}" "$1"
complicate "${hintcmd:7}"
fi
}
function hint_commands {
prg "Usage: srvctl command [argument]"
prg " currently available commands for $SC_USER"
if [[ -d $SC_HOME/srvctl-includes ]] && [[ $SC_HOME != /root ]]
then
title "COMMAND"
else
echo ""
fi
if [ -d /root/srvctl-includes ]
then
title "COMMAND - from root"
for sourcefile in /root/srvctl-includes/*.sh
do
hint_on_file "$sourcefile"
done
title "COMMAND - from srvctl"
fi
local tvhc module
for dir in $SC_MODULES
do
module="${dir##*/}"
tvhc="SC_USE_${module^^}"
if [[ ${!tvhc} == true ]]
then
for sourcefile in $dir/commands/*.sh
do
hint_on_file "$sourcefile"
done
fi
done
if [ -d "$SC_HOME/srvctl-includes" ] && [ "$SC_HOME" != "/root" ]
then
title "COMMAND - from $SC_USER"
for sourcefile in $SC_HOME/srvctl-includes/*.sh
do
hint_on_file "$sourcefile"
done
fi
echo ''
for dir in $SC_MODULES
do
module="${dir##*/}"
tvhc="SC_USE_${module^^}"
if [[ ${!tvhc} == true ]]
then
if [[ -f "$dir/command.sh" ]]
then
hint_on_file "$dir/command.sh"
fi
fi
done
## print formatted hint about man
hint "help [COMMAND]" "See more detailed descriptions about COMMAND or about all commands."
## newline
echo ''
echo ''
}
function help_on_file {
[[ -f "$1" ]] || return 133
local hintstr command
hintstr="$(head "$1" | grep "$HINT")"
command="$(basename "$1")"
hint "${command:0: -3}" "${hintstr:7}" "$1"
printf "${YELLOW}%-4s" ""
echo ''
grep "$HELP" "$1" | sed "s/$HELP/ /g"
printf "${CLEAR}%-4s" ""
echo ""
}
function help_commands {
title "srvctl COMMAND [arguments]"
title "COMMAND"
if [[ -z $ARG ]]
then
if [[ -d /root/srvctl-includes ]]
then
title "COMMAND - from root"
for sourcefile in /root/srvctl-includes/*.sh
do
help_on_file "$sourcefile"
done
title "COMMAND - from srvctl"
fi
for dir in $SC_MODULES
do
for sourcefile in $dir/commands/*.sh
do
help_on_file "$sourcefile"
done
done
if [[ -d $SC_HOME/srvctl-includes ]] && [[ $SC_HOME != /root ]]
then
title "COMMAND - from $SC_USER"
for sourcefile in $SC_HOME/srvctl-includes/*.sh
do
help_on_file "$sourcefile"
done
fi
return 0
else
local arg
arg="${ARG,,}"
if [[ -f /root/srvctl-includes/$arg.sh ]]
then
help_on_file "/root/srvctl-includes/$arg.sh"
msg "Custom command from root"
return
fi
for dir in $SC_MODULES
do
if [[ -f $dir/commands/$arg ]]
then
help_on_file "$dir/commands/$arg.sh"
prg "srvctl v3 command"
return
fi
done
if [[ -f $SC_HOME/srvctl-includes/$arg.sh ]] && [[ $SC_HOME != /root ]]
then
help_on_file "$SC_HOME/srvctl-includes/$arg.sh"
prg "Custom command defined in $SC_HOME/srvctl-includes/$arg.sh"
return
fi
err "Pardon? '$ARG' is not a command"
return 46
fi
}
function test_srvctl_modules() {
local conf
conf=/var/local/srvctl/modules.conf
if [[ $USER == root ]]
then
#conf=/var/local/srvctl/modules.conf
mkdir -p /var/local/srvctl/
fi
if [[ $SC_HOME ]]
then
conf="$SC_HOME/.srvctl/modules.conf"
mkdir -p "$SC_HOME/.srvctl"
fi
if [[ ! -f $conf ]] || [[ $CMD == update-install ]] || [[ $CMD == test-modules ]]
then
msg "Srvctl modules configuration"
## test value / test result on tested module
local tvtm trtm module
for dir in $SC_MODULES
do
module="${dir##*/}"
tvtm="SC_USE_${module^^}"
trtm=false
if [[ -f $dir/module-condition.sh ]]
then
# shellcheck disable=SC1090
trtm="$(source "$dir/module-condition.sh")"
if [[ $trtm == true ]]
then
trtm=true
else
trtm=false
fi
ntc "tested module: $tvtm=$trtm"
else
err "Module $module has no condition file in $dir"
fi
#declare $tv=$tr
echo "export $tvtm=$trtm" >> "$conf"
done
fi
if [[ $CMD == 'test-modules' ]]
then
msg "srvctl modules selected"
exit 0
fi
if [[ -f /var/local/srvctl/modules.conf ]]
then
debug "@source /var/local/srvctl/modules.conf"
source /var/local/srvctl/modules.conf
fi
if [[ -f $conf ]]
then
debug "@source $conf"
## dynamic source
# shellcheck disable=SC1090
source "$conf"
fi
for dir in $SC_MODULES
do
module="${dir##*/}"
export "SC_USE_${module^^}"
readonly "SC_USE_${module^^}"
done
}
function set_permissions() {
msg "Set permissions."
chmod -R 600 /etc/srvctl
chmod 755 /etc/srvctl
chmod 644 /etc/srvctl/*.conf
chmod 644 /etc/srvctl/*.json
chmod -R 600 "$SC_DATASTORE_RW_DIR"
chmod 755 "$SC_DATASTORE_RW_DIR"
chmod 644 "$SC_DATASTORE_RW_DIR"/*.json
[[ -d "$SC_MOUNTS_DIR" ]] && chmod 700 "$SC_MOUNTS_DIR"
[[ -d "$SC_ROOTFS_DIR" ]] && chmod 700 "$SC_ROOTFS_DIR"
}
#function hint_cms {
# for sourcefile in $SC_INSTALL_DIR/ve-cms/*
# do
# [[ -f "$sourcefile" ]] && source "$sourcefile"
# done
#}