From 21d5151e3989de6e41e40f7b3e15260afd0d8929 Mon Sep 17 00:00:00 2001 From: Slawek Kaplonski Date: Tue, 19 Nov 2024 16:15:25 +0100 Subject: [PATCH] Add Tobiko CLI tool to execute tobiko tools This patch adds base `tobiko` CLI tool and one command for it which is `ping`. Using this new CLI tool it is now possible to do `tobiko ping ` to run tobiko function to ping some server and store results in the file. This can be used e.g. to run background ping process in the POD on OpenShift and use that in the Tobiko tests. Related: #TOBIKO-100 Change-Id: I33aea73cead5a4f406ce1a08c819fe056e17e9a1 --- setup.cfg | 3 +++ tobiko/cmd/__init__.py | 27 +++++++++++++++++++ tobiko/cmd/_main.py | 54 +++++++++++++++++++++++++++++++++++++ tobiko/cmd/_ping.py | 60 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+) create mode 100644 tobiko/cmd/__init__.py create mode 100644 tobiko/cmd/_main.py create mode 100644 tobiko/cmd/_ping.py diff --git a/setup.cfg b/setup.cfg index 8627dea0d..fd765230f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,8 +28,11 @@ packages = [entry_points] console_scripts = + tobiko = tobiko.cmd:main tobiko-fixture = tobiko.cmd.fixture:main tobiko-keystone-credentials = tobiko.openstack.keystone._credentials:print_credentials +tobiko.cli_commands = + ping = tobiko.cmd:TobikoPing oslo.config.opts = tobiko = tobiko.config:list_tobiko_options diff --git a/tobiko/cmd/__init__.py b/tobiko/cmd/__init__.py new file mode 100644 index 000000000..b17ca89b0 --- /dev/null +++ b/tobiko/cmd/__init__.py @@ -0,0 +1,27 @@ +# Copyright (c) 2024 Red Hat, Inc. +# +# All Rights Reserved. +# +# 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. +from __future__ import absolute_import + +import sys + +from tobiko.cmd import _ping +from tobiko.cmd import _main + +main = _main.main +TobikoPing = _ping.TobikoPing + +if __name__ == '__main__': + sys.exit(main(sys.argv[1:])) diff --git a/tobiko/cmd/_main.py b/tobiko/cmd/_main.py new file mode 100644 index 000000000..61791edaf --- /dev/null +++ b/tobiko/cmd/_main.py @@ -0,0 +1,54 @@ +# Copyright (c) 2024 Red Hat, Inc. +# +# All Rights Reserved. +# +# 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. + +from __future__ import absolute_import + +import sys + +from cliff import app +from cliff import commandmanager +from oslo_log import log as logging +from pbr import version + + +class Main(app.App): + + log = logging.getLogger(__name__) + + def __init__(self): + super(Main, self).__init__( + description='Tobiko CLI application', + version=version.VersionInfo('tobiko').version_string_with_vcs(), + command_manager=commandmanager.CommandManager( + 'tobiko.cli_commands'), + deferred_help=True, + ) + + def initialize_app(self, argv): + self.log.debug('tobiko initialize_app') + + def prepare_to_run_command(self, cmd): + self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__) + + def clean_up(self, cmd, result, err): + self.log.debug('tobiko clean_up %s', cmd.__class__.__name__) + if err: + self.log.debug('tobiko got an error: %s', err) + + +def main(argv=sys.argv[1:]): + the_app = Main() + return the_app.run(argv) diff --git a/tobiko/cmd/_ping.py b/tobiko/cmd/_ping.py new file mode 100644 index 000000000..184a45e3b --- /dev/null +++ b/tobiko/cmd/_ping.py @@ -0,0 +1,60 @@ +# Copyright (c) 2024 Red Hat, Inc. +# +# All Rights Reserved. +# +# 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. + +from __future__ import absolute_import + +import sys + +from cliff import command +from oslo_log import log as logging + +from tobiko.shell import ping + +LOG = logging.getLogger(__name__) + + +class TobikoPing(command.Command): + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument( + 'server', + help='Address of the server to ping' + ) + parser.add_argument( + '--result-file', + default='tobiko_ping_results', + help='Name of the directory where ping log files are ' + 'stored.' + ) + return parser + + def take_action(self, parsed_args): + error_code = 0 + try: + LOG.debug("Starting ping server: %s", parsed_args.server) + ping.write_ping_to_file(ping_ip=parsed_args.server, + output_dir=parsed_args.result_file) + LOG.debug("Finished ping server: %s", parsed_args.server) + except Exception as e: + if hasattr(e, 'errno'): + error_code = e.errno + else: + error_code = 1 + LOG.error("Failed to ping server '%s'. Error: %s", + parsed_args.server, e) + if error_code: + sys.exit(error_code)