refactor networks loading

master
Kebler Network System Administrator 2022-12-30 09:14:39 -08:00
parent e205bf3c50
commit 9197b1edd0
4 changed files with 138 additions and 85 deletions

View File

@ -1,6 +1,29 @@
#!/bin/bash #!/bin/bash
# to be sourced (below) not run in subshell! # to be sourced (below) not run in subshell!
function network_path () {
[[ ! $1 ]] && return 1
local network;
network=$1
[[ $2 ]] && network=$2/$network
if [[ $(isAbsPath $network) ]]; then
[[ ! -d $network ]] && continue
else
if [[ -d ${HOME}/${BASH_SHELL_USER}/${network} ]];then
network=${HOME}/${BASH_SHELL_USER}/${network}
else
if [[ -d ${BASH_NETWORKS_DIR}/${network} ]];then
network=${BASH_NETWORKS_DIR}/${network}
else
return 2
fi
fi
fi
echo $network
}
function network_dirs () { function network_dirs () {
(return 0 2>/dev/null) || (echo network_dirs must be sourced; return 1) (return 0 2>/dev/null) || (echo network_dirs must be sourced; return 1)
@ -11,38 +34,26 @@ function network_dirs () {
[[ ! $1 ]] && return 1 [[ ! $1 ]] && return 1
local networks; local networks;
networks=$1 networks=$1
[[ ! $1 ]] && return 2
[[ -f $1 ]] && networks=$(cat $1 | tr -s "\n" " ") [[ -f $1 ]] && networks=$(cat $1 | tr -s "\n" " ")
echo networks to add $networks
for network in $networks; for network in $networks;
# while read -r network || [ -n "$network" ]
do do
if [[ ${network} = *!* ]]; then if [[ ${network} = *!* ]]; then
network=${network/!/} network=${network/!/}
BASH_NETWORK_HOME=$network BASH_NETWORK_HOME=$network
fi fi
[[ $2 ]] && network=$2/$network network=$(network_path $network $2)
if [[ $(isAbsPath $network) ]]; then if [[ $network ]]; then
[[ ! -d $network ]] && continue [[ $network =~ $BASH_NETWORK_HOME ]] && BASH_NETWORK_HOME_DIR=${network}
else # echo adding $network to list of networks
if [[ -d ${HOME}/${BASH_SHELL_USER}/${network} ]];then BASH_SHELL_NETWORK_PATHS+="${network} "
network=${HOME}/${BASH_SHELL_USER}/${network}
else
if [[ -d ${BASH_NETWORKS_DIR}/${network} ]];then
network=${BASH_NETWORKS_DIR}/${network}
else
continue
fi
fi
fi fi
[[ $network =~ $NETWORK_HOME ]] && BASH_NETWORK_HOME_DIR=${network}
# echo adding $network to list of networks
BASH_SHELL_NETWORK_DIRS+="${network} "
done done
# done < "$1"
# echo after network-dirs $1
# echo DIRS: $BASH_SHELL_NETWORK_DIRS
export BASH_NETWORK_HOME export BASH_NETWORK_HOME
export BASH_NETWORK_HOME_DIR export BASH_NETWORK_HOME_DIR
export BASH_SHELL_NETWORK_DIRS export BASH_SHELL_NETWORK_PATHS
}
}

View File

@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
# module_load network-dirs # module_load network-dirs
# # These will be loaded for all users on a host # # These will be loaded for all users on a host
# network_dirs "$BASH_SHELL_HOST/.networks" # network_dirs "$BASH_SHELL_HOST/.networks"
# # these loaded only for specific user on a host and take precedence # # these loaded only for specific user on a host and take precedence
@ -9,7 +10,73 @@
# # env | grep NETWORK # # env | grep NETWORK
# # NETWORK_HOME # # NETWORK_HOME
# # NETWORK_HOME_DIR # # NETWORK_HOME_DIR
# # BASH_SHELL_NETWORK_DIRS # # BASH_SHELL_NETWORK_PATHS
function network_path () {
module_load path
[[ $? -ne 0 ]] && echo unable to access the file module && return 1
[[ ! $1 ]] && return 1
local network;
network=$1
[[ $2 ]] && network=$2/$network
if [[ $(isAbsPath $network) ]]; then
[[ ! -d $network ]] && continue
else
if [[ -d ${HOME}/${BASH_SHELL_USER}/${network} ]];then
network=${HOME}/${BASH_SHELL_USER}/${network}
else
if [[ -d ${BASH_NETWORKS_DIR}/${network} ]];then
network=${BASH_NETWORKS_DIR}/${network}
else
return 2
fi
fi
fi
echo $network
}
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 networks
for dir in $lookin; do
# echo checking in $dir
if [[ -f $dir/.networks ]]; then
local _networks=$(cat $dir/.networks | tr -s "\n" " ")
for network in $_networks; do
[[ ! "$networks" == *" $network"* ]] && networks+=" $network" || echo $network already in list
# echo $networks
done
fi
done
for network in $networks; do
# echo $network
if [[ ${network} = *!* ]]; then
network=${network/!/}
BASH_NETWORK_HOME=$network
fi
network=$(network_path $network $2)
# echo $network
if [[ $network ]]; then
[[ $network =~ $BASH_NETWORK_HOME ]] && BASH_NETWORK_HOME_PATH=${network}
# echo adding $network to list of networks
BASH_SHELL_NETWORK_PATHS+="${network} "
fi
done
export BASH_NETWORK_HOME
export BASH_NETWORK_HOME_PATH
export BASH_SHELL_NETWORK_PATHS
# env | grep NETWORK
}
loadssh () { loadssh () {
module_load ssh module_load ssh
@ -24,80 +91,55 @@ loadssh () {
load_home_network() { load_home_network() {
# unset BASH_SHELL_NETWORKS_LOAD unset BASH_NETWORK_HOME
# export BASH_SHELL_HOME_NETWORK_LOAD=true unset BASH_NETWORK_HOME_PATH
_load_networks -h get_network_paths
# loadssh if [[ $BASH_NETWORK_HOME_PATH ]]; then
# echo loading home $BASH_NETWORK_HOME_PATH
shell_process_directory $BASH_NETWORK_HOME_PATH
[[ $BASH_SHELL_LOADED ]] && load_shell_host_user_dev
fi
} }
load_all_networks () { load_all_networks () {
# unset BASH_SHELL_HOME_NETWORK_LOAD # unset BASH_SHELL_HOME_NETWORK_LOAD
# export BASH_SHELL_NETWORKS_LOAD=true # export BASH_SHELL_NETWORKS_LOAD=true
_load_networks -a unset BASH_SHELL_NETWORK_PATHS
# dssh 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
} }
load_network () { load_a_network () {
unset BASH_SHELL_HOME_NETWORK_LOAD [[ ! $1 ]] && return 1
unset BASH_SHELL_NETWORKS_LOAD for dir in $1; do
_load_networks $@ # echo network $dir processed
# loadssh shell_process_directory $dir
done
} }
clear_networks () { clear_networks () {
unset BASH_SHELL_NETWORKS_LOAD unset BASH_SHELL_NETWORKS_LOAD
unset BASH_SHELL_HOME_NETWORK_LOAD unset BASH_SHELL_HOME_NETWORK_LOAD
unset BASH_SHELL_NETWORK_DIRS unset BASH_SHELL_NETWORK_PATHS
unset BASH_SHELL_NETWORK unset BASH_SHELL_NETWORK
reload_shell reload_shell
ssh_config ssh_config
} }
# called from reload_shell networks_load () {
_load_networks () {
# echo load_networks called $@ # echo load_networks called $@
[[ $1 == "-h" ]] && BASH_SHELL_HOME_NETWORK_LOAD=true && shift 1 [[ $1 == "-h" ]] && { shift 1; load_home_network || return $?; }
[[ $1 == "-a" ]] && BASH_SHELL_NETWORKS_LOAD=true && shift 1 [[ $1 == "-a" ]] && { shift 1; load_all_networks || return $?; }
[[ $1 ]] && load_a_network $@ || return 1
if [[ ! $BASH_SHELL_NETWORKS_LOAD ]] && [[ ! $BASH_SHELL_HOME_NETWORK_LOAD ]]; then # echo loading ssh
[[ ! $1 ]] && return 0
# echo $1 was passed
fi
# echo loading networks
module_load network-dirs
# echo before unset $BASH_SHELL_NETWORK_DIRS
unset BASH_SHELL_NETWORK_DIRS
unset BASH_NETWORK_HOME
unset BASH_NETWORK_HOME_DIR
# echo after unset $BASH_SHELL_NETWORK_DIRS
if [[ $1 ]]; then
network_dirs $1
else
# These will be added for all users on a host
network_dirs "$BASH_SHELL_HOST/.networks"
# these added only for specific user on a host and take precedence
network_dirs "$HOME/${BASH_SHELL_USER:-"shell"}/.networks"
# these added only for development and take precedence
network_dirs "$BASH_SHELL_DEV/.networks"
fi
if [[ $BASH_SHELL_HOME_NETWORK_LOAD ]]; then
# echo Home nework $BASH_NETWORK_HOME_DIR processed
shell_process_directory $BASH_NETWORK_HOME_DIR
export BASH_SHELL_NETWORK_DIRS=$BASH_NETWORK_HOME_DIR
else
for dir in ${1:-$BASH_SHELL_NETWORK_DIRS}; do
# echo network $dir processed
shell_process_directory $dir
done
fi
unset BASH_SHELL_NETWORKS_LOAD
unset BASH_SHELL_HOME_NETWORK_LOAD
loadssh loadssh
[[ $BASH_SHELL_LOADED ]] && load_shell_host_user_dev [[ $BASH_SHELL_LOADED ]] && load_shell_host_user_dev
# echo loaded $BASH_SHELL_NETWORK_DIRS
} }

View File

@ -1,8 +1,10 @@
#!/bin/bash #!/bin/bash
# this will superceed the ssh binary in order to source all the config files # this will superceed the ssh binary in order to source all the config files
module_load file # loads find and build_file module_load file # loads find and build_file
function ssh_config() { function ssh_config() {
# echo ssh_config $@
local CDIRS local CDIRS
local CDIR local CDIR
local DIRS local DIRS
@ -69,9 +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_NETWORK_DIRS "$BASH_SHELL_HOST" "$HOME/$BASH_SHELL_USER" "$BASH_SHELL_DEV")
# echo DIRS "${DIRS[@]}" # echo DIRS "${DIRS[@]}"
# echo $SSH_CONFIG # echo $SSH_CONFIG
CDIRS=() CDIRS=()
@ -111,14 +111,14 @@ if [[ (! $SSH_CONFIG || $RESET ) ]]; then
for CDIR in "${CDIRS[@]}"; do for CDIR in "${CDIRS[@]}"; do
# FILES=$(find -n '*.cfg' -d 0 $CDIR) # FILES=$(find -n '*.cfg' -d 0 $CDIR)
for f in $(_find -n '*.cfg' -p 'archive off' -d 0 $CDIR); do for f in $(_find -n '*.cfg' -p 'archive off' -d 0 $CDIR); do
# echo "Processing $f"; # echo "adding ssh config file $f";
[[ $f ]] && build_file "$f" $SSH_CONFIG [[ $f ]] && build_file "$f" $SSH_CONFIG
done done
[[ -f $CDIR/config.env ]] && build_file $CDIR/config.env /tmp/config.env [[ -f $CDIR/config.env ]] && build_file $CDIR/config.env /tmp/config.env
done done
# append any tradtional home config # append any tradtional home config
[[ -f "$HOME/.ssh/config" ]] && build_file -l "$HOME/.ssh/config" $SSH_CONFIG [[ -f "$HOME/.ssh/config" ]] && build_file "$HOME/.ssh/config" $SSH_CONFIG
[[ -f "$HOME/.ssh/config.env" ]] && build_file "$HOME/.ssh/config.env" "/tmp/config.env" [[ -f "$HOME/.ssh/config.env" ]] && build_file "$HOME/.ssh/config.env" "/tmp/config.env"
# now replace any variables in file # now replace any variables in file

View File

@ -123,7 +123,7 @@ function umntBackup() {
function enable_mounts() { function enable_mounts() {
DIRS=($BASH_SHELL_NETWORK_DIRS "$BASH_SHELL_HOST" "$HOME/$BASH_SHELL_USER" "$BASH_SHELL_DEV") DIRS=($BASH_SHELL_NETWORK_PATHS "$BASH_SHELL_HOST" "$BASH_SHELL_USER_DIR" "$BASH_SHELL_DEV")
# echo DIRS "${DIRS[@]}" # echo DIRS "${DIRS[@]}"
CDIRS=() CDIRS=()
j=0 j=0
@ -144,7 +144,7 @@ function enable_mounts() {
for CDIR in "${CDIRS[@]}"; do for CDIR in "${CDIRS[@]}"; do
# echo $CDIR # echo $CDIR
for f in $(_find -n '*.mnt' -p 'archive off' -d 0 $CDIR); do for f in $(_find -n '*.mnt' -p 'archive off' -d 0 $CDIR); do
# echo "Processing $f"; # echo "adding mounts file $f";
[[ $f ]] && source "$f" [[ $f ]] && source "$f"
done done
done done