git_hap-config_autoupdate.j2 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env bash
  2. eval `ssh-agent`
  3. ssh-add "{{ hap_git_key }}"
  4. #variables:
  5. git_repo="{{ hap_git_repo }}"
  6. folder_dest="{{ hap_git_dest }}"
  7. hap_folder="{{ hap_git_folder }}"
  8. hap_path="{{ hap_git_bin }}"
  9. logfile="{{ hap_git_logfile }}"
  10. #update the status of the repo (without pulling or modifing):
  11. git -C $folder_dest fetch
  12. #store the status of the repo:
  13. git_before=$(git -C $folder_dest rev-parse HEAD)
  14. git_after=$(git -C $folder_dest rev-parse @{u})
  15. #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:
  16. if [ "$git_before" != "$git_after" ]; then
  17. echo "$(date +"%Y-%m-%d_%H:%M:%S") - INFO - the repo has been modified, doing my thing" | tee -a $logfile
  18. git -C $folder_dest pull --ff-only >/dev/null 2>&1
  19. echo "$(date +"%Y-%m-%d_%H:%M:%S") - INFO - testing newer haproxy configfile" | tee -a $logfile
  20. #test the new configfile, if it's ok then update haproxy config and restart the service
  21. if $hap_path -c -V -f $folder_dest/haproxy.cfg >/dev/null 2>&1; then
  22. echo "$(date +"%Y-%m-%d_%H:%M:%S") - INFO - haproxy file syntax is ok, deploying in production" | tee -a $logfile
  23. cp $folder_dest/haproxy.cfg $hap_folder/haproxy.cfg
  24. systemctl restart haproxy
  25. else
  26. echo "$(date +"%Y-%m-%d_%H:%M:%S") - ERROR - the test of the new haproxy configfile FAILED!" | tee -a $logfile
  27. fi
  28. echo "- - -" | tee -a $logfile
  29. fi
  30. #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
  31. if ! cmp $folder_dest/haproxy.cfg $hap_folder/haproxy.cfg >/dev/null 2>&1
  32. then
  33. echo "$(date +"%Y-%m-%d_%H:%M:%S") - WARNING - the local and repo's versions of the configfile differs, resyncing" | tee -a $logfile
  34. if $hap_path -c -V -f $folder_dest/haproxy.cfg >/dev/null 2>&1; then
  35. echo "$(date +"%Y-%m-%d_%H:%M:%S") - INFO - !diff local remote! resyncing - haproxy file syntax is ok, deploying in production" | tee -a $logfile
  36. cp $folder_dest/haproxy.cfg $hap_folder/haproxy.cfg
  37. systemctl restart haproxy
  38. else
  39. echo "$(date +"%Y-%m-%d_%H:%M:%S") - ERROR - !diff local remote! resyncing - the test of the new haproxy configfile FAILED!" | tee -a $logfile
  40. fi
  41. fi
  42. eval "$(ssh-agent -k)"