refactor: improve network loading use LOADED environment variable

master
Kebler Network System Administrator 2023-01-01 10:43:15 -08:00
parent 24ed7b60dc
commit 8e83daa3a5
3 changed files with 39 additions and 36 deletions

View File

@ -39,10 +39,10 @@ function network_path () {
function get_network_paths () { function get_network_paths () {
(return 0 2>/dev/null) || (echo network_dirs must be sourced; return 1)
local lookin="$BASH_SHELL_HOST $BASH_SHELL_USER_DIR $BASH_SHELL_DEV" local lookin="$BASH_SHELL_HOST $BASH_SHELL_USER_DIR $BASH_SHELL_DEV"
local networks local networks
local home
[[ $1 == "-h" ]] && home=true && shift 1
for dir in $lookin; do for dir in $lookin; do
# echo checking in $dir # echo checking in $dir
if [[ -f $dir/.networks ]]; then if [[ -f $dir/.networks ]]; then
@ -73,11 +73,12 @@ for network in $networks; do
export BASH_NETWORK_HOME_PATH export BASH_NETWORK_HOME_PATH
export BASH_SHELL_NETWORK_PATHS export BASH_SHELL_NETWORK_PATHS
[[ $home ]] && echo $BASH_NETWORK_HOME_PATH || echo $BASH_SHELL_NETWORK_PATHS
# env | grep NETWORK # env | grep NETWORK
} }
loadssh () { loadssh () {
module_load ssh module_load ssh
module_load ssh-config module_load ssh-config
@ -86,39 +87,44 @@ loadssh () {
module_load ssh-pubkey module_load ssh-pubkey
module_load sshfs module_load sshfs
enable_mounts enable_mounts
export BASH_SHELL_SSH_LOADED=true
# echo ssh modules are loaded # echo ssh modules are loaded
} }
load_home_network() { load_home_network() {
unset BASH_NETWORK_HOME local home
unset BASH_NETWORK_HOME_PATH home=$(get_network_paths -h)
get_network_paths # echo returned $home
if [[ $BASH_NETWORK_HOME_PATH ]]; then if [[ $home ]]; then
# echo loading home $BASH_NETWORK_HOME_PATH # echo loading home $BASH_NETWORK_HOME_PATH
shell_process_directory $BASH_NETWORK_HOME_PATH [[ ! "$BASH_SHELL_NETWORKS_LOADED" == *"$home"* ]] && BASH_SHELL_NETWORKS_LOADED+="$home "
export BASH_SHELL_NETWORKS_LOADED
fi fi
} }
load_all_networks () { loaded_networks () {
# unset BASH_SHELL_HOME_NETWORK_LOAD echo $BASH_SHELL_NETWORKS_LOADED
# export BASH_SHELL_NETWORKS_LOAD=true }
load_networks () {
# echo load networks $1
local network; local networks; local path
if [[ $1 ]]; then
for network in "$@"; do
networks+="$(network_path $network) "
done
else
unset BASH_SHELL_NETWORK_PATHS unset BASH_SHELL_NETWORK_PATHS
get_network_paths networks=$(get_network_paths)
if [[ $BASH_SHELL_NETWORK_PATHS ]]; then
for dir in ${1:-$BASH_SHELL_NETWORK_PATHS}; do
# echo network $dir processed
shell_process_directory $dir
done
fi fi
} # echo networks to load $networks
for path in $networks; do
load_a_network () { # echo network $path processed
[[ ! $1 ]] && return 1 shell_process_directory $path
for dir in $1; do [[ ! "$BASH_SHELL_NETWORKS_LOADED" == *"$path"* ]] && BASH_SHELL_NETWORKS_LOADED+="$path "
# echo network $dir processed
shell_process_directory $dir
done done
export BASH_SHELL_NETWORKS_LOADED
} }
clear_networks () { clear_networks () {
@ -132,13 +138,10 @@ clear_networks () {
networks_load () { networks_load () {
# echo load_networks called $@ # echo networks_load called $@
[[ $1 == "-h" ]] && { shift 1; load_home_network || return $?; } [[ $1 == "-h" ]] && { shift 1; load_home_network || return $?; } || load_networks "$@"
[[ $1 == "-a" ]] && { shift 1; load_all_networks || return $?; } if [[ $BASH_SHELL_SSH_LOADED ]]; then enable_mounts; ssh_config; else loadssh; fi
[[ $1 ]] && { load_a_network $@ || return 1; }
# echo loading ssh
loadssh
[[ $BASH_SHELL_LOADED ]] && load_shell_host_user_dev [[ $BASH_SHELL_LOADED ]] && load_shell_host_user_dev
} }

View File

@ -71,7 +71,7 @@ if [[ (! $SSH_CONFIG || $RESET ) ]]; then
chmod 600 $SSH_CONFIG chmod 600 $SSH_CONFIG
fi fi
[[ ! $DIRS ]] && DIRS=($BASH_SHELL_NETWORK_PATHS "$BASH_SHELL_HOST" "$BASH_SHELL_USER_DIR" "$BASH_SHELL_DEV") [[ ! $DIRS ]] && DIRS=($BASH_SHELL_NETWORKS_LOADED "$BASH_SHELL_HOST" "$BASH_SHELL_USER_DIR" "$BASH_SHELL_DEV")
# echo DIRS "${DIRS[@]}" # echo DIRS "${DIRS[@]}"
# echo $SSH_CONFIG # echo $SSH_CONFIG
CDIRS=() CDIRS=()

View File

@ -123,7 +123,7 @@ function umntBackup() {
function enable_mounts() { function enable_mounts() {
DIRS=($BASH_SHELL_NETWORK_PATHS "$BASH_SHELL_HOST" "$BASH_SHELL_USER_DIR" "$BASH_SHELL_DEV") DIRS=($BASH_SHELL_NETWORKS_LOADED "$BASH_SHELL_HOST" "$BASH_SHELL_USER_DIR" "$BASH_SHELL_DEV")
# echo DIRS "${DIRS[@]}" # echo DIRS "${DIRS[@]}"
CDIRS=() CDIRS=()
j=0 j=0