Files

263 lines
7.5 KiB
Bash
Executable File

#!/bin/bash
# bash deamon to check git for new content
# make a pull from repository
# - with ssh keys ( problem with private key in this repository )
# - with https user login ( problrm with TLS error with own gogs
# - with auth token (not implemented in gogs yet)
# ssh://gogs@gogs.serinty.net:22104/Serinty/serinty-website.git
#GITCLONEURL=https://${GITUSER}:${GITPASSWD}@${GITSERVER}:${GITSERVERPORT}/${GITPROJECTUSER}/${GITPROJECTNAME}.git
# shellcheck source=./getOrCreateSshKeys.sh
source getOrCreateSshKeys.sh
# Set default values for variables
# one day
IDLE_TIME=${IDLE_TIME:-86400}
# port over 1024 for normal user
SWSPORT=${SWSPORT:=8970}
# projectname of the git repository with the data and conf for zola
GITPROJECTNAME=${GITPROJECTNAME:-serinty-website}
# projectname of the git repository with the theme for zola
GITTHEMENAME=${GITTHEMENAME:-serinty-zola-theme}
# the name of the theme, which is configed in the zola project, can be diffrent to the GITTHEMENAME
THEME_NAME=${THEME_NAME:-$GITTHEMENAME}
# User or Organisation name of the repository
GITPROJECTUSER=${GITPROJECTUSER:-Serinty}
# name or ip of the git server
GITSERVER=${GITSERVER:-gogs.serinty.net}
# ssh port of the git server
GITSERVERPORT=${GITSERVERPORT:-22104}
# ssh git user
GITUSER=${GITUSER:-gogs}
# password of the https login, not used because https isn't used
GITPASSWD=${GITPASSWD:-gogspw}
# url to clone zola data and config
GITCLONEURL=${GITCLONEURL:-"ssh://$GITUSER@$GITSERVER:$GITSERVERPORT/$GITPROJECTUSER/$GITPROJECTNAME.git"}
# url to clone zola data and config
GITTHEMECLONEURL=${GITTHEMECLONEURL:-"ssh://$GITUSER@$GITSERVER:$GITSERVERPORT/$GITPROJECTUSER/$GITTHEMENAME.git"}
BASE_DOMAIN=${BASE_DOMAIN:-"docker.local"}
BASE_PROTOCOL=${BASE_PROTOCOL:-"http"}
BASE_URL=${BASE_URL:-"${BASE_PROTOCOL}://${BASE_DOMAIN}:${SWSPORT}"}
# Zola output build and sws path
#OUTPUT_DIR=${OUTPUT_DIR:-$HOME/$GITPROJECTUSER/$GITPROJECTNAME/public}
OUTPUT_DIR=${OUTPUT_DIR:-$HOME/$GITPROJECTNAME/public}
GITBRANCHNAME=${GITBRANCHNAME:-"master"}
# set trap to exit this process
# at this time only exit this process if it get a exit code
trap "exit" EXIT
startSWSifNotRunning () {
#static-web-server is /usr/local/bin/static-web-server and in path
if pidof "static-web-server" >/dev/null
then
echo "webserver is running"
else
echo "try to start webserver:"
if [ -f "$OUTPUT_DIR/index.html" ]; then
echo "starting webserver on port $SWSPORT with content:"
ls -la "$OUTPUT_DIR" && static-web-server --port "$SWSPORT" --log-level warn --root "$OUTPUT_DIR" &
else
echo "ERROR: index.html not found in $OUTPUT_DIR"
exit 2
fi
fi
}
isNewContent() {
# Fehlercode 2 bei Fehlern, 1 wenn keine neuen Inhalte, 0 wenn neue Inhalte vorhanden
# Überprüfe, ob es sich um ein Git-Repository handelt
if [ ! -d .git ]; then
return 2
fi
# Ermittle aktuellen Branch
current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ $? -ne 0 ]; then
return 2
fi
# Hole Remote-Änderungen
git fetch origin --quiet 2>/dev/null
if [ $? -ne 0 ]; then
return 2
fi
# Prüfe auf neue Commits
new_commits=$(git rev-list --count HEAD..origin/"$current_branch" 2>/dev/null)
if [ $? -ne 0 ]; then
return 2
fi
# Ergebnisinterpretation
if [ "$new_commits" -gt 0 ]; then
return 0 # Neue Inhalte vorhanden
else
return 1 # Keine neuen Inhalte
fi
}
# pull new data an build site
buildSite () {
goProjectPath
echo "build website with newer data to ${OUTPUT_DIR}"
zola build --force --base-url "${BASE_URL}" --output-dir "${OUTPUT_DIR}" || errorAndExit "can't build website" 2
}
goProjectPath () {
echo "change into project directory ${HOME}/${GITPROJECTNAME}"
cd "${HOME}/${GITPROJECTNAME}" || errorAndExit "error on changing into project path" 5
}
# write error text into err console and exit
# param1: error text
# param2: exit code
errorAndExit () {
error_text=$1
# if error text is empty, set default text
if [ -z "$error_text" ]
then
error_text="unknown error"
fi
error_code=$2
#if error code is empty, set default code
if [ -z "$error_code" ]
then
error_code=1
fi
echo "ERROR: $error_text"
exit "$error_code"
}
# clone or pull the theme
getTheme () {
goProjectPath
if [ ! -d ./themes ]
then
echo "make themes directory"
mkdir themes || errorAndExit "can't make themes directory" 1
fi
echo "change into themes directory"
cd ./themes || errorAndExit "can't change into themes directory" 2
if [ ! -d ./"${THEME_NAME}" ]
then
echo "clone theme with ${GITTHEMECLONEURL} ${THEME_NAME}"
git clone -b "${GITBRANCHNAME}" "${GITTHEMECLONEURL}" "${THEME_NAME}" || errorAndExit "can't clone theme" 3
if [ -s ./"$THEME_NAME" ]
then
echo "change into themes directory $THEME_NAME"
cd "$THEME_NAME"
echo "checking for makefile"
if [ -f makefile ]
then
echo "integrate external libraries"
make || errorAndExit "error interate external libraries" 6
fi
fi
buildSite
echo "change into themes directory"
cd ./themes || errorAndExit "can't change into themes directory" 2
fi
if [ -d ./"${THEME_NAME}" ]
then
echo "change into theme directory ${THEME_NAME}"
cd ./"${THEME_NAME}" || errorAndExit "can't change into theme directory" 2
echo "check if there are changes in the theme repository"
git fetch || errorAndExit "can't fetch theme repository" 4
# with --quiet diff though only error codes
#if git diff --quiet FETCH_HEAD;
if isNewContent;
then
echo "getting updated design"
git pull origin "${GITBRANCHNAME}" --allow-unrelated-histories --ff-only || errorAndExit "can't pull theme repository" 4
if [ -f makefile ]
then
echo "integrate external libraries"
make || errorAndExit "error interate external libraries" 6
fi
buildSite
elif [ $? -eq 1 ]; then
echo "no changes detected in theme"
goProjectPath
else
echo "error checking repository"
fi
else
errorAndExit "no theme ${THEME_NAME} found" 5
fi
}
getContent () {
goProjectPath
# check if there are changes in the content repository
echo "check if there are changes in the content repository"
#git fetch || errorAndExit "can't fetch content repository" 4
#if git diff --quiet FETCH_HEAD;
if isNewContent;
then
echo "getting updated content"
git pull origin "${GITBRANCHNAME}" --allow-unrelated-histories --ff-only || errorAndExit "can't pull content repository" 1
buildSite
elif [ $? -eq 1 ]; then
echo "no changes detected in the content repository"
if [ ! -d public ]
then
echo "no public found, start buiding site"
getTheme
buildSite
fi
else
errorAndExit "can't fetch content repository" 4
fi
}
echo "Starting $_"
getOrCreateSshKeys
echo "please instert public ssh key into git repository to access ${GITCLONEURL} and restart this container:"
cat ~/.ssh/id_ed25519.pub
ssh-keygen -lf ~/.ssh/id_ed25519.pub
echo "Using git command: $GIT_SSH_COMMAND"
echo "Working directory:"
pwd || echo "Error: pwd failed"
echo "put git server ${GITSERVER} to known hosts"
ssh-keyscan -p "${GITSERVERPORT}" -H "${GITSERVER}" >> ~/.ssh/known_hosts
zola --version
echo "checking if local repository directory exists"
ls -la
if [ ! -d "${GITPROJECTNAME}" ]; then
echo "no local repository directory found, cloning ${GITPROJECTNAME} from ${GITCLONEURL}"
git clone -b "${GITBRANCHNAME}" "${GITCLONEURL}" || errorAndExit "error cloning project" 9
goProjectPath
getTheme
else
goProjectPath
getContent
getTheme
fi
while true
do
getContent
getTheme
startSWSifNotRunning
echo "Sleeping for ${IDLE_TIME} seconds"
sleep "${IDLE_TIME}"
done