status.py 3.7 KB
Newer Older
Mark Hewitt's avatar
Mark Hewitt committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

"""Code to handle status commands"""

import click
Mark Hewitt's avatar
Mark Hewitt committed
16
from requests.exceptions import RequestException
Mark Hewitt's avatar
Mark Hewitt committed
17

Mark Hewitt's avatar
Mark Hewitt committed
18
from osducli.click_cli import CustomClickCommand, State, command_with_output
Mark Hewitt's avatar
Mark Hewitt committed
19
from osducli.cliclient import CliOsduClient, handle_cli_exceptions
Mark Hewitt's avatar
Mark Hewitt committed
20
21
22
23
24
25
26
27
28
from osducli.commands.entitlements._const import (
    ENTITLEMENTS_SERVICE_NAME,
    ENTITLEMENTS_STATUS_PATH,
)
from osducli.commands.legal._const import LEGAL_SERVICE_NAME, LEGAL_STATUS_PATH
from osducli.commands.schema._const import SCHEMA_SERVICE_NAME, SCHEMA_STATUS_PATH
from osducli.commands.search._const import SEARCH_SERVICE_NAME, SEARCH_STATUS_PATH
from osducli.commands.unit._const import UNIT_SERVICE_NAME, UNIT_STATUS_PATH
from osducli.commands.workflow._const import WORKFLOW_SERVICE_NAME, WORKFLOW_STATUS_PATH
Mark Hewitt's avatar
Mark Hewitt committed
29
from osducli.config import (
Mark Hewitt's avatar
Mark Hewitt committed
30
    CONFIG_ENTITLEMENTS_URL,
Mark Hewitt's avatar
Mark Hewitt committed
31
32
33
34
35
36
37
38
    CONFIG_FILE_URL,
    CONFIG_LEGAL_URL,
    CONFIG_SCHEMA_URL,
    CONFIG_SEARCH_URL,
    CONFIG_STORAGE_URL,
    CONFIG_UNIT_URL,
    CONFIG_WORKFLOW_URL,
)
Mark Hewitt's avatar
Mark Hewitt committed
39
40
41
from osducli.log import get_logger

logger = get_logger(__name__)
Mark Hewitt's avatar
Mark Hewitt committed
42
43


44
@click.command(cls=CustomClickCommand)
Mark Hewitt's avatar
Mark Hewitt committed
45
@handle_cli_exceptions
Mark Hewitt's avatar
Mark Hewitt committed
46
47
@command_with_output("results[]")
def _click_command(state: State):
Mark Hewitt's avatar
Mark Hewitt committed
48
49
    # def _click_command(ctx, debug, config, hostname):
    """Shows the status of OSDU services"""
Mark Hewitt's avatar
Mark Hewitt committed
50
    return status(state)
Mark Hewitt's avatar
Mark Hewitt committed
51
52


Mark Hewitt's avatar
Mark Hewitt committed
53
def status(state: State):  # pylint: disable=unused-argument
Mark Hewitt's avatar
Mark Hewitt committed
54
55
    """status command entry point

Mark Hewitt's avatar
Mark Hewitt committed
56
    User friendly mode displays results as received for responsiveness.
Mark Hewitt's avatar
Mark Hewitt committed
57
58
59
60
    Args:
        state (State): Global state
    """
    connection = CliOsduClient(state.config)
Mark Hewitt's avatar
Mark Hewitt committed
61
62
63
    results = []
    services = [
        ("File service", CONFIG_FILE_URL, "readiness_check"),
Mark Hewitt's avatar
Mark Hewitt committed
64
65
66
67
        (ENTITLEMENTS_SERVICE_NAME, CONFIG_ENTITLEMENTS_URL, ENTITLEMENTS_STATUS_PATH),
        (LEGAL_SERVICE_NAME, CONFIG_LEGAL_URL, LEGAL_STATUS_PATH),
        (SCHEMA_SERVICE_NAME, CONFIG_SCHEMA_URL, SCHEMA_STATUS_PATH),
        (SEARCH_SERVICE_NAME, CONFIG_SEARCH_URL, SEARCH_STATUS_PATH),
Mark Hewitt's avatar
Mark Hewitt committed
68
        ("Storage service", CONFIG_STORAGE_URL, "health"),
Mark Hewitt's avatar
Mark Hewitt committed
69
70
        (UNIT_SERVICE_NAME, CONFIG_UNIT_URL, UNIT_STATUS_PATH),
        (WORKFLOW_SERVICE_NAME, CONFIG_WORKFLOW_URL, WORKFLOW_STATUS_PATH),
Mark Hewitt's avatar
Mark Hewitt committed
71
72
    ]
    for service in services:
Mark Hewitt's avatar
Mark Hewitt committed
73
        result = check_status(connection, service[0], service[1], service[2])
Mark Hewitt's avatar
Mark Hewitt committed
74
75
76
77
78
        results.append(result)
        if state.is_user_friendly_mode():
            print(f"{result['name'].ljust(20)} {result['status']}\t {result['reason']}")

    return None if state.is_user_friendly_mode() else {"results": results}
Mark Hewitt's avatar
Mark Hewitt committed
79
80


Mark Hewitt's avatar
Mark Hewitt committed
81
def check_status(connection: CliOsduClient, name: str, config_url_key: str, url_extra_path: str):
Mark Hewitt's avatar
Mark Hewitt committed
82
    """Check the status of the given service"""
Mark Hewitt's avatar
Mark Hewitt committed
83
84
    try:
        response = connection.cli_get(config_url_key, url_extra_path)
Mark Hewitt's avatar
Mark Hewitt committed
85
86
87
88
89
90
91
92
93
94
        _status = response.status_code
        _reason = response.reason
    except RequestException as _ex:  # pylint: disable=broad-except
        exception_message = str(_ex) if len(str(_ex)) > 0 else "Unknown Error"
        logger.debug(exception_message)
        _status = _ex.response.status_code if _ex.response else -1
        _reason = _ex.response.reason if _ex.response else exception_message

    result = {"name": name, "status": _status, "reason": _reason}
    return result