Completed
Push — master ( fe49eb...265317 )
by W
06:11
created

st2common.setup()   B

Complexity

Conditions 5

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 37
rs 8.0896
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
"""
17
This module contains common script setup and teardown code.
18
19
Note: In this context script is every module which is not long running and can be executed from the
20
command line (e.g. st2-submit-debug-info, st2-register-content, etc.).
21
"""
22
23
from __future__ import absolute_import
24
25
import logging as stdlib_logging
26
27
from oslo_config import cfg
28
29
from st2common import log as logging
30
from st2common.service_setup import db_setup
31
from st2common.service_setup import db_teardown
32
from st2common.logging.filters import LogLevelFilter
33
from st2common.transport.bootstrap_utils import register_exchanges
34
35
__all__ = [
36
    'setup',
37
    'teardown',
38
39
    'db_setup',
40
    'db_teardown'
41
]
42
43
LOG = logging.getLogger(__name__)
44
45
46
def register_common_cli_options():
47
    """
48
    Register common CLI options.
49
    """
50
    cfg.CONF.register_cli_opt(cfg.BoolOpt('verbose', short='v', default=False))
51
52
53
def setup(config, setup_db=True, register_mq_exchanges=True):
54
    """
55
    Common setup function.
56
57
    Currently it performs the following operations:
58
59
    1. Parses config and CLI arguments
60
    2. Establishes DB connection
61
    3. Suppress DEBUG log level if --verbose flag is not used
62
    4. Registers RabbitMQ exchanges
63
64
    :param config: Config object to use to parse args.
65
    """
66
    # Register common CLI options
67
    register_common_cli_options()
68
69
    # Parse args to setup config
70
    config.parse_args()
71
72
    # Set up logging
73
    log_level = stdlib_logging.DEBUG
74
    stdlib_logging.basicConfig(format='%(asctime)s %(levelname)s [-] %(message)s', level=log_level)
75
76
    if not cfg.CONF.verbose:
77
        # Note: We still want to print things at the following log levels: INFO, ERROR, CRITICAL
78
        exclude_log_levels = [stdlib_logging.AUDIT, stdlib_logging.DEBUG]
79
        handlers = stdlib_logging.getLoggerClass().manager.root.handlers
80
81
        for handler in handlers:
82
            handler.addFilter(LogLevelFilter(log_levels=exclude_log_levels))
83
84
    # All other setup code which requires config to be parsed and logging to be correctly setup
85
    if setup_db:
86
        db_setup()
87
88
    if register_mq_exchanges:
89
        register_exchanges()
90
91
92
def teardown():
93
    """
94
    Common teardown function.
95
    """
96
    db_teardown()
97