From a0f44558fa80586b9ca4b8d3f5940812eefeac55 Mon Sep 17 00:00:00 2001 From: "kebler.net" Date: Thu, 9 Feb 2023 09:26:51 -0800 Subject: [PATCH] add shell base install script and corresponding remote install fixed-imporved remote script and added support for script from HEREDOC or stdin --- modules/install-shell-base.lib | 39 +++++++++++++++++ modules/remote.mod | 76 ++++++++++++++++++++-------------- 2 files changed, 85 insertions(+), 30 deletions(-) create mode 100644 modules/install-shell-base.lib diff --git a/modules/install-shell-base.lib b/modules/install-shell-base.lib new file mode 100644 index 0000000..3bff94e --- /dev/null +++ b/modules/install-shell-base.lib @@ -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 +# 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 + diff --git a/modules/remote.mod b/modules/remote.mod index c5a6844..80d8a17 100644 --- a/modules/remote.mod +++ b/modules/remote.mod @@ -4,27 +4,35 @@ remote_script () { # usage: remote_script script -- host # 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 $@