Monasca Log REST API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2017 FUJITSU LIMITED
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import sys
  15. from oslo_config import cfg
  16. from oslo_log import log
  17. from oslo_policy import opts as policy_opts
  18. from monasca_log_api import conf
  19. from monasca_log_api import version
  20. CONF = conf.CONF
  21. LOG = log.getLogger(__name__)
  22. _CONF_LOADED = False
  23. _GUNICORN_MARKER = 'gunicorn'
  24. def _is_running_under_gunicorn():
  25. """Evaluates if api runs under gunicorn"""
  26. content = filter(lambda x: x != sys.executable and _GUNICORN_MARKER in x,
  27. sys.argv or [])
  28. return len(list(content) if not isinstance(content, list) else content) > 0
  29. def get_config_files():
  30. """Get the possible configuration files accepted by oslo.config
  31. This also includes the deprecated ones
  32. """
  33. # default files
  34. conf_files = cfg.find_config_files(project='monasca',
  35. prog='monasca-log-api')
  36. # deprecated config files (only used if standard config files are not there)
  37. if len(conf_files) == 0:
  38. old_conf_files = cfg.find_config_files(project='monasca',
  39. prog='log-api')
  40. if len(old_conf_files) > 0:
  41. LOG.warning('Found deprecated old location "{}" '
  42. 'of main configuration file'.format(old_conf_files))
  43. conf_files += old_conf_files
  44. return conf_files
  45. def parse_args(argv=None):
  46. global _CONF_LOADED
  47. if _CONF_LOADED:
  48. LOG.debug('Configuration has been already loaded')
  49. return
  50. log.set_defaults()
  51. log.register_options(CONF)
  52. argv = (argv if argv is not None else sys.argv[1:])
  53. args = ([] if _is_running_under_gunicorn() else argv or [])
  54. CONF(args=args,
  55. prog=sys.argv[1:],
  56. project='monasca',
  57. version=version.version_str,
  58. default_config_files=get_config_files(),
  59. description='RESTful API to collect log files')
  60. log.setup(CONF,
  61. product_name='monasca-log-api',
  62. version=version.version_str)
  63. conf.register_opts()
  64. policy_opts.set_defaults(CONF)
  65. _CONF_LOADED = True