From bcba8bbfd438b3372d89070ff9d65fe3ddddbc23 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Wed, 18 Jan 2023 16:43:21 -0800 Subject: [PATCH] refactor shell install scripts add bindfs and new dir_copy script --- .bash-shell-include | 4 +- .gitignore | 1 + TODO.md | 4 - alias/network | 3 - env/{00-options.env => 00-shell-options} | 0 env/01-path | 18 --- env/01-system-path | 12 ++ env/02-defaults.env | 5 - env/02-prompt | 2 + env/archive/path | 17 --- env/colors.env | 22 +--- function/acl | 1 - function/archive/aws | 46 ------- function/archive/aws-recordset | 86 ------------- function/archive/aws-set-network-ip-old | 105 ---------------- function/archive/caddy | 12 -- function/archive/nas | 21 ---- function/archive/test | 1 - function/rsync | 16 --- {function => functions}/01-basic | 15 +-- functions/02-system-path | 1 + functions/03-security | 1 + functions/logging | 1 + {setup => install}/.gitignore | 1 + {setup => install/files}/etc/bash.bashrc | 0 {setup => install/files}/etc/profile | 0 .../files}/etc/profile.d/02-root.sh | 0 .../files}/etc/profile.d/03-startup.sh | 0 install/files/root/.bash_profile | 23 ++++ install/files/root/.bashrc | 24 ++++ install/files/root/.profile | 7 ++ {setup => install/files}/user/.bash_logout | 0 install/files/user/.bash_profile | 27 ++++ {setup => install/files}/user/.bashrc | 5 +- install/files/user/.profile | 2 + install/install.sh | 17 +++ install/scripts/copy-etc.sh | 16 +++ install/scripts/copy-root.sh | 11 ++ install/scripts/copy-user.sh | 31 +++++ install/scripts/remote-install.sh | 6 + lang/gcc | 2 - misc/less | 2 - misc/qt | 1 - misc/xterm.off | 5 - modules/add-ppa.sh | 117 ------------------ modules/{scripting => }/debug.lib | 0 modules/scripting/bundle.sh | 15 ++- .../comment => modules/scripting/comment.lob | 0 .../logit => modules/scripting/logit.func | 0 {function => modules}/systemd | 0 modules/utility/bindfs.lib | 33 +++++ modules/utility/dir.lib | 56 +++++++++ modules/utility/dirs.sh | 81 ------------ modules/utility/env.lib | 3 +- .../utility/fancy-prompt.func | 32 ++++- modules/utility/filesystem.mod | 1 + modules/{ => utility}/github-bin-fetch.sh | 1 + modules/{acl.lib => utility/security.lib} | 75 +++++++++++ .../utility/system-path.lib | 0 modules/utility/{user.sh => user.lib} | 43 +++++++ modules/utility/{watch.sh => watch.func} | 0 setup/backup.sh | 36 ------ setup/deploy-user.sh | 42 ------- setup/deploy.sh | 49 -------- setup/fetch.sh | 16 --- setup/root/.bash_profile | 5 - setup/root/.bashrc | 1 - setup/root/.profile | 2 - setup/user/.bash_profile | 6 - setup/user/.profile | 2 - 70 files changed, 442 insertions(+), 748 deletions(-) delete mode 100644 TODO.md delete mode 100644 alias/network rename env/{00-options.env => 00-shell-options} (100%) delete mode 100644 env/01-path create mode 100644 env/01-system-path delete mode 100644 env/02-defaults.env create mode 100644 env/02-prompt delete mode 100644 env/archive/path delete mode 100644 function/acl delete mode 100755 function/archive/aws delete mode 100644 function/archive/aws-recordset delete mode 100644 function/archive/aws-set-network-ip-old delete mode 100644 function/archive/caddy delete mode 100644 function/archive/nas delete mode 100644 function/archive/test delete mode 100755 function/rsync rename {function => functions}/01-basic (83%) create mode 100644 functions/02-system-path create mode 100644 functions/03-security create mode 100644 functions/logging rename {setup => install}/.gitignore (60%) rename {setup => install/files}/etc/bash.bashrc (100%) rename {setup => install/files}/etc/profile (100%) rename {setup => install/files}/etc/profile.d/02-root.sh (100%) rename {setup => install/files}/etc/profile.d/03-startup.sh (100%) create mode 100644 install/files/root/.bash_profile create mode 100644 install/files/root/.bashrc create mode 100644 install/files/root/.profile rename {setup => install/files}/user/.bash_logout (100%) create mode 100644 install/files/user/.bash_profile rename {setup => install/files}/user/.bashrc (75%) create mode 100644 install/files/user/.profile create mode 100644 install/install.sh create mode 100755 install/scripts/copy-etc.sh create mode 100755 install/scripts/copy-root.sh create mode 100644 install/scripts/copy-user.sh create mode 100644 install/scripts/remote-install.sh delete mode 100644 lang/gcc delete mode 100644 misc/less delete mode 100644 misc/qt delete mode 100644 misc/xterm.off delete mode 100755 modules/add-ppa.sh rename modules/{scripting => }/debug.lib (100%) rename function/comment => modules/scripting/comment.lob (100%) rename function/logit => modules/scripting/logit.func (100%) rename {function => modules}/systemd (100%) create mode 100644 modules/utility/bindfs.lib create mode 100644 modules/utility/dir.lib delete mode 100644 modules/utility/dirs.sh rename misc/00-prompt => modules/utility/fancy-prompt.func (60%) rename modules/{ => utility}/github-bin-fetch.sh (89%) rename modules/{acl.lib => utility/security.lib} (68%) mode change 100755 => 100644 rename function/02-path => modules/utility/system-path.lib (100%) rename modules/utility/{user.sh => user.lib} (77%) rename modules/utility/{watch.sh => watch.func} (100%) delete mode 100755 setup/backup.sh delete mode 100755 setup/deploy-user.sh delete mode 100755 setup/deploy.sh delete mode 100755 setup/fetch.sh delete mode 100644 setup/root/.bash_profile delete mode 100644 setup/root/.bashrc delete mode 100644 setup/root/.profile delete mode 100644 setup/user/.bash_profile delete mode 100644 setup/user/.profile diff --git a/.bash-shell-include b/.bash-shell-include index 3d41e54..848b7c6 100644 --- a/.bash-shell-include +++ b/.bash-shell-include @@ -1,7 +1,7 @@ -function +functions +function alias env -misc lang app tools diff --git a/.gitignore b/.gitignore index 9db9d0c..7a22207 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ */archive/* +*archive* /ssh/config/_config diff --git a/TODO.md b/TODO.md deleted file mode 100644 index dd288bd..0000000 --- a/TODO.md +++ /dev/null @@ -1,4 +0,0 @@ -# TODOS - -add ability to disable loading shell repos -add ability to disable alias in non-interactive and interactive shells diff --git a/alias/network b/alias/network deleted file mode 100644 index 4f5acb5..0000000 --- a/alias/network +++ /dev/null @@ -1,3 +0,0 @@ -alias ports="netstat -tulpn" -alias flushdns="systemctl restart systemd-resolved" -alias essh="sudo editor /etc/ssh/sshd_config" diff --git a/env/00-options.env b/env/00-shell-options similarity index 100% rename from env/00-options.env rename to env/00-shell-options diff --git a/env/01-path b/env/01-path deleted file mode 100644 index b318959..0000000 --- a/env/01-path +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/bash -# if [[ $(groups | grep sudo ) ]]; then -# path_insert /sbin after /usr/bin -# pathg bin -# path_insert /usr/sbin after :/sbin -# pathg bin -# path_insert /usr/local/sbin after /usr/sbin -# pathg sbin -# fi - - if [[ $(groups | grep sudo ) ]]; then - export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin - fi - -# with prependthese take prescendence over stuff ones in /usr and /bin -path_prepend /opt/bin -path_prepend "$HOME/bin" -path_prepend "$HOME/.local/bin" diff --git a/env/01-system-path b/env/01-system-path new file mode 100644 index 0000000..c3f73d9 --- /dev/null +++ b/env/01-system-path @@ -0,0 +1,12 @@ +#!/bin/bash + +# if sudo user then give access to these paths + if [[ $(groups | grep sudo ) ]]; then + export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin + fi + +# prepend these take prescendence over stuff ones in /usr and /bin +# requires system-path module loaded +path_prepend /opt/bin +path_prepend "$HOME/bin" +path_prepend "$HOME/.local/bin" diff --git a/env/02-defaults.env b/env/02-defaults.env deleted file mode 100644 index 8058f21..0000000 --- a/env/02-defaults.env +++ /dev/null @@ -1,5 +0,0 @@ -# used by aliases that need an editor -# export EDITOR=atom -# export ELECTRON_TRASH=gio -export EDITOR=nano -export BROWSER=google-chrome diff --git a/env/02-prompt b/env/02-prompt new file mode 100644 index 0000000..433c3b5 --- /dev/null +++ b/env/02-prompt @@ -0,0 +1,2 @@ +module_load fancy-prompt +fancy_prompt \ No newline at end of file diff --git a/env/archive/path b/env/archive/path deleted file mode 100644 index 689062d..0000000 --- a/env/archive/path +++ /dev/null @@ -1,17 +0,0 @@ -# set PATH so it includes user's private bin if it exists -if [ -d "$HOME/bin" ] ; then - PATH="$HOME/bin:$PATH" -fi - -# set PATH so it includes user's private bin if it exists -if [ -d "$HOME/.local/bin" ] ; then - PATH="$HOME/.local/bin:$PATH" -fi - -# if running bash -if [ -n "$BASH_VERSION" ]; then - # include .bashrc if it exists - if [ -f "$HOME/.bashrc" ]; then - . "$HOME/.bashrc" - fi -fi diff --git a/env/colors.env b/env/colors.env index 59f763e..121acb9 100644 --- a/env/colors.env +++ b/env/colors.env @@ -1,22 +1,2 @@ -#!/bin/bash -export TERM=xterm-color -export CLICOLOR=1 -export LSCOLORS=ExFxCxDxBxegedabagacad +# !/bin/bash -export COLOR_NC='\e[0m' # No Color -export COLOR_BLACK='\e[0;30m' -export COLOR_GRAY='\e[1;30m' -export COLOR_RED='\e[0;31m' -export COLOR_LIGHT_RED='\e[1;31m' -export COLOR_GREEN='\e[0;32m' -export COLOR_LIGHT_GREEN='\e[1;32m' -export COLOR_BROWN='\e[0;33m' -export COLOR_YELLOW='\e[1;33m' -export COLOR_BLUE='\e[0;34m' -export COLOR_LIGHT_BLUE='\e[1;34m' -export COLOR_PURPLE='\e[0;35m' -export COLOR_LIGHT_PURPLE='\e[1;35m' -export COLOR_CYAN='\e[0;36m' -export COLOR_LIGHT_CYAN='\e[1;36m' -export COLOR_LIGHT_GRAY='\e[0;37m' -export COLOR_WHITE='\e[1;37m' diff --git a/function/acl b/function/acl deleted file mode 100644 index 468fb51..0000000 --- a/function/acl +++ /dev/null @@ -1 +0,0 @@ -module_load acl \ No newline at end of file diff --git a/function/archive/aws b/function/archive/aws deleted file mode 100755 index 14ef56b..0000000 --- a/function/archive/aws +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash -# aws cli from docker image on any machine -function aws() { -AWS_CLI_IMAGE=${AWS_CLI_IMAGE:-amazon/aws-cli:latest} -AWS_WORKING_DIR=${AWS_WORKING_DIR:-$HOME/awscli} -local cmd -cmd="docker run --rm -ti - --env AWS_ACCESS_KEY_ID - --env AWS_CONFIG_FILE - --env AWS_SHARED_CREDENTIALS_FILE - --env AWS_SECRET_ACCESS_KEY - --env AWS_DEFAULT_REGION - -v ${HOME}/.aws:/root/.aws - -v ${AWS_WORKING_DIR}:/aws - ${AWS_CLI_IMAGE} $* - --no-cli-pager - " - # echo ${cmd} - # local res - echo "$(eval ${cmd})" - # echo $RES - -} - -# `! [ -z $AWS_ACCESS_KEY_ID ] && echo "--env AWS_ACCESS_KEY_ID"` \ -# `! [ -z $AWS_SECRET_ACCESS_KEY ] && echo "--env AWS_SECRET_ACCESS_KEY"` \ -# `! [ -z $AWS_DEFAULT_REGION ] && echo "--env AWS_DEFAULT_REGION"` \ - -test () { - -echo $(aws route53 list-hosted-zones) - -} - -aws-get-zone-id () { -local records -local domain -domain=$(get-domain $1) -echo $domain -aws route53 list-hosted-zones --query "'HostedZones[?Name==\`${domain}.\`]'" -echo returned -echo $RES -# local res -# res=$(echo $records | grep ID | awk -F'[\"#]+' '{print $4}' | awk -F'[/#]+' '{print $3}') -# echo "$res" -} diff --git a/function/archive/aws-recordset b/function/archive/aws-recordset deleted file mode 100644 index 84e534d..0000000 --- a/function/archive/aws-recordset +++ /dev/null @@ -1,86 +0,0 @@ -#!/bin/bash - -function aws-get-zone-record () { - local record - echo get record $1 $(get-domain $1) - record=$( - aws route53 list-resource-record-sets \ - --hosted-zone-id $(aws-get-zone-id $1) - ) - echo "$record" -} - -function aws-get-zone-record-prop () { - local record - record=$(aws-get-zone-record $1) - local res - res=$(get-prop-value "$record" $2) - echo "$res" -} - - -function aws-get-zone-record-value () { - local record - record=$(aws-get-zone-record $1) - local res - res=$(get-prop-value "$record" "ResourceRecords[0].Value") - echo "$res" -} - -function aws-update-zone-record-value () { - -if [ "$#" -ne 2 ]; then - echo "Both recordset and value required" - exit 0 -fi - -RECORDSET=$1 -VALUE=$2 -COMMENT=${3:-"Updating Record"} -RECORD=$(aws-get-zone-record "$RECORDSET") -echo the reccord $RECORD -# ZONENAME=$(get-domain $RECORDSET) -ZONEID=$(aws-get-zone-id "$(get-domain $RECORDSET )") -# The Time-To-Live of this recordset -# echo Zone Name and ID for recordset $RECORDSET $ZONENAME $ZONEID -# TTL=60 # call this later with increase - # Fill a temp file with valid JSON -CHANGE="{ - \"Comment\":\"$COMMENT\", - \"Changes\":[ - { - \"Action\":\"UPSERT\", - \"ResourceRecordSet\":{ - \"ResourceRecords\":[ - { - \"Value\":\"$VALUE\" - } - ], - \"Name\":\"$RECORDSET\", - \"Type\":\"$(get-prop-value "$RECORD" Type)\", - \"TTL\":\"$(get-prop-value "$RECORD" TTL)\" - } - } - ] -} -" -echo updating record with -batch=\'${CHANGE}\' -echo batch - - - # Update the Hosted Zone record -CMD="aws route53 change-resource-record-sets - --hosted-zone-id ${ZONEID} - --change-batch $batch - " - -echo "${CMD}" -local res -res=$($CMD) -echo "$res" - - -# echo confirm change $(aws-get-zone-record-value $RECORDSET) -# end function -} diff --git a/function/archive/aws-set-network-ip-old b/function/archive/aws-set-network-ip-old deleted file mode 100644 index 81986c6..0000000 --- a/function/archive/aws-set-network-ip-old +++ /dev/null @@ -1,105 +0,0 @@ -#!/bin/bash -function route53-set-record () { -# (optional) You might need to set your PATH variable at the top here -# depending on how you run this script -# PATH=PATH - -# Hosted Zone ID e.g. BJBK35SKMM9OE -ZONEID="ZF04FGKB9QHN2" - -# The CNAME you want to update e.g. hello.example.com -RECORDSET="238.kebler.net" - -# More advanced options below -# The Time-To-Live of this recordset -TTL=300 -# Change this if you want -COMMENT="Auto updating @ `date`" -# Change to AAAA if using an IPv6 address -TYPE="A" - -# Get the external IP address from OpenDNS (more reliable than other providers) -IP=`dig +short myip.opendns.com @resolver1.opendns.com` - -echo current ip is $IP - - -# Get current dir -# (from http://stackoverflow.com/a/246128/920350) -# DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -DIR=$HOME -LOGFILE="$DIR/update-route53.log" -IPFILE="$DIR/update-route53.ip" - -if ! valid_ip $IP; then - echo "Invalid IP address: $IP" >> "$LOGFILE" - exit 1 -fi - -# Check if the IP has changed -if [ ! -f "$IPFILE" ] - then - touch "$IPFILE" -fi - -if grep -Fxq "$IP" "$IPFILE"; then - # code if found - echo "IP is still $IP. Exiting" >> "$LOGFILE" - exit 0 -else - echo "IP has changed to $IP" >> "$LOGFILE" - # Fill a temp file with valid JSON - TMPFILE=$(mktemp /tmp/temporary-file.XXXXXXXX) - cat > ${TMPFILE} << EOF - { - "Comment":"$COMMENT", - "Changes":[ - { - "Action":"UPSERT", - "ResourceRecordSet":{ - "ResourceRecords":[ - { - "Value":"$IP" - } - ], - "Name":"$RECORDSET", - "Type":"$TYPE", - "TTL":$TTL - } - } - ] - } -EOF - - # Update the Hosted Zone record - aws route53 change-resource-record-sets \ - --hosted-zone-id $ZONEID \ - --change-batch file://"$TMPFILE" >> "$LOGFILE" - echo "IP Changed in Route53" >> "$LOGFILE" - - # Clean up - rm $TMPFILE -fi - -# All Done - cache the IP address for next time -echo "$IP" > "$IPFILE" - -} -# end function - -function valid_ip() -{ - local ip=$1 - local stat=1 - - if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then - OIFS=$IFS - IFS='.' - ip=($ip) - IFS=$OIFS - [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ - && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] - stat=$? - fi - return $stat -} diff --git a/function/archive/caddy b/function/archive/caddy deleted file mode 100644 index f50ca02..0000000 --- a/function/archive/caddy +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -function caddye(){ - rrem sysadmin@nas.kebler.net "gedit /opt/caddy/caddy.conf" -} - -function caddys(){ - rrem sysadmin@nas.kebler.net "sudo systemctl status caddy" -} - -function caddyj(){ - rrem sysadmin@nas.kebler.net "journalctl -u caddy | tail -f -n -100" -} diff --git a/function/archive/nas b/function/archive/nas deleted file mode 100644 index a164e7a..0000000 --- a/function/archive/nas +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash -# nas - -## rrem already loaded in 00-basic - -function nasfm(){ - rrem sysadmin@nas.kebler.net "nemo $1" -} - -function nasdisk(){ - rrem sysadmin@nas.kebler.net "sudo gnome-disks" -} - - -function nasgparted(){ - rrem sysadmin@nas.kebler.net "sudo gparted" -} - -function nasdata(){ - rrem sysadmin@nas.kebler.net "nemo /mnt/data" -} diff --git a/function/archive/test b/function/archive/test deleted file mode 100644 index 3725cda..0000000 --- a/function/archive/test +++ /dev/null @@ -1 +0,0 @@ -A test diff --git a/function/rsync b/function/rsync deleted file mode 100755 index f4dc501..0000000 --- a/function/rsync +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -# simple copy using rsync. Preserves everything -# present working directory to supplied destination -module_load confirm -function cprs () { - local usesudo - local cmd - [[ $1 == "-s" ]] && usesudo='sudo' && shift - [ $# -ne 2 ] && echo two directories source and destination need to be passed && return 1 - [ ! -d "$1" ] && echo source: $1 is not a directory && return 1 - cmd="$usesudo rsync --exclude *[C]ache* --exclude node_modules --progress -aAru $1 $2" - echo $cmd - confirm Do you want to start the rsync copy? || return 0 - echo copying..... - eval $cmd -} diff --git a/function/01-basic b/functions/01-basic similarity index 83% rename from function/01-basic rename to functions/01-basic index 6ca8d66..38ff1c2 100644 --- a/function/01-basic +++ b/functions/01-basic @@ -1,13 +1,7 @@ #!/bin/bash # 00 will get loaded first -##-- functions which may be used by any of the alias file --# - -group_add() { - [[ ! $(getent group $1) ]] && echo "no group $1, aborting" && return 1 - echo adding group $1 to $USER - sudo usermod -a -G $1 $USER -} +## user_reload() { save=$PWD @@ -95,13 +89,6 @@ osinfo () { cat /etc/upstream-release/lsb-release } -# adduserid name id# -# add a suer with specific id number -adduserid () { -sudo groupadd -g $2 $1 -sudo useradd -d ${/home/$1} -s /bin/bash -u $2 -g $1 $1 -} - fsudo () # run a function as sudo { [[ "$(type -t $1)" == "function" ]] && diff --git a/functions/02-system-path b/functions/02-system-path new file mode 100644 index 0000000..de7f419 --- /dev/null +++ b/functions/02-system-path @@ -0,0 +1 @@ +module_load system-path \ No newline at end of file diff --git a/functions/03-security b/functions/03-security new file mode 100644 index 0000000..1d8055b --- /dev/null +++ b/functions/03-security @@ -0,0 +1 @@ +module_load security \ No newline at end of file diff --git a/functions/logging b/functions/logging new file mode 100644 index 0000000..42de141 --- /dev/null +++ b/functions/logging @@ -0,0 +1 @@ +module_load logit \ No newline at end of file diff --git a/setup/.gitignore b/install/.gitignore similarity index 60% rename from setup/.gitignore rename to install/.gitignore index 5ae98e6..4e41694 100644 --- a/setup/.gitignore +++ b/install/.gitignore @@ -1,2 +1,3 @@ /backup-users/ /backup/ +/scripts/archive \ No newline at end of file diff --git a/setup/etc/bash.bashrc b/install/files/etc/bash.bashrc similarity index 100% rename from setup/etc/bash.bashrc rename to install/files/etc/bash.bashrc diff --git a/setup/etc/profile b/install/files/etc/profile similarity index 100% rename from setup/etc/profile rename to install/files/etc/profile diff --git a/setup/etc/profile.d/02-root.sh b/install/files/etc/profile.d/02-root.sh similarity index 100% rename from setup/etc/profile.d/02-root.sh rename to install/files/etc/profile.d/02-root.sh diff --git a/setup/etc/profile.d/03-startup.sh b/install/files/etc/profile.d/03-startup.sh similarity index 100% rename from setup/etc/profile.d/03-startup.sh rename to install/files/etc/profile.d/03-startup.sh diff --git a/install/files/root/.bash_profile b/install/files/root/.bash_profile new file mode 100644 index 0000000..2f7455a --- /dev/null +++ b/install/files/root/.bash_profile @@ -0,0 +1,23 @@ +# This .bash_profile is for use ONLY! with UCI bash shell system +# this is only sourced for login shell + +# uncomment these for UCI Shell debugging. +# echo ---- sourcing system .bash_profile for user root --- +# [[ $USER_ROOT ]] && echo 'Root User' || echo 'Non Root User' +# [[ $SHELL_INTERACTIVE ]] && echo 'Interactive' || echo 'Not interactive' +# [[ $SHELL_LOGIN ]] && echo 'Login shell' || echo 'Not login shell' +# [[ $SSH_SESSION ]] && echo ssh remote user || echo local user +# env | grep BASH +# echo --------------------- + + +if [[ $SHELL_INTERACTIVE ]]; then + # only source .bashrc if interactive login shell + [[ -f "/root/.bashrc" ]] && source "/root/.bashrc" + else + # non-interactive root login commands + export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin +fi + +# any (interactive or not) root login shell commands can go here. + diff --git a/install/files/root/.bashrc b/install/files/root/.bashrc new file mode 100644 index 0000000..51a69c4 --- /dev/null +++ b/install/files/root/.bashrc @@ -0,0 +1,24 @@ +# this .bashrc for use ONLY with uci bash shell system + +# uncomment these for debugging. +# echo ---- sourcing system .bashrc for user $USER --- +# [[ $USER_ROOT ]] && echo 'Root User' || echo 'Non Root User' +# [[ $SHELL_INTERACTIVE ]] && echo 'Interactive' || echo 'Not interactive' +# [[ $SHELL_LOGIN ]] && echo 'Login shell' || echo 'Not login shell' +# [[ $SSH_SESSION ]] && echo ssh remote user || echo local user +# env | grep BASH +# echo --------------------- + + +# uncomment for custom processing +#if [[ $SHELL_LOGIN ]]; then +# echo "login shell (called from .bash_profile)" +# fi + +if [[ $SSH_SESSION ]]; then +# echo remote ssh session + cd /opt + fi + +# anything below will be sourced by all shell types (except non-interactive/login) +export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin diff --git a/install/files/root/.profile b/install/files/root/.profile new file mode 100644 index 0000000..b041322 --- /dev/null +++ b/install/files/root/.profile @@ -0,0 +1,7 @@ +## This .profile is for use ONLY! with UCI bash shell system +# this is only sourced for non bash login shell which should never happen + +echo /root/.profile being sourced +echo WARNING: /root/.profile should never be sourced for root +[[ ! -f /root/.bash_profile ]] && echo unless .bash_profile does not exist which it does not! +echo corrective action required diff --git a/setup/user/.bash_logout b/install/files/user/.bash_logout similarity index 100% rename from setup/user/.bash_logout rename to install/files/user/.bash_logout diff --git a/install/files/user/.bash_profile b/install/files/user/.bash_profile new file mode 100644 index 0000000..862c145 --- /dev/null +++ b/install/files/user/.bash_profile @@ -0,0 +1,27 @@ +#!/bin/bash +# sourced for bash login shell only + +# uncomment these for debugging. +# echo ---- sourcing system .bash_profile for user $USER --- +# [[ $USER_ROOT ]] && echo 'Root User' || echo 'Non Root User' +# [[ $SHELL_INTERACTIVE ]] && echo 'Interactive' || echo 'Not interactive' +# [[ $SHELL_LOGIN ]] && echo 'Login shell' || echo 'Not login shell' +# [[ $SSH_SESSION ]] && echo ssh remote user || echo local user +# env | grep BASH +# echo --------------------- + + +# only source .bashrc if interactive login shell +if [[ $SHELL_INTERACTIVE ]]; then + [[ -f "/root/.bashrc" ]] && source "/root/.bashrc" + else + export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin +fi + +# only source .bashrc if interactive login shell +if [[ $SHELL_INTERACTIVE ]];then + [[ -f "$HOME/.bashrc" ]] && source "$HOME/.bashrc" + else + # noninteractive login shell + : + fi \ No newline at end of file diff --git a/setup/user/.bashrc b/install/files/user/.bashrc similarity index 75% rename from setup/user/.bashrc rename to install/files/user/.bashrc index a95ae83..9d3a202 100644 --- a/setup/user/.bashrc +++ b/install/files/user/.bashrc @@ -10,10 +10,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 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 + [[ -d $HOME/BASH_SHELL_USER_DIR ]] && shell_process_directory "BASH_SHELL_USER_DIR" ||\ + echo no user shell directory at $BASH_SHELL_USER_DIR to process, create one or clone a template fi # uncomment to add non-interactive setup/sourcing # else diff --git a/install/files/user/.profile b/install/files/user/.profile new file mode 100644 index 0000000..dbbb1d5 --- /dev/null +++ b/install/files/user/.profile @@ -0,0 +1,2 @@ +# echo $USER .profile +# called at GUI login or when .bash_profile does not exist" diff --git a/install/install.sh b/install/install.sh new file mode 100644 index 0000000..c6ef3d3 --- /dev/null +++ b/install/install.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +uci_bash_shell_install () { + local usesudo + export install_dir="$(dirname $(realpath "${BASH_SOURCE:-$0}"))" + export BASH_SHELL_BASE="$(dirname $install_dir)" + # export BASH_SHELL_BASE="$(dirname "$(cd "$(dirname "${BASH_SOURCE:-$0}")" >/dev/null 2>&1 ; pwd -P )")" + echo "Detected The UCI BASH Shell install directory: BASH_INSTALL_DIR=$install_dir" + echo "Detected The UCI BASH Shell base directory: BASH_SHELL_BASE=$BASH_SHELL_BASE" + [[ $EUID -ne 0 ]] && usesudo=sudo + $usersudo $SHELL $BASH_SHELL_BASE/install/script/copy-etc.sh + echo $usersudo $SHELL $BASH_SHELL_BASE/install/script/copy-root.sh + [[ $(id -u $1 2> /dev/null) ]] && echo $usersudo $SHELL $BASH_SHELL_BASE/install/script/copy-user.sh $1 + } + +# # if script was executed then call the function +(return 0 2>/dev/null) || uci_bash_shell_install "$@" \ No newline at end of file diff --git a/install/scripts/copy-etc.sh b/install/scripts/copy-etc.sh new file mode 100755 index 0000000..f3bc11b --- /dev/null +++ b/install/scripts/copy-etc.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +uci_bash_shell_etc_copy () { +echo -e "************ copying uci shell profile and bash.bashrc files to /etc ********" +files=$(find $BASH_SHELL_BASE/install/files/etc/ -maxdepth 1 -type f) +for file in $files; do install -m 644 -o root -g root $file /etc; done +echo setting BASH_SHELL_BASE to $BASH_SHELL_BASE in etc/bash.bashrc and /etc/profile +sed -i 's:_BASH_SHELL_BASE_:'${BASH_SHELL_BASE}':' /etc/bash.bashrc +sed -i 's:BASH_SHELL_BASE=.*:BASH_SHELL_BASE='${BASH_SHELL_BASE}':' /etc/profile +echo -e "************ copying uci shell profile.d/ files /etc/profile.d/ ********" +files=$(find $BASH_SHELL_BASE/install/files/etc/profile.d -maxdepth 1 -type f) +for file in $files; do install -m 644 -o root -g root $file /etc/profile.d; done +} + +# # if script was executed then call the function +(return 0 2>/dev/null) || uci_bash_shell_etc_copy "$@" diff --git a/install/scripts/copy-root.sh b/install/scripts/copy-root.sh new file mode 100755 index 0000000..317cd19 --- /dev/null +++ b/install/scripts/copy-root.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +uci_bash_shell_root_copy () { +echo -e "************ copying uci shell .profile and .bashrc files to /root ********" +group=root +files=$(find $BASH_SHELL_BASE/setup/root/ -type f) +for file in $files; do install -m 640 -o root -g $group $file /root; done +} + +# # if script was executed then call the function +(return 0 2>/dev/null) || uci_bash_shell_root_copy "$@" \ No newline at end of file diff --git a/install/scripts/copy-user.sh b/install/scripts/copy-user.sh new file mode 100644 index 0000000..fda3eb1 --- /dev/null +++ b/install/scripts/copy-user.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +uci_bash_shelluser_copy () { +local user; local user_home; local dir +user=${1:-$USER} +dir=${2:-shell} +[[ $(id -u $1 2> /dev/null) -eq 0 ]] && echo user is root use root user copy script, exiting && return 1 +[[ ! $(id -u $user) -ge 1000 ]] && echo "no user $user or user not a regular" && return 2 +[[! $(getent passwd | grep $user | grep /bin/bash) ]] && echo user $user is not using bash shell, exiting && return 3 + +user_home=$( getent passwd $user | cut -d: -f6 ) +[[ ! $user_home ]] && echo no user home directory in which to install shell files, exiting && return 4 + +echo -e "*********** copying UCI BASH Shell .profile and bash_profile and .bashrc for user: $user *******" +mkdir -P +files=$(find $Install_dir/files/user -type f) +for file in $files; do +install -C -m 660 -o $user -g $user $file $user_home +done + +sed -i '/[[ $BASH_SHELL_BASE_LOADED = true ]]/ i\ BASH_SHELL_USER_DIR='$dir'' $user_home/.bashrc + +dir=$user_home/$dir +echo -e "*********** create UCI BASH Shell directories for user in $dir *******" +mkdir -p $dir/env $dir/functions $dir/modules $dir/ssh/config $dir/ssh/mounts $dir/startup +chown -R $user:$user $dir +} + + +# # if script was executed then call the function +(return 0 2>/dev/null) || uci_bash_shelluser "$@" \ No newline at end of file diff --git a/install/scripts/remote-install.sh b/install/scripts/remote-install.sh new file mode 100644 index 0000000..4a563bf --- /dev/null +++ b/install/scripts/remote-install.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# module will install the uci-shell system on a remote machine +# must be run as sudo +mkdir -p /shell +chown 1000:users /shell +git clone https://git.kebler.net/base/shell-base.git /shell/base \ No newline at end of file diff --git a/lang/gcc b/lang/gcc deleted file mode 100644 index 63f3155..0000000 --- a/lang/gcc +++ /dev/null @@ -1,2 +0,0 @@ -# colored GCC warnings and errors -export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' diff --git a/misc/less b/misc/less deleted file mode 100644 index 975ba70..0000000 --- a/misc/less +++ /dev/null @@ -1,2 +0,0 @@ -# make less more friendly for non-text input files, see lesspipe(1) -[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)" diff --git a/misc/qt b/misc/qt deleted file mode 100644 index 22e0e52..0000000 --- a/misc/qt +++ /dev/null @@ -1 +0,0 @@ -export QT_SELECT=qt5 diff --git a/misc/xterm.off b/misc/xterm.off deleted file mode 100644 index 9a97eec..0000000 --- a/misc/xterm.off +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -# set a fancy prompt (non-color, unless we know we "want" color) -case "$TERM" in - xterm|xterm-color|*-256color) color_prompt=yes;; -esac diff --git a/modules/add-ppa.sh b/modules/add-ppa.sh deleted file mode 100755 index 1b8c2c3..0000000 --- a/modules/add-ppa.sh +++ /dev/null @@ -1,117 +0,0 @@ -#!/bin/bash -add-ppa () { - -if [ $EUID != 0 ]; then - sudo bash -c "$(declare -f add-ppa); add-ppa $*" - else - VERSION=jammy - KEYSDIR=/etc/apt/trusted.gpg.d - KEYSERVER=keyserver.ubuntu.com - - declare OPTION; declare OPTARG; declare OPTIND - while getopts 'v:p:s:k:c:d:oi' OPTION; do - echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} - case "$OPTION" in - i) - INSTALL=true - ;; - v) - VERSION=$OPTARG - ;; - p) - PACKAGE=$OPTARG - ;; - c) - CMD=$OPTARG - ;; - d) - KEYSDIR=$OPTARG - ;; - s) - KEYSERVER=$OPTARG - ;; - o) - # overwrite any exising public key - KEYOVERWRITE=true - ;; - *) echo unknown run option -$OPTARG - echo "USAGE: add-ppa package/branch (e.g. git-core/ppa)" - echo "available options -v ; -p " - ;; - esac - done - - shift $((OPTIND - 1)) - - #check input - if [ -z ${1+x} ]; then - echo "No ppa provided!" - return 1 - fi - - LAUNCHPAD="https://ppa.launchpadcontent.net" - DEV=$(echo $1 | cut -d ':' -f 2 | cut -d '/' -f1 ) - PACKAGE=${PACKAGE:-$DEV} - CMD=${CMD:-$PACKAGE} - BRANCH=${2:-$(echo $1| cut -d '/' -f 2)} - URL="$LAUNCHPAD/$DEV/$BRANCH/ubuntu $VERSION main" - - echo "*********** Adding PPA Repository ************" - echo DEVELOPER: $DEV - echo BRANCH: $BRANCH - echo PACKAGE: $PACKAGE - echo COMMAND: $CMD - echo URL: $URL - - if [[ -t 0 ]]; then - read -n 1 -p "do you want to continue [y]=>" REPLY - [[ $REPLY != "y" ]] && return 0 - fi - - echo -e "\n*********************************************" - #create source list file - echo "deb $URL" > /etc/apt/sources.list.d/$DEV.list - echo "***** added /etc/apt/sources.list.d/$DEV.list with****" - cat /etc/apt/sources.list.d/$DEV.list - echo "*********************************************" - - KEYFILE=$KEYSDIR/$DEV.gpg - - [[ $KEYOVERWRITE ]] && rm $KEYFILE - if [ ! -f $KEYFILE ]; then - # using an update error to grab key id - KEY_ERROR=/tmp/${DEV}_key_error - touch $KEY_ERROR - apt-get update > /dev/null 2> $KEY_ERROR - cat $KEY_ERROR - KEY=$(sed -n 's/^.*NO_PUBKEY //p' "$KEY_ERROR" | head -1) - # echo Reposity Public Key Settings - # echo KEYS DIRECTORY: $KEYSDIR - # echo KEY SERVER: $KEYSERVER - # echo KEY: $KEY - if [ ! $KEY ]; then - echo can not determine $DEV/$BRANCH key sign - echo "removing file: /etc/apt/sources.list.d/$DEV.list and aborting" - rm /etc/apt/sources.list.d/$DEV.list - return 1 - fi - echo downloading and saving public key $KEY for $DEV/$BRANCH to $KEYFILE - gpg --keyserver $KEYSERVER --recv $KEY - gpg --export $KEY > $KEYFILE - else - echo " >>>>>> $KEYFILE already exists, using that key $KEY <<<<<" - fi - echo ppa repo $DEV/$BRANCH for package $PACKAGE now registered, updating... - apt-get update 1> /dev/null - if [[ $INSTALL ]]; then - echo installing $PACKAGE - [[ -t 0 ]] && apt policy $PACKAGE - apt-get install $PACKAGE -y - $CMD --version - fi -fi - -} - -# # if script was executed then call the function -(return 0 2>/dev/null) || add-ppa $@ \ No newline at end of file diff --git a/modules/scripting/debug.lib b/modules/debug.lib similarity index 100% rename from modules/scripting/debug.lib rename to modules/debug.lib diff --git a/modules/scripting/bundle.sh b/modules/scripting/bundle.sh index 7880177..24f7e70 100644 --- a/modules/scripting/bundle.sh +++ b/modules/scripting/bundle.sh @@ -10,15 +10,24 @@ bundle () { -[[ ! -f $1 ]] && return 1 +local file + +if [[ $1 == "-m" ]]; then + shift 1 + file=$(module_find $1) + else + file=$1 +fi + +[[ ! -f $file ]] && return 1 module_load file if [[ ! $2 == "__recurse__" ]]; then tmp_file=$( mktemp -t TEMP_FILE_bundle.XXXXXXXX ) chmod 600 "$tmp_file" -\cp $1 $tmp_file +\cp $file $tmp_file else -tmp_file=$1 +tmp_file=$file fi # echo current temp file: $tmp_file modules=$(sed -n -e 's/^module_load //p' < $tmp_file) diff --git a/function/comment b/modules/scripting/comment.lob similarity index 100% rename from function/comment rename to modules/scripting/comment.lob diff --git a/function/logit b/modules/scripting/logit.func similarity index 100% rename from function/logit rename to modules/scripting/logit.func diff --git a/function/systemd b/modules/systemd similarity index 100% rename from function/systemd rename to modules/systemd diff --git a/modules/utility/bindfs.lib b/modules/utility/bindfs.lib new file mode 100644 index 0000000..ac16db0 --- /dev/null +++ b/modules/utility/bindfs.lib @@ -0,0 +1,33 @@ +#!/bin/bash + +#bmount () {} +# sudo bindfs -o allow_other -o map=root/1001:@root/@1001 /var/lib/docker/volumes /data/dvols + + +dir_rebind_user () { + local usesudo; local dir;local user;local group + # $1 new owner + # $2 dir + +[ $# -lt 2 ] && echo minimum args needed to rebind are user and directory && return 1 +[[ $EUID -ne 0 ]] && usesudo=sudo + +[[ $(id -u $1 2> /dev/null) ]] || { echo user $1 does not exist can not continue; return 2; } + +dir=$(realpath $2) +user=$(stat -c '%U' $dir) +group=$(stat -c '%G' $dir) +$usesudo bindfs --force-user=$1 --force-group=$1 --create-for-user=$user --create-for-group=$group --chown-ignore --chgrp-ignore $dir $dir +# echo use \'dir_rebind_user_remove $dir\' to remove +} + +dir_rebind_user_remove () { + local usersudo + [[ $EUID -ne 0 ]] && usesudo=sudo + $usesudo fusermount -u $1 +} + +if [ -v PS1 ]; then + alias dru="dir_rebind_user" + alias drur="dir_rebind_user_remove" +fi \ No newline at end of file diff --git a/modules/utility/dir.lib b/modules/utility/dir.lib new file mode 100644 index 0000000..eb9bc84 --- /dev/null +++ b/modules/utility/dir.lib @@ -0,0 +1,56 @@ +#!/bin/bash +# simple local copy wintin or merge one directory to another using rsync. Preserves everything +# present working directory to supplied destination + +isDir() { + if [[ -d $1 ]] + then + echo "true" + return 0 + else + return 1 + fi +} + +function dir_copy () { + + [[ ! $(which rsync) ]] && { echo rsync must be installed to use this function; return 5; } + module_load confirm + local usesudo;local cmd;local src;local noconfirm;local merge + + declare OPTION + declare OPTARG + declare OPTIND + while getopts 'sem' OPTION; do + case "$OPTION" in + s) + usesudo=true + ;; + e) + noconfirm=true + ;; + m) + merge=true + ;; + *) + echo unknown option $OPTION + return 1 + ;; + esac + done + +shift $(( OPTIND - 1 )) + +[ $# -ne 2 ] && echo two directories source and destination need to be passed && return 2 +src=$(realpath -s $1) +dest=$(realpath -s $2) +[ ! -d "$src" ] && echo source: $src is not a directory && return 3 +[[ $merge ]] && src=${src}/ +cmd="$usesudo rsync --exclude *[C]ache* --exclude node_modules --progress -aAru $src $dest" +echo $cmd +[[ ! -v PS1 ]] && noconfirm=true +[[ ! $noconfirm ]] && { confirm Do you want to $([[ $merge ]] && echo merge $src into || echo copy $src within) $dest? || return 0; } +echo copying..... +eval $cmd +} + diff --git a/modules/utility/dirs.sh b/modules/utility/dirs.sh deleted file mode 100644 index d7a092f..0000000 --- a/modules/utility/dirs.sh +++ /dev/null @@ -1,81 +0,0 @@ -################## BEGIN: MODULE dirs ############### - -isDir() { - if [[ -d $1 ]] - then - echo "true" - return 0 - else - return 1 - fi -} - -chmodr () { - -# Generic Script for recursively setting permissions for directories and files -# to defined or default permissions using chmod. -# -# Takes a path to recurse through and options for specifying directory and/or -# file permissions. -# Outputs a list of affected directories and files. -# -# If no options are specified, it recursively resets all directory and file -# permissions to the default for most OSs (dirs: 755, files: 644). - -usage() -{ - echo "Usage: $0 PATH -d DIRPERMS -f FILEPERMS" - echo "Arguments:" - echo "PATH: path to the root directory you wish to modify permissions for" - echo "Options:" - echo " -d DIRPERMS, directory permissions" - echo " -f FILEPERMS, file permissions" - return 1 - -} - -# Check if user entered arguments -if [ $# -lt 1 ] ; then - usage - return 1 -fi - -# Get options -while getopts d:f: opt -do - case "$opt" in - d) DIRPERMS="$OPTARG";; - f) FILEPERMS="$OPTARG";; - \?) usage;; - esac -done - -# Shift option index so that $1 now refers to the first argument -shift $(($OPTIND - 1)) - -# Default directory and file permissions, if not set on command line -if [ -z "$DIRPERMS" ] && [ -z "$FILEPERMS" ] ; then - DIRPERMS=755 - FILEPERMS=644 -fi - -# Set the root path to be the argument entered by the user -ROOT=$1 - -# Check if the root path is a valid directory -if [ ! -d $ROOT ] ; then - echo "$ROOT does not exist or isn't a directory!" ; return 2 -fi - -# Recursively set directory/file permissions based on the permission variables -if [ -n "$DIRPERMS" ] ; then - find $ROOT -type d -print0 | xargs -0 chmod -v $DIRPERMS -fi - -if [ -n "$FILEPERMS" ] ; then - find $ROOT -type f -print0 | xargs -0 chmod -v $FILEPERMS -fi - -} - -################## END: MODULE dirs ############### \ No newline at end of file diff --git a/modules/utility/env.lib b/modules/utility/env.lib index c865ec9..2b42ccb 100644 --- a/modules/utility/env.lib +++ b/modules/utility/env.lib @@ -1,6 +1,7 @@ #!/bin/bash # environment and environment file functions +module_load iecho clean_env_file () { local compact @@ -29,7 +30,7 @@ env_file () { # ignore non assignment lines list_env_file_vars () { env=$(env_file $1) -# cat $env + cat $env [[ ! $env ]] && { iecho "Env file ${1} doesn't exist"; return 1; } clean_env_file $env | grep -v '^#'| sed -n -E 's/(.*)=[^ =].*/\1/p' | xargs } diff --git a/misc/00-prompt b/modules/utility/fancy-prompt.func similarity index 60% rename from misc/00-prompt rename to modules/utility/fancy-prompt.func index eb9b94e..c2e965c 100644 --- a/misc/00-prompt +++ b/modules/utility/fancy-prompt.func @@ -1,16 +1,45 @@ #!/bin/bash + +# load module and call fancy_prompt # see # used http://bashrcgenerator.com/ # and # https://unix.stackexchange.com/questions/148/colorizing-your-terminal-and-shell-environment/174#174 +# TODO move parse_git_branch, check for git before returning a value + function parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' } +function set_prompt_colors () { + export TERM=xterm-color + export CLICOLOR=1 + export LSCOLORS=ExFxCxDxBxegedabagacad + + export COLOR_NC='\e[0m' # No Color + export COLOR_BLACK='\e[0;30m' + export COLOR_GRAY='\e[1;30m' + export COLOR_RED='\e[0;31m' + export COLOR_LIGHT_RED='\e[1;31m' + export COLOR_GREEN='\e[0;32m' + export COLOR_LIGHT_GREEN='\e[1;32m' + export COLOR_BROWN='\e[0;33m' + export COLOR_YELLOW='\e[1;33m' + export COLOR_BLUE='\e[0;34m' + export COLOR_LIGHT_BLUE='\e[1;34m' + export COLOR_PURPLE='\e[0;35m' + export COLOR_LIGHT_PURPLE='\e[1;35m' + export COLOR_CYAN='\e[0;36m' + export COLOR_LIGHT_CYAN='\e[1;36m' + export COLOR_LIGHT_GRAY='\e[0;37m' + export COLOR_WHITE='\e[1;37m' +} + function fancy_prompt () { # if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then + set_prompt_colors function c () { local color @@ -53,5 +82,4 @@ PS1=$(echo "$user$machine$dir$branch$promptc") # echo $PS1 } -# fancy prompt is on by default comment out to disable default -fancy_prompt + diff --git a/modules/utility/filesystem.mod b/modules/utility/filesystem.mod index 88e2e22..b13f9cc 100644 --- a/modules/utility/filesystem.mod +++ b/modules/utility/filesystem.mod @@ -15,3 +15,4 @@ find_mountpoint () { return 1 fi } + diff --git a/modules/github-bin-fetch.sh b/modules/utility/github-bin-fetch.sh similarity index 89% rename from modules/github-bin-fetch.sh rename to modules/utility/github-bin-fetch.sh index 52f4cf9..e23408b 100644 --- a/modules/github-bin-fetch.sh +++ b/modules/utility/github-bin-fetch.sh @@ -1,6 +1,7 @@ #!/bin/bash # if you have run into github api anonymous access limits which happens during debugging/dev then add user and token here or sourced from a separate file # set to location for tokens in file +# DEPRECATED use python based lastversion https://github.com/dvershinin/lastversion source ~/githubapitoken if [ "$GITHUB_TOKEN" != "" ]; then diff --git a/modules/acl.lib b/modules/utility/security.lib old mode 100755 new mode 100644 similarity index 68% rename from modules/acl.lib rename to modules/utility/security.lib index 22f2c49..397e968 --- a/modules/acl.lib +++ b/modules/utility/security.lib @@ -1,4 +1,6 @@ #!/bin/bash +################## BEGIN: MODULE security ############### + module_load confirm module_load helpers #source ${BASH_SOURCE[0]}/confirm.sh @@ -147,3 +149,76 @@ share_dir() { getfacl -p $dir } + + + + +chmodr () { + +# Generic Script for recursively setting permissions for directories and files +# to defined or default permissions using chmod. +# +# Takes a path to recurse through and options for specifying directory and/or +# file permissions. +# Outputs a list of affected directories and files. +# +# If no options are specified, it recursively resets all directory and file +# permissions to the default for most OSs (dirs: 755, files: 644). + +usage() +{ + echo "Usage: $0 PATH -d DIRPERMS -f FILEPERMS" + echo "Arguments:" + echo "PATH: path to the root directory you wish to modify permissions for" + echo "Options:" + echo " -d DIRPERMS, directory permissions" + echo " -f FILEPERMS, file permissions" + return 1 + +} + +# Check if user entered arguments +if [ $# -lt 1 ] ; then + usage + return 1 +fi + +# Get options +while getopts d:f: opt +do + case "$opt" in + d) DIRPERMS="$OPTARG";; + f) FILEPERMS="$OPTARG";; + \?) usage;; + esac +done + +# Shift option index so that $1 now refers to the first argument +shift $(($OPTIND - 1)) + +# Default directory and file permissions, if not set on command line +if [ -z "$DIRPERMS" ] && [ -z "$FILEPERMS" ] ; then + DIRPERMS=755 + FILEPERMS=644 +fi + +# Set the root path to be the argument entered by the user +ROOT=$1 + +# Check if the root path is a valid directory +if [ ! -d $ROOT ] ; then + echo "$ROOT does not exist or isn't a directory!" ; return 2 +fi + +# Recursively set directory/file permissions based on the permission variables +if [ -n "$DIRPERMS" ] ; then + find $ROOT -type d -print0 | xargs -0 chmod -v $DIRPERMS +fi + +if [ -n "$FILEPERMS" ] ; then + find $ROOT -type f -print0 | xargs -0 chmod -v $FILEPERMS +fi + +} + +################## END: MODULE dirs ############### \ No newline at end of file diff --git a/function/02-path b/modules/utility/system-path.lib similarity index 100% rename from function/02-path rename to modules/utility/system-path.lib diff --git a/modules/utility/user.sh b/modules/utility/user.lib similarity index 77% rename from modules/utility/user.sh rename to modules/utility/user.lib index 3e8aa7b..7013e81 100755 --- a/modules/utility/user.sh +++ b/modules/utility/user.lib @@ -1,7 +1,50 @@ #!/bin/bash +is_user () { + local id;local user + echo -n $(id -u $1 2> /dev/null) +} + +user_group_add() { + [[ ! $(getent group $1) ]] && echo "no group $1, aborting" && return 1 + echo adding group $1 to $USER + sudo usermod -a -G $1 $USER +} + +# TODO change a user name +# sudo usermod -d /home/joedoe -m johndoe +# sudo groupmod -n johndoe student1 +# sudo usermod -u 5001 johndoe + # clone a user +# adduserid name id# +# add a suer with specific id number +# adduserid () { +# sudo groupadd -g $2 $1 +# sudo useradd -d ${/home/$1} -s /bin/bash -u $2 -g $1 $1 +# } + + +adduserid() { + +[[ "$#" -lt 1 ]] && echo a user name is requied, aborting && return 1 +name=$1 +uid=${2:-1000} +gid=${3:-$uid} +echo $name, $uid, $gid + +[[ $(getent group $gid) ]] && echo group id $gid already exists, aborting && return 3 +[[ $(getent group $name) ]] && echo group name $name already exists, aborting && return 3 +[[ $(getent passwd $uid) ]] && echo user id $uid already exists, aborting && return 2 +[[ $(getent passwd $name) ]] && echo user name $name already exists, aborting && return 2 + +addgroup --gid $gid $name +adduser --uid $uid --ingroup $name --gecos "" --disabled-password --no-create-home $name +# cat /etc/group | grep $name && cat /etc/passwd | grep $name + +} + function clone_user_ () { module_load confirm diff --git a/modules/utility/watch.sh b/modules/utility/watch.func similarity index 100% rename from modules/utility/watch.sh rename to modules/utility/watch.func diff --git a/setup/backup.sh b/setup/backup.sh deleted file mode 100755 index 711f708..0000000 --- a/setup/backup.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash -[[ ! $(groups | grep sudo) ]] && echo this script must be run by a user with sudo privileges && exit 1 -[[ ! $BASH_SHELL_BASE ]] && BASH_SHELL_BASE="$(dirname "$(cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")" -echo Backng up to $BASH_SHELL_BASE/setup/backup -echo ok to continue? -echo "type \`yes\` fully or just for no" -read -e answer -[[ ! $answer = "yes" ]] && exit 1 -set -o errexit -mkdir -p $BASH_SHELL_BASE/setup/backup/etc/profile.d -# mkdir -p $BASH_SHELL_BASE/setup/backup/user -mkdir -p $BASH_SHELL_BASE/setup/backup/root -# host system files in /etc -[[ -f /etc/profile ]] && cp -v /etc/profile $BASH_SHELL_BASE/setup/backup/etc -[[ -f /etc/bash.bashrc ]] && cp -v /etc/bash.bashrc $BASH_SHELL_BASE/setup/backup/etc -[[ -d /etc/profile.d ]] && cp -vR /etc/profile.d/ $BASH_SHELL_BASE/setup/backup/etc/ -# current user files in $HOME -# [[ -f $HOME/.bash_profile ]] && cp -v $HOME/.bash_profile $BASH_SHELL_BASE/setup/backup/user -# [[ -f $HOME/.profile ]] && cp -v $HOME/.profile $BASH_SHELL_BASE/setup/backup/user -# [[ -f $HOME/.bashrc ]] && cp -v $HOME/.bashrc $BASH_SHELL_BASE/setup/backup/user -# [[ -f $HOME/.bash_logout ]] && cp -v $HOME/.bash_logout $BASH_SHELL_BASE/setup/backup/user -if [[ $(stat -c "%G" /root) = "sudo" ]]; then -[[ -f /root/.bash_profile ]] && cp -v /root/.bash_profile $BASH_SHELL_BASE/setup/backup/root -[[ -f /root/.profile ]] && cp -v /root/.profile $BASH_SHELL_BASE/setup/backup/root -[[ -f /root/.bashrc ]] && cp -v /root/.bashrc $BASH_SHELL_BASE/setup/backup/root -[[ -f /root/.bash_logout ]] && cp -v /root/.bash_logout $BASH_SHELL_BASE/setup/backup/root -else -echo no sudo group read access to /root so backing up /root as root -sudo -E -- bash -c '[[ ! -f /root/.profile ]] && exit 0; cp -v /root/.profile $BASH_SHELL_BASE/setup/backup/root' -sudo -E -- bash -c '[[ ! -f /root/.bashrc ]] && exit 0; cp -v /root/.bashrc $BASH_SHELL_BASE/setup/backup/root' -sudo -E -- bash -c '[[ ! -f /root/.bash_profile ]] && exit 0; cp -v /root/.bash_profile $BASH_SHELL_BASE/setup/backup/root' -sudo -E -- bash -c '[[ ! -f /root/.bash_logout ]] && exit 0; cp -v /root/.bash_logout $BASH_SHELL_BASE/setup/backup/root' -fi -set +o errexit -echo backup of system shell files complete! -exit 0 diff --git a/setup/deploy-user.sh b/setup/deploy-user.sh deleted file mode 100755 index 6634c67..0000000 --- a/setup/deploy-user.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash - -_USER=${1:-$USER} -[[ ! $(id -u $_USER) -ge 1000 ]] && echo "no user $_USER or user not a regular" && exit 1 -echo user: $_USER deployment -[[ ! $BASH_SHELL_BASE ]] && BASH_SHELL_BASE="$(dirname "$(cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")" -echo Base Shell Directory as detected is $BASH_SHELL_BASE -echo ok to continue? -echo "type \`yes\` fully or just to exit" -read -e answer -[[ ! $answer = "yes" ]] && exit 1 - -UBDIR=$BASH_SHELL_BASE/setup/backup-users/$_USER -_USER_DIR=$(getent passwd "$USER" | cut -d : -f 6) -files=".bash_profile .profile .bashrc .bash_logout" -# uncomment for debugging -# rm -rf $UBDIR -if [[ ! -d $UBDIR ]]; then - echo "first backing up $_USER_DIR shell files before deploying" - mkdir -p $UBDIR - for file in $files ; do - [[ -f $_USER_DIR/$file ]] && sudo install -C -m 660 -o root -g sudo $_USER_DIR/$file $UBDIR -done -fi -echo "ready to deploy $_USER shell files" -echo "Double check files in $UBDIR" -echo "continue? type \`yes\` fully or just for no" -read -e answer -[[ ! $answer = "yes" ]] && echo aborting deploy && exit 1 -echo -----------------;echo deploying user shell files to /home/$_USER -echo -files=$(find $BASH_SHELL_BASE/setup/user/ -type f) -for file in $files; do -cmd="install -C -m 660 -o $_USER -g sudo $file $_USER_DIR" -[[ $USER = "$_USER" ]] && eval $cmd || sudo -E -- bash -c "$cmd" -done -echo "the default user bash shell repo subdirecty is \`shell\`" -echo "enter an alternative subdirectory under $_USER_DIR or just to accept default" -read -e answer -[[ ! $answer ]] && echo will source default user shell repo at $_USER_DIR/shell && exit 0 -sed -i '/[[ $BASH_SHELL_BASE_LOADED = true ]]/ i\ BASH_SHELL_USER='$answer'' $_USER_DIR/.bashrc -echo will be processing user shell repo at $_USER_DIR/$answer diff --git a/setup/deploy.sh b/setup/deploy.sh deleted file mode 100755 index 0ae7e24..0000000 --- a/setup/deploy.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash -BASH_SHELL_BASE="$(dirname "$(cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")" -echo Base Shell Directory as detected is $BASH_SHELL_BASE -echo ok to continue? -echo "type \`yes\` fully or just for no" -read -e answer -[[ ! $answer = "yes" ]] && exit 1 -[[ ! $(groups | grep sudo) ]] && echo this script must be run by a user with sudo privileges && exit 1 -# uncomment for debugging -# rm -rf $BASH_SHELL_BASE/setup/backup/ -if [[ ! $(stat -c "%G" /root) = "sudo" ]]; then -echo "Allow all sudoers read access to /root directory and files?" -echo "type \`yes\` fully or just for no" -read -e answer -if [[ $answer = "yes" ]]; then -sudo chown -R root:sudo /root -sudo chmod -R g+rX /root -echo sudo group and permissions on /root were set || \ -echo error during sudo access setup -else -echo;echo sudoer access to /root was declined -fi -fi -if [[ ! -d $BASH_SHELL_BASE/setup/backup ]]; then - echo "backing up shell files before deploying" - $BASH_SHELL_BASE/setup/backup.sh - [[ $? -ne 0 ]] && echo issue with backup did not deploy && exit 1 - # echo backup finished to $BASH_SHELL_BASE/setup/backup -fi -echo "ready to deploy shell files" -echo "Double check files in $BASH_SHELL_BASE/setup/backup" -echo "continue? type \`yes\` fully or just for no" -read -e answer -[[ ! $answer = "yes" ]] && echo aborting deploy && exit 1 -echo -----------------;echo deploying /etc shell files -files=$(find $BASH_SHELL_BASE/setup/etc/ -maxdepth 1 -type f) -for file in $files; do sudo install -C -m 644 -o root -g root $file /etc; done -echo setting BASH_SHELL_BASE to $BASH_SHELL_BASE in etc/bash.bashrc and /etc/profile -sudo sed -i 's:_BASH_SHELL_BASE_:'${BASH_SHELL_BASE}':' /etc/bash.bashrc -sudo sed -i 's:BASH_SHELL_BASE=.*:BASH_SHELL_BASE='${BASH_SHELL_BASE}':' /etc/profile -files=$(find $BASH_SHELL_BASE/setup/etc/profile.d -maxdepth 1 -type f) -for file in $files; do sudo install -C -m 644 -o root -g root $file /etc/profile.d; done -echo -----------------;echo deploying /root shell files -group=root -[[ $(stat -c "%G" /root) = "sudo" ]] && group=sudo -files=$(find $BASH_SHELL_BASE/setup/root/ -type f) -for file in $files; do sudo install -C -m 640 -o root -g $group $file /root; done -echo ----------------- -source $BASH_SHELL_BASE/setup/deploy-user.sh diff --git a/setup/fetch.sh b/setup/fetch.sh deleted file mode 100755 index 85b3b80..0000000 --- a/setup/fetch.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -mkdir -p $BASH_SHELL_BASE/setup/etc/profile.d -mkdir -p $BASH_SHELL_BASE/setup/user -mkdir -p $BASH_SHELL_BASE/setup/root -cp -vi /etc/profile $BASH_SHELL_BASE/setup/etc -cp -vi /etc/bash.bashrc $BASH_SHELL_BASE/setup/etc -cp -vi /etc/profile.d/02-root.sh $BASH_SHELL_BASE/setup/etc/profile.d -cp -vi /etc/profile.d/03-startup.sh $BASH_SHELL_BASE/setup/etc/profile.d -cp -vi $HOME/.bash_profile $BASH_SHELL_BASE/setup/user -cp -vi $HOME/.profile $BASH_SHELL_BASE/setup/user -cp -vi $HOME/.bashrc $BASH_SHELL_BASE/setup/user -cp -vi $HOME/.bash_logout $BASH_SHELL_BASE/setup/user -cp -vi /root/.bash_profile $BASH_SHELL_BASE/setup/root -cp -vi /root/.profile $BASH_SHELL_BASE/setup/root -cp -vi /root/.bashrc $BASH_SHELL_BASE/setup/root -cp -vi /root/.bash_logout $BASH_SHELL_BASE/setup/root diff --git a/setup/root/.bash_profile b/setup/root/.bash_profile deleted file mode 100644 index 6cfeb96..0000000 --- a/setup/root/.bash_profile +++ /dev/null @@ -1,5 +0,0 @@ -# echo $USER .bash_profile - export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin - if [ -f ~/.bashrc ]; then - . ~/.bashrc - fi diff --git a/setup/root/.bashrc b/setup/root/.bashrc deleted file mode 100644 index a988723..0000000 --- a/setup/root/.bashrc +++ /dev/null @@ -1 +0,0 @@ -# echo $USER .bashrc diff --git a/setup/root/.profile b/setup/root/.profile deleted file mode 100644 index 754e83d..0000000 --- a/setup/root/.profile +++ /dev/null @@ -1,2 +0,0 @@ -echo $USER .profile -echo "for bash login shell only called if .bash_profile does not exist" diff --git a/setup/user/.bash_profile b/setup/user/.bash_profile deleted file mode 100644 index 684b513..0000000 --- a/setup/user/.bash_profile +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -# uncomment for debugging -# echo ".bash_profile sourced at user $USER login" - -# only source .bashrc if interactive login shell -[[ $SHELL_INTERACTIVE ]] && [[ -f "$HOME/.bashrc" ]] && source "$HOME/.bashrc" \ No newline at end of file diff --git a/setup/user/.profile b/setup/user/.profile deleted file mode 100644 index ee76ceb..0000000 --- a/setup/user/.profile +++ /dev/null @@ -1,2 +0,0 @@ - echo .profile, user $USER - echo "warning: this only is sourced if .bash_profile doesn't exist" \ No newline at end of file