Skip to content

Add sonic-clear logging command #3151

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
23 changes: 23 additions & 0 deletions clear/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess
import sys
import click
import glob
import utilities_common.cli as clicommon
import utilities_common.multi_asic as multi_asic_util
from sonic_py_common.general import getstatusoutput_noshell_pipe
Expand Down Expand Up @@ -545,6 +546,28 @@ def route(prefix, vrf, namespace):
command += ['-n', str(namespace)]
clicommon.run_command(command)

@click.option('--all', '-a', is_flag=True, help='Delete also compressed logs')
@cli.command()
def logging(all):
"""Clear logging files"""
log_path_arr = ["/var/log"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put a check here to ensure /var/log directory exists before setting log_path_arr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if os.path.exists("/var/log.tmpfs"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not plan to delete existing logs in /var/log/ if both the directories /var/log/ and /var/log.tmpfs/ have syslogs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks.

log_path_arr += ["/var/log.tmpfs"]

files_to_delete=[]
for log_path in log_path_arr:
if all:
files_to_delete += glob.glob(f"{log_path}/syslog*")
else:
if os.path.isfile(f"{log_path}/syslog"):
files_to_delete += [f"{log_path}/syslog"]
if os.path.isfile(f"{log_path}/syslog.1"):
files_to_delete += [f"{log_path}/syslog.1"]

for f in files_to_delete:
cmd = ['sudo', 'rm','-f',f]
run_command(cmd)


# Load plugins and register them
helper = util_base.UtilHelper()
Expand Down
26 changes: 26 additions & 0 deletions tests/clear_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import click
import pytest
import glob
import clear.main as clear
from click.testing import CliRunner
from unittest.mock import patch, MagicMock
from unittest import mock

class TestClear(object):
def setup(self):
Expand Down Expand Up @@ -319,3 +321,27 @@ def test_flowcnt_route_route(self, mock_run_command):
def teardown(self):
print('TEAR DOWN')

class TestClearLogging(object):
def setup(self):
print('SETUP')

@patch('clear.main.run_command')
@patch("glob.glob", MagicMock(return_value=['abc']))
def test_logging_all(self, mock_run_command):
runner = CliRunner()
result = runner.invoke(clear.cli.commands['logging'], ['--all'])
assert result.exit_code == 0
mock_run_command.assert_called_with(['sudo', 'rm' ,'-f', 'abc'])

@patch('clear.main.run_command')
@patch("os.path.isfile",MagicMock(return_value=True))
def test_logging(self, mock_run_command):
runner = CliRunner()
result = runner.invoke(clear.cli.commands['logging'])
assert result.exit_code == 0
mock_run_command.assert_has_calls([mock.call(['sudo', 'rm' ,'-f', '/var/log/syslog']),
mock.call(['sudo', 'rm' ,'-f', '/var/log/syslog.1'])],
any_order=True)

def teardown(self):
print('TEAR DOWN')