add sudo module with Sudo function for running functions or alias as sudo

add install-shell-repo library for easy install of other shell repos (host, network)
master
Kebler Network System Administrator 2023-02-09 08:34:18 -08:00
parent 2454f268ca
commit 99c01d4bb7
3 changed files with 69 additions and 5 deletions

View File

@ -2,6 +2,7 @@
# 00 will get loaded first # 00 will get loaded first
## ##
module_load systemd
user_reload() { user_reload() {
save=$PWD save=$PWD
@ -89,10 +90,5 @@ osinfo () {
cat /etc/upstream-release/lsb-release cat /etc/upstream-release/lsb-release
} }
fsudo () # run a function as sudo
{
[[ "$(type -t $1)" == "function" ]] &&
ARGS="$@" && sudo bash -c "$(declare -f $1); $ARGS"
}
alias ssudo="ssudo " alias ssudo="ssudo "

View File

@ -0,0 +1,47 @@
#!/bin/bash
# assumes that bash is installed
#!/bin/bash
# must! be run as sudo
install_shell_repo () {
# TODO have a cross distro package install
[[ ! $(which git) ]] && apt-get install --no-install-recommends git -y
[[ ! $EUID -eq 0 ]] && { echo ERROR script must be run as root; return; }
local user
[[ $1 == "-u" ]] && user=$2 && shift 2
local dir=${2:-/shell/any}
local repo=${1:-host}
local rpath=$dir/$repo
mkdir -p $rpath
# TODO set git host via option or env var
git clone https://git.kebler.net/bash/shell-$repo.git $rpath
chown -R ${user:-1000}:${user:-1000} $rpath
chmod -R +r $rpath
}
install_shell_host () {
module_load sudo
local user
[[ $1 == "-u" ]] && user="-u $2" && shift 2
Sudo install_shell_repo $user host $1
}
install_shell_network () {
module_load sudo
local user
[[ $1 == "-u" ]] && user="-u $2" && shift 2
Sudo install_shell_repo $user network $1
}
update_shell_host () {
cd $BASH_SHELL_ANY_HOST || { echo not directory $BASH_SHELL_ANY_HOST; return 2; }
git pull origin master
}
update_shell_network () {
cd $BASH_SHELL_ANY_NETWORK || { echo not directory $BASH_SHELL_ANY_NETWORK; return 2; }
git pull origin master
}

21
modules/utility/sudo.lib Normal file
View File

@ -0,0 +1,21 @@
#!/bin/bash
fsudo () # run a function as sudo
{
[[ "$(type -t $1)" == "function" ]] &&
ARGS="$@" && sudo bash -c "$(declare -f $1); $ARGS"
}
function Sudo {
local firstArg=$1
if [ $(type -t $firstArg) = function ]
then
shift && command sudo bash -c "$(declare -f $firstArg);$firstArg $*"
elif [ $(type -t $firstArg) = alias ]
then
alias sudo='\sudo '
eval "sudo $@"
else
command sudo "$@"
fi
}