From 3f990beb97c2f72a6af28dd24f70b016606acc61 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Wed, 1 Dec 2021 12:19:33 +0100 Subject: [PATCH] Refactor common configuration bits from service commands The prepare_service call from ironic.common.service is changed to also configure guru meditation and profiler. A new call prepare_command is provided for the cases it's not required. Change-Id: I5b9b7b7bc827c8bcda06e9a967deae8577ad87f4 --- ironic/api/wsgi.py | 2 +- ironic/cmd/api.py | 18 +--------- ironic/cmd/conductor.py | 18 +--------- ironic/cmd/dbsync.py | 2 +- ironic/common/service.py | 34 ++++++++++++++++++- .../do_not_run_create_benchmark_data.py | 2 +- tools/benchmark/generate-statistics.py | 2 +- 7 files changed, 39 insertions(+), 39 deletions(-) diff --git a/ironic/api/wsgi.py b/ironic/api/wsgi.py index c464731390..4673897012 100644 --- a/ironic/api/wsgi.py +++ b/ironic/api/wsgi.py @@ -29,7 +29,7 @@ LOG = log.getLogger(__name__) def initialize_wsgi_app(argv=sys.argv): i18n.install('ironic') - service.prepare_service(argv) + service.prepare_command(argv) LOG.debug("Configuration:") CONF.log_opt_values(LOG, log.DEBUG) diff --git a/ironic/cmd/api.py b/ironic/cmd/api.py index 38ec7bd1fb..4a4b381c86 100644 --- a/ironic/cmd/api.py +++ b/ironic/cmd/api.py @@ -21,16 +21,9 @@ import sys from oslo_config import cfg from oslo_log import log -try: - from oslo_reports import guru_meditation_report as gmr - from oslo_reports import opts as gmr_opts -except ImportError: - gmr = None -from ironic.common import profiler from ironic.common import service as ironic_service from ironic.common import wsgi_service -from ironic import version CONF = cfg.CONF @@ -39,16 +32,7 @@ LOG = log.getLogger(__name__) def main(): # Parse config file and command line options, then start logging - ironic_service.prepare_service(sys.argv) - - if gmr is not None: - gmr_opts.set_defaults(CONF) - gmr.TextGuruMeditation.setup_autorun(version) - else: - LOG.debug('Guru meditation reporting is disabled ' - 'because oslo.reports is not installed') - - profiler.setup('ironic_api', CONF.host) + ironic_service.prepare_service('ironic_api', sys.argv) # Build and start the WSGI app launcher = ironic_service.process_launcher() diff --git a/ironic/cmd/conductor.py b/ironic/cmd/conductor.py index d2ee20f5f9..19fb05cb41 100644 --- a/ironic/cmd/conductor.py +++ b/ironic/cmd/conductor.py @@ -23,17 +23,10 @@ import sys from oslo_config import cfg from oslo_log import log -try: - from oslo_reports import guru_meditation_report as gmr - from oslo_reports import opts as gmr_opts -except ImportError: - gmr = None from oslo_service import service -from ironic.common import profiler from ironic.common import rpc_service from ironic.common import service as ironic_service -from ironic import version CONF = cfg.CONF @@ -64,14 +57,7 @@ def main(): assert 'ironic.conductor.manager' not in sys.modules # Parse config file and command line options, then start logging - ironic_service.prepare_service(sys.argv) - - if gmr is not None: - gmr_opts.set_defaults(CONF) - gmr.TextGuruMeditation.setup_autorun(version, conf=CONF) - else: - LOG.debug('Guru meditation reporting is disabled ' - 'because oslo.reports is not installed') + ironic_service.prepare_service('ironic_conductor', sys.argv) mgr = rpc_service.RPCService(CONF.host, 'ironic.conductor.manager', @@ -79,8 +65,6 @@ def main(): issue_startup_warnings(CONF) - profiler.setup('ironic_conductor', CONF.host) - launcher = service.launch(CONF, mgr, restart_method='mutate') launcher.wait() diff --git a/ironic/cmd/dbsync.py b/ironic/cmd/dbsync.py index 484e8096c6..9cdfa2f4f1 100644 --- a/ironic/cmd/dbsync.py +++ b/ironic/cmd/dbsync.py @@ -338,5 +338,5 @@ def main(): if not set(sys.argv) & valid_commands: sys.argv.append('upgrade') - service.prepare_service(sys.argv) + service.prepare_command(sys.argv) CONF.command.func() diff --git a/ironic/common/service.py b/ironic/common/service.py index e0a8f1852d..db83c147a9 100644 --- a/ironic/common/service.py +++ b/ironic/common/service.py @@ -15,15 +15,29 @@ # under the License. from oslo_log import log +try: + from oslo_reports import guru_meditation_report as gmr + from oslo_reports import opts as gmr_opts +except ImportError: + gmr = None from oslo_service import service from ironic.common import config +from ironic.common import profiler from ironic.conf import CONF from ironic.conf import opts from ironic import objects +from ironic import version -def prepare_service(argv=None): +LOG = log.getLogger(__name__) + + +def prepare_command(argv=None): + """Prepare any Ironic command for execution. + + Sets up configuration and logging, registers objects. + """ argv = [] if argv is None else argv log.register_options(CONF) opts.update_opt_defaults() @@ -35,5 +49,23 @@ def prepare_service(argv=None): objects.register_all() +def prepare_service(name, argv=None, conf=CONF): + """Prepare an Ironic service executable. + + In addition to what `prepare_command` does, set up guru meditation + reporting and profiling. + """ + prepare_command(argv) + + if gmr is not None: + gmr_opts.set_defaults(CONF) + gmr.TextGuruMeditation.setup_autorun(version, conf=CONF) + else: + LOG.debug('Guru meditation reporting is disabled ' + 'because oslo.reports is not installed') + + profiler.setup(name, CONF.host) + + def process_launcher(): return service.ProcessLauncher(CONF, restart_method='mutate') diff --git a/tools/benchmark/do_not_run_create_benchmark_data.py b/tools/benchmark/do_not_run_create_benchmark_data.py index 1e050a0f1a..afdb3c7f29 100644 --- a/tools/benchmark/do_not_run_create_benchmark_data.py +++ b/tools/benchmark/do_not_run_create_benchmark_data.py @@ -90,7 +90,7 @@ def _mix_up_nodes_data(): def main(): - service.prepare_service() + service.prepare_command() CONF.set_override('debug', False) _create_test_nodes() diff --git a/tools/benchmark/generate-statistics.py b/tools/benchmark/generate-statistics.py index 65e8d664f0..c6a7197c52 100644 --- a/tools/benchmark/generate-statistics.py +++ b/tools/benchmark/generate-statistics.py @@ -183,7 +183,7 @@ def _report_conductors(): def main(): - service.prepare_service() + service.prepare_command() CONF.set_override('debug', False) _assess_db_performance() _assess_db_and_object_performance()