Skip to content

Commit

Permalink
Update logging funciton to use sudo only if allowed (#16)
Browse files Browse the repository at this point in the history
* Update header with basic functions

* Update logging validation, give sudo and exit on error options

Co-authored-by: Evert Ramos <[email protected]>
  • Loading branch information
evertramos and evertramos authored Feb 8, 2022
1 parent 574ec07 commit 7df5cb3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
33 changes: 27 additions & 6 deletions header
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,31 @@
# This function has one main objective:
# 1.
#
# You must/might inform the parameters below:
# You must/might inform the argument(s) below:
# 1.
# 2. [optional] (default: )
# 99. [optional] (default: true) Stop execution on error
# d1. [optional] (default: true) Stop execution on error
# d2. [optional] (default: true) Allow run with 'sudo'
#
# [CAUTION] The optional argument(s) replaces previously parameter as of:
# d1. STOP_EXECUTION_ON_ERROR
# d2. ALLOW_RUN_WITH_SUDO
#
#-----------------------------------------------------------------------

function_name()
{
local LOCAL_ LOCAL_STOP_EXECUTION_ON_ERROR

# Local variables
local LOCAL_ LOCAL_STOP_EXECUTION_ON_ERROR LOCAL_ALLOW_RUN_WITH_SUDO LOCAL_RUN_WITH_SUDO

# Required arguments
LOCAL_=${1:-null}
LOCAL_STOP_EXECUTION_ON_ERROR=${99:-true}

# Optional arguments
LOCAL_STOP_EXECUTION_ON_ERROR="${d1:-null}" && [[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == null ]] && LOCAL_STOP_EXECUTION_ON_ERROR=${STOP_EXECUTION_ON_ERROR:-true}
# LOCAL_ALLOW_RUN_WITH_SUDO="${d1:-null}" && [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == null ]] && LOCAL_ALLOW_RUN_WITH_SUDO=${ALLOW_RUN_WITH_SUDO:-true}

# Validate required parameters
if [[ $LOCAL_ == "" || $LOCAL_ == null ]]; then
if [[ ! $(declare -F echoerror) == "" ]]; then
echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'" ${LOCAL_STOP_EXECUTION_ON_ERROR}
Expand All @@ -46,13 +57,23 @@ function_name()
fi
fi

# Show debug message
if [[ "$DEBUG" == true ]]; then
if [[ ! $(declare -F echowarning) == "" ]]; then
echowarning "You are running..... xyz - [function: ${FUNCNAME[0]}]"
else
echo "You are running..... xyz - [function: ${FUNCNAME[0]}]"
[[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == true ]] && exit 1
fi
fi


# [EXAMPLE] Run a function with 'sudo'
# Allows 'sudo' to run in this function with a condition
# [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == true ]] && LOCAL_RUN_WITH_SUDO=sudo
# $LOCAL_RUN_WITH_SUDO mkdir -p
# [CONDITION] You may add a condition to check if it requires to run a command with 'sudo'
# [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == true ]] && ! system_check_user_folder_owner ${LOCAL_FOLDER%/*} && LOCAL_RUN_WITH_SUDO=sudo
# $LOCAL_RUN_WITH_SUDO mkdir -p $LOCAL_FOLDER > /dev/null 2>&1

}

57 changes: 50 additions & 7 deletions log/log.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,65 @@
# 1. Log activities
#
# You must/might inform the parameters below:
# 1.
# 2. [optional] (default: false)
# 1. [optional] (default: /var/log) Destination path
# 2. [optional] (default: basescript.log) Log's file name
# 3. [optional] (default: +%Y-%m-%d %H:%M:%S) Timestamp log
# 4. [optional] (default: true) Allow to run with sudo
# 5. [optional] (default: false) Stop execution on error
#
# [CAUTION] The optional argument replaces previously set parameter as of:
# 1. BASESCRIPT_LOG_BASE_PATH
# 2. BASESCRIPT_LOG_FILE_NAME
# 3. BASESCRIPT_LOG_TIMESTAMP_FLAG
# 4. ALLOW_RUN_WITH_SUDO
# 5. LOCAL_STOP_EXECUTION_ON_ERROR
#
#-----------------------------------------------------------------------

log()
{
local LOCAL_LOG_BASE_PATH LOCAL_LOG_NAME LOCAL_TIMESTAMP_FLAG LOCAL_TIMESTAMP LOCAL_LOG_FULL_PATH
# Local arguments
local LOCAL_LOG_BASE_PATH LOCAL_LOG_NAME LOCAL_TIMESTAMP_FLAG LOCAL_TIMESTAMP LOCAL_LOG_FULL_PATH LOCAL_STOP_EXECUTION_ON_ERROR LOCAL_ALLOW_RUN_WITH_SUDO LOCAL_RUN_WITH_SUDO

LOCAL_LOG_BASE_PATH=${BASESCRIPT_LOG_BASE_PATH:-"/var/log"}
LOCAL_LOG_NAME=${BASESCRIPT_LOG_FILE_NAME:-basescript.log}
LOCAL_TIMESTAMP_FLAG=${BASESCRIPT_LOG_TIMESTAMP_FLAG:-"+%Y-%m-%d %H:%M:%S"}
# Optional arguments
LOCAL_LOG_BASE_PATH="${1:-null}" && [[ "$LOCAL_LOG_BASE_PATH" == "null" ]] && LOCAL_LOG_BASE_PATH=${BASESCRIPT_LOG_BASE_PATH:-"/var/log"}
LOCAL_LOG_NAME="${2:-null}" && [[ "$LOCAL_LOG_NAME" == null ]] && LOCAL_LOG_NAME=${BASESCRIPT_LOG_FILE_NAME:-basescript.log}
LOCAL_TIMESTAMP_FLAG="${3:-null}" && [[ "$LOCAL_TIMESTAMP_FLAG" == null ]] && LOCAL_TIMESTAMP_FLAG=${BASESCRIPT_LOG_TIMESTAMP_FLAG:-"+%Y-%m-%d %H:%M:%S"}
LOCAL_ALLOW_RUN_WITH_SUDO="${4:-null}" && [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == null ]] && LOCAL_ALLOW_RUN_WITH_SUDO=${ALLOW_RUN_WITH_SUDO:-false}
LOCAL_STOP_EXECUTION_ON_ERROR="${5:-null}" && [[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == null ]] && LOCAL_STOP_EXECUTION_ON_ERROR=${STOP_EXECUTION_ON_ERROR:-false} # most other scripts this is set to true

# Extra parameters
LOCAL_TIMESTAMP=$(date "$LOCAL_TIMESTAMP_FLAG")
LOCAL_LOG_FULL_PATH="${LOCAL_LOG_BASE_PATH%/}/$LOCAL_LOG_NAME"

echo "$LOCAL_TIMESTAMP USER=$USER SCRIPT=$SCRIPT_PATH/$SCRIPT_NAME LOG='"$@"'" | sudo tee -a $LOCAL_LOG_FULL_PATH >/dev/null
# Allows 'sudo' to run this function if destination path it's not owned by the current user
if [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == true ]]; then
LOCAL_RUN_WITH_SUDO=sudo
else
LOCAL_RUN_WITH_SUDO="" # Set to empty!

# Check if file exist and users permissions
if [[ -f ${LOCAL_LOG_FULL_PATH} && ! -w ${LOCAL_LOG_FULL_PATH} ]] || [[ -d ${LOCAL_LOG_BASE_PATH} && ! -w ${LOCAL_LOG_BASE_PATH} ]]; then
if [[ ! $(declare -F echoerror) == "" ]]; then
echoerror "You dont have permission to write log at '${LOCAL_LOG_FULL_PATH}' - [${FUNCNAME[0]}]" "${LOCAL_STOP_EXECUTION_ON_ERROR}"
else
echo "You dont have permission to write log at '${LOCAL_LOG_FULL_PATH}' - [${FUNCNAME[0]}]"
[[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == true ]] && exit 1
fi
fi
fi

# Executa comando ou retornar erro
if ! echo "$LOCAL_TIMESTAMP USER=$USER SCRIPT=$SCRIPT_PATH/$SCRIPT_NAME LOG=$*" | $LOCAL_RUN_WITH_SUDO tee -a "${LOCAL_LOG_FULL_PATH}" >/dev/null 2>&1; then
if [[ ! $(declare -F echoerror) == "" ]]; then
echoerror "Logs could not be saved at '${LOCAL_LOG_FULL_PATH}' - [${FUNCNAME[0]}]" "${LOCAL_STOP_EXECUTION_ON_ERROR}"
else
echo "Logs could not be saved at '${LOCAL_LOG_FULL_PATH}' - [${FUNCNAME[0]}]"
[[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == true ]] && exit 1
fi
fi
}

# Implement test case
#BASESCRIPT_LOG_BASE_PATH="/log"
#log $*

0 comments on commit 7df5cb3

Please sign in to comment.