summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/resources/scripts/sdcBePy/tosca/main.py
blob: b3cf8828ca1db70c05bb573c43cd6dc8370a1564 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import os
import sys
from argparse import ArgumentParser

from sdcBePy.common import logger
from sdcBePy.common.properties import init_properties
from sdcBePy.common.sdcBeProxy import SdcBeProxy


def usage():
    print(sys.argv[0],
          '[-s <scheme> | --scheme=<scheme> ] [-i <be host> | --ip=<be host>] [-p <be port> | '
          '--port=<be port> ] --header=<header> ] [-u <user userId> | --user=<user userId> ] [-d <true|false> | '
          '--debug=<true|false>] [-v <true|false> | --updateVersion=<true|false>]')


def load_be_config(conf_path):
    with open(conf_path, 'r', encoding='utf-8') as f:
        return json.load(f)


def parse_param():
    parser = ArgumentParser()

    path = os.path.dirname(__file__)
    parser.add_argument('--conf', default=os.path.join(path, 'data', 'beConfig.json'))

    parser.add_argument('--ip', "-i")
    parser.add_argument('--port', "-p")
    parser.add_argument('--header')
    parser.add_argument('--adminUser', "-a")
    parser.add_argument('--https', action='store_true')
    parser.add_argument('--updateVersion', action='store_false')
    parser.add_argument('--debug', action='store_true')

    args, _ = parser.parse_known_args()

    return [args.conf, 'https' if args.https else 'http',
            args.ip, args.port, args.header, args.adminUser, args.updateVersion,
            args.debug]


def get_args():
    print('Number of arguments:', len(sys.argv), 'arguments.')

    conf_path, scheme, be_host, be_port, header, admin_user, update_version, debug = parse_param()
    defaults = load_be_config(conf_path)

    # Use defaults if param not provided by the user
    if be_host is None:
        be_host = defaults["beHost"]
    if be_port is None:
        be_port = defaults["bePort"]
    if admin_user is None:
        admin_user = defaults["adminUser"]

    if header is None:
        print('scheme =', scheme, ',be host =', be_host, ', be port =', be_port, ', user =', admin_user,
          ', debug =', debug, ', update_version =', update_version)
    else:
        print('scheme =', scheme, ',be host =', be_host, ', be port =', be_port, ', header =', header, ', user =', admin_user,
              ', debug =', debug, ', update_version =', update_version)

    init_properties(defaults["retryTime"], defaults["retryAttempt"], defaults["resourceLen"])
    return scheme, be_host, be_port, header, admin_user, update_version, debug


def parse_and_create_proxy():
    scheme, be_host, be_port, header, admin_user, update_version, debug = get_args()

    if debug is False:
        print('Disabling debug mode')
        logger.debugFlag = debug

    try:
        sdc_be_proxy = SdcBeProxy(be_host, be_port, header, scheme, admin_user, debug=debug)
    except AttributeError:
        usage()
        sys.exit(3)

    return sdc_be_proxy, update_version