aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/resources/scripts/sdcBePy/common/healthCheck.py
blob: 8d63ef3d985f92a2739a7d96f3f80c3a13aa6f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import sys
import time
from argparse import ArgumentParser
from datetime import datetime

from sdcBePy import properties
from sdcBePy.common.bColors import BColors
from sdcBePy.common.properties import init_properties
from sdcBePy.common.sdcBeProxy import SdcBeProxy

colors = BColors()


def check_backend(sdc_be_proxy=None, reply_append_count=1, be_host=None, be_port=None, header=None, scheme=None, debug=False, ca_cert=None, tls_cert=None, tls_key=None, tls_key_pw=None):
    if sdc_be_proxy is None:
        sdc_be_proxy = SdcBeProxy(be_host, be_port, header, scheme, tls_cert, tls_key, tls_key_pw, ca_cert, debug=debug)

    for i in range(1, reply_append_count + 1):
        if sdc_be_proxy.check_backend() == 200:
            print('[INFO]: Backend is up and running')
            return True
        else:
            print('[WARRING]: ' + datetime.now().strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL
                  + ' Backend not responding, try #' + str(i) + colors.END_C)
            time.sleep(properties.retry_time)

    return False


def run(be_host, be_port, header, protocol, tls_key, tls_cert, tls_key_pw, ca_cert):
    if not check_backend(reply_append_count=properties.retry_attempts, be_host=be_host,
                         be_port=be_port, header=header, scheme=protocol, ca_cert=ca_cert, tls_cert=tls_cert, tls_key=tls_key, tls_key_pw=tls_key_pw):
        print('[ERROR]: ' + time.strftime('%Y/%m/%d %H:%M:%S') + colors.FAIL + ' Backend is DOWN :-(' + colors.END_C)
        sys.exit()


def get_args():
    parser = ArgumentParser()

    parser.add_argument('-i', '--ip', required=True)
    parser.add_argument('-p', '--port', required=True)
    parser.add_argument('--header')
    parser.add_argument('--https', action='store_true')
    parser.add_argument('--tls_key')
    parser.add_argument('--tls_cert')
    parser.add_argument('--tls_key_pw')
    parser.add_argument('--ca_cert')

    args = parser.parse_args()

    init_properties(10, 10)
    return [args.ip, args.port, args.header, 'https' if args.https else 'http', args.tls_key, args.tls_cert, args.tls_key_pw, args.ca_cert]


def main():
    run(*get_args())


if __name__ == '__main__':
    main()