whack-a-service/Ansible_DEMO/repo/git_hap-config_autoupdate.j2

56 lines
2.4 KiB
Django/Jinja

#!/usr/bin/env bash
eval `ssh-agent`
ssh-add "{{ hap_git_key }}"
#variables:
git_repo="{{ hap_git_repo }}"
folder_dest="{{ hap_git_dest }}"
hap_folder="{{ hap_git_folder }}"
hap_path="{{ hap_git_bin }}"
logfile="{{ hap_git_logfile }}"
#update the status of the repo (without pulling or modifing):
git -C $folder_dest fetch
#store the status of the repo:
git_before=$(git -C $folder_dest rev-parse HEAD)
git_after=$(git -C $folder_dest rev-parse @{u})
#compare the status before and after the fetch, if it's different it mwans there are change in the repo. so we'll need to pull it and update the files:
if [ "$git_before" != "$git_after" ]; then
echo "$(date +"%Y-%m-%d_%H:%M:%S") - INFO - the repo has been modified, doing my thing" | tee -a $logfile
git -C $folder_dest pull --ff-only >/dev/null 2>&1
echo "$(date +"%Y-%m-%d_%H:%M:%S") - INFO - testing newer haproxy configfile" | tee -a $logfile
#test the new configfile, if it's ok then update haproxy config and restart the service
if $hap_path -c -V -f $folder_dest/haproxy.cfg >/dev/null 2>&1; then
echo "$(date +"%Y-%m-%d_%H:%M:%S") - INFO - haproxy file syntax is ok, deploying in production" | tee -a $logfile
cp $folder_dest/haproxy.cfg $hap_folder/haproxy.cfg
systemctl restart haproxy
else
echo "$(date +"%Y-%m-%d_%H:%M:%S") - ERROR - the test of the new haproxy configfile FAILED!" | tee -a $logfile
fi
echo "- - -" | tee -a $logfile
fi
#to keep the local version always the same as the repo's one, in this block we check if the local and repo's file are different, if they are we rewrite the local one because we want the changes to come only from the repo
if ! cmp $folder_dest/haproxy.cfg $hap_folder/haproxy.cfg >/dev/null 2>&1
then
echo "$(date +"%Y-%m-%d_%H:%M:%S") - WARNING - the local and repo's versions of the configfile differs, resyncing" | tee -a $logfile
if $hap_path -c -V -f $folder_dest/haproxy.cfg >/dev/null 2>&1; then
echo "$(date +"%Y-%m-%d_%H:%M:%S") - INFO - !diff local remote! resyncing - haproxy file syntax is ok, deploying in production" | tee -a $logfile
cp $folder_dest/haproxy.cfg $hap_folder/haproxy.cfg
systemctl restart haproxy
else
echo "$(date +"%Y-%m-%d_%H:%M:%S") - ERROR - !diff local remote! resyncing - the test of the new haproxy configfile FAILED!" | tee -a $logfile
fi
fi
eval "$(ssh-agent -k)"