Compare commits

..

2 Commits

Author SHA1 Message Date
David Kebler c756711123 allow multiple environment variable options 2024-02-21 14:51:14 -08:00
David Kebler e075997e4c move remote uci install to own file
add login shell and environment variable options to remote
2024-02-21 14:47:54 -08:00
3 changed files with 69 additions and 81 deletions

View File

@ -1,30 +0,0 @@
#!/bin/bash
# assumes that bash is installed
#!/bin/bash
# must! be run as sudo
install_shell_base () {
# TODO have a cross distro package install
module_load distro
set_distro
[[ ! $(command -v git) ]] && echo git must be installed first && $INSTALL_PKGS git
# TODO, avoid which in all scripts. or put which in environment if not on machine
[[ ! $(command -v which) ]] && echo 'which' must be installed first && $INSTALL_PKGS which
echo $USER, $EUID
[[ ! $EUID -eq 0 ]] && { echo ERROR script must be run as root; return 2; }
echo I am ROOT, now running script
echo UCI user: $1
# TODO make the repo and clone values dynamic or bundle current base on sending machine a archive
mkdir -p /shell/base
git clone https://git.kebler.net/bash/shell-base.git /shell/base
chown -R ${1:-$1000}:${1:-1000} /shell
chmod -R +r /shell
/bin/bash /shell/base/install/install.sh ${1:-1000}
}
# # if script was executed then call the function
( return 0 2>/dev/null ) || install_shell_base $@

View File

@ -0,0 +1,40 @@
#!/bin/bash
module_load remote
# remote_install_shell_base <script options> script command <sudo pass if host not root> <user to install, default is $USER>
remote_install_uci_shell () {
local supass;local user; local host
declare OPTION; declare OPTARG; declare OPTIND
while getopts 'u: s:p:cf:x:' OPTION; do
case "$OPTION" in
p)
#both for login and sudo
upass="-p $OPTARG"
;;
u) user="$OPTARG"
;;
s)
# only for running sudo command for non-root use logged in via sshkey
supass="-p $OPTARG"
;;
*)
echo unknown bundle option $OPTION
;;
esac
done
shift $(( OPTIND - 1 ))
host=$1
[[ ! $host ]] && >&2 echo remote_install_shell_base requires specifying a host
shift 1
user=${user:-$(get_user_host $host)}
[[ $user == "root" ]] && user="" && >&2 echo warning, will not set up any user than root for UCI shell
[[ ! $user ]] && >&2 echo unable to determin remote user for host $host, aborting && return 1
if [[ $supass ]] && [[ $upass ]]; then >&2 echo specify either -s or -p or neither but not both; return 1; fi
[[ $upass ]] && remote_script -s -f install_shell_base $host uci-shell-install $user -- $upass "$@" && return $?
[[ $supass ]] && remote_script $supass -f install_shell_base $host uci-shell-install $user -- $upass "$@" && return $?
remote_script -f install_shell_base $host uci-shell-install $user -- "$@"
}

View File

@ -22,8 +22,9 @@ remote_function () {
remote_script () {
local sshargs;local user;local supass;local cfn; local rfn; local args;
local script; local host; local dr; local supass
local hostname; local bscript; local ruser; local usesudo
local script; local host; local dr; local supass; local env_vars;
local login; local slogin
local hostname; local bscript; local ruser; local usesudo; local save
help() {
@ -36,6 +37,8 @@ host and script are required, script can be either path to a file containing a s
-u, remote user to run script as. default is ssh host user or root if using sudo
-p, password for ssh login, will also be used for sudo assuming remote user is in sudo group
-x, create script from available function instead of module or file
-l, use login shell on remote
-e, set some environment variables to run
-h, this help text
EOF
@ -62,7 +65,7 @@ EOF
local OPTION
local OPTARG
local OPTIND
while getopts 'hdu:sf:p:x:' OPTION; do
while getopts 'le:hdu:sf:p:x:' OPTION; do
# echo OPTION $OPTION ARG $OPTARG INDX $OPTIND
case "$OPTION" in
s)
@ -78,11 +81,17 @@ EOF
# dry run
dr=true
;;
e)
env_vars+=" $OPTARG"
;;
u)
# run remote command as another user
ruser=$OPTARG
# usesudo=true
;;
l)
login="bash -l"
;;
x)
rfn=$OPTARG
;;
@ -107,18 +116,17 @@ EOF
[[ $# -lt 1 ]] && echo fatal: remote_script requires a 'host' && help && return 1
host=$1;
host=$1; shift 1
[[ ! $host ]] && echo fatal: no host was passed unable to excute a remote script && return 3
user=$(get_user_host $host)
[[ ! $user ]] && echo fatal: unable to determine user at host $host, aborting remote script && return 4
hostname=$(get_hostname_host $host)
shift 1
# script can come from
if [[ $rfn ]]; then
if ! declare -f $rfn >/dev/null; then
if ! module_load $2; then
[[ -f $2 ]] && source $2
if ! module_load $1; then
[[ -f $1 ]] && source $1
fi
if ! declare -f $rfn >/dev/null; then echo fatal: remote-script, unable to source funtion $rfn, aborting; return 1; fi
shift
@ -128,10 +136,10 @@ EOF
declare -f $rfn > $script
cfn="-f $rfn"
else
script=$2
script=$1
shift 1
fi
[[ ! -f $script ]] && echo fatal: must pass a script to remote run && help && return 1
[[ ! $script ]] && echo fatal: must pass a script to remote run && help && return 1
debug echo host: $host user: $user hostname:$hostname script:$script function to run: $cfn
@ -140,10 +148,16 @@ EOF
>&2 echo fatal: remote_script unable to bundle script for sending, aborting; return 1;
fi
if [[ $usesudo ]] || [[ $supass ]]; then
[[ $usesudo ]] && supass=$(parse_option "${sshargs[*]}" -p)
if [[ $supass ]]; then
usesudo="echo '${supass}' | sudo -u ${ruser:-root} --stdin 2>/dev/null"
[[ $login ]] && login="" && slogin="-i"
usesudo="echo '${supass}' | sudo $slogin -u ${ruser:-root} --stdin 2>/dev/null"
echo remote script to be run as ${ruser:-root} using sudo
else
echo sudo requested but no sudo password supplied, aborting
return 5
fi
fi
debug remote script arguments $(remote_args "$@")
@ -154,7 +168,7 @@ EOF
# make remote script excuteable
ssh "${sshargs[@]}" "$host" "chmod +x $rscript"
# run the script
ssh "${sshargs[@]}" "$host" "$usesudo" "$rscript" "$(remote_args "$@")"
ssh "${sshargs[@]}" "$host" "$usesudo" $login "$env_vars" "$rscript" "$(remote_args "$@")"
# now delete it, save script if passed an explicit name
if ! ssh "${sshargs[@]}" $host rm -f $rscript; then echo unable to delete temporary remote file at $host:$rscript; fi
# ssh "${sshargs[@]}" "$host" "cat $rscript"
@ -185,41 +199,5 @@ rm -f $file &> /dev/null
# remote_install_shell_base <script options> script command <sudo pass if host not root> <user to install, default is $USER>
remote_install_shell_base () {
module_load remote
local supass;local user; local host
declare OPTION; declare OPTARG; declare OPTIND
while getopts 's:p:cf:x:' OPTION; do
case "$OPTION" in
p)
#both for login and sudo
upass="-p $OPTARG"
;;
u) user="$OPTARG"
;;
s)
# only for running sudo command for non-root use logged in via sshkey
supass="-p $OPTARG"
;;
*)
echo unknown bundle option $OPTION
;;
esac
done
shift $(( OPTIND - 1 ))
host=$1
[[ ! $host ]] && >&2 echo remote_install_shell_base requires specifying a host
shift 1
user=${user:-$(get_user_host $host)}
[[ $user == "root" ]] && user="" && >&2 echo warning, will not set up any user than root for UCI shell
[[ ! $user ]] && >&2 echo unable to determin remote user for host $host, aborting && return 1
if [[ $supass ]] && [[ $upass ]]; then >&2 echo specify either -s or -p or neither but not both; return 1; fi
[[ $upass ]] && remote_script -s -f install_shell_base $host install-shell-base $user -- $upass "$@" && return $?
[[ $supass ]] && remote_script $supass -f install_shell_base $host install-shell-base $user -- $upass "$@" && return $?
remote_script -f install_shell_base $host install-shell-base $user -- "$@"
}