From c80a4a9c17c7f8959ec5da49c2c439d7ae789c9e Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 4 Jul 2025 13:26:54 +0900 Subject: [PATCH] Mark error messages better, update new clone script --- src/bin/base_setup.sh | 12 ++++++------ src/bin/git_pull.sh | 2 +- src/bin/new_clone.sh | 26 ++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/src/bin/base_setup.sh b/src/bin/base_setup.sh index 4a8f222..12774f0 100755 --- a/src/bin/base_setup.sh +++ b/src/bin/base_setup.sh @@ -16,26 +16,26 @@ fi; error=0; if [ -z "${GIT_WEBHOOK_BASE_FOLDER}" ]; then - echo "Missing GIT_WEBHOOK_BASE_FOLDER entry"; + echo "[!] Missing GIT_WEBHOOK_BASE_FOLDER entry"; error=1; fi; if [ -z "${WWW_GROUP}" ]; then - echo "Missing WWW_GROUP entry"; + echo "[!] Missing WWW_GROUP entry"; error=1; elif ! getent group "${WWW_GROUP}" > /dev/null 2>&1; then - echo "Group ${WWW_GROUP} does not exist. Is it the Apache web group?"; + echo "[!] Group ${WWW_GROUP} does not exist. Is it the Apache web group?"; error=1; fi; if [ -z "${SUDO_USER}" ]; then - echo "Missing SUDO_USER entry"; + echo "[!] Missing SUDO_USER entry"; error=1; elif [ "${USE_SUDO}" = "0" ] && ! id "${SUDO_USER}" &>/dev/null; then - echo "SUDO is off, user must exist in system"; + echo "[!] SUDO is off, user must exist in system"; error=1; fi; # this script has to be run as root if [ "$(whoami)" != "root" ]; then - echo "Script must be run as root user"; + echo "[!] Script must be run as root user"; error=1; fi; diff --git a/src/bin/git_pull.sh b/src/bin/git_pull.sh index dd43a66..bd52898 100755 --- a/src/bin/git_pull.sh +++ b/src/bin/git_pull.sh @@ -17,7 +17,7 @@ BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/"; # check that repository path exists REPOSITORY_PATH="${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}${REPOSITORY}"; if [ ! -d "${REPOSITORY_PATH}$" ]; then - echo "${REPOSITORY} not found in clone folder"; + echo "[!] ${REPOSITORY} not found in clone folder"; exit; fi; LOG_FILE="${GIT_WEBHOOK_BASE_FOLDER}${LOG_FOLDER}${REPOSITORY}.log"; diff --git a/src/bin/new_clone.sh b/src/bin/new_clone.sh index 9253072..7958a1b 100755 --- a/src/bin/new_clone.sh +++ b/src/bin/new_clone.sh @@ -16,16 +16,34 @@ BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/"; . "${BASE_FOLDER}init.sh"; if [ -z "${REMOTE_HOST}" ]; then - echo "Must set a remote host for the repository"; + echo "[!] Must set a remote host for the repository"; exit; fi; +error=0 echo "* Validate SSH PEM Key exist and SSH config"; -# grep "Host ${REMOTE}" -# grep "IdentityFile" in this +if ! grep "Host ${REMOTE}" "${GIT_WEBHOOK_BASE_FOLDER}"/.ssh/config; then + echo "ssh config entry for Host ${REMOTE} is missing"; + error=1; +else + # make sure the identiy file is there + # grep "IdentityFile" in this + result=$(ssh "${REMOTE_HOST}"); + validate_string="You've successfully authenticated with the key" + if [[ "$result" != *"$validate_string"* ]]; then + echo "Could not connect to ${REMOTE_HOST}: ${result}"; + error=1; + fi; +fi; -echo "New clone"; +if [ $error -eq 1 ]; then + exit; +fi; +# from the repository get the last path without the .git so we have the target folder +GIT_REPOSITORY_FOLDER=$(basename "${REPOSITORY}" .git); + +echo "* New clone from ${REMOTE_HOST}:${REPOSITORY}/${BRANCH} into ${GIT_REPOSITORY_FOLDER}"; GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "clone" "-b" "${BRANCH}" "--single-branch" "--depth" "1" "--origin" "${REMOTE}" "${REMOTE_HOST}:${REPOSITORY}" "${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}") echo "Command: ${GIT_COMMAND[*]}" # echo ${GIT_COMMAND_BASE} clone -b "${BRANCH}" --single-branch --depth 1 --origin "${REMOTE}" "${REMOTE_HOST}:${REPOSITORY}" "${GIT_WEBHOOK_BASE_FOLDER}${CLONE_BASE}";