Merge "Enable JSON results output"

This commit is contained in:
Zuul 2019-06-25 16:37:59 +00:00 committed by Gerrit Code Review
commit d77fbb2111
3 changed files with 48 additions and 17 deletions

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import json
import sys import sys
import textwrap import textwrap
import traceback import traceback
@ -24,6 +25,8 @@ import six
from oslo_upgradecheck._i18n import _ from oslo_upgradecheck._i18n import _
CONF = None
class Code(enum.IntEnum): class Code(enum.IntEnum):
"""Status codes for the upgrade check command""" """Status codes for the upgrade check command"""
@ -92,6 +95,7 @@ class UpgradeCommands(object):
:returns: Code :returns: Code
""" """
global CONF
return_code = Code.SUCCESS return_code = Code.SUCCESS
# This is a list if 2-item tuples for the check name and it's results. # This is a list if 2-item tuples for the check name and it's results.
check_results = [] check_results = []
@ -121,22 +125,36 @@ class UpgradeCommands(object):
# NOTE(bnemec): We use six.text_type on the translated string to # NOTE(bnemec): We use six.text_type on the translated string to
# force immediate translation if lazy translation is in use. # force immediate translation if lazy translation is in use.
# See lp1801761 for details. # See lp1801761 for details.
t = prettytable.PrettyTable([six.text_type(self.display_title)],
hrules=prettytable.ALL) # Since registering opts can be overridden by consuming code, we can't
t.align = 'l' # assume that our locally defined option exists.
for name, result in check_results: if (hasattr(CONF, 'command') and hasattr(CONF.command, 'json') and
cell = ( CONF.command.json):
_('Check: %(name)s\n' output = {'name': six.text_type(self.display_title), 'checks': []}
'Result: %(result)s\n' for name, result in check_results:
'Details: %(details)s') % output['checks'].append(
{ {'check': name,
'name': name, 'result': result.code,
'result': UPGRADE_CHECK_MSG_MAP[result.code], 'details': result.details}
'details': self._get_details(result), )
} print(json.dumps(output))
) else:
t.add_row([cell]) t = prettytable.PrettyTable([six.text_type(self.display_title)],
print(t) hrules=prettytable.ALL)
t.align = 'l'
for name, result in check_results:
cell = (
_('Check: %(name)s\n'
'Result: %(result)s\n'
'Details: %(details)s') %
{
'name': name,
'result': UPGRADE_CHECK_MSG_MAP[result.code],
'details': self._get_details(result),
}
)
t.add_row([cell])
print(t)
return return_code return return_code
@ -154,6 +172,11 @@ def register_cli_options(conf, upgrade_command):
upgrade_action = subparsers.add_parser('upgrade') upgrade_action = subparsers.add_parser('upgrade')
upgrade_action.add_argument('check') upgrade_action.add_argument('check')
upgrade_action.set_defaults(action_fn=upgrade_command.check) upgrade_action.set_defaults(action_fn=upgrade_command.check)
upgrade_action.add_argument(
'--json',
action='store_true',
help='Output the results in JSON format. Default is to print '
'results in human readable table format.')
opt = cfg.SubCommandOpt('command', handler=add_parsers) opt = cfg.SubCommandOpt('command', handler=add_parsers)
conf.register_cli_opt(opt) conf.register_cli_opt(opt)
@ -194,6 +217,7 @@ def main(conf, project, upgrade_command,
the search behavior in oslo.config. the search behavior in oslo.config.
""" """
global CONF
register_cli_options(conf, upgrade_command) register_cli_options(conf, upgrade_command)
conf( conf(
@ -201,5 +225,5 @@ def main(conf, project, upgrade_command,
project=project, project=project,
default_config_files=default_config_files, default_config_files=default_config_files,
) )
CONF = conf
return run(conf) return run(conf)

View File

@ -0,0 +1,7 @@
---
features:
- |
oslo.upgradecheck now supports the command line flag ``--json`` to have
the upgrade check results output in a compact, machine readable JSON
format. The default output without this flag remains a human readable
table of the results.