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

View File

@ -1,6 +1,7 @@
#!/bin/bash
# module_load network-dirs
# # These will be loaded for all users on a host
# network_dirs "$BASH_SHELL_HOST/.networks"
# # these loaded only for specific user on a host and take precedence
@ -9,7 +10,73 @@
# # env | grep NETWORK
# # NETWORK_HOME
# # 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 () {
module_load ssh
@ -24,80 +91,55 @@ loadssh () {
load_home_network() {
# unset BASH_SHELL_NETWORKS_LOAD
# export BASH_SHELL_HOME_NETWORK_LOAD=true
_load_networks -h
# loadssh
unset BASH_NETWORK_HOME
unset BASH_NETWORK_HOME_PATH
get_network_paths
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 () {
# unset BASH_SHELL_HOME_NETWORK_LOAD
# export BASH_SHELL_NETWORKS_LOAD=true
_load_networks -a
# dssh
unset BASH_SHELL_NETWORK_PATHS
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 () {
unset BASH_SHELL_HOME_NETWORK_LOAD
unset BASH_SHELL_NETWORKS_LOAD
_load_networks $@
# loadssh
load_a_network () {
[[ ! $1 ]] && return 1
for dir in $1; do
# echo network $dir processed
shell_process_directory $dir
done
}
clear_networks () {
unset BASH_SHELL_NETWORKS_LOAD
unset BASH_SHELL_HOME_NETWORK_LOAD
unset BASH_SHELL_NETWORK_DIRS
unset BASH_SHELL_NETWORK_PATHS
unset BASH_SHELL_NETWORK
reload_shell
ssh_config
}
# called from reload_shell
_load_networks () {
networks_load () {
# echo load_networks called $@
# echo load_networks called $@
[[ $1 == "-h" ]] && BASH_SHELL_HOME_NETWORK_LOAD=true && shift 1
[[ $1 == "-a" ]] && BASH_SHELL_NETWORKS_LOAD=true && shift 1
if [[ ! $BASH_SHELL_NETWORKS_LOAD ]] && [[ ! $BASH_SHELL_HOME_NETWORK_LOAD ]]; then
[[ ! $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
[[ $1 == "-h" ]] && { shift 1; load_home_network || return $?; }
[[ $1 == "-a" ]] && { shift 1; load_all_networks || return $?; }
[[ $1 ]] && load_a_network $@ || return 1
# echo loading ssh
loadssh
[[ $BASH_SHELL_LOADED ]] && load_shell_host_user_dev
# echo loaded $BASH_SHELL_NETWORK_DIRS
}

View File

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

View File

@ -123,7 +123,7 @@ function umntBackup() {
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[@]}"
CDIRS=()
j=0
@ -144,7 +144,7 @@ function enable_mounts() {
for CDIR in "${CDIRS[@]}"; do
# echo $CDIR
for f in $(_find -n '*.mnt' -p 'archive off' -d 0 $CDIR); do
# echo "Processing $f";
# echo "adding mounts file $f";
[[ $f ]] && source "$f"
done
done