176 lines
4.8 KiB
Bash
176 lines
4.8 KiB
Bash
#!/bin/bash
|
|
|
|
module_load confirm
|
|
module_load net-utils
|
|
module_load ssh
|
|
module_load ssh-pubkey
|
|
module_load minimize
|
|
module_load_path
|
|
|
|
# echo sourcing functions
|
|
|
|
|
|
_v_ () {
|
|
# used internally to module for getting value from environment variable
|
|
var=${REMOTE_HOST_PREFIX:-"_"}_$1
|
|
echo ${!var}
|
|
}
|
|
|
|
remote_host_env () {
|
|
[[ $1 = "--set" || $1 = "-s" ]] && { local set; set=true; shift; }
|
|
local prefix=${REMOTE_HOST_PREFIX:-"_"} # double __ is default prefix
|
|
[[ $1 = "--prefix" || $1 = "-p" ]] && { local prefix; prefix=$2; export REMOTE_HOST_PREFIX=$prefix; shift 2; }
|
|
local envfile
|
|
envfile=${1:-$REMOTE_HOST_ENV_FILE}
|
|
# echo passed $1
|
|
# echo set $set
|
|
# echo REMOTE_HOST_ENV_FILE $REMOTE_HOST_ENV_FILE
|
|
# echo envfile $envfile
|
|
[[ ! $envfile ]] && { echo "must pass remote host environment file or set REMOTE_HOST_ENV"; return 1; }
|
|
[[ ! -f $envfile ]] && { echo ERROR environment file, $1, does not exist; return 2; }
|
|
|
|
if [[ $set || ! $REMOTE_HOST_ENV_FILE ]]; then
|
|
|
|
# curr="$(env | grep ^${prefix}_)"
|
|
# curr= $(sed 's/=.*//' <<<"$curr")
|
|
curr=$(sed 's/=.*//' <<< "$(env | grep ^${prefix}_)")
|
|
if [[ $curr ]]; then
|
|
echo "****** removing current values ************"
|
|
echo "$curr"
|
|
echo "--------------------"
|
|
while IFS= read -r var; do
|
|
# echo adding prefix: $prefix
|
|
# echo removing environment variable: $var
|
|
unset "$var"
|
|
done <<< "$curr"
|
|
fi
|
|
|
|
if [[ $1 ]]; then
|
|
echo exporting $1.env to REMOTE_HOST_ENV_FILE
|
|
export REMOTE_HOST_ENV_FILE=$(abs_path $1)
|
|
fi
|
|
|
|
while IFS= read -r var; do
|
|
# echo adding prefix: $prefix
|
|
# echo environment variable: ${prefix}_$var
|
|
export ${prefix}_$var
|
|
done <<< "$(minimize "$envfile")"
|
|
echo "****** Now Using Remote Host Environment ************"
|
|
env | grep ^${prefix}_
|
|
env | grep REMOTE
|
|
echo "************************************"
|
|
|
|
fi
|
|
|
|
[[ ! $(env | grep ^${prefix}_) ]] && { echo ERROR: no remote host environment has been set; return 4; }
|
|
|
|
return 0
|
|
}
|
|
|
|
remote_host_sshoptions () {
|
|
local key; local pw; local user;
|
|
|
|
[[ $1 = "-k" ]] && { shift; [[ $(_v_ KEY) ]] && key="-k $(_v_ KEY)"; }
|
|
[[ $1 = "-p" ]] && { shift; [[ $(_v_ USER_PW) ]] && pw="-p $(_v_ USER_PW)"; }
|
|
# echo key: $key
|
|
# echo pw: $pw
|
|
# echo envfile: $1
|
|
if ! remote_host_env; then
|
|
return $?
|
|
fi
|
|
[[ (! $pw) && $(_v_ KEY) ]] && key="-k $(_v_ KEY)"
|
|
[[ (! $key) && $(_v_ USER_PW) ]] && pw="-p $(_v_ USER_PW)"
|
|
|
|
echo $pw $(_v_ MP) $key "$@"
|
|
# ssh $pw $(_v_ MP) $key $user$(_v_ HOST_NAME)
|
|
}
|
|
|
|
remote_host_user () {
|
|
local host=$(_v_ HOST_NAME)
|
|
[[ ! $host ]] && { echo ERROR: environment file MUST include a HOST_NAME value; return 1; }
|
|
if ! remote_host_env; then
|
|
return $?
|
|
fi
|
|
[[ $(_v_ USER) ]] && user=$(_v_ USER)@
|
|
[[ $(_v_ USE_ROOT) ]] && user=root@
|
|
echo $user$host
|
|
|
|
}
|
|
|
|
remote_host_login () {
|
|
local sopts; local cmd
|
|
if [[ ! $* =~ "--" ]]; then
|
|
sopts=$*
|
|
else
|
|
sopts=$(sed 's/\(.*\)--.*/\1/' <<< "$*")
|
|
cmd=$(sed 's/.*--\(.*\)/\1/' <<< "$*")
|
|
fi
|
|
local options=$(remote_host_sshoptions "$sopts")
|
|
[[ $? -gt 0 ]] && return $?
|
|
local userhost=$(remote_host_user)
|
|
[[ $? -gt 0 ]] && return $?
|
|
echo ssh $options $userhost
|
|
ssh $options $userhost $cmd
|
|
}
|
|
|
|
remote_host_pubkey () {
|
|
# usage pubkey options -- ssh options,
|
|
local add=$(_v_ ADD_KEY)
|
|
local sudo=$(_v_ USER_PW)
|
|
[[ ! $add ]] && { echo no value given for ADD_KEY, aborting; return 2; }
|
|
local kopts; local sopts
|
|
if [[ ! $* =~ "--" ]]; then
|
|
kopts=$*
|
|
else
|
|
kopts=$(sed 's/\(.*\)--.*/\1/' <<< "$*")
|
|
sopts=$(sed 's/.*--\(.*\)/\1/' <<< "$*")
|
|
fi
|
|
echo adding public key: $add
|
|
kopts="$kopts -k $add"
|
|
module_load ssh-pubkey
|
|
echo sshpubkey $kopts $(remote_host_user) $(remote_host_sshoptions $sopts)
|
|
sshpubkey $kopts $(remote_host_user) $(remote_host_sshoptions $sopts)
|
|
echo sudo pw $sudo
|
|
sshpubkey -a root -s $sudo $kopts $(remote_host_user) $(remote_host_sshoptions $sopts)
|
|
|
|
}
|
|
|
|
# remote_host_shell () {
|
|
|
|
|
|
# }
|
|
|
|
remote_host_mount () {
|
|
module_load sshfs
|
|
local base=$(_v_ BASE_MOUNT_POINT)
|
|
if [[ $1 = "-u" ]]; then
|
|
umount $base/home
|
|
umount $base/root
|
|
return 0
|
|
fi
|
|
|
|
local mopts; local sopts
|
|
if [[ ! $* =~ "--" ]]; then
|
|
mopts=$*
|
|
else
|
|
mopts=$(sed 's/\(.*\)--.*/\1/' <<< "$*")
|
|
sopts=$(sed 's/.*--\(.*\)/\1/' <<< "$*")
|
|
fi
|
|
|
|
|
|
mkdir -p $base > /dev/null
|
|
base=${base:-"/mnt/_temp_"}
|
|
local user=$(_v_ USER)
|
|
# echo smount $mopts $(remote_host_user):/home/${user:-ubuntu} $base/home $(remote_host_sshoptions $sopts)
|
|
smount $mopts $user@$(_v_ HOST_NAME):/home/${user:-ubuntu} $base/$user $(remote_host_sshoptions $sopts)
|
|
smount $mopts root@$(_v_ HOST_NAME):/ $base/fs $(remote_host_sshoptions $sopts)
|
|
}
|
|
|
|
# below is run at module load
|
|
echo done loading module,functions available are
|
|
flist | grep remote_host
|
|
|
|
|
|
alias rhl="remote_host_login"
|
|
alias rhe="remote_host_env"
|
|
alias rhm="remote_host_mount" |