From 48043612926c3079b608cfc8b1baacb4850ea51e Mon Sep 17 00:00:00 2001 From: panda Date: Sun, 16 Jan 2022 02:15:16 +0100 Subject: [PATCH] added variables for logfile and haproxy configcheck --- .../repo/git_hap-config_autoupdate.j2 | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/Ansible_DEMO/repo/git_hap-config_autoupdate.j2 b/Ansible_DEMO/repo/git_hap-config_autoupdate.j2 index 021342e..a0b4daf 100644 --- a/Ansible_DEMO/repo/git_hap-config_autoupdate.j2 +++ b/Ansible_DEMO/repo/git_hap-config_autoupdate.j2 @@ -8,6 +8,9 @@ ssh-add "{{ hap_git_key }}" 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 @@ -18,14 +21,34 @@ 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 "differensies" - git -C $folder_dest pull --ff-only - cp $folder_dest/haproxy.cfg $hap_folder/haproxy.cfg - systemctl restart haproxy + 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 - cp $folder_dest/haproxy.cfg $hap_folder/haproxy.cfg - systemctl restart haproxy + 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