소스 검색

added variables for logfile and haproxy configcheck

panda 2 년 전
부모
커밋
4804361292
1개의 변경된 파일29개의 추가작업 그리고 6개의 파일을 삭제
  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 }}"
 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