From 1dbd3e9a72c961d7eeedee316cc9446a236b389b Mon Sep 17 00:00:00 2001 From: deajan Date: Tue, 20 Dec 2016 21:19:58 +0100 Subject: [PATCH] Added common files from osync --- dev/common_batch.sh | 82 ++++++++++++--------------- dev/common_install.sh | 125 +++++++++++++----------------------------- 2 files changed, 74 insertions(+), 133 deletions(-) diff --git a/dev/common_batch.sh b/dev/common_batch.sh index 80da8bd..bf5e97e 100755 --- a/dev/common_batch.sh +++ b/dev/common_batch.sh @@ -3,14 +3,14 @@ SUBPROGRAM=[prgname] PROGRAM="$SUBPROGRAM-batch" # Batch program to run osync / obackup instances sequentially and rerun failed ones AUTHOR="(L) 2013-2016 by Orsiris de Jong" CONTACT="http://www.netpower.fr - ozy@netpower.fr" -PROGRAM_BUILD=2016112402 +PROGRAM_BUILD=2016120401 ## Runs an osync /obackup instance for every conf file found ## If an instance fails, run it again if time permits if ! type "$BASH" > /dev/null; then - echo "Please run this script only with bash shell. Tested on bash >= 3.2" - exit 127 + echo "Please run this script only with bash shell. Tested on bash >= 3.2" + exit 127 fi ## If maximum execution time is not reached, failed instances will be rerun. Max exec time is in seconds. Example is set to 10 hours. @@ -66,6 +66,7 @@ function CheckEnvironment { SUBPROGRAM_EXECUTABLE=/usr/local/bin/$SUBPROGRAM.sh else Logger "Could not find [/usr/local/bin/$SUBPROGRAM.sh]" "CRITICAL" + ( >&2 echo "Could not find [/usr/local/bin/$SUBPROGRAM.sh]" ) exit 1 fi else @@ -78,62 +79,49 @@ function CheckEnvironment { } function Batch { - local runs=0 # Number of batch runs + local runs=1 # Number of batch runs local runList # Actual conf file list to run local runAgainList # List of failed conf files sto run again local confFile local result - ## Check for CONF_FILE_PATH - if [ -d "$CONF_FILE_PATH" ]; then - ## Get list of .conf files - for confFile in $CONF_FILE_PATH/*.conf - do - if [ -f "$confFile" ]; then - if [ "$runList" == "" ]; then - runList="$confFile" - else - runList=$runList" $confFile" - fi - fi - done - elif [ -f "$CONF_FILE_PATH" ] && [ "${CONF_FILE_PATH##*.}" == "conf" ]; then - runList="$CONF_FILE_PATH" - fi + local i - if [ "$runList" == "" ]; then + # Using -e because find will accept directories or files + if [ ! -e "$CONF_FILE_PATH" ]; then Logger "Cannot find conf file path [$CONF_FILE_PATH]." "CRITICAL" Usage - fi + else + # Ugly hack to read files into an array while preserving special characters + runList=() + while IFS= read -d $'\0' -r file; do runList+=("$file"); done < <(find "$CONF_FILE_PATH" -maxdepth 1 -iname "*.conf" -print0) - while ([ $MAX_EXECUTION_TIME -gt $SECONDS ] || [ $MAX_EXECUTION_TIME -eq 0 ]) && [ "$runList" != "" ] && [ $MAX_RUNS -gt $runs ] - do - Logger "$SUBPROGRAM instances will be run for: $runList" "NOTICE" - for confFile in $runList - do - $SUBPROGRAM_EXECUTABLE "$confFile" --silent $opts & - wait $! - result=$? - if [ $result != 0 ]; then - if [ $result == 1 ] || [ $result == 128 ]; then # Do not handle exit code 127 because it is already handled here - Logger "Run instance $(basename $confFile) failed with exit code [$result]." "ERROR" - if [ "$runAgainList" == "" ]; then - runAgainList="$confFile" - else - runAgainList=$runAgainList" $confFile" + while ([ $MAX_EXECUTION_TIME -gt $SECONDS ] || [ $MAX_EXECUTION_TIME -eq 0 ]) && [ "${#runList[@]}" -gt 0 ] && [ $runs -le $MAX_RUNS ]; do + runAgainList=() + Logger "Sequential run n°$runs of $SUBPROGRAM instances for:" "NOTICE" + for confFile in "${runList[@]}"; do + Logger "$(basename $confFile)" "NOTICE" + done + for confFile in "${runList[@]}"; do + $SUBPROGRAM_EXECUTABLE "$confFile" --silent $opts & + wait $! + result=$? + if [ $result != 0 ]; then + if [ $result == 1 ] || [ $result == 128 ]; then # Do not handle exit code 128 because it is already handled here + Logger "Instance $(basename $confFile) failed with exit code [$result]." "ERROR" + runAgainList+=("$confFile") + elif [ $result == 2 ]; then + Logger "Instance $(basename $confFile) finished with warnings." "WARN" fi - elif [ $result == 2 ]; then - Logger "Run instance $(basename $confFile) finished with warnings." "WARN" + else + Logger "Instance $(basename $confFile) succeed." "NOTICE" fi - else - Logger "Run instance $(basename $confFile) succeed." "NOTICE" - fi + done + runList=("${runAgainList[@]}") + runs=$(($runs + 1)) done - runList="$runAgainList" - runAgainList="" - runs=$(($runs + 1)) - done + fi } function Usage { @@ -174,7 +162,7 @@ do Usage ;; *) - opts="$i " + opts="$opts$i " ;; esac done diff --git a/dev/common_install.sh b/dev/common_install.sh index 9f97460..3d8dd77 100755 --- a/dev/common_install.sh +++ b/dev/common_install.sh @@ -1,17 +1,17 @@ #!/usr/bin/env bash +include #### _OFUNCTIONS_BOOTSTRAP SUBSET #### + PROGRAM=[prgname] PROGRAM_VERSION=[version] PROGRAM_BINARY=$PROGRAM".sh" PROGRAM_BATCH=$PROGRAM"-batch.sh" -SCRIPT_BUILD=2016112401 +SCRIPT_BUILD=2016121301 ## osync / obackup / pmocr / zsnap install script -## Tested on RHEL / CentOS 6 & 7, Fedora 23, Debian 7 & 8, Mint 17 and FreeBSD 8 & 10 +## Tested on RHEL / CentOS 6 & 7, Fedora 23, Debian 7 & 8, Mint 17 and FreeBSD 8, 10 and 11 ## Please adapt this to fit your distro needs -#TODO: silent mode and no stats mode - # Get current install.sh path from http://stackoverflow.com/a/246128/2635443 SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" @@ -35,80 +35,27 @@ PMOCR_SERVICE_FILE_SYSTEMD_SYSTEM="pmocr-srv@.service" ## Default log file if [ -w $FAKEROOT/var/log ]; then - LOG_FILE="$FAKEROOT/var/log/$PROGRAM-install.log" + LOG_FILE="$FAKEROOT/var/log/$PROGRAM-install.log" elif ([ "$HOME" != "" ] && [ -w "$HOME" ]); then - LOG_FILE="$HOME/$PROGRAM-install.log" + LOG_FILE="$HOME/$PROGRAM-install.log" else - LOG_FILE="./$PROGRAM-install.log" + LOG_FILE="./$PROGRAM-install.log" fi -# Generic quick logging function -function _QuickLogger { - local value="${1}" - local destination="${2}" # Destination: stdout, log, both - - if ([ "$destination" == "log" ] || [ "$destination" == "both" ]); then - echo -e "$(date) - $value" >> "$LOG_FILE" - elif ([ "$destination" == "stdout" ] || [ "$destination" == "both" ]); then - echo -e "$value" - fi -} - -function QuickLogger { - local value="${1}" - - if [ "$_SILENT" -eq 1 ]; then - _QuickLogger "$value" "log" - else - _QuickLogger "$value" "stdout" - fi -} - -function urlencode() { - # urlencode - - local LANG=C - local length="${#1}" - for (( i = 0; i < length; i++ )); do - local c="${1:i:1}" - case $c in - [a-zA-Z0-9.~_-]) printf "$c" ;; - *) printf '%%%02X' "'$c" ;; - esac - done -} - -function SetOSSettings { - local localOsVar - +include #### QuickLogger SUBSET #### +include #### UrlEncode SUBSET #### +include #### GetLocalOS SUBSET #### +function SetLocalOSSettings { USER=root - # 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 - # Detecting the special ubuntu userland in Windows 10 bash - if grep -i Microsoft /proc/sys/kernel/osrelease > /dev/null 2>&1; then - localOsVar="Microsoft" - else - localOsVar="$(uname -spio 2>&1)" - if [ $? != 0 ]; then - localOsVar="$(uname -v 2>&1)" - if [ $? != 0 ]; then - localOsVar="$(uname)" - fi - fi - fi - fi - - case $localOsVar in + case $LOCAL_OS in *"BSD"*) GROUP=wheel ;; - *"Darwin"*) + *"MacOSX"*) GROUP=admin ;; - *"MINGW"*|*"CYGWIN"*) + *"msys"*|*"Cygwin"*) USER="" GROUP="" ;; @@ -117,12 +64,17 @@ function SetOSSettings { ;; esac - if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ] && [ "$FAKEROOT" == "" ]); then - QuickLogger "Must be run as $USER." + if [ "$LOCAL_OS" == "Android" ] || [ "$LOCAL_OS" == "MacOSX" ] || [ "$LOCAL_OS" == "BusyBox" ]; then + QuickLogger "Cannot be installed on [$LOCAL_OS]. Please use $PROGRAM.sh directly." exit 1 fi - OS=$(urlencode "$localOsVar") + if ([ "$USER" != "" ] && [ "$(whoami)" != "$USER" ] && [ "$FAKEROOT" == "" ]); then + QuickLogger "Must be run as $USER." + exit 1 + fi + + OS=$(UrlEncode "$localOsVar") } function GetInit { @@ -262,22 +214,22 @@ function CopyServiceFiles { } function Statistics { - if type wget > /dev/null; then - wget -qO- "$STATS_LINK" > /dev/null 2>&1 - if [ $? == 0 ]; then - return 0 - fi + if type wget > /dev/null; then + wget -qO- "$STATS_LINK" > /dev/null 2>&1 + if [ $? == 0 ]; then + return 0 + fi fi - if type curl > /dev/null; then - curl "$STATS_LINK" -o /dev/null > /dev/null 2>&1 - if [ $? == 0 ]; then - return 0 - fi + if type curl > /dev/null; then + curl "$STATS_LINK" -o /dev/null > /dev/null 2>&1 + if [ $? == 0 ]; then + return 0 + fi fi - QuickLogger "Neiter wget nor curl could be used for. Cannot run statistics. Use the provided link please." - return 1 + QuickLogger "Neiter wget nor curl could be used for. Cannot run statistics. Use the provided link please." + return 1 } function Usage { @@ -288,13 +240,13 @@ function Usage { exit 127 } -_SILENT=0 +_LOGGER_SILENT=false _STATS=1 for i in "$@" do case $i in --silent) - _SILENT=1 + _LOGGER_SILENT=true ;; --no-stats) _STATS=0 @@ -308,7 +260,8 @@ if [ "$FAKEROOT" != "" ]; then mkdir -p "$SERVICE_DIR_SYSTEMD_SYSTEM" "$SERVICE_DIR_SYSTEMD_USER" "$BIN_DIR" fi -SetOSSettings +GetLocalOS +SetLocalOSSettings CreateConfDir CopyExampleFiles CopyProgram @@ -319,7 +272,7 @@ STATS_LINK="http://instcount.netpower.fr?program=$PROGRAM&version=$PROGRAM_VERSI QuickLogger "$PROGRAM installed. Use with $BIN_DIR/$PROGRAM" if [ $_STATS -eq 1 ]; then - if [ $_SILENT -eq 1 ]; then + if [ $_LOGGER_SILENT == true ]; then Statistics else QuickLogger "In order to make install statistics, the script would like to connect to $STATS_LINK"