availcheck.cli

  1# -*- coding: utf-8 -*-
  2# Copyright (C) 2024-2025 TU Dresden
  3# ralf.klammer@tu-dresden.de
  4
  5import click
  6import logging
  7
  8from json import dumps
  9
 10from .checker import (
 11    CheckerOneStop4All,
 12    CheckerEduTrain,
 13    CheckerSupportApps,
 14    CheckerKnowledgeHub,
 15    CheckerWebApps,
 16)
 17from .configs import CheckerConfigs
 18from .util import cli_startup
 19
 20log = logging.getLogger(__name__)
 21
 22
 23@click.group()
 24@click.option("--debug/--no-debug", "-d", is_flag=True, default=False)
 25@click.pass_context
 26def main(ctx, debug):
 27    cli_startup(log_level=debug and logging.DEBUG or logging.INFO)
 28    ctx.ensure_object(dict)
 29    ctx.obj["DEBUG"] = debug
 30
 31
 32@main.command()
 33@click.pass_context
 34@click.option("--path", default="/tmp", help="Path to save the results.")
 35def save_all(ctx, path, **kw):
 36
 37    results = {
 38        "OneStop4All - Prod": CheckerOneStop4All(instance="prod").save_checks(
 39            path
 40        ),
 41        "OneStop4All - Test": CheckerOneStop4All(instance="test").save_checks(
 42            path
 43        ),
 44        "EduTrain - Prod": CheckerEduTrain(instance="prod").save_checks(path),
 45        "EduTrain - Test": CheckerEduTrain(instance="test").save_checks(path),
 46        "JupyterHub - Prod": CheckerSupportApps(instance="prod").save_checks(
 47            path
 48        ),
 49    }
 50
 51    filename = f"{path.rstrip('/')}/collection.json"
 52    with open(filename, "w") as f:
 53        f.write(dumps(results))
 54
 55
 56@main.command()
 57@click.pass_context
 58def onestop4all(ctx, **kw):
 59
 60    CheckerOneStop4All(instance="prod").run_checks()
 61
 62    CheckerOneStop4All(instance="test").run_checks()
 63
 64
 65@main.command()
 66@click.pass_context
 67def develop(ctx, **kw):
 68
 69    r = CheckerOneStop4All(instance="prod").solr()
 70    print(r)
 71
 72
 73@main.command()
 74@click.pass_context
 75def edutrain(ctx, **kw):
 76
 77    CheckerEduTrain(instance="prod").run_checks()
 78
 79    CheckerEduTrain(instance="test").run_checks()
 80
 81
 82@main.command()
 83@click.pass_context
 84def supportapps(ctx, **kw):
 85
 86    CheckerSupportApps(instance="prod").run_checks()
 87
 88
 89@main.command()
 90@click.pass_context
 91def knowledgehub(ctx, **kw):
 92
 93    CheckerKnowledgeHub(instance="prod").run_checks()
 94
 95    CheckerKnowledgeHub(instance="test").run_checks()
 96
 97
 98@main.command()
 99@click.pass_context
100def webapps(ctx, **kw):
101
102    CheckerWebApps(instance="prod").run_checks()
103
104
105@click.group()
106@click.option("--debug/--no-debug", "-d", is_flag=True, default=False)
107@click.pass_context
108@click.option(
109    "-i",
110    "--instance",
111    default="prod",
112    show_default=True,
113    type=click.Choice(["prod", "test"], case_sensitive=False),
114    required=True,
115    help="Specify which instance to check.",
116)
117def checkmk(ctx, debug, instance):
118    """
119    Main group for all Checkmk related functions.
120    """
121    cli_startup(log_level=debug and logging.DEBUG or logging.INFO)
122    ctx.ensure_object(dict)
123    ctx.obj["DEBUG"] = debug
124    ctx.obj["INSTANCE"] = instance
125
126
127def create_check_command(name, checker_class):
128    """
129    Creates a new command for the Checkmk CLI tool.
130
131    This function dynamically generates a Checkmk command based on the provided
132    name and checker class. The generated command allows users to specify a
133    particular check to run or to run all available checks if no specific check
134    is provided.
135
136    Args:
137        name (str): The name of the command to create.
138        checker_class (type): The class responsible for performing the checks.
139
140    Returns:
141        function: The generated Checkmk command function.
142    """
143
144    @checkmk.command(name=name)
145    @click.pass_context
146    @click.option(
147        "-c",
148        "--check",
149        type=click.Choice(
150            CheckerConfigs(name).get_checks(as_list=True),
151            case_sensitive=False,
152        ),
153        help="Specify which check to run - do NOT SET to run all ALL checks.",
154    )
155    def check_command(ctx, check, **kw):
156        if check is not None:
157            click.echo(
158                checker_class(instance=ctx.obj["INSTANCE"]).run_check(check)
159            )
160        else:
161            for c in checker_class(instance=ctx.obj["INSTANCE"]).run_checks():
162                click.echo(c)
163
164
165create_check_command("onestop4all", CheckerOneStop4All)
166create_check_command("edutrain", CheckerEduTrain)
167create_check_command("supportapps", CheckerSupportApps)
168create_check_command("knowledgehub", CheckerKnowledgeHub)
169create_check_command("webapps", CheckerWebApps)
170
171
172@checkmk.command()
173@click.pass_context
174def all(ctx, **kw):
175    for checker_class in [
176        CheckerOneStop4All,
177        CheckerEduTrain,
178        CheckerSupportApps,
179        CheckerKnowledgeHub,
180        CheckerWebApps,
181    ]:
182        for c in checker_class(instance=ctx.obj["INSTANCE"]).run_checks():
183            click.echo(c)
184
185
186if __name__ == "__main__":
187    main(obj={})
log = <Logger availcheck.cli (WARNING)>
checkmk = <Group checkmk>

Main group for all Checkmk related functions.

def create_check_command(name, checker_class):
128def create_check_command(name, checker_class):
129    """
130    Creates a new command for the Checkmk CLI tool.
131
132    This function dynamically generates a Checkmk command based on the provided
133    name and checker class. The generated command allows users to specify a
134    particular check to run or to run all available checks if no specific check
135    is provided.
136
137    Args:
138        name (str): The name of the command to create.
139        checker_class (type): The class responsible for performing the checks.
140
141    Returns:
142        function: The generated Checkmk command function.
143    """
144
145    @checkmk.command(name=name)
146    @click.pass_context
147    @click.option(
148        "-c",
149        "--check",
150        type=click.Choice(
151            CheckerConfigs(name).get_checks(as_list=True),
152            case_sensitive=False,
153        ),
154        help="Specify which check to run - do NOT SET to run all ALL checks.",
155    )
156    def check_command(ctx, check, **kw):
157        if check is not None:
158            click.echo(
159                checker_class(instance=ctx.obj["INSTANCE"]).run_check(check)
160            )
161        else:
162            for c in checker_class(instance=ctx.obj["INSTANCE"]).run_checks():
163                click.echo(c)

Creates a new command for the Checkmk CLI tool.

This function dynamically generates a Checkmk command based on the provided name and checker class. The generated command allows users to specify a particular check to run or to run all available checks if no specific check is provided.

Args: name (str): The name of the command to create. checker_class (type): The class responsible for performing the checks.

Returns: function: The generated Checkmk command function.