Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONIC CLI for CLI-Sessions feature #3175

Merged
merged 5 commits into from
Sep 16, 2024
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
66 changes: 66 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7750,5 +7750,71 @@ def notice(db, category_list, max_events, namespace):
handle_asic_sdk_health_suppress(db, 'notice', category_list, max_events, namespace)


#
# 'serial_console' group ('config serial_console')
#
@config.group(cls=clicommon.AbbreviationGroup, name='serial_console')
def serial_console():
"""Configuring system serial-console behavior"""
pass


@serial_console.command('sysrq-capabilities')
@click.argument('sysrq_capabilities', metavar='<enabled|disabled>', required=True,
type=click.Choice(['enabled', 'disabled']))
def sysrq_capabilities(sysrq_capabilities):
"""Set serial console sysrq-capabilities state"""

config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry("SERIAL_CONSOLE", 'POLICIES',
{'sysrq_capabilities': sysrq_capabilities})


@serial_console.command('inactivity-timeout')
@click.argument('inactivity_timeout', metavar='<timeout>', required=True,
type=click.IntRange(0, 35000))
def inactivity_timeout_serial(inactivity_timeout):
"""Set serial console inactivity timeout"""

config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry("SERIAL_CONSOLE", 'POLICIES',
{'inactivity_timeout': inactivity_timeout})


#
# 'ssh' group ('config ssh')
#
@config.group(cls=clicommon.AbbreviationGroup, name='ssh')
def ssh():
"""Configuring system ssh behavior"""
pass


@ssh.command('inactivity-timeout')
@click.argument('inactivity_timeout', metavar='<timeout>', required=True,
type=click.IntRange(0, 35000))
def inactivity_timeout_ssh(inactivity_timeout):
"""Set ssh inactivity timeout"""

config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry("SSH_SERVER", 'POLICIES',
{'inactivity_timeout': inactivity_timeout})


@ssh.command('max-sessions')
@click.argument('max-sessions', metavar='<max-sessions>', required=True,
type=click.IntRange(0, 100))
def max_sessions(max_sessions):
"""Set max number of concurrent logins"""

config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry("SSH_SERVER", 'POLICIES',
{'max_sessions': max_sessions})


if __name__ == '__main__':
config()
40 changes: 40 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,46 @@ def received(db, namespace):
ctx.fail("ASIC/SDK health event is not supported on the platform")


#
# 'serial_console' command group ("show serial_console ...")
#
@cli.group('serial_console', invoke_without_command=True)
@clicommon.pass_db
def serial_console(db):
"""Show serial_console configuration"""

serial_console_table = db.cfgdb.get_entry('SERIAL_CONSOLE', 'POLICIES')

hdrs = ['inactivity-timeout', 'sysrq-capabilities']
data = []

data.append(serial_console_table.get('inactivity_timeout', '900 <default>'))
data.append(serial_console_table.get('sysrq_capabilities', 'disabled <default>'))

configuration = [data]
click.echo(tabulate(configuration, headers=hdrs, tablefmt='simple', missingval=''))


#
# 'ssh' command group ("show ssh ...")
#
@cli.group('ssh', invoke_without_command=True)
@clicommon.pass_db
def ssh(db):
"""Show ssh configuration"""

serial_console_table = db.cfgdb.get_entry('SSH_SERVER', 'POLICIES')

hdrs = ['inactivity-timeout', 'max-sessions']
data = []

data.append(serial_console_table.get('inactivity_timeout', '900 <default>'))
data.append(serial_console_table.get('max_session', '0 <default>'))

configuration = [data]
click.echo(tabulate(configuration, headers=hdrs, tablefmt='simple', missingval=''))


# Load plugins and register them
helper = util_base.UtilHelper()
helper.load_and_register_plugins(plugins, cli)
Expand Down
32 changes: 32 additions & 0 deletions tests/cli_sessions_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from click.testing import CliRunner

import config.main as config
import show.main as show
from utilities_common.db import Db


class TestCliSessionsCommands:
def test_config_command(self):
runner = CliRunner()

db = Db()

result = runner.invoke(config.config.commands['serial_console'].commands['sysrq-capabilities'],
['enabled'], obj=db)
assert result.exit_code == 0

result = runner.invoke(config.config.commands['serial_console'].commands['inactivity-timeout'],
['180'], obj=db)
assert result.exit_code == 0

result = runner.invoke(show.cli.commands['serial_console'], obj=db)
assert result.exit_code == 0

result = runner.invoke(config.config.commands['ssh'].commands['inactivity-timeout'], ['190'], obj=db)
assert result.exit_code == 0

result = runner.invoke(config.config.commands['ssh'].commands['max-sessions'], ['60'], obj=db)
assert result.exit_code == 0

result = runner.invoke(show.cli.commands['ssh'], obj=db)
assert result.exit_code == 0
Loading