refactor shell environmnet so user can have create customized shell.env

refactor default loading of networks
master
Kebler Network System Administrator 2022-12-30 09:18:39 -08:00
parent 34311b8183
commit bf62d67339
7 changed files with 125 additions and 82 deletions

View File

@ -45,7 +45,7 @@ export BASH_SHELL_NETWORK=$BASH_SHELL/network
export BASH_SHELL_HOST=$BASH_SHELL/host
# by default SHELL sources will be looked for under $HOME/shell
# but can be user set below to $HOME/$BASH_SHELL_USER
# but can be user set below to BASH_SHELL_USER_DIR
# export BASH_SHELL_USER=<some directory under $HOME>
if there is a script setup.sh in the root of these directoies it will be run. If not then all files no matter how deep will be sourced in alpha order order.

83
load.sh
View File

@ -1,30 +1,5 @@
#!/bin/bash
reload_shell () {
source $BASH_SHELL_BASE/load.sh $@
}
load_shell_host () {
module_load shell-process-directory
[[ $BASH_SHELL_HOST ]] && shell_process_directory $BASH_SHELL_HOST
}
load_shell_user () {
module_load shell-process-directory
[[ -d $HOME/${BASH_SHELL_USER:-shell} ]] && shell_process_directory $HOME/${BASH_SHELL_USER:-shell}
}
load_shell_dev () {
module_load shell-process-directory
[[ $BASH_SHELL_DEV ]] && shell_process_directory $BASH_SHELL_DEV
}
load_shell_host_user_dev () {
load_shell_host
load_shell_user
load_shell_dev
}
# PRIMARY SHELL LOAD SCRIPT - RUN FOR ALL INTERACTIVE SHELLS
# don't bother if bash is not the shell
@ -42,6 +17,42 @@ function debug () {
}
fi
reload_shell () {
source $BASH_SHELL_BASE/load.sh $@
}
load_custom_shell_env () {
[[ -f $BASH_SHELL_HOST/shell.env ]] && source $BASH_SHELL_HOST/shell.env
[[ -f $HOME/${BASH_SHELL_USER:-shell}/shell.env ]] && source $HOME/${BASH_SHELL_USER:-shell}/shell.env
[[ -f $BASH_SHELL_DEV/shell.env ]] && shell_process_directory $BASH_SHELL_DEV/shell.env
}
load_shell_host () {
module_load shell-process-directory
[[ -d $BASH_SHELL_HOST ]] && shell_process_directory $BASH_SHELL_HOST
}
load_shell_user () {
module_load shell-process-directory
BASH_SHELL_USER_DIR=${BASH_SHELL_USER_DIR:-$HOME/shell}
[[ -d $BASH_SHELL_USER_DIR ]] && shell_process_directory $BASH_SHELL_USER_DIR
}
load_shell_dev () {
module_load shell-process-directory
[[ $BASH_SHELL_DEV ]] && shell_process_directory $BASH_SHELL_DEV
}
load_shell_host_user_dev () {
load_shell_host
load_shell_user
load_shell_dev
}
### START LOAD SCRIPT #####
load_custom_shell_env
([[ $BASH_ENV ]] && [[ ! $- == *i* ]] && [[ $BASH_USE_ALIAS ]]) && shopt -s expand_aliases
# process the base directory by default
@ -60,10 +71,10 @@ module_load shell-process-directory
# shell_process_directory $BASH_SHELL_BASE
# shell_process_directory $BASH_SHELL_ANY_HOST
# shell_process_directory $BASH_SHELL_HOST
# export BASH_SHELL_BASE_LOADED=true
# process base any
# process base and any host and any network directories
# as set in shell.env
for dir in ${1:-$BASH_SHELL_DIRS}; do
# env | grep BASH
# echo base $dir
@ -71,15 +82,19 @@ for dir in ${1:-$BASH_SHELL_DIRS}; do
[[ "$dir" = "$BASH_SHELL_BASE" ]] && export BASH_SHELL_BASE_LOADED=true
done
# set one or the other of these in shell.env if you want to load networks with every shell
if [[ $BASH_SHELL_HOME_NETWORK_LOAD ]] || [[ $BASH_SHELL_NETWORKS_LOAD ]] || [[ $BASH_SHELL_NETWORK ]] ; then
if [[ $BASH_SHELL_ANY_NETWORK ]]; then
module_load networks
[[ $(is_function _load_networks) ]] && _load_networks $BASH_SHELL_NETWORK
fi
# # set one or the other of these in shell.env if you want to load networks with every shell
if [[ $BASH_SHELL_HOME_NETWORK_LOAD ]] || [[ $BASH_SHELL_ALL_NETWORKS_LOAD ]] || [[ $BASH_SHELL_NETWORKS_LOAD ]] ; then
module_load networks
if [[ $BASH_SHELL_ALL_NETWORKS_LOAD ]]; then
networks_load -a
else
[[ $BASH_SHELL_HOME_NETWORK_LOAD ]] && networks_load -h
fi
[[ $BASH_SHELL_NETWORKS_LOAD ]] && networks_load $BASH_SHELL_NETWORKS_LOAD
fi
load_shell_host_user_dev
load_shell_host
export BASH_SHELL_LOADED=true

View File

@ -56,7 +56,7 @@ fi
# BASH_SHELL_DIRS holds shell directories to search for modules. If will be searched
# in revsere order, user or network repos over host over base
DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} )
[ -d $HOME/$BASH_SHELL_USER ] && DIRS=("${DIRS[@]}" "$HOME/$BASH_SHELL_USER")
[ -d BASH_SHELL_USER_DIR ] && DIRS=("${DIRS[@]}" "BASH_SHELL_USER_DIR")
[ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV")
cnt=${#DIRS[@]}

View File

@ -1,5 +1,31 @@
#!/bin/bash
shell_source_subdirs () {
# default processing
[[ ! -d $1 ]] && echo no directory $1 from which to process subdirectories && return 1
local DIR=
local SUBDIRS
local IGNORE_FILE
DIR=$1
SUBDIRS=$([[ -f "$DIR/.bash-shell-include" ]] && cat "$DIR/.bash-shell-include" || cat "$BASH_SHELL_BASE/.bash-shell-include")
[[ $SSH_SESSION ]] && SUBDIRS+=" ssh/session"
IGNORE_FILE="$([[ -f "$DIR/.bash-shell-ignore" ]] && echo "$DIR" || echo "$BASH_SHELL_BASE")/.bash-shell-ignore"
# echo $DIR
# echo $SUBDIRS
for SUBDIR in $SUBDIRS; do
if [ -e "$DIR/$SUBDIR" ]; then
# echo processing subdirectory $DIR/$SUBDIR
# must quote all globs to avoid bash glob expansion which is likely on
# TODO have default set of ignores plus check for shell-ignore
# source_dir -p "archive" -x '"*.off" "*.md" "*TODO*" "LICENSE" "*.sample"' -d 0 $DIR/$SUBDIR
# source_dir -p "archive" -x "$excludes" -d 0 $DIR/$SUBDIR
source_dir -f "$IGNORE_FILE" -d 0 $DIR/$SUBDIR
# else
# echo no subdirectory $DIR/$SUBDIR to process, ignorning
fi
done
}
function shell_process_directory () {
local SUBDIRS
local DIR
@ -7,8 +33,8 @@ function shell_process_directory () {
module_load file
[[ $? -ne 0 ]] && echo unable to access the file module, aborting load && return 1
DIR=${1:-$BASH_SHELL_BASE}
# echo sourcing directory $DIR
# if [[ $DIR = "$BASH_SHELL_BASE" ]]; then
# BASH_SHELL_IGNORE=$(shell_get_ignores)
@ -32,29 +58,31 @@ function shell_process_directory () {
fi
if [[ $DIR != "$BASH_SHELL_BASE" ]] && [[ -f $DIR/load.sh ]]; then
# echo "$DIR/load.sh" found, running instead of default load
#
echo "$DIR/load.sh" found, running instead of base load.sh script
source "$DIR/load.sh"
else
# default processing
local SUBDIRS
local IGNORE_FILE
SUBDIRS=$([[ -f "$DIR/.bash-shell-include" ]] && cat "$DIR/.bash-shell-include" || cat "$BASH_SHELL_BASE/.bash-shell-include")
[[ $SSH_SESSION ]] && SUBDIRS+=" ssh/session"
IGNORE_FILE="$([[ -f "$DIR/.bash-shell-ignore" ]] && echo "$DIR" || echo "$BASH_SHELL_BASE")/.bash-shell-ignore"
# echo $DIR
# echo $SUBDIRS
for SUBDIR in $SUBDIRS; do
if [ -e "$DIR/$SUBDIR" ]; then
# echo processing subdirectory $DIR/$SUBDIR
# must quote all globs to avoid bash glob expansion which is likely on
# TODO have default set of ignores plus check for shell-ignore
# source_dir -p "archive" -x '"*.off" "*.md" "*TODO*" "LICENSE" "*.sample"' -d 0 $DIR/$SUBDIR
# source_dir -p "archive" -x "$excludes" -d 0 $DIR/$SUBDIR
source_dir -f "$IGNORE_FILE" -d 0 $DIR/$SUBDIR
# else
# echo no subdirectory $DIR/$SUBDIR to process, ignorning
fi
done
shell_source_subdirs $DIR
# # default processing
# local SUBDIRS
# local IGNORE_FILE
# SUBDIRS=$([[ -f "$DIR/.bash-shell-include" ]] && cat "$DIR/.bash-shell-include" || cat "$BASH_SHELL_BASE/.bash-shell-include")
# [[ $SSH_SESSION ]] && SUBDIRS+=" ssh/session"
# IGNORE_FILE="$([[ -f "$DIR/.bash-shell-ignore" ]] && echo "$DIR" || echo "$BASH_SHELL_BASE")/.bash-shell-ignore"
# # echo $DIR
# # echo $SUBDIRS
# for SUBDIR in $SUBDIRS; do
# if [ -e "$DIR/$SUBDIR" ]; then
# # echo processing subdirectory $DIR/$SUBDIR
# # must quote all globs to avoid bash glob expansion which is likely on
# # TODO have default set of ignores plus check for shell-ignore
# # source_dir -p "archive" -x '"*.off" "*.md" "*TODO*" "LICENSE" "*.sample"' -d 0 $DIR/$SUBDIR
# # source_dir -p "archive" -x "$excludes" -d 0 $DIR/$SUBDIR
# source_dir -f "$IGNORE_FILE" -d 0 $DIR/$SUBDIR
# # else
# # echo no subdirectory $DIR/$SUBDIR to process, ignorning
# fi
# done
fi
# echo "done sourcing directory $DIR"
# else

View File

@ -14,9 +14,9 @@ isFile() {
fi
}
# env_file_pretty () {
# }
#TODO
# env_file_pretty () {
# }
# https://www.baeldung.com/linux/envsubst-command
env_subs_file () {
@ -60,14 +60,16 @@ env_subs_file () {
build_file () {
# local file
# echo build file $1 $2
# todo merge no cr
[[ $1 == "-f" ]] && file=true && shift 1
[[ -f "$2" ]] || (iecho "output file $2 does not exist";return 1)
[[ -f "$2" ]] || { iecho "output file $2 does not exist"; return 1; }
if [[ -f "$1" ]]; then
# echo $1
# echo adding file $1 to $2
[[ $file ]] && echo -e "\n####### ADDED $1 ########" >> $2
# remove comment lines, remove blank last line
cat "$1" | sed '/^\s*#/d' | sed '/^$/{:a;N;s/\n$//;ta}' >> $2
else iecho "no such file $1 to append"
[[ ! $file ]] && echo -e "\n" >> $2
else iecho "no such file $1 to append to $2"
fi
}

View File

@ -11,9 +11,9 @@
if ( [[ $SHELL_INTERACTIVE ]] );then
if ( [[ $BASH_SHELL_BASE_LOADED = true ]] ) ; then
export BASH_SHELL_USER=${BASH_SHELL_USER:-"shell"}
# echo loading user $USER shell at $HOME/$BASH_SHELL_USER
[[ -d $HOME/$BASH_SHELL_USER ]] && shell_process_directory "$HOME/$BASH_SHELL_USER" ||\
echo no user shell directory $HOME/$BASH_SHELL_USER to process, create one or clone a template
# echo loading user $USER shell at BASH_SHELL_USER_DIR
[[ -d BASH_SHELL_USER_DIR ]] && shell_process_directory "BASH_SHELL_USER_DIR" ||\
echo no user shell directory BASH_SHELL_USER_DIR to process, create one or clone a template
fi
# uncomment to add non-interactive setup/sourcing
# else

View File

@ -4,6 +4,10 @@
# In a nutshell its a set of directories that are sourced when creating a shell
# The repos allow one to organize functionality across machines, users and networks
# In general do not change or edit this base shell.env file
# Instead add a custom shell.env to specific shell host directory,
# user shell directory or dev shell directory
# echo DEBUG: sourcing shell.env
# This file gets sourced early in /etc/profile for all login and
@ -15,7 +19,6 @@
# with NO_BASH_SHELL_SSH=true
# this does not effect non-interactive login shells like at user first login
# if bash is not installed then don't bother to continue
! command -v bash >/dev/null 2>&1 && echo no bash command && return 1
export SHELL=$(command -v bash )
@ -98,41 +101,34 @@ BASH_SHELL_DEV=${BASH_SHELL_DEV:-/opt/shell}
[[ -d $BASH_SHELL_DEV ]] && export BASH_SHELL_DEV
# by default USER shell sources will be looked for under $HOME/shell
# but the default directory for ALL users can be set manually below
# export BASH_SHELL_USER=<some directory under $HOME>
export BASH_SHELL_USER=${BASH_SHELL_USER:-shell}
# or add this export to .bashrc for custom directory. This can be done during or after using the user setup script
# export BASH_SHELL_USER_DIR=$HOME/shell
# set up default location to look for other network shell directories
BASH_NETWORKS_DIR=$parent/networks
# BASH_NETWORKS_DIR=<some directory> # alt directory to look for networks
export BASH_NETWORKS_DIR
# set one or the other of these if you want to load networks with every shell
export BASH_SHELL_HOME_NETWORK_LOAD=true
# set one or the other of these here for all users or put in shell.env under HOST/USER/DEV directories
# if you want to load networks with every shell
# export BASH_SHELL_HOME_NETWORK_LOAD=true
# export BASH_SHELL_NETWORKS_LOAD=true
# export BASH_SHELL_NETWORK=<some directory>
# export BASH_SHELL_NETWORK=238.kebler.net
# now load and export module loading functions library so it is always available
source $BASH_SHELL_BASE/module.lib
# echo done shell.env
# echo ALL DIRS: $BASH_SHELL_DIRS
# cat $HOME/.bashrc
# source "$HOME/.bashrc"
# env | grep BASH
# env | grep NETWORK_DIRS
# echo ---------------------------------
# echo $BASH_SHELL_DIRS
# echo ---------------------------------
# echo end shell env
#archived ---for deletion or better explanation
# echo enabling aliases with non-interactive shell
# DEPRECATED # export BASH_USE_ALIAS=true # will source aliases for non-interactive
# if not using implicit sourcing for non-interactive shells then one can do this per script
@ -143,3 +139,5 @@ source $BASH_SHELL_BASE/module.lib
# < your script code >
# shopt -u expand_aliases
####################