Jelajahi Sumber

added variables for logfile and haproxy configcheck

panda 2 tahun lalu
induk
melakukan
4804361292
1 mengubah file dengan 29 tambahan dan 6 penghapusan
  1. 29 6
      Ansible_DEMO/repo/git_hap-config_autoupdate.j2

+ 29 - 6
Ansible_DEMO/repo/git_hap-config_autoupdate.j2

@@ -8,6 +8,9 @@ ssh-add "{{ hap_git_key }}"
 git_repo="{{ hap_git_repo }}"
 git_repo="{{ hap_git_repo }}"
 folder_dest="{{ hap_git_dest }}"
 folder_dest="{{ hap_git_dest }}"
 hap_folder="{{ hap_git_folder }}"
 hap_folder="{{ hap_git_folder }}"
+hap_path="{{ hap_git_bin }}"
+logfile="{{ hap_git_logfile }}"
+
 
 
 #update the status of the repo (without pulling or modifing):
 #update the status of the repo (without pulling or modifing):
 git -C $folder_dest fetch
 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:
 #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
 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
 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
 if ! cmp $folder_dest/haproxy.cfg $hap_folder/haproxy.cfg >/dev/null 2>&1
 then
 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
 fi