Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit a09b0b2

Browse files
committed
initial add of state
1 parent 09f299a commit a09b0b2

File tree

1 file changed

+60
-22
lines changed

1 file changed

+60
-22
lines changed

entrypoint.sh

Lines changed: 60 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,26 @@ readonly MOUNT_PATH_RPC_PIPEFS='/var/lib/nfs/rpc_pipefs'
5858

5959
readonly REGEX_EXPORTS_LINES_TO_SKIP='^\s*#|^\s*$'
6060

61+
readonly LOG_LEVEL_INFO='INFO'
62+
readonly LOG_LEVEL_DEBUG='DEBUG'
63+
64+
readonly STATE_LOG_LEVEL='log_level'
65+
readonly STATE_IS_LOGGING_DEBUG='is_logging_debug'
66+
readonly STATE_IS_LOGGING_INFO='is_logging_info'
67+
68+
# "state" is our only global variable, which is an associative array of normalized data
69+
declare -A state
70+
71+
72+
######################################################################################
73+
### string utils
74+
######################################################################################
75+
76+
toupper() {
77+
78+
echo "$1" | awk '{ print toupper($0) }'
79+
}
80+
6181

6282
######################################################################################
6383
### logging
@@ -84,7 +104,7 @@ log_header() {
84104

85105
echo "
86106
==================================================================
87-
$(echo "$1" | awk '{print toupper($0)}')
107+
$(toupper "$1")
88108
=================================================================="
89109
}
90110

@@ -152,7 +172,7 @@ stop_mount() {
152172
if mount | grep -Eq ^"$type on $path\\s+"; then
153173

154174
local args=()
155-
if is_debug_requested; then
175+
if is_logging_debug; then
156176
args+=('-v')
157177
fi
158178
args+=("$path")
@@ -176,7 +196,7 @@ stop_nfsd() {
176196
stop_exportfs() {
177197

178198
local args=('-ua')
179-
if is_debug_requested; then
199+
if is_logging_debug; then
180200
args+=('-v')
181201
fi
182202

@@ -293,15 +313,6 @@ has_linux_capability() {
293313
return 1
294314
}
295315

296-
is_debug_requested() {
297-
298-
if echo "${!ENV_VAR_NFS_LOG_LEVEL}" | grep -Eqi '^DEBUG$'; then
299-
return 0
300-
fi
301-
302-
return 1
303-
}
304-
305316
get_requested_count_nfsd_threads() {
306317

307318
if [[ -n "${!ENV_VAR_NFS_SERVER_THREAD_COUNT}" ]]; then
@@ -313,6 +324,12 @@ get_requested_count_nfsd_threads() {
313324
fi
314325
}
315326

327+
is_logging_debug() {
328+
329+
[[ -n ${state[$STATE_IS_LOGGING_DEBUG]} ]] && return 0 || return 1
330+
}
331+
332+
316333
######################################################################################
317334
### runtime configuration assertions
318335
######################################################################################
@@ -400,6 +417,23 @@ assert_log_level() {
400417
### initialization
401418
######################################################################################
402419

420+
init_state_logging() {
421+
422+
# if the user didn't request a specific log level, the default is INFO
423+
local -r normalized_log_level=$(toupper "${!ENV_VAR_NFS_LOG_LEVEL:-$LOG_LEVEL_INFO}")
424+
425+
if ! echo "$normalized_log_level" | grep -Eq 'DEBUG|INFO'; then
426+
bail "the only acceptable values for $ENV_VAR_NFS_LOG_LEVEL are: DEBUG, INFO"
427+
fi
428+
429+
state[$STATE_LOG_LEVEL]=$normalized_log_level;
430+
state[$STATE_IS_LOGGING_INFO]=1
431+
432+
if [[ $normalized_log_level = "$LOG_LEVEL_DEBUG" ]]; then
433+
state[$STATE_IS_LOGGING_DEBUG]=1
434+
fi
435+
}
436+
403437
init_trap() {
404438

405439
trap stop SIGTERM SIGINT
@@ -409,13 +443,17 @@ init_exports() {
409443

410444
# first, see if it's bind-mounted
411445
if mount | grep -Eq "^[^ ]+ on $PATH_FILE_ETC_EXPORTS type "; then
412-
log "$PATH_FILE_ETC_EXPORTS is bind-mounted"
446+
if is_logging_debug; then
447+
log "$PATH_FILE_ETC_EXPORTS is bind-mounted"
448+
fi
413449
return
414450
fi
415451

416452
# maybe it's baked-in to the image
417453
if [[ -f $PATH_FILE_ETC_EXPORTS && -r $PATH_FILE_ETC_EXPORTS && -s $PATH_FILE_ETC_EXPORTS ]]; then
418-
log "$PATH_FILE_ETC_EXPORTS is baked into the image"
454+
if is_logging_debug; then
455+
log "$PATH_FILE_ETC_EXPORTS is baked into the image"
456+
fi
419457
return
420458
fi
421459

@@ -482,7 +520,6 @@ init_assertions() {
482520
assert_port "$ENV_VAR_NFS_PORT_STATD_OUT"
483521
assert_nfs_version
484522
assert_nfsd_threads
485-
assert_log_level
486523

487524
# check kernel modules
488525
assert_kernel_mod nfs
@@ -515,7 +552,7 @@ boot_helper_mount() {
515552
local -r type=$(basename "$path")
516553
local args=('-t' "$type" "$path")
517554

518-
if is_debug_requested; then
555+
if is_logging_debug; then
519556
args+=('-vvv')
520557
fi
521558

@@ -585,7 +622,7 @@ boot_main_mounts() {
585622
boot_main_exportfs() {
586623

587624
local args=('-ar')
588-
if is_debug_requested; then
625+
if is_logging_debug; then
589626
args+=('-v')
590627
fi
591628

@@ -598,7 +635,7 @@ boot_main_mountd() {
598635
read -r -a version_flags <<< "$(boot_helper_get_version_flags)"
599636
local -r port=$(get_requested_port_mountd)
600637
local args=('--port' "$port" "${version_flags[@]}")
601-
if is_debug_requested; then
638+
if is_logging_debug; then
602639
args+=('--debug' 'all')
603640
fi
604641

@@ -623,7 +660,7 @@ boot_main_idmapd() {
623660

624661
local args=('-S')
625662
local func=boot_helper_start_daemon
626-
if is_debug_requested; then
663+
if is_logging_debug; then
627664
args+=('-vvv' '-f')
628665
func=boot_helper_start_non_daemon
629666
fi
@@ -652,7 +689,7 @@ boot_main_nfsd() {
652689
local -r port=$(get_requested_port_nfsd)
653690
local args=('--tcp' '--udp' '--port' "$port" "${version_flags[@]}" "$threads")
654691

655-
if is_debug_requested; then
692+
if is_logging_debug; then
656693
args+=('--debug')
657694
fi
658695

@@ -670,7 +707,7 @@ boot_main_svcgssd() {
670707
fi
671708

672709
local args=('-f')
673-
if is_debug_requested; then
710+
if is_logging_debug; then
674711
args+=('-vvv')
675712
fi
676713

@@ -716,7 +753,7 @@ summarize_exports() {
716753
# if debug is enabled, read /var/lib/nfs/etab as it contains the "real" export data. but it also contains more
717754
# information that most people will usually need to see
718755
local file_to_read="$PATH_FILE_ETC_EXPORTS"
719-
if is_debug_requested; then
756+
if is_logging_debug; then
720757
file_to_read='/var/lib/nfs/etab'
721758
fi
722759

@@ -759,6 +796,7 @@ init() {
759796

760797
log_header 'setting up ...'
761798

799+
init_state_logging
762800
init_exports
763801
init_assertions
764802
init_trap

0 commit comments

Comments
 (0)