From 9ffb2ccfb0858528f0310a2ba8b27c457947d5af Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 1 Jun 2020 22:45:11 -0400 Subject: [PATCH 01/16] Revert "scripts: Various shell script cleanups (#4008)" This reverts commit fba4fd53146fc85043e47ed26e96a7dc93bb10d6. The series of commits I made for PR #4008 were squashed into a single commit and rebased. Somewhere along the way a mistake was made in a merge conflict resolution, resulting in some bad code in `bin/buildForWindows.sh`. This commit reverts the bad squashed commit. --- bin/buildForWindows.sh | 52 ++++++++++------------------ bin/debugRun.sh | 15 +++++--- bin/installDeps.sh | 77 +++++++++++++++++++++++++----------------- bin/run.sh | 40 +++++++++++----------- bin/safeRun.sh | 77 +++++++++++++++++++++--------------------- 5 files changed, 132 insertions(+), 129 deletions(-) diff --git a/bin/buildForWindows.sh b/bin/buildForWindows.sh index b991b7ce3..e4ca9a883 100755 --- a/bin/buildForWindows.sh +++ b/bin/buildForWindows.sh @@ -1,23 +1,5 @@ #!/bin/sh -pecho() { printf %s\\n "$*"; } -log() { pecho "$@"; } -error() { log "ERROR: $@" >&2; } -fatal() { error "$@"; exit 1; } -is_cmd() { command -v "$@" >/dev/null 2>&1; } - -# Move to the folder where ep-lite is installed -cd "$(dirname "$0")"/.. - -# Is wget installed? -is_cmd wget || fatal "Please install wget" - -# Is zip installed? -is_cmd zip || fatal "Please install zip" - -# Is zip installed? -is_cmd unzip || fatal "Please install unzip" -======= #Move to the folder where ep-lite is installed cd $(dirname $0) @@ -47,9 +29,9 @@ hash unzip > /dev/null 2>&1 || { START_FOLDER=$(pwd); TMP_FOLDER=$(mktemp -d) -log "create a clean environment in $TMP_FOLDER..." -cp -ar . "$TMP_FOLDER" -cd "$TMP_FOLDER" +echo "create a clean environment in $TMP_FOLDER..." +cp -ar . $TMP_FOLDER +cd $TMP_FOLDER rm -rf node_modules rm -f etherpad-lite-win.zip @@ -57,33 +39,33 @@ rm -f etherpad-lite-win.zip # making the windows package smaller export NODE_ENV=production -log "do a normal unix install first..." +echo "do a normal unix install first..." bin/installDeps.sh || exit 1 -log "copy the windows settings template..." +echo "copy the windows settings template..." cp settings.json.template settings.json -log "resolve symbolic links..." +echo "resolve symbolic links..." cp -rL node_modules node_modules_resolved rm -rf node_modules mv node_modules_resolved node_modules -log "download windows node..." +echo "download windows node..." cd bin wget "https://nodejs.org/dist/latest-erbium/win-x86/node.exe" -O ../node.exe -log "remove git history to reduce folder size" +echo "remove git history to reduce folder size" rm -rf .git/objects -log "remove windows jsdom-nocontextify/test folder" -rm -rf "$TMP_FOLDER"/src/node_modules/wd/node_modules/request/node_modules/form-data/node_modules/combined-stream/test -rm -rf "$TMP_FOLDER"/src/node_modules/nodemailer/node_modules/mailcomposer/node_modules/mimelib/node_modules/encoding/node_modules/iconv-lite/encodings/tables +echo "remove windows jsdom-nocontextify/test folder" +rm -rf $TMP_FOLDER/src/node_modules/wd/node_modules/request/node_modules/form-data/node_modules/combined-stream/test +rm -rf $TMP_FOLDER/src/node_modules/nodemailer/node_modules/mailcomposer/node_modules/mimelib/node_modules/encoding/node_modules/iconv-lite/encodings/tables -log "create the zip..." -cd "$TMP_FOLDER" -zip -9 -r "$START_FOLDER"/etherpad-lite-win.zip ./* +echo "create the zip..." +cd $TMP_FOLDER +zip -9 -r $START_FOLDER/etherpad-lite-win.zip ./* -log "clean up..." -rm -rf "$TMP_FOLDER" +echo "clean up..." +rm -rf $TMP_FOLDER -log "Finished. You can find the zip in the Etherpad root folder, it's called etherpad-lite-win.zip" \ No newline at end of file +echo "Finished. You can find the zip in the Etherpad root folder, it's called etherpad-lite-win.zip" diff --git a/bin/debugRun.sh b/bin/debugRun.sh index d9b18aaa2..246d53900 100755 --- a/bin/debugRun.sh +++ b/bin/debugRun.sh @@ -1,15 +1,20 @@ #!/bin/sh -# Move to the folder where ep-lite is installed -cd "$(dirname "$0")"/.. +#Move to the folder where ep-lite is installed +cd $(dirname $0) -# Prepare the environment +#Was this script started in the bin folder? if yes move out +if [ -d "../bin" ]; then + cd "../" +fi + +#Prepare the environment bin/installDeps.sh || exit 1 echo "If you are new to debugging Node.js with Chrome DevTools, take a look at this page:" echo "https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27" echo "Open 'chrome://inspect' on Chrome to start debugging." -# Use 0.0.0.0 to allow external connections to the debugger -# (ex: running Etherpad on a docker container). Use default port # (9229) +#Use 0.0.0.0 to allow external connections to the debugger +#(ex: running Etherpad on a docker container). Use default port # (9229) node --inspect=0.0.0.0:9229 node_modules/ep_etherpad-lite/node/server.js "$@" diff --git a/bin/installDeps.sh b/bin/installDeps.sh index 5e0bbb931..cac279e11 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -8,12 +8,6 @@ REQUIRED_NODE_MINOR=13 REQUIRED_NPM_MAJOR=5 REQUIRED_NPM_MINOR=5 -pecho() { printf %s\\n "$*"; } -log() { pecho "$@"; } -error() { log "ERROR: $@" >&2; } -fatal() { error "$@"; exit 1; } -is_cmd() { command -v "$@" >/dev/null 2>&1; } - require_minimal_version() { PROGRAM_LABEL="$1" VERSION_STRING="$2" @@ -22,50 +16,71 @@ require_minimal_version() { # Flag -s (--only-delimited on GNU cut) ensures no string is returned # when there is no match - DETECTED_MAJOR=$(pecho "$VERSION_STRING" | cut -s -d "." -f 1) - DETECTED_MINOR=$(pecho "$VERSION_STRING" | cut -s -d "." -f 2) + DETECTED_MAJOR=$(echo $VERSION_STRING | cut -s -d "." -f 1) + DETECTED_MINOR=$(echo $VERSION_STRING | cut -s -d "." -f 2) - [ -n "$DETECTED_MAJOR" ] || fatal "Cannot extract $PROGRAM_LABEL major version from version string \"$VERSION_STRING\"" + if [ -z "$DETECTED_MAJOR" ]; then + printf 'Cannot extract %s major version from version string "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" >&2 + exit 1 + fi - [ -n "$DETECTED_MINOR" ] || fatal "Cannot extract $PROGRAM_LABEL minor version from version string \"$VERSION_STRING\"" + if [ -z "$DETECTED_MINOR" ]; then + printf 'Cannot extract %s minor version from version string "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" >&2 + exit 1 + fi case "$DETECTED_MAJOR" in ''|*[!0-9]*) - fatal "$PROGRAM_LABEL major version from \"$VERSION_STRING\" is not a number. Detected: \"$DETECTED_MAJOR\"" + printf '%s major version from "%s" is not a number. Detected: "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" "$DETECTED_MAJOR" >&2 + exit 1 ;; esac case "$DETECTED_MINOR" in ''|*[!0-9]*) - fatal "$PROGRAM_LABEL minor version from \"$VERSION_STRING\" is not a number. Detected: \"$DETECTED_MINOR\"" + printf '%s minor version from "%s" is not a number. Detected: "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" "$DETECTED_MINOR" >&2 + exit 1 esac - [ "$DETECTED_MAJOR" -gt "$REQUIRED_MAJOR" ] || ([ "$DETECTED_MAJOR" -eq "$REQUIRED_MAJOR" ] && [ "$DETECTED_MINOR" -ge "$REQUIRED_MINOR" ]) \ - || fatal "Your $PROGRAM_LABEL version \"$VERSION_STRING\" is too old. $PROGRAM_LABEL $REQUIRED_MAJOR.$REQUIRED_MINOR.x or higher is required." + if [ "$DETECTED_MAJOR" -lt "$REQUIRED_MAJOR" ] || ([ "$DETECTED_MAJOR" -eq "$REQUIRED_MAJOR" ] && [ "$DETECTED_MINOR" -lt "$REQUIRED_MINOR" ]); then + printf 'Your %s version "%s" is too old. %s %d.%d.x or higher is required.\n' "$PROGRAM_LABEL" "$VERSION_STRING" "$PROGRAM_LABEL" "$REQUIRED_MAJOR" "$REQUIRED_MINOR" >&2 + exit 1 + fi } -# Move to the folder where ep-lite is installed -cd "$(dirname "$0")"/.. +#Move to the folder where ep-lite is installed +cd $(dirname $0) -# Is node installed? -# Not checking io.js, default installation creates a symbolic link to node -is_cmd node || fatal "Please install node.js ( https://nodejs.org )" +#Was this script started in the bin folder? if yes move out +if [ -d "../bin" ]; then + cd "../" +fi -# Is npm installed? -is_cmd npm || fatal "Please install npm ( https://npmjs.org )" +#Is node installed? +#Not checking io.js, default installation creates a symbolic link to node +hash node > /dev/null 2>&1 || { + echo "Please install node.js ( https://nodejs.org )" >&2 + exit 1 +} -# Check npm version +#Is npm installed? +hash npm > /dev/null 2>&1 || { + echo "Please install npm ( https://npmjs.org )" >&2 + exit 1 +} + +#Check npm version NPM_VERSION_STRING=$(npm --version) require_minimal_version "npm" "$NPM_VERSION_STRING" "$REQUIRED_NPM_MAJOR" "$REQUIRED_NPM_MINOR" -# Check node version +#Check node version NODE_VERSION_STRING=$(node --version) NODE_VERSION_STRING=${NODE_VERSION_STRING#"v"} require_minimal_version "nodejs" "$NODE_VERSION_STRING" "$REQUIRED_NODE_MAJOR" "$REQUIRED_NODE_MINOR" -# Get the name of the settings file +#Get the name of the settings file settings="settings.json" a=''; for arg in "$@"; do @@ -73,13 +88,13 @@ for arg in "$@"; do a=$arg done -# Does a $settings exist? if not copy the template -if [ ! -f "$settings" ]; then - log "Copy the settings template to $settings..." - cp settings.json.template "$settings" || exit 1 +#Does a $settings exist? if not copy the template +if [ ! -f $settings ]; then + echo "Copy the settings template to $settings..." + cp settings.json.template $settings || exit 1 fi -log "Ensure that all dependencies are up to date... If this is the first time you have run Etherpad please be patient." +echo "Ensure that all dependencies are up to date... If this is the first time you have run Etherpad please be patient." ( mkdir -p node_modules cd node_modules @@ -91,8 +106,8 @@ log "Ensure that all dependencies are up to date... If this is the first time y exit 1 } -# Remove all minified data to force node creating it new -log "Clearing minified cache..." +#Remove all minified data to force node creating it new +echo "Clearing minified cache..." rm -f var/minified* exit 0 diff --git a/bin/run.sh b/bin/run.sh index ff6b3de09..74fa56d68 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -1,37 +1,39 @@ #!/bin/sh -pecho() { printf %s\\n "$*"; } -log() { pecho "$@"; } -error() { log "ERROR: $@" >&2; } -fatal() { error "$@"; exit 1; } +#Move to the folder where ep-lite is installed +cd $(dirname $0) -# Move to the folder where ep-lite is installed -cd "$(dirname "$0")"/.. +#Was this script started in the bin folder? if yes move out +if [ -d "../bin" ]; then + cd "../" +fi ignoreRoot=0 -for ARG in "$@"; do +for ARG in "$@" +do if [ "$ARG" = "--root" ]; then ignoreRoot=1 fi done -# Stop the script if it's started as root -if [ "$(id -u)" -eq 0 ] && [ "$ignoreRoot" -eq 0 ]; then - cat <&2 -You shouldn't start Etherpad as root! -Please type 'Etherpad rocks my socks' (or restart with the '--root' -argument) if you still want to start it as root: -EOF - printf "> " >&2 +#Stop the script if it's started as root +if [ "$(id -u)" -eq 0 ] && [ $ignoreRoot -eq 0 ]; then + echo "You shouldn't start Etherpad as root!" + echo "Please type 'Etherpad rocks my socks' or supply the '--root' argument if you still want to start it as root" read rocks - [ "$rocks" = "Etherpad rocks my socks" ] || fatal "Your input was incorrect" + if [ ! "$rocks" == "Etherpad rocks my socks" ] + then + echo "Your input was incorrect" + exit 1 + fi fi -# Prepare the environment +#Prepare the environment bin/installDeps.sh "$@" || exit 1 -# Move to the node folder and start -log "Starting Etherpad..." +#Move to the node folder and start +echo "Started Etherpad..." SCRIPTPATH=$(pwd -P) exec node "$SCRIPTPATH/node_modules/ep_etherpad-lite/node/server.js" "$@" + diff --git a/bin/safeRun.sh b/bin/safeRun.sh index 6d43e3035..99a72bcc0 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -1,11 +1,10 @@ #!/bin/sh -# This script ensures that ep-lite is automatically restarting after -# an error happens +#This script ensures that ep-lite is automatically restarting after an error happens -# Handling Errors -# 0 silent -# 1 email +#Handling Errors +# 0 silent +# 1 email ERROR_HANDLING=0 # Your email address which should receive the error messages EMAIL_ADDRESS="no-reply@example.com" @@ -16,54 +15,54 @@ TIME_BETWEEN_EMAILS=600 # 10 minutes # DON'T EDIT AFTER THIS LINE -pecho() { printf %s\\n "$*"; } -log() { pecho "$@"; } -error() { log "ERROR: $@" >&2; } -fatal() { error "$@"; exit 1; } - LAST_EMAIL_SEND=0 - -# Move to the folder where ep-lite is installed -cd "$(dirname "$0")"/.. - -# Check if a logfile parameter is set LOG="$1" -[ -n "${LOG}" ] || fatal "Set a logfile as the first parameter" + +#Move to the folder where ep-lite is installed +cd $(dirname $0) + +#Was this script started in the bin folder? if yes move out +if [ -d "../bin" ]; then + cd "../" +fi + +#Check if a logfile parameter is set +if [ -z "${LOG}" ]; then + echo "Set a logfile as the first parameter" + exit 1 +fi + shift +while [ 1 ] +do + #Try to touch the file if it doesn't exist + if [ ! -f ${LOG} ]; then + touch ${LOG} || ( echo "Logfile '${LOG}' is not writeable" && exit 1 ) + fi -while true; do - # Try to touch the file if it doesn't exist - [ -f "${LOG}" ] || touch "${LOG}" || fatal "Logfile '${LOG}' is not writeable" + #Check if the file is writeable + if [ ! -w ${LOG} ]; then + echo "Logfile '${LOG}' is not writeable" + exit 1 + fi - # Check if the file is writeable - [ -w "${LOG}" ] || fatal "Logfile '${LOG}' is not writeable" + #Start the application + bin/run.sh $@ >>${LOG} 2>>${LOG} - # Start the application - bin/run.sh "$@" >>${LOG} 2>>${LOG} - - TIME_FMT=$(date +%Y-%m-%dT%H:%M:%S%z) - - # Send email - if [ "$ERROR_HANDLING" = 1 ]; then + #Send email + if [ $ERROR_HANDLING = 1 ]; then TIME_NOW=$(date +%s) TIME_SINCE_LAST_SEND=$(($TIME_NOW - $LAST_EMAIL_SEND)) - if [ "$TIME_SINCE_LAST_SEND" -gt "$TIME_BETWEEN_EMAILS" ]; then - { - cat <>${LOG} + echo "RESTART!" >>${LOG} - # Sleep 10 seconds before restart + #Sleep 10 seconds before restart sleep 10 done From be1f2152fca1cb3babe9c426586e53c6cbadd4f3 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 19:23:40 -0400 Subject: [PATCH 02/16] bin: Use single equals sign for string comparison Double equals is a non-POSIX bashism. --- bin/run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/run.sh b/bin/run.sh index 74fa56d68..9f7510b5d 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -21,7 +21,7 @@ if [ "$(id -u)" -eq 0 ] && [ $ignoreRoot -eq 0 ]; then echo "You shouldn't start Etherpad as root!" echo "Please type 'Etherpad rocks my socks' or supply the '--root' argument if you still want to start it as root" read rocks - if [ ! "$rocks" == "Etherpad rocks my socks" ] + if [ ! "$rocks" = "Etherpad rocks my socks" ] then echo "Your input was incorrect" exit 1 From 8e8b75be6c488bd40db13fde33d7a984db2785d5 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 17:25:51 -0400 Subject: [PATCH 03/16] bin: Use consistent comment formatting --- bin/buildForWindows.sh | 10 +++++----- bin/debugRun.sh | 10 +++++----- bin/installDeps.sh | 20 ++++++++++---------- bin/run.sh | 11 +++++------ bin/safeRun.sh | 25 +++++++++++++------------ 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/bin/buildForWindows.sh b/bin/buildForWindows.sh index e4ca9a883..157233744 100755 --- a/bin/buildForWindows.sh +++ b/bin/buildForWindows.sh @@ -1,26 +1,26 @@ #!/bin/sh -#Move to the folder where ep-lite is installed +# Move to the folder where ep-lite is installed cd $(dirname $0) -#Was this script started in the bin folder? if yes move out +# Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then cd "../" fi -#Is wget installed? +# Is wget installed? hash wget > /dev/null 2>&1 || { echo "Please install wget" >&2 exit 1 } -#Is zip installed? +# Is zip installed? hash zip > /dev/null 2>&1 || { echo "Please install zip" >&2 exit 1 } -#Is zip installed? +# Is zip installed? hash unzip > /dev/null 2>&1 || { echo "Please install unzip" >&2 exit 1 diff --git a/bin/debugRun.sh b/bin/debugRun.sh index 246d53900..75995e838 100755 --- a/bin/debugRun.sh +++ b/bin/debugRun.sh @@ -1,20 +1,20 @@ #!/bin/sh -#Move to the folder where ep-lite is installed +# Move to the folder where ep-lite is installed cd $(dirname $0) -#Was this script started in the bin folder? if yes move out +# Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then cd "../" fi -#Prepare the environment +# Prepare the environment bin/installDeps.sh || exit 1 echo "If you are new to debugging Node.js with Chrome DevTools, take a look at this page:" echo "https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27" echo "Open 'chrome://inspect' on Chrome to start debugging." -#Use 0.0.0.0 to allow external connections to the debugger -#(ex: running Etherpad on a docker container). Use default port # (9229) +# Use 0.0.0.0 to allow external connections to the debugger +# (ex: running Etherpad on a docker container). Use default port # (9229) node --inspect=0.0.0.0:9229 node_modules/ep_etherpad-lite/node/server.js "$@" diff --git a/bin/installDeps.sh b/bin/installDeps.sh index cac279e11..cbfb6b485 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -48,39 +48,39 @@ require_minimal_version() { fi } -#Move to the folder where ep-lite is installed +# Move to the folder where ep-lite is installed cd $(dirname $0) -#Was this script started in the bin folder? if yes move out +# Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then cd "../" fi -#Is node installed? -#Not checking io.js, default installation creates a symbolic link to node +# Is node installed? +# Not checking io.js, default installation creates a symbolic link to node hash node > /dev/null 2>&1 || { echo "Please install node.js ( https://nodejs.org )" >&2 exit 1 } -#Is npm installed? +# Is npm installed? hash npm > /dev/null 2>&1 || { echo "Please install npm ( https://npmjs.org )" >&2 exit 1 } -#Check npm version +# Check npm version NPM_VERSION_STRING=$(npm --version) require_minimal_version "npm" "$NPM_VERSION_STRING" "$REQUIRED_NPM_MAJOR" "$REQUIRED_NPM_MINOR" -#Check node version +# Check node version NODE_VERSION_STRING=$(node --version) NODE_VERSION_STRING=${NODE_VERSION_STRING#"v"} require_minimal_version "nodejs" "$NODE_VERSION_STRING" "$REQUIRED_NODE_MAJOR" "$REQUIRED_NODE_MINOR" -#Get the name of the settings file +# Get the name of the settings file settings="settings.json" a=''; for arg in "$@"; do @@ -88,7 +88,7 @@ for arg in "$@"; do a=$arg done -#Does a $settings exist? if not copy the template +# Does a $settings exist? if not copy the template if [ ! -f $settings ]; then echo "Copy the settings template to $settings..." cp settings.json.template $settings || exit 1 @@ -106,7 +106,7 @@ echo "Ensure that all dependencies are up to date... If this is the first time exit 1 } -#Remove all minified data to force node creating it new +# Remove all minified data to force node creating it new echo "Clearing minified cache..." rm -f var/minified* diff --git a/bin/run.sh b/bin/run.sh index 9f7510b5d..eafbd72a5 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -1,9 +1,9 @@ #!/bin/sh -#Move to the folder where ep-lite is installed +# Move to the folder where ep-lite is installed cd $(dirname $0) -#Was this script started in the bin folder? if yes move out +# Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then cd "../" fi @@ -16,7 +16,7 @@ do fi done -#Stop the script if it's started as root +# Stop the script if it's started as root if [ "$(id -u)" -eq 0 ] && [ $ignoreRoot -eq 0 ]; then echo "You shouldn't start Etherpad as root!" echo "Please type 'Etherpad rocks my socks' or supply the '--root' argument if you still want to start it as root" @@ -28,12 +28,11 @@ if [ "$(id -u)" -eq 0 ] && [ $ignoreRoot -eq 0 ]; then fi fi -#Prepare the environment +# Prepare the environment bin/installDeps.sh "$@" || exit 1 -#Move to the node folder and start +# Move to the node folder and start echo "Started Etherpad..." SCRIPTPATH=$(pwd -P) exec node "$SCRIPTPATH/node_modules/ep_etherpad-lite/node/server.js" "$@" - diff --git a/bin/safeRun.sh b/bin/safeRun.sh index 99a72bcc0..acf13ffd2 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -1,10 +1,11 @@ #!/bin/sh -#This script ensures that ep-lite is automatically restarting after an error happens +# This script ensures that ep-lite is automatically restarting after +# an error happens -#Handling Errors -# 0 silent -# 1 email +# Handling Errors +# 0 silent +# 1 email ERROR_HANDLING=0 # Your email address which should receive the error messages EMAIL_ADDRESS="no-reply@example.com" @@ -18,15 +19,15 @@ TIME_BETWEEN_EMAILS=600 # 10 minutes LAST_EMAIL_SEND=0 LOG="$1" -#Move to the folder where ep-lite is installed +# Move to the folder where ep-lite is installed cd $(dirname $0) -#Was this script started in the bin folder? if yes move out +# Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then cd "../" fi -#Check if a logfile parameter is set +# Check if a logfile parameter is set if [ -z "${LOG}" ]; then echo "Set a logfile as the first parameter" exit 1 @@ -35,21 +36,21 @@ fi shift while [ 1 ] do - #Try to touch the file if it doesn't exist + # Try to touch the file if it doesn't exist if [ ! -f ${LOG} ]; then touch ${LOG} || ( echo "Logfile '${LOG}' is not writeable" && exit 1 ) fi - #Check if the file is writeable + # Check if the file is writeable if [ ! -w ${LOG} ]; then echo "Logfile '${LOG}' is not writeable" exit 1 fi - #Start the application + # Start the application bin/run.sh $@ >>${LOG} 2>>${LOG} - #Send email + # Send email if [ $ERROR_HANDLING = 1 ]; then TIME_NOW=$(date +%s) TIME_SINCE_LAST_SEND=$(($TIME_NOW - $LAST_EMAIL_SEND)) @@ -63,6 +64,6 @@ do echo "RESTART!" >>${LOG} - #Sleep 10 seconds before restart + # Sleep 10 seconds before restart sleep 10 done From a87a9bb63bba47f468651ba79cb0d321f82339ac Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 17 May 2020 11:17:45 -0400 Subject: [PATCH 04/16] bin: Use `command` to check for commands `command` is more idiomatic than `hash`. (Also, `hash` has side effects.) --- bin/buildForWindows.sh | 8 +++++--- bin/installDeps.sh | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/bin/buildForWindows.sh b/bin/buildForWindows.sh index 157233744..90bf187ae 100755 --- a/bin/buildForWindows.sh +++ b/bin/buildForWindows.sh @@ -1,5 +1,7 @@ #!/bin/sh +is_cmd() { command -v "$@" >/dev/null 2>&1; } + # Move to the folder where ep-lite is installed cd $(dirname $0) @@ -9,19 +11,19 @@ if [ -d "../bin" ]; then fi # Is wget installed? -hash wget > /dev/null 2>&1 || { +is_cmd wget || { echo "Please install wget" >&2 exit 1 } # Is zip installed? -hash zip > /dev/null 2>&1 || { +is_cmd zip || { echo "Please install zip" >&2 exit 1 } # Is zip installed? -hash unzip > /dev/null 2>&1 || { +is_cmd unzip || { echo "Please install unzip" >&2 exit 1 } diff --git a/bin/installDeps.sh b/bin/installDeps.sh index cbfb6b485..30c42df54 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -8,6 +8,8 @@ REQUIRED_NODE_MINOR=13 REQUIRED_NPM_MAJOR=5 REQUIRED_NPM_MINOR=5 +is_cmd() { command -v "$@" >/dev/null 2>&1; } + require_minimal_version() { PROGRAM_LABEL="$1" VERSION_STRING="$2" @@ -58,13 +60,13 @@ fi # Is node installed? # Not checking io.js, default installation creates a symbolic link to node -hash node > /dev/null 2>&1 || { +is_cmd node || { echo "Please install node.js ( https://nodejs.org )" >&2 exit 1 } # Is npm installed? -hash npm > /dev/null 2>&1 || { +is_cmd npm || { echo "Please install npm ( https://npmjs.org )" >&2 exit 1 } From 5462d2109c0693df564a8bc9116bd2c8f7939841 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 17:28:53 -0400 Subject: [PATCH 05/16] bin: Create and use new logging functions These write errors to stderr and avoid unintentional backslash escape processing in their arguments. --- bin/buildForWindows.sh | 39 +++++++++++++++++---------------------- bin/installDeps.sh | 39 ++++++++++++++++----------------------- bin/run.sh | 10 +++++++--- bin/safeRun.sh | 15 +++++++++------ 4 files changed, 49 insertions(+), 54 deletions(-) diff --git a/bin/buildForWindows.sh b/bin/buildForWindows.sh index 90bf187ae..229e4ea2f 100755 --- a/bin/buildForWindows.sh +++ b/bin/buildForWindows.sh @@ -1,5 +1,9 @@ #!/bin/sh +pecho() { printf %s\\n "$*"; } +log() { pecho "$@"; } +error() { log "ERROR: $@" >&2; } +fatal() { error "$@"; exit 1; } is_cmd() { command -v "$@" >/dev/null 2>&1; } # Move to the folder where ep-lite is installed @@ -11,27 +15,18 @@ if [ -d "../bin" ]; then fi # Is wget installed? -is_cmd wget || { - echo "Please install wget" >&2 - exit 1 -} +is_cmd wget || fatal "Please install wget" # Is zip installed? -is_cmd zip || { - echo "Please install zip" >&2 - exit 1 -} +is_cmd zip || fatal "Please install zip" # Is zip installed? -is_cmd unzip || { - echo "Please install unzip" >&2 - exit 1 -} +is_cmd unzip || fatal "Please install unzip" START_FOLDER=$(pwd); TMP_FOLDER=$(mktemp -d) -echo "create a clean environment in $TMP_FOLDER..." +log "create a clean environment in $TMP_FOLDER..." cp -ar . $TMP_FOLDER cd $TMP_FOLDER rm -rf node_modules @@ -41,33 +36,33 @@ rm -f etherpad-lite-win.zip # making the windows package smaller export NODE_ENV=production -echo "do a normal unix install first..." +log "do a normal unix install first..." bin/installDeps.sh || exit 1 -echo "copy the windows settings template..." +log "copy the windows settings template..." cp settings.json.template settings.json -echo "resolve symbolic links..." +log "resolve symbolic links..." cp -rL node_modules node_modules_resolved rm -rf node_modules mv node_modules_resolved node_modules -echo "download windows node..." +log "download windows node..." cd bin wget "https://nodejs.org/dist/latest-erbium/win-x86/node.exe" -O ../node.exe -echo "remove git history to reduce folder size" +log "remove git history to reduce folder size" rm -rf .git/objects -echo "remove windows jsdom-nocontextify/test folder" +log "remove windows jsdom-nocontextify/test folder" rm -rf $TMP_FOLDER/src/node_modules/wd/node_modules/request/node_modules/form-data/node_modules/combined-stream/test rm -rf $TMP_FOLDER/src/node_modules/nodemailer/node_modules/mailcomposer/node_modules/mimelib/node_modules/encoding/node_modules/iconv-lite/encodings/tables -echo "create the zip..." +log "create the zip..." cd $TMP_FOLDER zip -9 -r $START_FOLDER/etherpad-lite-win.zip ./* -echo "clean up..." +log "clean up..." rm -rf $TMP_FOLDER -echo "Finished. You can find the zip in the Etherpad root folder, it's called etherpad-lite-win.zip" +log "Finished. You can find the zip in the Etherpad root folder, it's called etherpad-lite-win.zip" diff --git a/bin/installDeps.sh b/bin/installDeps.sh index 30c42df54..04e8ac13c 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -8,6 +8,10 @@ REQUIRED_NODE_MINOR=13 REQUIRED_NPM_MAJOR=5 REQUIRED_NPM_MINOR=5 +pecho() { printf %s\\n "$*"; } +log() { pecho "$@"; } +error() { log "ERROR: $@" >&2; } +fatal() { error "$@"; exit 1; } is_cmd() { command -v "$@" >/dev/null 2>&1; } require_minimal_version() { @@ -18,35 +22,30 @@ require_minimal_version() { # Flag -s (--only-delimited on GNU cut) ensures no string is returned # when there is no match - DETECTED_MAJOR=$(echo $VERSION_STRING | cut -s -d "." -f 1) - DETECTED_MINOR=$(echo $VERSION_STRING | cut -s -d "." -f 2) + DETECTED_MAJOR=$(pecho $VERSION_STRING | cut -s -d "." -f 1) + DETECTED_MINOR=$(pecho $VERSION_STRING | cut -s -d "." -f 2) if [ -z "$DETECTED_MAJOR" ]; then - printf 'Cannot extract %s major version from version string "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" >&2 - exit 1 + fatal "Cannot extract $PROGRAM_LABEL major version from version string \"$VERSION_STRING\"" fi if [ -z "$DETECTED_MINOR" ]; then - printf 'Cannot extract %s minor version from version string "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" >&2 - exit 1 + fatal "Cannot extract $PROGRAM_LABEL minor version from version string \"$VERSION_STRING\"" fi case "$DETECTED_MAJOR" in ''|*[!0-9]*) - printf '%s major version from "%s" is not a number. Detected: "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" "$DETECTED_MAJOR" >&2 - exit 1 + fatal "$PROGRAM_LABEL major version from \"$VERSION_STRING\" is not a number. Detected: \"$DETECTED_MAJOR\"" ;; esac case "$DETECTED_MINOR" in ''|*[!0-9]*) - printf '%s minor version from "%s" is not a number. Detected: "%s"\n' "$PROGRAM_LABEL" "$VERSION_STRING" "$DETECTED_MINOR" >&2 - exit 1 + fatal "$PROGRAM_LABEL minor version from \"$VERSION_STRING\" is not a number. Detected: \"$DETECTED_MINOR\"" esac if [ "$DETECTED_MAJOR" -lt "$REQUIRED_MAJOR" ] || ([ "$DETECTED_MAJOR" -eq "$REQUIRED_MAJOR" ] && [ "$DETECTED_MINOR" -lt "$REQUIRED_MINOR" ]); then - printf 'Your %s version "%s" is too old. %s %d.%d.x or higher is required.\n' "$PROGRAM_LABEL" "$VERSION_STRING" "$PROGRAM_LABEL" "$REQUIRED_MAJOR" "$REQUIRED_MINOR" >&2 - exit 1 + fatal "Your $PROGRAM_LABEL version \"$VERSION_STRING\" is too old. $PROGRAM_LABEL $REQUIRED_MAJOR.$REQUIRED_MINOR.x or higher is required." fi } @@ -60,16 +59,10 @@ fi # Is node installed? # Not checking io.js, default installation creates a symbolic link to node -is_cmd node || { - echo "Please install node.js ( https://nodejs.org )" >&2 - exit 1 -} +is_cmd node || fatal "Please install node.js ( https://nodejs.org )" # Is npm installed? -is_cmd npm || { - echo "Please install npm ( https://npmjs.org )" >&2 - exit 1 -} +is_cmd npm || fatal "Please install npm ( https://npmjs.org )" # Check npm version NPM_VERSION_STRING=$(npm --version) @@ -92,11 +85,11 @@ done # Does a $settings exist? if not copy the template if [ ! -f $settings ]; then - echo "Copy the settings template to $settings..." + log "Copy the settings template to $settings..." cp settings.json.template $settings || exit 1 fi -echo "Ensure that all dependencies are up to date... If this is the first time you have run Etherpad please be patient." +log "Ensure that all dependencies are up to date... If this is the first time you have run Etherpad please be patient." ( mkdir -p node_modules cd node_modules @@ -109,7 +102,7 @@ echo "Ensure that all dependencies are up to date... If this is the first time } # Remove all minified data to force node creating it new -echo "Clearing minified cache..." +log "Clearing minified cache..." rm -f var/minified* exit 0 diff --git a/bin/run.sh b/bin/run.sh index eafbd72a5..9ae768f1b 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -1,5 +1,10 @@ #!/bin/sh +pecho() { printf %s\\n "$*"; } +log() { pecho "$@"; } +error() { log "ERROR: $@" >&2; } +fatal() { error "$@"; exit 1; } + # Move to the folder where ep-lite is installed cd $(dirname $0) @@ -23,8 +28,7 @@ if [ "$(id -u)" -eq 0 ] && [ $ignoreRoot -eq 0 ]; then read rocks if [ ! "$rocks" = "Etherpad rocks my socks" ] then - echo "Your input was incorrect" - exit 1 + fatal "Your input was incorrect" fi fi @@ -32,7 +36,7 @@ fi bin/installDeps.sh "$@" || exit 1 # Move to the node folder and start -echo "Started Etherpad..." +log "Starting Etherpad..." SCRIPTPATH=$(pwd -P) exec node "$SCRIPTPATH/node_modules/ep_etherpad-lite/node/server.js" "$@" diff --git a/bin/safeRun.sh b/bin/safeRun.sh index acf13ffd2..fca97cb14 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -16,6 +16,11 @@ TIME_BETWEEN_EMAILS=600 # 10 minutes # DON'T EDIT AFTER THIS LINE +pecho() { printf %s\\n "$*"; } +log() { pecho "$@"; } +error() { log "ERROR: $@" >&2; } +fatal() { error "$@"; exit 1; } + LAST_EMAIL_SEND=0 LOG="$1" @@ -29,8 +34,7 @@ fi # Check if a logfile parameter is set if [ -z "${LOG}" ]; then - echo "Set a logfile as the first parameter" - exit 1 + fatal "Set a logfile as the first parameter" fi shift @@ -38,13 +42,12 @@ while [ 1 ] do # Try to touch the file if it doesn't exist if [ ! -f ${LOG} ]; then - touch ${LOG} || ( echo "Logfile '${LOG}' is not writeable" && exit 1 ) + touch ${LOG} || fatal "Logfile '${LOG}' is not writeable" fi # Check if the file is writeable if [ ! -w ${LOG} ]; then - echo "Logfile '${LOG}' is not writeable" - exit 1 + fatal "Logfile '${LOG}' is not writeable" fi # Start the application @@ -62,7 +65,7 @@ do fi fi - echo "RESTART!" >>${LOG} + pecho "RESTART!" >>${LOG} # Sleep 10 seconds before restart sleep 10 From a28b7c75955e40f448953f47dfa199a98b42477b Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 17:36:16 -0400 Subject: [PATCH 06/16] bin: Use assertion-style condition checks --- bin/installDeps.sh | 13 ++++--------- bin/run.sh | 5 +---- bin/safeRun.sh | 12 +++--------- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/bin/installDeps.sh b/bin/installDeps.sh index 04e8ac13c..af8912114 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -25,13 +25,9 @@ require_minimal_version() { DETECTED_MAJOR=$(pecho $VERSION_STRING | cut -s -d "." -f 1) DETECTED_MINOR=$(pecho $VERSION_STRING | cut -s -d "." -f 2) - if [ -z "$DETECTED_MAJOR" ]; then - fatal "Cannot extract $PROGRAM_LABEL major version from version string \"$VERSION_STRING\"" - fi + [ -n "$DETECTED_MAJOR" ] || fatal "Cannot extract $PROGRAM_LABEL major version from version string \"$VERSION_STRING\"" - if [ -z "$DETECTED_MINOR" ]; then - fatal "Cannot extract $PROGRAM_LABEL minor version from version string \"$VERSION_STRING\"" - fi + [ -n "$DETECTED_MINOR" ] || fatal "Cannot extract $PROGRAM_LABEL minor version from version string \"$VERSION_STRING\"" case "$DETECTED_MAJOR" in ''|*[!0-9]*) @@ -44,9 +40,8 @@ require_minimal_version() { fatal "$PROGRAM_LABEL minor version from \"$VERSION_STRING\" is not a number. Detected: \"$DETECTED_MINOR\"" esac - if [ "$DETECTED_MAJOR" -lt "$REQUIRED_MAJOR" ] || ([ "$DETECTED_MAJOR" -eq "$REQUIRED_MAJOR" ] && [ "$DETECTED_MINOR" -lt "$REQUIRED_MINOR" ]); then - fatal "Your $PROGRAM_LABEL version \"$VERSION_STRING\" is too old. $PROGRAM_LABEL $REQUIRED_MAJOR.$REQUIRED_MINOR.x or higher is required." - fi + [ "$DETECTED_MAJOR" -gt "$REQUIRED_MAJOR" ] || ([ "$DETECTED_MAJOR" -eq "$REQUIRED_MAJOR" ] && [ "$DETECTED_MINOR" -ge "$REQUIRED_MINOR" ]) \ + || fatal "Your $PROGRAM_LABEL version \"$VERSION_STRING\" is too old. $PROGRAM_LABEL $REQUIRED_MAJOR.$REQUIRED_MINOR.x or higher is required." } # Move to the folder where ep-lite is installed diff --git a/bin/run.sh b/bin/run.sh index 9ae768f1b..cab223fb4 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -26,10 +26,7 @@ if [ "$(id -u)" -eq 0 ] && [ $ignoreRoot -eq 0 ]; then echo "You shouldn't start Etherpad as root!" echo "Please type 'Etherpad rocks my socks' or supply the '--root' argument if you still want to start it as root" read rocks - if [ ! "$rocks" = "Etherpad rocks my socks" ] - then - fatal "Your input was incorrect" - fi + [ "$rocks" = "Etherpad rocks my socks" ] || fatal "Your input was incorrect" fi # Prepare the environment diff --git a/bin/safeRun.sh b/bin/safeRun.sh index fca97cb14..149f95d22 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -33,22 +33,16 @@ if [ -d "../bin" ]; then fi # Check if a logfile parameter is set -if [ -z "${LOG}" ]; then - fatal "Set a logfile as the first parameter" -fi +[ -n "${LOG}" ] || fatal "Set a logfile as the first parameter" shift while [ 1 ] do # Try to touch the file if it doesn't exist - if [ ! -f ${LOG} ]; then - touch ${LOG} || fatal "Logfile '${LOG}' is not writeable" - fi + [ -f ${LOG} ] || touch ${LOG} || fatal "Logfile '${LOG}' is not writeable" # Check if the file is writeable - if [ ! -w ${LOG} ]; then - fatal "Logfile '${LOG}' is not writeable" - fi + [ -w ${LOG} ] || fatal "Logfile '${LOG}' is not writeable" # Start the application bin/run.sh $@ >>${LOG} 2>>${LOG} From 57237b856874ef58aea41cd05b9c201a90e0e7fc Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 17:40:16 -0400 Subject: [PATCH 07/16] bin: Quote expansions that are subject to field splitting --- bin/buildForWindows.sh | 16 ++++++++-------- bin/debugRun.sh | 2 +- bin/installDeps.sh | 10 +++++----- bin/run.sh | 4 ++-- bin/safeRun.sh | 14 +++++++------- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/bin/buildForWindows.sh b/bin/buildForWindows.sh index 229e4ea2f..7636fecea 100755 --- a/bin/buildForWindows.sh +++ b/bin/buildForWindows.sh @@ -7,7 +7,7 @@ fatal() { error "$@"; exit 1; } is_cmd() { command -v "$@" >/dev/null 2>&1; } # Move to the folder where ep-lite is installed -cd $(dirname $0) +cd "$(dirname "$0")" # Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then @@ -27,8 +27,8 @@ START_FOLDER=$(pwd); TMP_FOLDER=$(mktemp -d) log "create a clean environment in $TMP_FOLDER..." -cp -ar . $TMP_FOLDER -cd $TMP_FOLDER +cp -ar . "$TMP_FOLDER" +cd "$TMP_FOLDER" rm -rf node_modules rm -f etherpad-lite-win.zip @@ -55,14 +55,14 @@ log "remove git history to reduce folder size" rm -rf .git/objects log "remove windows jsdom-nocontextify/test folder" -rm -rf $TMP_FOLDER/src/node_modules/wd/node_modules/request/node_modules/form-data/node_modules/combined-stream/test -rm -rf $TMP_FOLDER/src/node_modules/nodemailer/node_modules/mailcomposer/node_modules/mimelib/node_modules/encoding/node_modules/iconv-lite/encodings/tables +rm -rf "$TMP_FOLDER"/src/node_modules/wd/node_modules/request/node_modules/form-data/node_modules/combined-stream/test +rm -rf "$TMP_FOLDER"/src/node_modules/nodemailer/node_modules/mailcomposer/node_modules/mimelib/node_modules/encoding/node_modules/iconv-lite/encodings/tables log "create the zip..." -cd $TMP_FOLDER -zip -9 -r $START_FOLDER/etherpad-lite-win.zip ./* +cd "$TMP_FOLDER" +zip -9 -r "$START_FOLDER"/etherpad-lite-win.zip ./* log "clean up..." -rm -rf $TMP_FOLDER +rm -rf "$TMP_FOLDER" log "Finished. You can find the zip in the Etherpad root folder, it's called etherpad-lite-win.zip" diff --git a/bin/debugRun.sh b/bin/debugRun.sh index 75995e838..abb27c40b 100755 --- a/bin/debugRun.sh +++ b/bin/debugRun.sh @@ -1,7 +1,7 @@ #!/bin/sh # Move to the folder where ep-lite is installed -cd $(dirname $0) +cd "$(dirname "$0")" # Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then diff --git a/bin/installDeps.sh b/bin/installDeps.sh index af8912114..b7105531e 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -22,8 +22,8 @@ require_minimal_version() { # Flag -s (--only-delimited on GNU cut) ensures no string is returned # when there is no match - DETECTED_MAJOR=$(pecho $VERSION_STRING | cut -s -d "." -f 1) - DETECTED_MINOR=$(pecho $VERSION_STRING | cut -s -d "." -f 2) + DETECTED_MAJOR=$(pecho "$VERSION_STRING" | cut -s -d "." -f 1) + DETECTED_MINOR=$(pecho "$VERSION_STRING" | cut -s -d "." -f 2) [ -n "$DETECTED_MAJOR" ] || fatal "Cannot extract $PROGRAM_LABEL major version from version string \"$VERSION_STRING\"" @@ -45,7 +45,7 @@ require_minimal_version() { } # Move to the folder where ep-lite is installed -cd $(dirname $0) +cd "$(dirname "$0")" # Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then @@ -79,9 +79,9 @@ for arg in "$@"; do done # Does a $settings exist? if not copy the template -if [ ! -f $settings ]; then +if [ ! -f "$settings" ]; then log "Copy the settings template to $settings..." - cp settings.json.template $settings || exit 1 + cp settings.json.template "$settings" || exit 1 fi log "Ensure that all dependencies are up to date... If this is the first time you have run Etherpad please be patient." diff --git a/bin/run.sh b/bin/run.sh index cab223fb4..e2fe8cb23 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -6,7 +6,7 @@ error() { log "ERROR: $@" >&2; } fatal() { error "$@"; exit 1; } # Move to the folder where ep-lite is installed -cd $(dirname $0) +cd "$(dirname "$0")" # Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then @@ -22,7 +22,7 @@ do done # Stop the script if it's started as root -if [ "$(id -u)" -eq 0 ] && [ $ignoreRoot -eq 0 ]; then +if [ "$(id -u)" -eq 0 ] && [ "$ignoreRoot" -eq 0 ]; then echo "You shouldn't start Etherpad as root!" echo "Please type 'Etherpad rocks my socks' or supply the '--root' argument if you still want to start it as root" read rocks diff --git a/bin/safeRun.sh b/bin/safeRun.sh index 149f95d22..2eba74d4c 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -25,7 +25,7 @@ LAST_EMAIL_SEND=0 LOG="$1" # Move to the folder where ep-lite is installed -cd $(dirname $0) +cd "$(dirname "$0")" # Was this script started in the bin folder? if yes move out if [ -d "../bin" ]; then @@ -39,21 +39,21 @@ shift while [ 1 ] do # Try to touch the file if it doesn't exist - [ -f ${LOG} ] || touch ${LOG} || fatal "Logfile '${LOG}' is not writeable" + [ -f "${LOG}" ] || touch "${LOG}" || fatal "Logfile '${LOG}' is not writeable" # Check if the file is writeable - [ -w ${LOG} ] || fatal "Logfile '${LOG}' is not writeable" + [ -w "${LOG}" ] || fatal "Logfile '${LOG}' is not writeable" # Start the application - bin/run.sh $@ >>${LOG} 2>>${LOG} + bin/run.sh "$@" >>${LOG} 2>>${LOG} # Send email - if [ $ERROR_HANDLING = 1 ]; then + if [ "$ERROR_HANDLING" = 1 ]; then TIME_NOW=$(date +%s) TIME_SINCE_LAST_SEND=$(($TIME_NOW - $LAST_EMAIL_SEND)) - if [ $TIME_SINCE_LAST_SEND -gt $TIME_BETWEEN_EMAILS ]; then - printf "Server was restarted at: $(date)\nThe last 50 lines of the log before the error happens:\n $(tail -n 50 ${LOG})" | mail -s "Pad Server was restarted" $EMAIL_ADDRESS + if [ "$TIME_SINCE_LAST_SEND" -gt "$TIME_BETWEEN_EMAILS" ]; then + printf "Server was restarted at: $(date)\nThe last 50 lines of the log before the error happens:\n $(tail -n 50 "${LOG}")" | mail -s "Pad Server was restarted" "$EMAIL_ADDRESS" LAST_EMAIL_SEND=$TIME_NOW fi From 2f76066d95e080aa2d3d37e390f919e1758bf174 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 17:43:04 -0400 Subject: [PATCH 08/16] bin: Simplify while loop condition --- bin/safeRun.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/safeRun.sh b/bin/safeRun.sh index 2eba74d4c..d9ea8d98c 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -36,8 +36,7 @@ fi [ -n "${LOG}" ] || fatal "Set a logfile as the first parameter" shift -while [ 1 ] -do +while true; do # Try to touch the file if it doesn't exist [ -f "${LOG}" ] || touch "${LOG}" || fatal "Logfile '${LOG}' is not writeable" From 335705e03d4339d149ba5e02c5f6c3838b130efd Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 17:43:45 -0400 Subject: [PATCH 09/16] bin: Simplify cd to install dir --- bin/buildForWindows.sh | 7 +------ bin/debugRun.sh | 7 +------ bin/installDeps.sh | 7 +------ bin/run.sh | 7 +------ bin/safeRun.sh | 7 +------ 5 files changed, 5 insertions(+), 30 deletions(-) diff --git a/bin/buildForWindows.sh b/bin/buildForWindows.sh index 7636fecea..818522ad0 100755 --- a/bin/buildForWindows.sh +++ b/bin/buildForWindows.sh @@ -7,12 +7,7 @@ fatal() { error "$@"; exit 1; } is_cmd() { command -v "$@" >/dev/null 2>&1; } # Move to the folder where ep-lite is installed -cd "$(dirname "$0")" - -# Was this script started in the bin folder? if yes move out -if [ -d "../bin" ]; then - cd "../" -fi +cd "$(dirname "$0")"/.. # Is wget installed? is_cmd wget || fatal "Please install wget" diff --git a/bin/debugRun.sh b/bin/debugRun.sh index abb27c40b..d9b18aaa2 100755 --- a/bin/debugRun.sh +++ b/bin/debugRun.sh @@ -1,12 +1,7 @@ #!/bin/sh # Move to the folder where ep-lite is installed -cd "$(dirname "$0")" - -# Was this script started in the bin folder? if yes move out -if [ -d "../bin" ]; then - cd "../" -fi +cd "$(dirname "$0")"/.. # Prepare the environment bin/installDeps.sh || exit 1 diff --git a/bin/installDeps.sh b/bin/installDeps.sh index b7105531e..5e0bbb931 100755 --- a/bin/installDeps.sh +++ b/bin/installDeps.sh @@ -45,12 +45,7 @@ require_minimal_version() { } # Move to the folder where ep-lite is installed -cd "$(dirname "$0")" - -# Was this script started in the bin folder? if yes move out -if [ -d "../bin" ]; then - cd "../" -fi +cd "$(dirname "$0")"/.. # Is node installed? # Not checking io.js, default installation creates a symbolic link to node diff --git a/bin/run.sh b/bin/run.sh index e2fe8cb23..23bc3b905 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -6,12 +6,7 @@ error() { log "ERROR: $@" >&2; } fatal() { error "$@"; exit 1; } # Move to the folder where ep-lite is installed -cd "$(dirname "$0")" - -# Was this script started in the bin folder? if yes move out -if [ -d "../bin" ]; then - cd "../" -fi +cd "$(dirname "$0")"/.. ignoreRoot=0 for ARG in "$@" diff --git a/bin/safeRun.sh b/bin/safeRun.sh index d9ea8d98c..038d9f5ea 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -25,12 +25,7 @@ LAST_EMAIL_SEND=0 LOG="$1" # Move to the folder where ep-lite is installed -cd "$(dirname "$0")" - -# Was this script started in the bin folder? if yes move out -if [ -d "../bin" ]; then - cd "../" -fi +cd "$(dirname "$0")"/.. # Check if a logfile parameter is set [ -n "${LOG}" ] || fatal "Set a logfile as the first parameter" From ab408ce653d984a581e743addc9b0dec8f5ec247 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 17:48:11 -0400 Subject: [PATCH 10/16] bin: Put log parameter handling logic together --- bin/safeRun.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/safeRun.sh b/bin/safeRun.sh index 038d9f5ea..918f13cc2 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -22,15 +22,15 @@ error() { log "ERROR: $@" >&2; } fatal() { error "$@"; exit 1; } LAST_EMAIL_SEND=0 -LOG="$1" # Move to the folder where ep-lite is installed cd "$(dirname "$0")"/.. # Check if a logfile parameter is set +LOG="$1" [ -n "${LOG}" ] || fatal "Set a logfile as the first parameter" - shift + while true; do # Try to touch the file if it doesn't exist [ -f "${LOG}" ] || touch "${LOG}" || fatal "Logfile '${LOG}' is not writeable" From e4fec3883b8f3bf78b49ae08386a9db6b1b304a1 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 18:08:28 -0400 Subject: [PATCH 11/16] bin: Improve readability of email body --- bin/safeRun.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bin/safeRun.sh b/bin/safeRun.sh index 918f13cc2..b242199b1 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -47,7 +47,14 @@ while true; do TIME_SINCE_LAST_SEND=$(($TIME_NOW - $LAST_EMAIL_SEND)) if [ "$TIME_SINCE_LAST_SEND" -gt "$TIME_BETWEEN_EMAILS" ]; then - printf "Server was restarted at: $(date)\nThe last 50 lines of the log before the error happens:\n $(tail -n 50 "${LOG}")" | mail -s "Pad Server was restarted" "$EMAIL_ADDRESS" + { + cat < Date: Thu, 14 May 2020 18:14:25 -0400 Subject: [PATCH 12/16] bin: Log the date in the restart message --- bin/safeRun.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/safeRun.sh b/bin/safeRun.sh index b242199b1..7bb307f8f 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -41,6 +41,8 @@ while true; do # Start the application bin/run.sh "$@" >>${LOG} 2>>${LOG} + TIME_FMT=$(date) + # Send email if [ "$ERROR_HANDLING" = 1 ]; then TIME_NOW=$(date +%s) @@ -49,7 +51,7 @@ while true; do if [ "$TIME_SINCE_LAST_SEND" -gt "$TIME_BETWEEN_EMAILS" ]; then { cat <>${LOG} + pecho "RESTART! ${TIME_FMT}" >>${LOG} # Sleep 10 seconds before restart sleep 10 From edfe59e84f3e791d1240dd485a0ce7763f83e4e8 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 18:15:33 -0400 Subject: [PATCH 13/16] bin: Improve restart notification email --- bin/safeRun.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/safeRun.sh b/bin/safeRun.sh index 7bb307f8f..2488e463b 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -52,11 +52,11 @@ while true; do { cat < Date: Thu, 14 May 2020 18:16:16 -0400 Subject: [PATCH 14/16] bin: Use ISO 8601 date format --- bin/safeRun.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/safeRun.sh b/bin/safeRun.sh index 2488e463b..6d43e3035 100755 --- a/bin/safeRun.sh +++ b/bin/safeRun.sh @@ -41,7 +41,7 @@ while true; do # Start the application bin/run.sh "$@" >>${LOG} 2>>${LOG} - TIME_FMT=$(date) + TIME_FMT=$(date +%Y-%m-%dT%H:%M:%S%z) # Send email if [ "$ERROR_HANDLING" = 1 ]; then From 4f0b1fa7eceb7b2a61c45e655882b4e987462361 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 19:21:11 -0400 Subject: [PATCH 15/16] bin: Improve the run-as-root confirmation prompt * Send the prompt to stderr * Print "> " without a newline so the user knows it is a prompt * Wrap long lines * Use a here-document --- bin/run.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/bin/run.sh b/bin/run.sh index 23bc3b905..c7d2d35e5 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -18,8 +18,12 @@ done # Stop the script if it's started as root if [ "$(id -u)" -eq 0 ] && [ "$ignoreRoot" -eq 0 ]; then - echo "You shouldn't start Etherpad as root!" - echo "Please type 'Etherpad rocks my socks' or supply the '--root' argument if you still want to start it as root" + cat <&2 +You shouldn't start Etherpad as root! +Please type 'Etherpad rocks my socks' (or restart with the '--root' +argument) if you still want to start it as root: +EOF + printf "> " >&2 read rocks [ "$rocks" = "Etherpad rocks my socks" ] || fatal "Your input was incorrect" fi From 17a1b96736240448a63489c4ac34f70080f48e42 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Thu, 14 May 2020 19:28:53 -0400 Subject: [PATCH 16/16] bin: Fix 'for' loop style --- bin/run.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/run.sh b/bin/run.sh index c7d2d35e5..ff6b3de09 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -9,8 +9,7 @@ fatal() { error "$@"; exit 1; } cd "$(dirname "$0")"/.. ignoreRoot=0 -for ARG in "$@" -do +for ARG in "$@"; do if [ "$ARG" = "--root" ]; then ignoreRoot=1 fi