add shell base install script and corresponding remote install

fixed-imporved remote script and added support for script from HEREDOC or stdin
master
Kebler Network System Administrator 2023-02-09 09:26:51 -08:00
parent 2b426a1225
commit a0f44558fa
2 changed files with 85 additions and 30 deletions

View File

@ -0,0 +1,39 @@
#!/bin/bash
# assumes that bash is installed
#!/bin/bash
# must! be run as sudo
install_shell_base () {
# 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; }
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}
}
# remote_install_shell_base -r <remote host> <sudo pass if host not root> <user to install, default is $USER>
# module_load install-shell-base && remote_install_shell_base -r newboxr
remote_install_shell_base () {
module_load remote
local supass;local user
if [[ $1 == "-r" ]]; then
shift 1;
user=$2;
else
[[ ! $2 ]] && { echo sudo password for remote user required; return 2; }
supass="-p $2"
user=$3
fi
remote_script $supass install-shell-base $user -- $1
}
# # if script was executed then call the function
(return 0 2>/dev/null) || install_shell_base $@
# example
# $ module_load install-shell-base
# $ remote_install_shell_base -r portabler david

View File

@ -4,27 +4,35 @@ remote_script () {
# usage: remote_script script -- <ssh setup options> host <ssh options>
# see ssh function
#
local sshargs;local user;local supass;local fn;
local sshargs;local user;local supass;local fn;local args;local file
[[ "$*" =~ "--" ]] && sshargs=$(sed 's/.*--\(.*\)/\1/' <<< "$@")
[[ ! $sshargs ]] && { echo missing remote machine, must provide at least a hostname, -- hostname; return 3; }
# reset arguments to just those before --
set -- $(sed 's/\(.*\)--.*/\1/' <<< "$@")
local OPTION
local OPTARG
local OPTIND
while getopts 'u:s:f:' OPTION; do
# echo OPTION $OPTION ARG $OPTARG
while getopts 'u:s:f:p:' OPTION; do
# echo OPTION $OPTION ARG $OPTARG INDX $OPTIND
case "$OPTION" in
s)
# script filename to run
file=$OPTARG
;;
f)
# run a function within the script
fn=$OPTARG
;;
u)
# run command as another user
user=$OPTARG
;;
s)
p)
# password of sudo account for running as root or other user
supass=$OPTARG
;;
;;
*)
echo unknown remote script option -$OPTARG
return 1
@ -32,33 +40,26 @@ local OPTION
esac
done
shift $((OPTIND - 1))
shift $((OPTIND - 1))
local script=$1
[[ ! -f $script ]] && { echo cannot find script $script to run; return 1; }
[[ "$*" =~ "--" ]] && sshargs=$(sed 's/.*--\(.*\)/\1/' <<< "$@")
[[ ! $sshargs ]] && { echo must provide at least a hostname; return 3; }
# escape arguments with spaces
local i=2
local count=0
declare -a args
while [[ ! ${!i} = "--" ]]
do
#echo "${!i}"
args[count]=$(printf '%q' "${!i}")
count=$((count+1))
i=$((i+1))
done
[[ ! $file ]] && { file=$1;shift 1; }
if [[ ! -f $file ]]; then
file=$(module_find $file)
[[ ! $file ]] && { echo no module or script at $file to run; return 1; }
fi
# remaining script arguments
args=$*
# echo arguments for script: $args
script=$file
# if script loads a module then use bundler
if [[ $(cat $script | grep module_load) ]]; then
echo bundling modules with script
# echo bundling modules with script
local temp
module_load bundle
script=$( mktemp -t TEMP_FILE_script.XXXXXXXX )
bundle $1 $script
bundle $file $script
temp=true
fi
@ -74,23 +75,38 @@ if [[ $supass ]]; then
fi
fi
src=$(cat $script)
src="$(cat $script);"
if [[ $fn ]]; then
# echo running function
cmd="bash -c '$src; $fn ${args[*]}'"
cmd="bash -c '$src $fn ${args[*]}'"
else
# echo running as script
local sargs
[[ ${args[*]} ]] && sargs=$(printf '%s\n' "set -- ${args[*]}")
cmd="bash -c '$sargs; $(cat $script)'"
[[ ${args} ]] && sargs="$(printf '%s\n' "set -- ${args}"); "
cmd="bash -c '$sargs $src'"
fi
# echo ssh $sshargs "$_sudo" "$cmd"
ssh $sshargs "$_sudo" "$cmd"
[[ $temp ]]&& rm -f $script
}
# can be used with a file or HEREDOC
# todo test with < $file or with a pipe
remote_stdin () {
local file
# echo args $@
file=$( mktemp -t TEMP_FILE_script.XXXXXXXX )
cat > $file
echo remote_script -s "$file" "$@"
#remote_script -s "$file" "$@"
rm $file
}
# # if script was executed then call the function
(return 0 2>/dev/null) ||remote_script $@