From 73e5e18c06e61e96b48c852b463bbf884fc86f7f Mon Sep 17 00:00:00 2001 From: Evert Ramos Date: Thu, 8 Jul 2021 09:24:37 -0300 Subject: [PATCH] Add sudo option on functions with default to 'true' --- common/common-create-folder.sh | 10 +++-- env/env-create-if-not-exists.sh | 6 ++- env/env-update-variable.sh | 13 ++----- file/file-update-file.sh | 14 ++++--- git/git-clone-repo.sh | 17 ++++++-- system/run-function.sh | 8 +++- system/system-check-user-folder-owner.sh | 49 ++++++++++++++++++++++++ 7 files changed, 91 insertions(+), 26 deletions(-) create mode 100755 system/system-check-user-folder-owner.sh diff --git a/common/common-create-folder.sh b/common/common-create-folder.sh index bdda3b0..4b3328d 100755 --- a/common/common-create-folder.sh +++ b/common/common-create-folder.sh @@ -31,20 +31,22 @@ common_create_folder() { - local LOCAL_FOLDER LOCAL_STOP_EXECUTION_ON_ERROR + local LOCAL_FOLDER LOCAL_STOP_EXECUTION_ON_ERROR LOCAL_ALLOW_RUN_WITH_SUDO LOCAL_RUN_WITH_SUDO LOCAL_FOLDER=${1} LOCAL_STOP_EXECUTION_ON_ERROR=${2:-true} + LOCAL_ALLOW_RUN_WITH_SUDO=${ALLOW_RUN_WITH_SUDO:-true} [[ $LOCAL_FOLDER == "" ]] && echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'" [[ "$DEBUG" == true ]] && echo "Creating folder '$LOCAL_FOLDER'" - mkdir -p $LOCAL_FOLDER > /dev/null 2>&1 + # Allows 'sudo' to run this function if destination path it's not owned by the current user + [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == true ]] && ! system_check_user_folder_owner ${LOCAL_FOLDER%/*} && LOCAL_RUN_WITH_SUDO=sudo - [[ ! -d "$LOCAL_FOLDER" ]] && sudo mkdir -p $LOCAL_FOLDER > /dev/null 2>&1 + $LOCAL_RUN_WITH_SUDO mkdir -p $LOCAL_FOLDER > /dev/null 2>&1 - [[ ! -d "$LOCAL_FOLDER" ]] && [[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == true ]] && echoerror "The folder '$LOCAL_FOLDER' could not be created." + [[ ! -d "$LOCAL_FOLDER" ]] && [[ "$LOCAL_STOP_EXECUTION_ON_ERROR" == true ]] && echoerror "The folder '$LOCAL_FOLDER' could not be created. \nCheck folder permissions." [[ ! -d "$LOCAL_FOLDER" ]] && COMMON_CREATE_FOLDER_ERROR = true diff --git a/env/env-create-if-not-exists.sh b/env/env-create-if-not-exists.sh index 82c4bd0..eea2cf8 100644 --- a/env/env-create-if-not-exists.sh +++ b/env/env-create-if-not-exists.sh @@ -42,9 +42,11 @@ env_create_if_not_exists() [[ $LOCAL_FULL_PATH == "" || $LOCAL_FULL_PATH == null ]] && echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'" - [[ -f "$LOCAL_FULL_PATH" ]] && return 0 + [[ "$DEBUG" == true ]] && echo "[env_create_if_not_exists] Checking if file '$LOCAL_ENV_FINAL_FILE' already exists." + [[ -f "$LOCAL_ENV_FINAL_FILE" ]] && return 0 - [[ "$DEBUG" == true ]] && echo "Creating file '$LOCAL_FULL_PATH'" + [[ "$DEBUG" == true ]] && echo "Creating file '$LOCAL_ENV_FINAL_FILE' from '$LOCAL_ENV_EXAMPLE_FILE'." cp $LOCAL_ENV_EXAMPLE_FILE $LOCAL_ENV_FINAL_FILE + [[ ! -f "$LOCAL_ENV_FINAL_FILE" ]] && sudo cp $LOCAL_ENV_EXAMPLE_FILE $LOCAL_ENV_FINAL_FILE } diff --git a/env/env-update-variable.sh b/env/env-update-variable.sh index 2df95a2..be10b48 100755 --- a/env/env-update-variable.sh +++ b/env/env-update-variable.sh @@ -45,16 +45,9 @@ env_update_variable() [[ $LOCAL_NEW_VALUE == "" || $LOCAL_NEW_VALUE == null ]] && echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}' \nReplace string: $LOCAL_VARIABLE" - [[ "$DEBUG" == true ]] && echo "Updating in '$LOCAL_ENV_FINAL_FILE' the variable '$LOCAL_VARIABLE' with value '$LOCAL_NEW_VALUE'" - + [[ "$DEBUG" == true ]] && echo "[env_update_variable] Create '$LOCAL_ENV_FINAL_FILE' at '$LOCAL_FULL_PATH' if it does not exists." [[ "$LOCAL_CREATE_IF_NOT_EXIST" == true ]] && env_create_if_not_exists $LOCAL_FULL_PATH - if [[ ! -f "$LOCAL_ENV_FINAL_FILE" ]]; then - REPONSE_ENV_UPDATE_VARIABLE="File '$LOCAL_ENV_FINAL_FILE' does not exist." - return 0 - fi - - sed -i '/'"$LOCAL_VARIABLE"'/c\'"$LOCAL_VARIABLE=$LOCAL_NEW_VALUE"'' $LOCAL_ENV_FINAL_FILE - - return 0 + [[ "$DEBUG" == true ]] && echo "[env_update_variable] Updating in '$LOCAL_ENV_FINAL_FILE' the variable '$LOCAL_VARIABLE' with value '$LOCAL_NEW_VALUE'" + file_update_file $LOCAL_ENV_FINAL_FILE $LOCAL_VARIABLE $LOCAL_NEW_VALUE true true } diff --git a/file/file-update-file.sh b/file/file-update-file.sh index 4fffefa..22fadbe 100755 --- a/file/file-update-file.sh +++ b/file/file-update-file.sh @@ -38,13 +38,14 @@ file_update_file() { - local LOCAL_FULL_FILE_PATH LOCAL_FROM_STRING LOCAL_TO_STRING LOCAL_STOP_EXECUTION_ON_ERROR LOCAL_UPDATE_VARIABLE_VALUE + local LOCAL_FULL_FILE_PATH LOCAL_FROM_STRING LOCAL_TO_STRING LOCAL_STOP_EXECUTION_ON_ERROR LOCAL_UPDATE_VARIABLE_VALUE LOCAL_ALLOW_RUN_WITH_SUDO LOCAL_RUN_WITH_SUDO LOCAL_FULL_FILE_PATH=${1:-null} LOCAL_FROM_STRING=${2:-null} LOCAL_TO_STRING=${3:-null} LOCAL_STOP_EXECUTION_ON_ERROR=${4:-false} LOCAL_UPDATE_VARIABLE_VALUE=${5:-false} + LOCAL_ALLOW_RUN_WITH_SUDO=${ALLOW_RUN_WITH_SUDO:-true} # Check required [[ $LOCAL_TO_STRING == "" || $LOCAL_TO_STRING == null ]] && echoerror "You must inform the required argument(s) to the function: '${FUNCNAME[0]}'" @@ -54,14 +55,17 @@ file_update_file() # Debug message [[ "$DEBUG" == true ]] && echo "Replace '$LOCAL_FROM_STRING' to '$LOCAL_TO_STRING' in '$LOCAL_FULL_FILE_PATH'." - + + # Allows 'sudo' to run this function if destination path it's not owned by the current user + [[ "$LOCAL_ALLOW_RUN_WITH_SUDO" == true ]] && ! system_check_user_folder_owner ${LOCAL_FULL_FILE_PATH%/*} && LOCAL_RUN_WITH_SUDO=sudo + cd $(dirname $LOCAL_FULL_FILE_PATH) >/dev/null 2>&1 if [[ "$LOCAL_UPDATE_VARIABLE_VALUE" == true ]]; then # To remove dots (.) use the line below -# sed -i "/$LOCAL_FROM_STRING=/c\\$LOCAL_FROM_STRING=${LOCAL_TO_STRING//.}" $LOCAL_FULL_FILE_PATH - sed -i "/$LOCAL_FROM_STRING=/c\\$LOCAL_FROM_STRING=$LOCAL_TO_STRING" $LOCAL_FULL_FILE_PATH +# $LOCAL_RUN_WITH_SUDO sed -i "/$LOCAL_FROM_STRING=/c\\$LOCAL_FROM_STRING=${LOCAL_TO_STRING//.}" $LOCAL_FULL_FILE_PATH + $LOCAL_RUN_WITH_SUDO sed -i "/$LOCAL_FROM_STRING=/c\\$LOCAL_FROM_STRING=$LOCAL_TO_STRING" $LOCAL_FULL_FILE_PATH else - sed -i -e "s/$LOCAL_FROM_STRING/$LOCAL_TO_STRING/g" $LOCAL_FULL_FILE_PATH + $LOCAL_RUN_WITH_SUDO sed -i -e "s/$LOCAL_FROM_STRING/$LOCAL_TO_STRING/g" $LOCAL_FULL_FILE_PATH fi cd - > /dev/null 2>&1 } diff --git a/git/git-clone-repo.sh b/git/git-clone-repo.sh index 28f48bb..293fbae 100644 --- a/git/git-clone-repo.sh +++ b/git/git-clone-repo.sh @@ -53,8 +53,7 @@ git_clone_repo() # Prepare the FULL_PATH with LOCAL_GIT_FOLDER_NAME [[ $LOCAL_GIT_FOLDER_NAME != "" ]] && LOCAL_FULL_PATH=${LOCAL_FULL_PATH%/}"/"$LOCAL_GIT_FOLDER_NAME - [[ "$DEBUG" == true ]] && echo "Cloning repo '$LOCAL_GIT_REPO' branch '$LOCAL_REPO_BRANCH' to '$LOCAL_FULL_PATH'." - + [[ "$DEBUG" == true ]] && echo "[git_clone_repo] Testing if repo '$LOCAL_GIT_REPO' on branch '$LOCAL_REPO_BRANCH' is accesible." LOCAL_GIT_REPO_ONLINE=$(git ls-remote --exit-code --quiet ${LOCAL_GIT_REPO} --tags ${LOCAL_REPO_BRANCH}) LOCAL_GIT_REPO_ONLINE=$? if [[ $LOCAL_GIT_REPO_ONLINE != 0 ]]; then @@ -62,12 +61,24 @@ git_clone_repo() return 0 fi - mkdir -p $LOCAL_FULL_PATH + [[ "$DEBUG" == true ]] && echo "[git_clone_repo] Creating folder '$LOCAL_FULL_PATH'." + mkdir -p $LOCAL_FULL_PATH > /dev/null 2>&1 + [[ ! -d "$LOCAL_FULL_PATH" ]] && sudo mkdir -p $LOCAL_FULL_PATH > /dev/null 2>&1 + [[ ! -d "$LOCAL_FULL_PATH" ]] && RESPONSE_GIT_CLONE_REPO="Error creating folder '$LOCAL_FULL_PATH'" && return 0 + + [[ "$DEBUG" == true ]] && echo "[git_clone_repo] Cloning repo '$LOCAL_GIT_REPO' branch '$LOCAL_REPO_BRANCH' to '$LOCAL_FULL_PATH'." if [[ "$LOCAL_FULL_REPO" == true ]]; then git clone --branch $LOCAL_REPO_BRANCH $LOCAL_GIT_REPO $LOCAL_FULL_PATH > /dev/null 2>&1 else git clone --depth 1 --branch $LOCAL_REPO_BRANCH $LOCAL_GIT_REPO $LOCAL_FULL_PATH > /dev/null 2>&1 fi + if [[ ! -f "${LOCAL_FULL_PATH%/}/.env" ]]; then + if [[ "$LOCAL_FULL_REPO" == true ]]; then + sudo git clone --branch $LOCAL_REPO_BRANCH $LOCAL_GIT_REPO $LOCAL_FULL_PATH > /dev/null 2>&1 + else + sudo git clone --depth 1 --branch $LOCAL_REPO_BRANCH $LOCAL_GIT_REPO $LOCAL_FULL_PATH > /dev/null 2>&1 + fi + fi return 0 } diff --git a/system/run-function.sh b/system/run-function.sh index 9f191a3..24c78bb 100755 --- a/system/run-function.sh +++ b/system/run-function.sh @@ -41,7 +41,9 @@ run_function() # Check $SILENT mode if [[ "$SILENT" == true ]]; then - if [[ ! -z $5 ]]; then + if [[ ! -z $6 ]]; then + $1 "$2" "$3" "$4" "$5" "$6" + elif [[ ! -z $5 ]]; then $1 "$2" "$3" "$4" "$5" elif [[ ! -z $4 ]]; then $1 "$2" "$3" "$4" @@ -55,7 +57,9 @@ run_function() else # Call the specified function if [[ -n "$(type -t "$1")" ]] && [[ "$(type -t "$1")" = function ]]; then - if [[ ! -z $5 ]]; then + if [[ ! -z $6 ]]; then + $1 "$2" "$3" "$4" "$5" "$6" + elif [[ ! -z $5 ]]; then $1 "$2" "$3" "$4" "$5" elif [[ ! -z $4 ]]; then $1 "$2" "$3" "$4" diff --git a/system/system-check-user-folder-owner.sh b/system/system-check-user-folder-owner.sh new file mode 100755 index 0000000..beedb6b --- /dev/null +++ b/system/system-check-user-folder-owner.sh @@ -0,0 +1,49 @@ +#----------------------------------------------------------------------- +# +# Basescript function +# +# The basescript functions were designed to work as abstract function, +# so it could be used in many different contexts executing specific job +# always remembering Unix concept DOTADIW - "Do One Thing And Do It Well" +# +# Developed by +# Evert Ramos +# +# Copyright Evert Ramos +# +#----------------------------------------------------------------------- +# +# Be careful when editing this file, it is part of a bigger script! +# +# Basescript - https://github.com/evertramos/basescript +# +#----------------------------------------------------------------------- + +#----------------------------------------------------------------------- +# This function has one main objective: +# 1. Check if user has permission on specific folder +# +# You must/might inform the parameters below: +# 1. Full path of the folder you are checking +# +#----------------------------------------------------------------------- + +system_check_user_folder_owner() +{ + local LOCAL_PATH + + LOCAL_PATH=${1:-null} + + [[ $LOCAL_PATH == "" || $LOCAL_PATH == null ]] && echoerror "You must inform the folder path to the function: '${FUNCNAME[0]}'" + + [[ "$DEBUG" == true ]] && echo "Checking permissions in path '$LOCAL_PATH'." + + if [[ "$(stat -c '%u' ${LOCAL_PATH})" == "$UID" ]]; then + SYSTEM_CHECK_USER_FOLDER_OWNER=true + return 0 + else + SYSTEM_CHECK_USER_FOLDER_OWNER=false + return 1 + fi +} +