Use this for files, scripts, aliases, etc to deploy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.3 KiB

#!/bin/sh
## keeps the ansible playbooks updated from gitea
## a quick check confirms alterationa to local files
## else.. those are updated with latest from gitea
set -u
## -- path to the Ansible dirs
#ANSBASE=/var/lib/ansible/
#ANSBASE="/root/ansible"
ANSBASE="/root/git"
REPO="ansible-fmb"
## -- node set to the first parameter passed
NODE=${1:-"ts-el9.local"}
INVENTORY=${ANSBASE}/${REPO}
function apply_ansible
{
cd ${ANSBASE}/${REPO}
echo -e "$(date +'%F_%R:%S'): ++ Change detected! Applying playbooks for ${NODE} from ${REPO} ..."
#ansible-playbook -D -i ${ANSBASE}/${REPO}/inventory -l ${NODE} ${ANSBASE}/${REPO}/standalone-playbooks/epel.yml
#ansible-playbook -D -i ${ANSBASE}/${REPO}/inventory -l ${NODE} ${ANSBASE}/${REPO}/site.yml
ansible-playbook -D -i inventory -l ${NODE} standalone-playbooks/epel.yml
ansible-playbook -D -i inventory -l ${NODE} site.yml
}
echo "$(date +'%F_%R:%S'): ==== $(basename $0): ${NODE} START ==== "
## -- create directory if missing
[ ! -d ${ANSBASE} ] && mkdir -p ${ANSBASE}
cd ${ANSBASE}
if [ ! -d ${ANSBASE}/${REPO} ]
then
## -- initial clone here if dir is missing (and initial apply)
echo "$(date +'%F_%R:%S'): initial clone needed:"
#git clone deploy:/tombstones/${REPO}.git ${ANSBASE}/${REPO}
git clone ssh://git@deploy:/tombstones/${REPO}.git ${ANSBASE}/${REPO}
apply_ansible
elif [ -d ${ANSBASE}/${REPO} ]
then
## -- check first
echo -e "$(date +'%F_%R:%S'): Checking ${REPO} on Gitea ... \c"
cd ${ANSBASE}/${REPO}
STATUS="$(git status | tail -n1)"
## -- Checking if something changed locally that needs to be fixed
if [[ ! ${STATUS} =~ "nothing to commit" ]]
then
## -- oops... something has changed our end - fix it!
echo "fix required - showing diff on next line:"
git diff
git fetch origin &> /dev/null
git reset --hard origin/master &> /dev/null
echo "$(date +'%F_%R:%S'): ${REPO} fixed!"
else
echo " ${STATUS}"
fi
## -- perform an update (pull) here
RESULTS="$(git pull | tail -n1)"
echo "$(date +'%F_%R:%S'): ${RESULTS}"
if [[ ! ${RESULTS} =~ "up to date" ]]
then
## -- looks like there's been some changes
apply_ansible
fi
fi
echo "$(date +'%F_%R:%S'): ------ $(basename $0): ${NODE} END ------"