shell-network/modules/remote.mod

226 lines
6.9 KiB
Modula-2
Raw Normal View History

#!/bin/bash
module_load ssh
module_load bundle
module_load helpers
module_load ssh-copy
remote_function () {
[[ $2 ]] && module_load $2
if declare -f $1; then
local file
file=$(mkrfilename function)
$(declare -f $1) > $file
echo $file
else
>&2 echo fatal: unable to source funtion $1, aborting
return 1
fi
}
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
help() {
cat <<EOF
usage: remote_script <remote_script options> host script <script args> -- <ssh script options>
host and script are required, script can be either path to a file containing a script to a UCI module
-s, execute remote script using sudo
-f, run a function in the passed script, otherwise script is assumed to be bash executed.
-d, dry run. don't run the rscript just output the command that will be run
-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
-h, this help text
EOF
}
# parse ssh arguments from the rest
debug $( ( IFS=$','; echo all arguments: "$*" ) )
for ((d=1; d<$#; ++d)); do
[[ ${!d} == "--" ]] && break
done
if [[ $d -lt $# ]]; then # if there are extra ssh arguments
debug found -- at $d
sshargs=("${@:$d+1:$#}")
debug $( ( IFS=$','; echo "ssh arguments: ${sshargs[*]}" ) )
# [[ ! ${sshargs[0]} ]] && { echo missing remote machine, must provide at least a hostname, -- hostname; return 3; }
args=("${@:1:$d-1}")
# reset script arguments to just those before --
set -- "${args[@]}"
debug $( ( IFS=$','; echo remaining arguments to parse: "$*" ) )
fi
# parse remote_script options
local OPTION
local OPTARG
local OPTIND
while getopts 'hdu:sf:p:x:' OPTION; do
# echo OPTION $OPTION ARG $OPTARG INDX $OPTIND
case "$OPTION" in
s)
# using sudo, password will come from ssh user password or if you supply it with -p
usesudo=true
;;
f)
# run a function within the script
# any function arguments appear after script
cfn="-f $OPTARG"
;;
d)
# dry run
dr=true
;;
u)
# run remote command as another user
ruser=$OPTARG
# usesudo=true
;;
x)
rfn=$OPTARG
;;
p)
# password of sudo account for running as root or other user
# will force running as root on remote if ruser is not specified
supass=$OPTARG
;;
h)
help
return 0
;;
*)
>&2 echo fatal: unknown remote script option $OPTION, aborting
help
return 1
;;
esac
done
shift $((OPTIND - 1))
[[ $# -lt 1 ]] && echo fatal: remote_script requires a 'host' && help && return 1
host=$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
fi
if ! declare -f $rfn >/dev/null; then echo fatal: remote-script, unable to source funtion $rfn, aborting; return 1; fi
shift
fi
local file
script=$(mkrfilename temp_function)
declare -f $rfn > $script
cfn="-f $rfn"
else
script=$2
shift 1
fi
[[ ! -f $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
bscript=$(mkrfilename bundle_script )
if ! bundle -c $cfn $script $bscript; then
>&2 echo fatal: remote_script unable to bundle script for sending, aborting; return 1;
fi
[[ $usesudo ]] && supass=$(parse_option "${sshargs[*]}" -p)
if [[ $supass ]]; then
usesudo="echo '${supass}' | sudo -u ${ruser:-root} --stdin 2>/dev/null"
echo remote script to be run as ${ruser:-root} using sudo
fi
debug remote script arguments $(remote_args "$@")
debug ssh arguments $(remote_args "${sshargs[@]}")
rscript=${save:-$(mkrfilename REMOTE_SCRIPT)}
if sshcp -d $host $bscript $rscript -- "${sshargs[@]}"; then
# make remote script excuteable
ssh "${sshargs[@]}" "$host" "chmod +x $rscript"
# run the script
ssh "${sshargs[@]}" "$host" "$usesudo" "$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"
else
>&2 echo fatal: remote_script failed because script could not be copied; return 1;
fi
rm -f $bscript
[[ $rfn ]] && [[ "${script}" =~ "temp_function" ]] && rm -f $script
}
# END REMOTE SCRIPT
# can be used with a file or HEREDOC
# todo test with < $file or with a pipe
remote_stdin () {
local file
# echo args $@
file=$( mkrfilename remote_script )
cat > $file
remote_script $1 "$file" "$@"
rm -f $file &> /dev/null
}
# # if script was executed then call the function
(return 0 2>/dev/null) ||remote_script $@
2023-12-14 21:31:40 -08:00
# 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
2023-12-14 21:31:40 -08:00
local supass;local user; local host
2023-12-14 21:31:40 -08:00
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
2023-12-14 21:31:40 -08:00
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 -- "$@"
}