From 9cafba436dc1f2096f23f03bf7529e11651434eb Mon Sep 17 00:00:00 2001 From: deajan Date: Mon, 14 Nov 2016 23:10:37 +0100 Subject: [PATCH] Refactor GetRemoteOS --- dev/ofunctions.sh | 76 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/dev/ofunctions.sh b/dev/ofunctions.sh index 5db2324..233f48f 100644 --- a/dev/ofunctions.sh +++ b/dev/ofunctions.sh @@ -1,6 +1,6 @@ #### MINIMAL-FUNCTION-SET BEGIN #### -## FUNC_BUILD=2016111403 +## FUNC_BUILD=2016111404 ## BEGIN Generic bash functions written in 2013-2016 by Orsiris de Jong - http://www.netpower.fr - ozy@netpower.fr ## To use in a program, define the following variables: @@ -501,7 +501,7 @@ function LoadConfigFile { } function Spinner { - if [ $_SILENT == true ] || [ $_LOGGER_ERR_ONLY == true ]; then + if [ $_SILENT == true ] || [ "$_LOGGER_ERR_ONLY" == true ]; then return 0 fi @@ -915,8 +915,7 @@ function GetLocalOS { local localOsVar # There's no good way to tell if currently running in BusyBox shell. Using sluggish way. - ls --help 2>&1 | grep -i BusyBox > /dev/null - if [ $? == 0 ]; then + if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then localOsVar="BusyBox" else localOsVar="$(uname -spio 2>&1)" @@ -961,6 +960,73 @@ function GetLocalOS { function GetRemoteOS { __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG + local remoteOsVar + +$SSH_CMD bash -s << 'ENDSSH' >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" 2>&1 + +function GetOs { + local localOsVar + + # There's no good way to tell if currently running in BusyBox shell. Using sluggish way. + if ls --help 2>&1 | grep -i "BusyBox" > /dev/null; then + localOsVar="BusyBox" + else + localOsVar="$(uname -spio 2>&1)" + if [ $? != 0 ]; then + localOsVar="$(uname -v 2>&1)" + if [ $? != 0 ]; then + localOsVar="$(uname)" + fi + fi + fi + + echo "$localOsVar" +} + +GetOs + +ENDSSH + + if [ -f "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID" ]; then + remoteOsVar=$(cat "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID") + case $remoteOsVar in + *"Linux"*) + REMOTE_OS="Linux" + ;; + *"BSD"*) + REMOTE_OS="BSD" + ;; + *"MINGW32"*|*"CYGWIN"*) + REMOTE_OS="msys" + ;; + *"Darwin"*) + REMOTE_OS="MacOSX" + ;; + *"BusyBox"*) + REMOTE_OS="BUSYBOX" + ;; + *"ssh"*|*"SSH"*) + Logger "Cannot connect to remote system." "CRITICAL" + exit 1 + ;; + *) + if [ "$IGNORE_OS_TYPE" == "yes" ]; then #DOC: Undocumented debug only setting + Logger "Running on unknown remote OS [$remoteOsVar]." "WARN" + return + fi + Logger "Running on remote OS failed. Please report to the author if the OS is not supported." "CRITICAL" + Logger "Remote OS said:\n$remoteOsVar" "CRITICAL" + exit 1 + esac + Logger "Remote OS: [$remoteOsVar]." "DEBUG" + else + Logge "Cannot get Remote OS" "CRITICAL" + fi +} + +function OldGetRemoteOS { + __CheckArguments 0 $# ${FUNCNAME[0]} "$@" #__WITH_PARANOIA_DEBUG + local retval local cmd local remoteOsVar @@ -969,7 +1035,7 @@ function GetRemoteOS { CheckConnectivity3rdPartyHosts CheckConnectivityRemoteHost - cmd=$SSH_CMD' "ls --help 2>&1 | grep -i BusyBox > /dev/null"' + cmd=$SSH_CMD' "if [ ls --help 2>&1 | grep -i BusyBox > /dev/null ]; then exit 0; else exit 1"' Logger "cmd: $cmd" "DEBUG" eval "$cmd" & WaitForTaskCompletion $! 120 240 ${FUNCNAME[0]}"-0" true $KEEP_LOGGING