aboutsummaryrefslogtreecommitdiffstats
path: root/library/recipes/tirole/__main__.py
blob: e61c1517d793c9ece881f5b60574ee108c151e43 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import argparse
import datetime
import json
import os
import requests
import sys

import yatest.common
from library.python.testing.recipe import declare_recipe
from library.recipes.common import start_daemon, stop_daemon
from yatest.common import network

TIROLE_PORT_FILE = "tirole.port"
TIROLE_PID_FILE = "tirole.pid"

CONFIG_PATH = './tirole.config.json'


PORT_MANAGER = network.PortManager()


def _gen_config(roles_dir):
    http_port = PORT_MANAGER.get_tcp_port(80)

    cfg = {
        "http_common": {
            "listen_address": "localhost",
            "port": http_port,
        },
        "logger": {
            "file": yatest.common.output_path("tirole-common.log"),
        },
        "service": {
            "common": {
                "access_log": yatest.common.output_path("tirole-access.log"),
            },
            "tvm": {
                "self_tvm_id": 1000001,
            },
            "key_map": {
                "keys_file": yatest.common.source_path("library/recipes/tirole/data/sign.keys"),
                "default_key": "1",
            },
            "unittest": {
                "roles_dir": yatest.common.source_path(roles_dir) + "/",
            },
        },
    }

    with open(CONFIG_PATH, 'wt') as f:
        json.dump(cfg, f, sort_keys=True, indent=4)

    return http_port


def start(argv):
    _log('Starting Tirole recipe')

    parser = argparse.ArgumentParser()
    parser.add_argument('--roles-dir', dest='roles_dir', type=str, required=True)
    input_args = parser.parse_args(argv)

    http_port = _gen_config(input_args.roles_dir)

    print(http_port, file=sys.stderr)
    with open(TIROLE_PORT_FILE, "w") as f:
        f.write(str(http_port))

    # launch
    args = [
        yatest.common.build_path() + '/passport/infra/daemons/tirole/cmd/tirole',
        '-c',
        CONFIG_PATH,
    ]

    def check():
        try:
            r = requests.get("http://localhost:%d/ping" % http_port)
            if r.status_code == 200:
                return True
            else:
                _log("ping: %d : %s" % (r.status_code, r.text))
        except Exception as e:
            _log("ping: %s" % e)
        return False

    start_daemon(command=args, environment=os.environ.copy(), is_alive_check=check, pid_file_name=TIROLE_PID_FILE)


def stop(argv):
    with open(TIROLE_PID_FILE) as f:
        pid = f.read()
    if not stop_daemon(pid):
        _log("pid is dead: %s" % pid)


def _log(msg):
    print("%s : tirole-recipe : %s" % (datetime.datetime.now(), msg), file=sys.stdout)


if __name__ == "__main__":
    declare_recipe(start, stop)