Move script bin folder and config to src

This commit is contained in:
2025-07-04 10:46:22 +09:00
parent 22d18ae4dd
commit 2d5d728a7a
10 changed files with 123 additions and 63 deletions

2
src/bin/.shellcheckrc Normal file
View File

@@ -0,0 +1,2 @@
shell=bash
external-sources=true

107
src/bin/base_setup.sh Normal file
View File

@@ -0,0 +1,107 @@
#!/usr/bin/env bash
# AUTHOR: Clemens Schwaighofer
# DATE: 2025/7/4
# DESC: Initial setup of the webhook clone folder structure
BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/";
CONFIG_BASE="${BASE_FOLDER}../config/";
if [ -f "${CONFIG_BASE}webhook.cfg" ]; then
# shellcheck source=../config/webhook.cfg"
# shellcheck disable=SC1091
source <(grep "=" "${CONFIG_BASE}webhook.cfg" | sed 's/ *= */=/g')
fi;
# abort on not set
error=0;
if [ -z "${GIT_REPOSITORY_FOLDER}" ]; then
echo "Missing GIT_REPOSITORY_FOLDER entry";
error=1;
fi;
if [ -z "${WWW_GROUP}" ]; then
echo "Missing WWW_GROUP entry";
error=1;
else
# check that this group exists, we do not create this, this is the apache group
echo "";
fi;
if [ -z "${SUDO_USER}" ]; then
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";
error=1;
fi;
if [ $error -eq 1 ]; then
exit;
fi;
# Define base folders
CLONE_BASE="clone-base/"
LOG_FOLDER="log/"
SCRIPT_FOLDER="scripts/"
CONFIG_FOLDER="config/"
WWW_WEBHOOK_INCOMING="/www/webhook-incoming";
WWW_ADMIN="/www/admin";
# add trailing slash if missing
GIT_REPOSITORY_FOLDER="${GIT_REPOSITORY_FOLDER%/}/"
if [ -d "${GIT_REPOSITORY_FOLDER}" ]; then
echo "Base folder already exists, update check";
echo "[TODO] -> Not implemented exit";
exit;
else
echo "=> Create new folder structure";
# User for sudo, but only if SUDO is enabled
if [ "${USE_SUDO}" != "0" ]; then
echo "+ Add user ${WWW_GROUP}:${SUDO_USER} with base folder ${GIT_REPOSITORY_FOLDER}";
useradd -d "${GIT_REPOSITORY_FOLDER}" -m -s /usr/sbin/nologin "${SUDO_USER}"
fi;
if [ -d "${GIT_REPOSITORY_FOLDER}" ]; then
echo "+ Create Folder: ${GIT_REPOSITORY_FOLDER}";
mkdir "${GIT_REPOSITORY_FOLDER}";
fi;
echo "+ Set folder user/group";
# user is not mandatory, but we need to set the group
setfacl -m u:"${SUDO_USER}":rwx -R "${GIT_REPOSITORY_FOLDER}"
setfacl -m -d u:"${SUDO_USER}":rwx -R "${GIT_REPOSITORY_FOLDER}"
setfacl -m g:"${WWW_GROUP}":rx -R "${GIT_REPOSITORY_FOLDER}"
# SSH
echo "+ Add .ssh folder"
sudo -u "${SUDO_USER}" mkdir "${GIT_REPOSITORY_FOLDER}"/.ssh/
sudo -u "${SUDO_USER}" touch "${GIT_REPOSITORY_FOLDER}"/.ssh/config
sudo -u "${SUDO_USER}" chmod 700 "${GIT_REPOSITORY_FOLDER}"/.ssh/
sudo -u "${SUDO_USER}" chmod 600 "${GIT_REPOSITORY_FOLDER}"/.ssh/config
# All other FOLDER
echo "+ Other folders for clone base: ${CLONE_BASE}, logs, scripts, www/webhook-incoming"
sudo -u "${SUDO_USER}" \
mkdir -p \
"${GIT_REPOSITORY_FOLDER}${CLONE_BASE}" \
"${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}" \
"${GIT_REPOSITORY_FOLDER}${SCRIPT_FOLDER}" \
"${GIT_REPOSITORY_FOLDER}${CONFIG_FOLDER}" \
"${GIT_REPOSITORY_FOLDER}${WWW_WEBHOOK_INCOMING}" \
"${GIT_REPOSITORY_FOLDER}${WWW_ADMIN}";
# set basic folder rights, clone folder is excluded
chmod 700 \
"${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}" \
"${GIT_REPOSITORY_FOLDER}${SCRIPT_FOLDER}" \
"${GIT_REPOSITORY_FOLDER}${CONFIG_FOLDER}" \
"${GIT_REPOSITORY_FOLDER}${WWW_WEBHOOK_INCOMING}" \
"${GIT_REPOSITORY_FOLDER}${WWW_ADMIN}";
# setfacl -m u:"${SUDO_USER}":rwx -R "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}"
# setfacl -d -m u:"${SUDO_USER}":rwx -R "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}"
# web user must have access to the clone folder, RWX
setfacl -m g:"${WWW_GROUP}":rwx -R "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}"
setfacl -d -m g:"${WWW_GROUP}":rwx -R "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}"
# Copy files
echo "+ Copy basic script and config files";
# git_pull.sh, init.sh, new_clone.sh, webhook.default.cfg
cp "${BASE_FOLDER}new_clone.sh" "${BASE_FOLDER}init.sh" "${BASE_FOLDER}git_clone.sh" "${GIT_REPOSITORY_FOLDER}${SCRIPT_FOLDER}";
cp "${CONFIG_BASE}/webhook.default.cfg" "${GIT_REPOSITORY_FOLDER}${CONFIG_FOLDER}";
fi;
# __END__

40
src/bin/git_pull.sh Executable file
View File

@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# AUTHOR: Clemens Schwaighofer
# DATE: 2025/6/27
# DESC: fetch and merge from a remote repositry
REPOSITORY="$1";
BRANCH="$2";
REMOTE="$3";
if [ -n "${REMOTE}" ]; then
REMOTE="origin"
fi;
BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/";
# shellcheck source=init.sh
. "${BASE_FOLDER}init.sh";
# check that repository path exists
REPOSITORY_PATH="${GIT_REPOSITORY_FOLDER}${CLONE_BASE}${REPOSITORY}";
if [ ! -d "${REPOSITORY_PATH}$" ]; then
echo "${REPOSITORY} not found in clone folder";
exit;
fi;
LOG_FILE="${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}${REPOSITORY}.log";
# fetch to null
# ${GIT_COMMAND} -C "${REPOSITORY_PATH}" fetch -q "${REMOTE}" "${BRANCH}";
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${REPOSITORY_PATH}" "fetch" "-q" "${REMOTE}" "${BRANCH}")
"${GIT_COMMAND[@]}"
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${REPOSITORY_PATH}" "diff" "--stat" "HEAD" "${REMOTE}/${BRANCH}")
# changes=$(${GIT_COMMAND_BASE} -C "${REPOSITORY_PATH}" diff --stat HEAD "${REMOTE}"/"${BRANCH}");
changes=$("${GIT_COMMAND[@]}")
if [ -n "${changes}" ]; then
echo "[$(date +"%Y-%m-%d %H:%M:%S")] Changes" &>> "$LOG_FILE";
# ${GIT_COMMAND_BASE} -C "/${REPOSITORY_PATH}" merge "${REMOTE}"/"${BRANCH}"
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "-C" "${REPOSITORY_PATH}" merge "${REMOTE}/${BRANCH}")
"${GIT_COMMAND[@]}" &>> "$LOG_FILE";
echo "=[END]===>" &>> "$LOG_FILE";
fi;
# __END__

58
src/bin/init.sh Normal file
View File

@@ -0,0 +1,58 @@
#!/usr/bin/env bash
# AUTHOR: Clemens Schwaighofer
# DATE: 2025/6/27
# DESC: Setup basic variables
CONFIG_BASE="${BASE_FOLDER}../config/";
if [ -f "${CONFIG_BASE}webhook.cfg" ]; then
# shellcheck source=../config/webhook.cfg"
# shellcheck disable=SC1091
source <(grep "=" "${CONFIG_BASE}webhook.cfg" | sed 's/ *= */=/g')
fi;
if [ "${USE_SUDO}" != "0" ] && ! id "${SUDO_USER}" &>/dev/null; then
echo "sudo user ${SUDO_USER} does not exist";
exit;
fi;
# check that user exist
# check that git exists
if [ -z "$(command -v git)" ]; then
echo "git is not installed";
exit;
fi;
GIT_COMMAND_BASE=("git");
if [ -n "${USE_SUDO}" ]; then
GIT_COMMAND_BASE=("sudo" "-u" "${SUDO_USER}" "git");.
fi;
# add trailing slash if not set
GIT_REPOSITORY_FOLDER="${GIT_REPOSITORY_FOLDER%/}/"
CLONE_BASE="clone-base/"
LOG_FOLDER="log/"
# base folder does not exist
if [ ! -d "${GIT_REPOSITORY_FOLDER}" ]; then
echo "Base folder: ${GIT_REPOSITORY_FOLDER} not found";
exit;
fi;
# branch name not set
if [ -n "${BRANCH}" ]; then
echo "No branch name given";
exit;
fi;
# check that log folder exists
if [ ! -d "${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}" ]; then
echo "Log folder does not exist: ${GIT_REPOSITORY_FOLDER}${LOG_FOLDER}";
exit;
fi;
# check that the base clone folder exists
if [ ! -d "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}" ]; then
echo "Clone base folder does not exist: ${GIT_REPOSITORY_FOLDER}${CLONE_BASE}";
exit;
fi;
export GIT_COMMAND_BASE;

33
src/bin/new_clone.sh Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bash
# AUTHOR: Clemens Schwaighofer
# DATE: 2025/6/27
# DESC: create a new basic clone
REPOSITORY="$1";
BRANCH="$2";
REMOTE_HOST="$3";
REMOTE="$4";
if [ -n "${REMOTE}" ]; then
REMOTE="origin"
fi;
BASE_FOLDER=$(dirname "$(readlink -f "$0")")"/";
# shellcheck source=init.sh
. "${BASE_FOLDER}init.sh";
if [ -z "${REMOTE_HOST}" ]; then
echo "Must set a remote host for the repository";
exit;
fi;
echo "* Validate SSH PEM Key exist and SSH config";
# grep "Host ${REMOTE}"
# grep "IdentityFile" in this
echo "New clone";
GIT_COMMAND=("${GIT_COMMAND_BASE[@]}" "clone" "-b" "${BRANCH}" "--single-branch" "--depth" "1" "--origin" "${REMOTE}" "${REMOTE_HOST}:${REPOSITORY}" "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}")
echo "Command: ${GIT_COMMAND[*]}"
# echo ${GIT_COMMAND_BASE} clone -b "${BRANCH}" --single-branch --depth 1 --origin "${REMOTE}" "${REMOTE_HOST}:${REPOSITORY}" "${GIT_REPOSITORY_FOLDER}${CLONE_BASE}";
# __END__

3
src/config/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
*.cfg
!*.default.cfg
!.gitignore

View File

@@ -0,0 +1,4 @@
GIT_REPOSITORY_FOLDER=""
WWW_GROUP=""
SUDO_USER=""
USE_SUDO=0