From c8980d92f4f3878d1d5101a4fa087e3a002880f3 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 30 Jul 2015 23:06:22 -0400 Subject: [PATCH] Add a configuration for the directory to generate gmr If projects choose to have their own config parameter, they can use setup_autorun's log_dir parameter. if they want to let oslo.report's manage the configuration, they will have to call set_defaults and then use setup_autorun's conf parameter. Change-Id: I9ed0191628f2d552acd8130687b636671bc07a3e --- oslo_reports/_i18n.py | 35 +++++++++++++++ oslo_reports/guru_meditation_report.py | 21 ++++++--- oslo_reports/opts.py | 61 ++++++++++++++++++++++++++ setup.cfg | 4 ++ 4 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 oslo_reports/_i18n.py create mode 100644 oslo_reports/opts.py diff --git a/oslo_reports/_i18n.py b/oslo_reports/_i18n.py new file mode 100644 index 0000000..2fa94a0 --- /dev/null +++ b/oslo_reports/_i18n.py @@ -0,0 +1,35 @@ +# 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. + +"""oslo.i18n integration module. + +See http://docs.openstack.org/developer/oslo.i18n/usage.html . + +""" + +import oslo_i18n + + +_translators = oslo_i18n.TranslatorFactory(domain='oslo.reports') + +# The primary translation function using the well-known name "_" +_ = _translators.primary + +# Translators for log levels. +# +# The abbreviated names are meant to reflect the usual use of a short +# name like '_'. The "L" is for "log" and the other letter comes from +# the level. +_LI = _translators.log_info +_LW = _translators.log_warning +_LE = _translators.log_error +_LC = _translators.log_critical diff --git a/oslo_reports/guru_meditation_report.py b/oslo_reports/guru_meditation_report.py index ed62338..85b1869 100644 --- a/oslo_reports/guru_meditation_report.py +++ b/oslo_reports/guru_meditation_report.py @@ -23,16 +23,24 @@ For example, in a nova command module (under nova/cmd): .. code-block:: python :emphasize-lines: 8,9,10 + from oslo_config import cfg + from oslo_log import log as oslo_logging + from oslo_reports import opts as gmr_opts + from oslo_reports import guru_meditation_report as gmr + CONF = cfg.CONF # maybe import some options here... def main(): - config.parse_args(sys.argv) - logging.setup('blah') + oslo_logging.register_options(CONF) + gmr_opts.set_defaults(CONF) - TextGuruMeditation.register_section('Some Special Section', + CONF(sys.argv[1:], default_config_files=['myapp.conf']) + oslo_logging.setup(CONF, 'myapp') + + gmr.TextGuruMeditation.register_section('Some Special Section', special_section_generator) - TextGuruMeditation.setup_autorun(version_object) + gmr.TextGuruMeditation.setup_autorun(version_object, conf=CONF) server = service.Service.create(binary='some-service', topic=CONF.some_service_topic) @@ -106,7 +114,7 @@ class GuruMeditation(object): @classmethod def setup_autorun(cls, version, service_name=None, - log_dir=None, signum=None): + log_dir=None, signum=None, conf=None): """Set Up Auto-Run This method sets up the Guru Meditation Report to automatically @@ -117,6 +125,7 @@ class GuruMeditation(object): :param service_name: this program name used to construct logfile name :param logdir: path to a log directory where to create a file :param signum: the signal to associate with running the report + :param conf: Configuration object, managed by the caller. """ if not signum and hasattr(signal, 'SIGUSR1'): @@ -124,6 +133,8 @@ class GuruMeditation(object): signum = signal.SIGUSR1 if signum: + if log_dir is None and conf is not None: + log_dir = conf.oslo_reports.log_dir signal.signal(signum, lambda sn, tb: cls.handle_signal( version, service_name, log_dir, tb)) diff --git a/oslo_reports/opts.py b/oslo_reports/opts.py new file mode 100644 index 0000000..94072bf --- /dev/null +++ b/oslo_reports/opts.py @@ -0,0 +1,61 @@ +# 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. + +__all__ = [ + 'list_opts', + 'set_defaults', +] + +import copy + +from oslo_config import cfg + +from oslo_reports._i18n import _ + + +_option_group = 'oslo_reports' + +_options = [ + cfg.StrOpt('log_dir', + help=_('Path to a log directory where to create a file')), +] + + +def list_opts(): + """Return a list of oslo.config options available in the library. + + The returned list includes all oslo.config options which may be registered + at runtime by the library. + Each element of the list is a tuple. The first element is the name of the + group under which the list of elements in the second element will be + registered. A group name of None corresponds to the [DEFAULT] group in + config files. + This function is also discoverable via the 'oslo_messaging' entry point + under the 'oslo.config.opts' namespace. + The purpose of this is to allow tools like the Oslo sample config file + generator to discover the options exposed to users by this library. + + :returns: a list of (group_name, opts) tuples + """ + + return [(_option_group, copy.deepcopy(_options))] + + +def set_defaults(conf): + """Set defaults for configuration variables. + + Overrides default options values. + + :param conf: Configuration object, managed by the caller. + :type conf: oslo.config.cfg.ConfigOpts + """ + conf.register_opts(_options, group=_option_group) diff --git a/setup.cfg b/setup.cfg index 5635525..2131e71 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,6 +27,10 @@ packages = [pbr] warnerrors = true +[entry_points] +oslo.config.opts = + oslo.reports = oslo_reports.opts:list_opts + [build_sphinx] source-dir = doc/source build-dir = doc/build