Skip to content

Commit

Permalink
deprecate the two script instead of removing them
Browse files Browse the repository at this point in the history
Signed-off-by: Xiangce Liu <[email protected]>
  • Loading branch information
xiangce committed Jan 10, 2025
1 parent 4cc2d5a commit 46e6395
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 0 deletions.
109 changes: 109 additions & 0 deletions insights/ocp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""
Top level OpenShift 4 component
===============================
The :py:func:`conf` component recognizes insights-operator and must-gather
archives.
"""
import logging
import os
import yaml
import warnings

from fnmatch import fnmatch
from insights.core.plugins import component, datasource
from insights.core.context import ExecutionContext, fs_root

from insights.core.archives import extract
from insights.parsr.query import from_dict, Result
from insights.util import content_type

log = logging.getLogger(__name__)

try:
# requires pyyaml installed after libyaml
Loader = yaml.CSafeLoader
except:
log.info("Couldn't find libyaml loader. Falling back to python loader.")
Loader = yaml.SafeLoader


@fs_root
class InsightsOperatorContext(ExecutionContext):
"""Recognizes insights-operator archives"""
marker = "config/featuregate"


@fs_root
class MustGatherContext(ExecutionContext):
"""Recognizes must-gather archives"""
marker = "cluster-scoped-resources"


contexts = [InsightsOperatorContext, MustGatherContext]


def _get_files(path):
for root, dirs, names in os.walk(path):
for name in names:
yield os.path.join(root, name)


def _load(path):
with open(path) as f:
for doc in yaml.load_all(f, Loader=Loader):
yield from_dict(doc, src=path)


def _process(path, excludes=None):
excludes = excludes if excludes is not None else []
for f in _get_files(path):
if excludes and any(fnmatch(f, e) for e in excludes):
continue
try:
for d in _load(f):
yield d
except Exception:
log.debug("Failed to load %s; skipping.", f)


def analyze(paths, excludes=None):
warnings.warn(
"This '{0}' is deprecated and will be removed in {1}.".format('ocp.analyze', '3.6.0'),
DeprecationWarning,
stacklevel=2,
)
if not isinstance(paths, list):
paths = [paths]

results = []
for path in paths:
if content_type.from_file(path) == "text/plain":
results.extend(_load(path))
elif os.path.isdir(path):
results.extend(_process(path, excludes))
else:
with extract(path) as ex:
results.extend(_process(ex.tmp_dir, excludes))

return Result(children=results)


@datasource(contexts)
def conf_root(broker):
for ctx in contexts:
if ctx in broker:
return broker[ctx].root


@component(conf_root)
def conf(root):
"""
The ``conf`` component parses all configuration in an insights-operator or
must-gather archive and returns an object that is part of the parsr common
data model. It can be navigated and queried in a standard way. See the
`tutorial`_ for details.
.. _tutorial: https://insights-core.readthedocs.io/en/latest/notebooks/Parsr%20Query%20Tutorial.html
"""
return analyze(root, excludes=["*.log"])
87 changes: 87 additions & 0 deletions insights/ocpshell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/env python
import argparse
import logging
import warnings

from insights.ocp import analyze


log = logging.getLogger(__name__)

banner = """
Openshift Configuration Explorer
Tutorial: https://github.com/RedHatInsights/insights-core/blob/master/docs/notebooks/Parsr%20Query%20Tutorial.ipynb
conf is the top level configuration. Use conf.get_keys() to see first level keys.
Available Predicates
lt, le, eq, gt, ge
isin, contains
startswith, endswith, matches
ieq, icontains, istartswith, iendswith
Available Operators
~ (not)
| (or)
& (and)
Example
api = conf.where("kind", "KubeAPIServer")
latest = api.status.latestAvailableRevision.value
api.status.nodeStatuses.where("currentRevision", ~eq(latest))
"""


def parse_args():
p = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
p.add_argument("archives", nargs="+", help="Archive or directory to analyze.")
p.add_argument("-D", "--debug", help="Verbose debug output.", action="store_true")
p.add_argument("--exclude", default="*.log", help="Glob patterns to exclude separated by commas")
return p.parse_args()


def parse_exclude(exc):
return [e.strip() for e in exc.split(",")]


def main():
warnings.warn(
"This '{0}' is deprecated and will be removed in {1}.".format('insights.ocpshell', '3.6.0'),
DeprecationWarning,
stacklevel=2,
)
args = parse_args()
archives = args.archives
if args.debug:
logging.basicConfig(level=logging.DEBUG)

excludes = parse_exclude(args.exclude) if args.exclude else ["*.log"]

conf = analyze(archives, excludes) # noqa F841 / unused var

# import all the built-in predicates
from insights.parsr.query import (lt, le, eq, gt, ge, isin, contains, # noqa: F401,F403
startswith, endswith, ieq, icontains, istartswith, iendswith,
matches, make_child_query)
q = make_child_query

import IPython
from traitlets.config.loader import Config

ns = dict(locals())
ns["analyze"] = analyze
ns["ALL"] = None
ns["ANY"] = None

IPython.core.completer.Completer.use_jedi = False
c = Config()
c.TerminalInteractiveShell.banner1 = banner
IPython.start_ipython([], user_ns=ns, config=c)


if __name__ == "__main__":
main()

0 comments on commit 46e6395

Please sign in to comment.