From 2d5bef79de82a8725e78f8e2651ecfde8d3f84df Mon Sep 17 00:00:00 2001 From: Mehdi Abaakouk Date: Wed, 21 Sep 2016 09:54:04 +0200 Subject: [PATCH] Add oslo config glue Sem-Ver: feature --- cotyledon/oslo_config_glue.py | 44 +++++++++++++++++++++++++++++++ cotyledon/tests/examples.py | 21 +++++++++++++++ cotyledon/tests/test_cotyledon.py | 11 ++++++++ setup.cfg | 4 +++ test-requirements.txt | 1 + 5 files changed, 81 insertions(+) create mode 100644 cotyledon/oslo_config_glue.py diff --git a/cotyledon/oslo_config_glue.py b/cotyledon/oslo_config_glue.py new file mode 100644 index 0000000..7628536 --- /dev/null +++ b/cotyledon/oslo_config_glue.py @@ -0,0 +1,44 @@ +# 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. + +import copy +import logging + +from oslo_config import cfg + +LOG = logging.getLogger(__name__) + +service_opts = [ + cfg.BoolOpt('log_options', + default=True, + help='Enables or disables logging values of all ' + 'registered options when starting a service (at DEBUG ' + 'level).'), + cfg.IntOpt('graceful_shutdown_timeout', + default=60, + help='Specify a timeout after which a gracefully shutdown ' + 'server will exit. Zero value means endless wait.'), +] + + +def load_options(service, conf): + """Load some service configuration from oslo config object.""" + conf.register_opts(service_opts) + service.graceful_shutdown_timeout = conf.graceful_shutdown_timeout + if conf.log_options: + LOG.debug('Full set of CONF:') + conf.log_opt_values(LOG, logging.DEBUG) + + +def list_opts(self): + """Entry point for oslo-config-generator.""" + return [(None, copy.deepcopy(service_opts))] diff --git a/cotyledon/tests/examples.py b/cotyledon/tests/examples.py index 49d242a..3dc399b 100644 --- a/cotyledon/tests/examples.py +++ b/cotyledon/tests/examples.py @@ -15,7 +15,10 @@ import sys import threading import time +from oslo_config import cfg + import cotyledon +from cotyledon import oslo_config_glue LOG = logging.getLogger(__name__) @@ -54,6 +57,17 @@ class BuggyService(cotyledon.Service): LOG.error("time.sleep done") +class OsloService(cotyledon.Service): + name = "oslo" + + def __init__(self, worker_id): + conf = cfg.ConfigOpts() + conf([], project='gnocchi', validate_default_values=True, + version="0.1") + oslo_config_glue.load_options(self, conf) + oslo_config_glue.list_opts() + + def example_app(): logging.basicConfig(level=logging.DEBUG) p = cotyledon.ServiceManager() @@ -67,3 +81,10 @@ def buggy_app(): p = cotyledon.ServiceManager() p.add(BuggyService) p.run() + + +def oslo_app(): + logging.basicConfig(level=logging.DEBUG) + p = cotyledon.ServiceManager() + p.add(OsloService) + p.run() diff --git a/cotyledon/tests/test_cotyledon.py b/cotyledon/tests/test_cotyledon.py index 4711328..a20e30f 100644 --- a/cotyledon/tests/test_cotyledon.py +++ b/cotyledon/tests/test_cotyledon.py @@ -219,3 +219,14 @@ class TestBuggyCotyledon(Base): b'exiting buggy(0) [XXXX] now.', b'DEBUG:cotyledon:Shutdown finish' ], lines[-2:]) + + +class TestOsloCotyledon(Base): + name = "cotyledon-oslo" + + def test_options(self): + lines = self.get_lines(1) + self.assertEqual( + b'DEBUG:cotyledon.oslo_config_glue:Full set of CONF:', + lines[0]) + self.subp.terminate() diff --git a/setup.cfg b/setup.cfg index 6403060..dfb7851 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,6 +26,10 @@ packages = console_scripts = cotyledon-example = cotyledon.tests.examples:example_app cotyledon-buggy = cotyledon.tests.examples:buggy_app + cotyledon-oslo = cotyledon.tests.examples:oslo_app + +oslo.config.opts = + cotyledon.service = cotyledon.oslo_config_glue:list_opts [build_sphinx] source-dir = doc/source diff --git a/test-requirements.txt b/test-requirements.txt index 21a7e3b..fca6b13 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -9,6 +9,7 @@ python-subunit>=0.0.18 sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 oslosphinx>=2.5.0 # Apache-2.0 oslotest>=1.10.0 # Apache-2.0 +oslo.config>=3.14.0 # Apache-2.0 testrepository>=0.0.18 testscenarios>=0.4 testtools>=1.4.0