Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 46 additions & 51 deletions leapp/reporting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ class Report(Model):
report = fields.JSON()


def _add_to_dict(data, path, value, leaf_list=False):
def _add_to_dict(data, path, value, leaf_list=False, flat_list=False):
for p in path[:-1]:
if p not in data:
data[p] = {}
data = data[p]

if leaf_list:
data[path[-1]] = data.get(path[-1], []) + [value]
list_val = [value]
if flat_list and isinstance(value, list):
list_val = value
data[path[-1]] = data.get(path[-1], []) + list_val
else:
if path[-1] in data:
raise ValueError('Path {} is already taken'.format('.'.join(path)))
Expand Down Expand Up @@ -74,7 +77,7 @@ class BaseListPrimitive(BasePrimitive):

def apply(self, report):
"""Add report entry to the report dict based on provided path"""
_add_to_dict(report, self.path, self.value, leaf_list=True)
_add_to_dict(report, self.path, self.value, leaf_list=True, flat_list=True)


class Title(BasePrimitive):
Expand Down Expand Up @@ -116,19 +119,55 @@ def __init__(self, value=None):
self._value = value


class Flags(BasePrimitive):
"""Report flags"""
name = 'flags'
class Groups(BaseListPrimitive):
"""Report groups."""
name = 'groups'

INHIBITOR = 'inhibitor'
FAILURE = 'failure'
ACCESSIBILITY = 'accessibility'
AUTHENTICATION = 'authentication'
BOOT = 'boot'
COMMUNICATION = 'communication'
DESKTOP = 'desktop environment'
DRIVERS = 'drivers'
EMAIL = 'email'
ENCRYPTION = 'encryption'
FILESYSTEM = 'filesystem'
FIREWALL = 'firewall'
HIGH_AVAILABILITY = 'high availability'
KERNEL = 'kernel'
MONITORING = 'monitoring'
NETWORK = 'network'
OS_FACTS = 'OS facts'
POST = 'post'
PUBLIC_CLOUD = 'public cloud'
PYTHON = 'python'
REPOSITORY = 'repository'
RHUI = 'rhui'
SANITY = 'sanity'
SECURITY = 'security'
SELINUX = 'selinux'
SERVICES = 'services'
TIME_MANAGEMENT = 'time management'
TOOLS = 'tools'
UPGRADE_PROCESS = 'upgrade process'

def __init__(self, value=None):
if not isinstance(value, list):
raise TypeError('Value of "Flags" must be a list')
raise TypeError('Value of "Groups" must be a list')
self._value = value


# To gradually switch from using Tags and Flags to using Groups make Tags and Flags aliases of Groups
Tags = Groups
Flags = Groups

# To allow backwards-compatibility with previous report-schema
# Groups that match _DEPRECATION_FLAGS will be shown as flags, the rest as tags
_DEPRECATION_FLAGS = [Groups.INHIBITOR, Groups.FAILURE]


class Key(BasePrimitive):
"""Stable identifier for report entries."""
name = 'key'
Expand All @@ -141,50 +180,6 @@ def __init__(self, uuid):
self._value = uuid


class Tags(BasePrimitive):
"""Report tags"""
name = 'tags'

class _Value(object):
def __init__(self, value):
self.value = value

ACCESSIBILITY = _Value('accessibility')
AUTHENTICATION = _Value('authentication')
BOOT = _Value('boot')
COMMUNICATION = _Value('communication')
DESKTOP = _Value('desktop environment')
DRIVERS = _Value('drivers')
EMAIL = _Value('email')
ENCRYPTION = _Value('encryption')
FILESYSTEM = _Value('filesystem')
FIREWALL = _Value('firewall')
HIGH_AVAILABILITY = _Value('high availability')
KERNEL = _Value('kernel')
MONITORING = _Value('monitoring')
NETWORK = _Value('network')
OS_FACTS = _Value('OS facts')
PUBLIC_CLOUD = _Value('public cloud')
PYTHON = _Value('python')
REPOSITORY = _Value('repository')
RHUI = _Value('rhui')
SANITY = _Value('sanity')
SECURITY = _Value('security')
SELINUX = _Value('selinux')
SERVICES = _Value('services')
TIME_MANAGEMENT = _Value('time management')
TOOLS = _Value('tools')
UPGRADE_PROCESS = _Value('upgrade process')

def __init__(self, value=None):
if not isinstance(value, list):
raise TypeError('Value of "Tags" must be a list')
if not all(isinstance(v, Tags._Value) for v in value):
raise TypeError('Unsupported tag value passed for Report Tags.')
# after the objects validation we need the actual values in the list
self._value = [v.value for v in value]


class ExternalLink(BaseListPrimitive):
"""External link report detail field"""
name = 'external'
Expand Down
5 changes: 2 additions & 3 deletions leapp/utils/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ def print_error(error):

def report_inhibitors(context_id):
# The following imports are required to be here to avoid import loop problems
from leapp.reporting import Flags # pylint: disable=import-outside-toplevel
from leapp.utils.report import fetch_upgrade_report_messages # pylint: disable=import-outside-toplevel
from leapp.utils.report import fetch_upgrade_report_messages, is_inhibitor # noqa; pylint: disable=import-outside-toplevel
reports = fetch_upgrade_report_messages(context_id)
inhibitors = [report for report in reports if Flags.INHIBITOR in report.get('flags', [])]
inhibitors = [report for report in reports if is_inhibitor(report)]
if inhibitors:
text = 'UPGRADE INHIBITED'
with pretty_block(text=text, end_text=text, color=Color.red, target=sys.stdout):
Expand Down
32 changes: 28 additions & 4 deletions leapp/utils/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
import json
import os

from leapp.reporting import Remediation, Severity, create_report_from_error, create_report_from_deprecation
from leapp.reporting import (
_DEPRECATION_FLAGS,
Groups,
Remediation,
Severity,
create_report_from_error,
create_report_from_deprecation
)
from leapp.utils.audit import get_messages, get_audit_entry


Expand Down Expand Up @@ -83,8 +90,18 @@ def fetch_upgrade_report_messages(context_id):
}


def is_inhibitor(message):
"""
A helper function to check if a message is inhibiting upgrade.
It takes care of possible differences in report schema, like tags/flags -> groups merge in 1.2.0
"""
# NOTE(ivasilev) As groups and flags are mutual exclusive, let's not bother with report_schema version
# and just check both fields for inhibitor presence
return Groups.INHIBITOR in message.get('groups', message.get('flags', []))


def importance(message):
if 'inhibitor' in message.get('flags', []):
if is_inhibitor(message):
return 0
return SEVERITY_LEVELS.get(message['severity'], 99)

Expand All @@ -95,8 +112,8 @@ def generate_report_file(messages_to_report, context, path, report_schema='1.1.0
if path.endswith(".txt"):
with open(path, 'w') as f:
for message in sorted(messages_to_report, key=importance):
is_inhibitor = 'inhibitor' in message.get('flags', [])
f.write('Risk Factor: {}{}\n'.format(message['severity'], ' (inhibitor)' if is_inhibitor else ''))
f.write('Risk Factor: {}{}\n'.format(message['severity'],
' (inhibitor)' if is_inhibitor(message) else ''))
f.write('Title: {}\n'.format(message['title']))
f.write('Summary: {}\n'.format(message['summary']))
remediation = Remediation.from_dict(message.get('detail', {}))
Expand All @@ -115,4 +132,11 @@ def generate_report_file(messages_to_report, context, path, report_schema='1.1.0
messages_to_report = list(messages_to_report)
for m in messages_to_report:
m.pop('key')
if report_schema_tuple < (1, 2, 0):
# groups were introduced in 1.2.0, before that there was a tags/flags split
messages_to_report = list(messages_to_report)
for msg in messages_to_report:
groups = msg.pop('groups', [])
msg['flags'] = [g for g in groups if g in _DEPRECATION_FLAGS]
msg['tags'] = [g for g in groups if g not in _DEPRECATION_FLAGS]
json.dump({'entries': messages_to_report, 'leapp_run_id': context}, f, indent=2)
2 changes: 1 addition & 1 deletion packaging/leapp.spec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# This is kind of help for more flexible development of leapp repository,
# so people do not have to wait for new official release of leapp to ensure
# it is installed/used the compatible one.
%global framework_version 2.2
%global framework_version 3.0

# IMPORTANT: everytime the requirements are changed, increment number by one
# - same for Provides in deps subpackage
Expand Down
Loading