From 5b9ff6278916d48b15eeee813de28ac9997b2b4e Mon Sep 17 00:00:00 2001 From: Senthil Kumar Guruswamy Date: Fri, 1 Oct 2021 10:48:03 -0700 Subject: [PATCH] show commands for SYSTEM READY --- show/main.py | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/show/main.py b/show/main.py index f4998218f2..ab65e32415 100755 --- a/show/main.py +++ b/show/main.py @@ -16,6 +16,7 @@ from utilities_common.db import Db import utilities_common.constants as constants from utilities_common.general import load_db_config +import socket # mock the redis for unit test purposes # try: @@ -66,6 +67,19 @@ GEARBOX_TABLE_PHY_PATTERN = r"_GEARBOX_TABLE:phy:*" + +class Dict2Obj(object): + """dict to dict2obj + d: data""" + def __init__(self, d): + for a, b in list(d.items()): + if isinstance(b, (list, tuple)): + setattr(self, a, [Dict2Obj(x) if isinstance( + x, dict) else x for x in b]) + else: + setattr(self, a, Dict2Obj(b) if isinstance(b, dict) else b) + + # To be enhanced. Routing-stack information should be collected from a global # location (configdb?), so that we prevent the continous execution of this # bash oneliner. To be revisited once routing-stack info is tracked somewhere. @@ -1689,6 +1703,91 @@ def ztp(status, verbose): cmd = cmd + " --verbose" run_command(cmd, display_cmd=verbose) + +@cli.group('system', invoke_without_command=False) +def system(): + """ Show system status""" + pass + +@system.group('status',invoke_without_command=False) +@click.pass_context +def system_st(ctx): + """ Shows the system ready status""" + pass + +@system_st.command('core') +@click.option('-q', '--quiet', is_flag=True, help="quiet mode") +def system_status_core(quiet): + """ Shows the core system ready status""" + sys_core_status = "Down" + try: + response = send_data("status_core", None, quiet) + click.echo("{}\n".format(response.status)) + click.echo("{}".format(response.coresrvs)) + except Exception as e: + click.echo("Exception: {}--\n {}".format(str(e),sys_core_status)) + +@system_st.command('all') +@click.option('-q', '--quiet', is_flag=True, help="quiet mode") +@click.option('-b', '--brief', is_flag=True, help="brief display") +@click.option('-d', '--detail', is_flag=True, help="detailed display") +def system_status_all(quiet,brief,detail): + """Shows the system ready status of all the services""" + sys_all_status = "Down" + try: + response = send_data("status_all", None, quiet) + + if brief: + click.echo("{}\n".format(response.status)) + elif detail: + click.echo("{}\n".format(response.status)) + click.echo("{}".format(response.allsrvs)) + if 'System is not ready' in response.status: + click.echo("Status(systemctl) of Failed services:") + cmd = "systemctl status --no-pager {}".format(response.dnsrvs_name) + click.echo(run_command(cmd, return_cmd=True)) + else: + click.echo("{}\n".format(response.status)) + click.echo("{}".format(response.allsrvs)) + + except Exception as e: + click.echo("Exception: {}--\n {}".format(str(e),sys_all_status)) + +def send_data(command, data, quiet=False ): + + SERVER_ADDRESS = '/var/run/sysready.socket' + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + try: + sock.connect(SERVER_ADDRESS) + except socket.error as msg: + raise click.Abort(str(msg)) + return #lgtm [py/unreachable-statement] + response={} + try: + request = {"command":command, "data":data} + sock.sendall(json.dumps(request).encode('utf-8')) + res=sock.recv(10240).decode('utf-8') + if res == '': + sock.close() + raise click.Abort("No response") + + jdata=json.loads(res) + response = Dict2Obj(jdata) + if response.status == False : + sock.close() + raise click.Abort(response.msg) + except Exception as e: + if quiet: + sock.close() + raise click.Abort(str(e)) + + click.echo("{}".format(str(e))) + sock.close() + sys.exit(1) + + sock.close() + return response + # Load plugins and register them helper = util_base.UtilHelper() for plugin in helper.load_plugins(plugins):