From 252df96e59fc72f5e08a10d21c97998028168455 Mon Sep 17 00:00:00 2001 From: deajan Date: Thu, 9 Feb 2017 12:12:41 +0100 Subject: [PATCH] Updated ofunctions from osync --- dev/ofunctions.sh | 112 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 102 insertions(+), 10 deletions(-) diff --git a/dev/ofunctions.sh b/dev/ofunctions.sh index f2b8dbd..38f94c0 100644 --- a/dev/ofunctions.sh +++ b/dev/ofunctions.sh @@ -2,8 +2,8 @@ #### OFUNCTIONS FULL SUBSET #### #### OFUNCTIONS MINI SUBSET #### -_OFUNCTIONS_VERSION=2.1-RC1+dev -_OFUNCTIONS_BUILD=2017010401 +_OFUNCTIONS_VERSION=2.1-RC2 +_OFUNCTIONS_BUILD=2017020801 #### _OFUNCTIONS_BOOTSTRAP SUBSET #### _OFUNCTIONS_BOOTSTRAP=true #### _OFUNCTIONS_BOOTSTRAP SUBSET END #### @@ -125,7 +125,7 @@ function joinString { function _Logger { local logValue="${1}" # Log to file local stdValue="${2}" # Log to screeen - local toStderr="${3:-false}" # Log to stderr instead of stdout + local toStdErr="${3:-false}" # Log to stderr instead of stdout if [ "$logValue" != "" ]; then echo -e "$logValue" >> "$LOG_FILE" @@ -134,7 +134,7 @@ function _Logger { fi if [ "$stdValue" != "" ] && [ "$_LOGGER_SILENT" != true ]; then - if [ $toStderr == true ]; then + if [ $toStdErr == true ]; then # Force stderr color in subshell (>&2 echo -e "$stdValue") @@ -387,7 +387,12 @@ function SendAlert { attachment=true fi if [ -e "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP" ]; then - body="$MAIL_ALERT_MSG"$'\n\n'"$(cat $RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP)" + if [ "$MAIL_BODY_CHARSET" != "" ] && type iconv > /dev/null 2>&1; then + iconv -f UTF-8 -t $MAIL_BODY_CHARSET "$RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP" > "$RUN_DIR/$PROGRAM._Logger.iconv.$SCRIPT_PID.$TSTAMP" + body="$MAIL_ALERT_MSG"$'\n\n'"$(cat $RUN_DIR/$PROGRAM._Logger.iconv.$SCRIPT_PID.$TSTAMP)" + else + body="$MAIL_ALERT_MSG"$'\n\n'"$(cat $RUN_DIR/$PROGRAM._Logger.$SCRIPT_PID.$TSTAMP)" + fi fi if [ $ERROR_ALERT == true ]; then @@ -1084,6 +1089,8 @@ function ArrayContains () { #### GetLocalOS SUBSET #### function GetLocalOS { local localOsVar + local localOsName + local localOsVer # 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 @@ -1144,8 +1151,14 @@ function GetLocalOS { Logger "Local OS: [$localOsVar]." "DEBUG" fi + # Get linux versions + if [ -f "/etc/os-release" ]; then + localOsName=$(GetConfFileValue "/etc/os-release" "NAME") + localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION") + fi + # Add a global variable for statistics in installer - LOCAL_OS_FULL="$localOsVar" + LOCAL_OS_FULL="$localOsVar ($localOsName $localOsVer)" } #### GetLocalOS SUBSET END #### @@ -1160,10 +1173,12 @@ function GetRemoteOS { local remoteOsVar -$SSH_CMD bash -s << 'ENDSSH' >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" 2>&1 +$SSH_CMD env _REMOTE_TOKEN="$_REMOTE_TOKEN" bash -s << 'ENDSSH' >> "$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP" 2>&1 function GetOs { local localOsVar + local localOsName + local localOsVer # 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 @@ -1182,7 +1197,13 @@ function GetOs { fi fi fi - echo "$localOsVar" + # Get linux versions + if [ -f "/etc/os-release" ]; then + localOsName=$(GetConfFileValue "/etc/os-release" "NAME") + localOsVer=$(GetConfFileValue "/etc/os-release" "VERSION") + fi + + echo "$localOsVar ($localOsName $localOsVer)" } GetOs @@ -1280,7 +1301,7 @@ function RunRemoteCommand { fi Logger "Running command [$command] on remote host." "NOTICE" - cmd=$SSH_CMD' "$command" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP'" 2>&1' + cmd=$SSH_CMD' "env _REMOTE_TOKEN="'$_REMOTE_TOKEN'" $command" > "'$RUN_DIR/$PROGRAM.${FUNCNAME[0]}.$SCRIPT_PID.$TSTAMP'" 2>&1' Logger "cmd: $cmd" "DEBUG" eval "$cmd" & WaitForTaskCompletion $! 0 $hardMaxTime $SLEEP_TIME $KEEP_LOGGING true true false @@ -1646,6 +1667,11 @@ function InitLocalOSDependingSettings { if [ "$LOCAL_OS" == "msys" ] || [ "$LOCAL_OS" == "Cygwin" ]; then FIND_CMD=$(dirname $BASH)/find PING_CMD='$SYSTEMROOT\system32\ping -n 2' + + # On BSD, when not root, min ping interval is 1s + elif [ "$LOCAL_OS" == "BSD" ] && [ "$LOCAL_USER" != "root" ]; then + FIND_CMD=find + PING_CMD="ping -c 2 -i 1" else FIND_CMD=find PING_CMD="ping -c 2 -i .2" @@ -1695,7 +1721,7 @@ function InitRemoteOSDependingSettings { fi ## Set rsync default arguments - RSYNC_ARGS="-rltD" + RSYNC_ARGS="-rltD -8" if [ "$_DRYRUN" == true ]; then RSYNC_DRY_ARG="-n" DRY_WARNING="/!\ DRY RUN " @@ -1790,4 +1816,70 @@ function ParentPid { fi } +# Neat version compare function found at http://stackoverflow.com/a/4025065/2635443 +# Returns 0 if equal, 1 if $1 > $2 and 2 if $1 < $2 +vercomp () { + if [[ $1 == $2 ]] + then + return 0 + fi + local IFS=. + local i ver1=($1) ver2=($2) + # fill empty fields in ver1 with zeros + for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) + do + ver1[i]=0 + done + for ((i=0; i<${#ver1[@]}; i++)) + do + if [[ -z ${ver2[i]} ]] + then + # fill empty fields in ver2 with zeros + ver2[i]=0 + fi + if ((10#${ver1[i]} > 10#${ver2[i]})) + then + return 1 + fi + if ((10#${ver1[i]} < 10#${ver2[i]})) + then + return 2 + fi + done + return 0 +} + +#### GetConfFileValue SUBSET #### +function GetConfFileValue () { + local file="${1}" + local name="${2}" + local value + + value=$(grep "^$name=" "$file") + if [ $? == 0 ]; then + value="${value##*=}" + echo "$value" + else + Logger "Cannot get value for [$name] in config file [$file]." "ERROR" + fi +} +#### GetConfFileValue SUBSET END #### + +#### SetConfFileValue SUBSET #### +function SetConfFileValue () { + local file="${1}" + local name="${2}" + local value="${3}" + + if grep "^$name=" "$file" > /dev/null; then + # Using -i.tmp for BSD compat + sed -i.tmp "s/^$name=.*/$name=$value/" "$file" + rm -f "$file.tmp" + Logger "Set [$name] to [$value] in config file [$file]." "DEBUG" + else + Logger "Cannot set value [$name] to [$value] in config file [$file]." "ERROR" + fi +} +#### SetConfFileValue SUBSET END #### + #### OFUNCTIONS FULL SUBSET END ####