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