eb77f93c336cde2b9c74204db3a90282a1c66aa7
[sdc.git] / catalog-be / sdc-backend-init / chef-repo / cookbooks / sdc-catalog-be-setup / templates / default / check_Backend_Health.py.erb
1 #!/usr/bin/python
2 import subprocess
3 #from time import sleep
4 import time
5 from datetime import datetime
6
7 beStat=0
8 BE_IP="<%= @be_ip %>"
9 BE_PORT="<%= @be_port %>"
10 PROTOCOL="<%= @protocol %>"
11
12 class bcolors:
13     HEADER    = '\033[95m'
14     OKBLUE    = '\033[94m'
15     OKGREEN   = '\033[92m'
16     WARNING   = '\033[93m'
17     FAIL      = '\033[91m'
18     ENDC      = '\033[0m'
19     BOLD      = '\033[1m'
20     UNDERLINE = '\033[4m'
21
22
23 ##############################
24 #    Functions
25 ##############################
26 def checkBackend():
27     command="curl -k -s -o /dev/null -I -w \"%{http_code}\" -i "+PROTOCOL+"://" + BE_IP + ":" + BE_PORT + "/sdc2/rest/v1/user/jh0003"
28
29     proc = subprocess.Popen( command , shell=True , stdout=subprocess.PIPE )
30     (out, err) = proc.communicate()
31     result = out.strip()
32     return result
33
34 ##############################
35 #    Main
36 ##############################
37
38 for i in range(1,10):
39     myResult = checkBackend()
40     if myResult == '200':
41         print '[INFO]: Backend is up and running'
42         beStat=1
43         break
44     else:
45         currentTime = datetime.now()
46         print '[ERROR]: ' + currentTime.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + ' Backend not responding, try #' + str(i) + bcolors.ENDC
47         time.sleep(10)
48
49 if beStat == 0:
50     print '[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + bcolors.FAIL + 'Backend is DOWN :-(' + bcolors.ENDC
51     exit()