GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — develop-v1.6.0 ( 9d5181...7efb31 )
by
unknown
04:49
created

setup()   F

Complexity

Conditions 13

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
dl 0
loc 69
rs 2.4914
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like setup() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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 service setup and teardown code.
18
"""
19
20
from __future__ import absolute_import
21
22
import os
23
24
from oslo_config import cfg
25
26
from st2common import log as logging
27
from st2common.models import db
28
from st2common.constants.logging import DEFAULT_LOGGING_CONF_PATH
29
from st2common.persistence import db_init
30
from st2common.transport.bootstrap_utils import register_exchanges
31
from st2common.signal_handlers import register_common_signal_handlers
32
from st2common.util.debugging import enable_debugging
33
from st2common.models.utils.profiling import enable_profiling
34
from st2common import triggers
35
36
from st2common.rbac.migrations import run_all as run_all_rbac_migrations
37
38
__all__ = [
39
    'setup',
40
    'teardown',
41
42
    'db_setup',
43
    'db_teardown'
44
]
45
46
LOG = logging.getLogger(__name__)
47
48
49
def setup(service, config, setup_db=True, register_mq_exchanges=True,
50
          register_signal_handlers=True, register_internal_trigger_types=False,
51
          run_migrations=True, config_args=None):
52
    """
53
    Common setup function.
54
55
    Currently it performs the following operations:
56
57
    1. Parses config and CLI arguments
58
    2. Establishes DB connection
59
    3. Set log level for all the loggers to DEBUG if --debug flag is present or
60
       if system.debug config option is set to True.
61
    4. Registers RabbitMQ exchanges
62
    5. Registers common signal handlers
63
    6. Register internal trigger types
64
65
    :param service: Name of the service.
66
    :param config: Config object to use to parse args.
67
    """
68
    # Set up logger which logs everything which happens during and before config
69
    # parsing to sys.stdout
70
    logging.setup(DEFAULT_LOGGING_CONF_PATH, excludes=None)
71
72
    # Parse args to setup config.
73
    if config_args:
74
        config.parse_args(config_args)
75
    else:
76
        config.parse_args()
77
78
    config_file_paths = cfg.CONF.config_file
79
    config_file_paths = [os.path.abspath(path) for path in config_file_paths]
80
    LOG.debug('Using config files: %s', ','.join(config_file_paths))
81
82
    # Setup logging.
83
    logging_config_path = config.get_logging_config_path()
84
    logging_config_path = os.path.abspath(logging_config_path)
85
86
    LOG.debug('Using logging config: %s', logging_config_path)
87
    logging.setup(logging_config_path, redirect_stderr=cfg.CONF.log.redirect_stderr,
88
                  excludes=cfg.CONF.log.excludes)
89
90
    if cfg.CONF.debug or cfg.CONF.system.debug:
91
        enable_debugging()
92
93
    if cfg.CONF.profile:
94
        enable_profiling()
95
96
    # All other setup which requires config to be parsed and logging to
97
    # be correctly setup.
98
    if setup_db:
99
        db_setup()
100
101
    if register_mq_exchanges:
102
        register_exchanges()
103
104
    if register_signal_handlers:
105
        register_common_signal_handlers()
106
107
    if register_internal_trigger_types:
108
        triggers.register_internal_trigger_types()
109
110
    # TODO: This is a "not so nice" workaround until we have a proper migration system in place
111
    if run_migrations:
112
        run_all_rbac_migrations()
113
114
    if cfg.CONF.rbac.enable and not cfg.CONF.auth.enable:
115
        msg = ('Authentication is not enabled. RBAC only works when authentication is enabled.'
116
               'You can either enable authentication or disable RBAC.')
117
        raise Exception(msg)
118
119
120
def teardown():
121
    """
122
    Common teardown function.
123
    """
124
    db_teardown()
125
126
127
def db_setup():
128
    username = getattr(cfg.CONF.database, 'username', None)
129
    password = getattr(cfg.CONF.database, 'password', None)
130
131
    connection = db_init.db_setup_with_retry(
132
        db_name=cfg.CONF.database.db_name, db_host=cfg.CONF.database.host,
133
        db_port=cfg.CONF.database.port, username=username, password=password,
134
        ssl=cfg.CONF.database.ssl, ssl_keyfile=cfg.CONF.database.ssl_keyfile,
135
        ssl_certfile=cfg.CONF.database.ssl_certfile,
136
        ssl_cert_reqs=cfg.CONF.database.ssl_cert_reqs,
137
        ssl_ca_certs=cfg.CONF.database.ssl_ca_certs,
138
        ssl_match_hostname=cfg.CONF.database.ssl_match_hostname)
139
    return connection
140
141
142
def db_teardown():
143
    return db.db_teardown()
144