shell-network/modules/networks.mod

147 lines
3.7 KiB
Bash

#!/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
# network_dirs "$HOME/${BASH_SHELL_USER:-"shell"}/.networks"
# # these are exported from network_dirs
# # env | grep NETWORK
# # NETWORK_HOME
# # NETWORK_HOME_DIR
# # 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 () {
local lookin="$BASH_SHELL_HOST $BASH_SHELL_USER_DIR $BASH_SHELL_DEV"
local networks
local home
[[ $1 == "-h" ]] && home=true && shift 1
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
[[ $home ]] && echo $BASH_NETWORK_HOME_PATH || echo $BASH_SHELL_NETWORK_PATHS
# env | grep NETWORK
}
loadssh () {
module_load ssh
module_load ssh-config
ssh_config
module_load ssh-copy
module_load ssh-pubkey
module_load sshfs
enable_mounts
export BASH_SHELL_SSH_LOADED=true
# echo ssh modules are loaded
}
load_home_network() {
local home
home=$(get_network_paths -h)
# echo returned $home
if [[ $home ]]; then
# echo loading home $BASH_NETWORK_HOME_PATH
[[ ! "$BASH_SHELL_NETWORKS_LOADED" == *"$home"* ]] && BASH_SHELL_NETWORKS_LOADED+="$home "
export BASH_SHELL_NETWORKS_LOADED
fi
}
loaded_networks () {
echo $BASH_SHELL_NETWORKS_LOADED
}
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
networks=$(get_network_paths)
fi
# echo networks to load $networks
for path in $networks; do
# echo network $path processed
shell_process_directory $path
[[ ! "$BASH_SHELL_NETWORKS_LOADED" == *"$path"* ]] && BASH_SHELL_NETWORKS_LOADED+="$path "
done
export BASH_SHELL_NETWORKS_LOADED
}
clear_networks () {
unset BASH_SHELL_NETWORKS_LOAD
unset BASH_SHELL_HOME_NETWORK_LOAD
unset BASH_SHELL_NETWORK_PATHS
unset BASH_SHELL_NETWORK
reload_shell
ssh_config
}
networks_load () {
# echo networks_load called $@
[[ $1 == "-h" ]] && { shift 1; load_home_network || return $?; } || load_networks "$@"
if [[ $BASH_SHELL_SSH_LOADED ]]; then enable_mounts; ssh_config; else loadssh; fi
[[ $BASH_SHELL_LOADED ]] && load_shell_host_user_dev
}