Skip to content

Commit

Permalink
show commands for SYSTEM READY
Browse files Browse the repository at this point in the history
  • Loading branch information
sg893052 committed Nov 5, 2021
1 parent 5bdf270 commit 5b9ff62
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 5b9ff62

Please sign in to comment.