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

Unit Tests for Kdump Remote Mode. #3453

Draft
wants to merge 56 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
8e1a4b4
kdump-remote-configurations
Ghulam-Bahoo Jul 4, 2024
3893fc9
Corrected Functions
Ghulam-Bahoo Jul 10, 2024
03c2b63
Corrected Functions
Ghulam-Bahoo Jul 10, 2024
6f34f88
Corrected Functions
Ghulam-Bahoo Jul 10, 2024
18caaba
Corrected Functions
Ghulam-Bahoo Jul 10, 2024
7d7226c
Corrected Functions
Ghulam-Bahoo Jul 10, 2024
49b511a
Corrected Functions
Ghulam-Bahoo Jul 10, 2024
a67a777
Corrected Functions
Ghulam-Bahoo Jul 11, 2024
2094bc7
Corrected Functions
Ghulam-Bahoo Jul 11, 2024
7ae0635
Corrected Functions
Ghulam-Bahoo Jul 11, 2024
518837e
Corrected Function Names
Ghulam-Bahoo Jul 11, 2024
d74f909
Corrected Function Names
Ghulam-Bahoo Jul 11, 2024
17e0790
Corrected Function Names
Ghulam-Bahoo Jul 11, 2024
999d298
Corrected Function Names
Ghulam-Bahoo Jul 11, 2024
361bb75
Corrected Function Names
Ghulam-Bahoo Jul 12, 2024
48eb48d
Corrected Function Names
Ghulam-Bahoo Jul 12, 2024
194f333
Corrected Indentation
Ghulam-Bahoo Jul 12, 2024
45fe33e
Corrected Indentation
Ghulam-Bahoo Jul 12, 2024
8c97669
Corrected Functions
Ghulam-Bahoo Jul 12, 2024
2453d25
Corrected Functions
Ghulam-Bahoo Jul 19, 2024
cfbdd13
Corrected Functions
Ghulam-Bahoo Jul 19, 2024
1b7091b
Corrected Functions
Ghulam-Bahoo Jul 19, 2024
82f8000
Corrected Functions
Ghulam-Bahoo Jul 19, 2024
7efce5d
Corrected Functions
Ghulam-Bahoo Jul 19, 2024
12f0279
Corrected Functions
Ghulam-Bahoo Jul 19, 2024
3c22c75
Corrected Functions
Ghulam-Bahoo Jul 19, 2024
6d2f7ce
Corrected Functions
Ghulam-Bahoo Jul 22, 2024
f4856c7
Corrected Functions
Ghulam-Bahoo Jul 22, 2024
2d0cacb
Corrected Functions
Ghulam-Bahoo Jul 22, 2024
c1f2076
Corrected Functions
Ghulam-Bahoo Jul 22, 2024
d3e975a
Corrected Functions
Ghulam-Bahoo Jul 22, 2024
18895d3
Corrected Functions
Ghulam-Bahoo Jul 22, 2024
4fe15b1
Corrected Functions
Ghulam-Bahoo Jul 23, 2024
8588324
Corrected Functions
Ghulam-Bahoo Jul 23, 2024
65a07b0
Corrected Functions
Ghulam-Bahoo Jul 23, 2024
247cb24
Corrected Functions
Ghulam-Bahoo Jul 23, 2024
edd60cf
Corrected Functions
Ghulam-Bahoo Jul 23, 2024
c9d4b83
Corrected Functions
Ghulam-Bahoo Jul 23, 2024
146c2c8
Merge branch 'sonic-net:master' into kdump_ssh_Enhancement
muhammadalihussnain Jul 24, 2024
668a357
Before Disabling Remove Key and Path
muhammadalihussnain Jul 24, 2024
16927d0
b4 disable remove key and path
muhammadalihussnain Jul 24, 2024
301b953
resolved db.mode_entry issue
muhammadalihussnain Jul 24, 2024
d32c898
removed hash from Remove function
muhammadalihussnain Jul 24, 2024
26530b9
erased space line 142 kdump.py
muhammadalihussnain Jul 25, 2024
fb3c04c
Corrected Functions
Ghulam-Bahoo Jul 25, 2024
557d2da
Corrected Functions
Ghulam-Bahoo Jul 25, 2024
88f72c5
Corrected Functions
Ghulam-Bahoo Jul 25, 2024
8d20e9c
Unit Tests
Ghulam-Bahoo Jul 25, 2024
2f1028d
Unit Tests
Ghulam-Bahoo Jul 26, 2024
2d9dcfb
Unit Tests
Ghulam-Bahoo Jul 26, 2024
92da9b9
Unit Tests
Ghulam-Bahoo Jul 26, 2024
61eeb30
Unit Tests
Ghulam-Bahoo Jul 30, 2024
9814455
Unit Tests
Ghulam-Bahoo Jul 30, 2024
b5429be
Unit Tests
Ghulam-Bahoo Jul 30, 2024
3c62ae0
Unit Tests
Ghulam-Bahoo Jul 30, 2024
1be5cf7
Unit Tests
Ghulam-Bahoo Jul 30, 2024
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
214 changes: 200 additions & 14 deletions config/kdump.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sys

import click
from utilities_common.cli import AbbreviationGroup, pass_db

import re

from utilities_common.cli import AbbreviationGroup, pass_db
from pathlib import Path
#
# 'kdump' group ('sudo config kdump ...')
#
Expand All @@ -12,7 +12,6 @@ def kdump():
"""Configure the KDUMP mechanism"""
pass


def check_kdump_table_existence(kdump_table):
"""Checks whether the 'KDUMP' table is configured in Config DB.

Expand All @@ -32,9 +31,15 @@ def check_kdump_table_existence(kdump_table):
sys.exit(2)


def echo_reboot_warning():
"""Prints the warning message about reboot requirements."""
click.echo("KDUMP configuration changes may require a reboot to take effect.")
click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.")
#
# 'disable' command ('sudo config kdump disable')
#


@kdump.command(name="disable", short_help="Disable the KDUMP mechanism")
@pass_db
def kdump_disable(db):
Expand All @@ -43,13 +48,13 @@ def kdump_disable(db):
check_kdump_table_existence(kdump_table)

db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "false"})
click.echo("KDUMP configuration changes may require a reboot to take effect.")
click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.")

echo_reboot_warning()

#
# 'enable' command ('sudo config kdump enable')
#


@kdump.command(name="enable", short_help="Enable the KDUMP mechanism")
@pass_db
def kdump_enable(db):
Expand All @@ -58,13 +63,13 @@ def kdump_enable(db):
check_kdump_table_existence(kdump_table)

db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "true"})
click.echo("KDUMP configuration changes may require a reboot to take effect.")
click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.")

echo_reboot_warning()

#
# 'memory' command ('sudo config kdump memory ...')
#


@kdump.command(name="memory", short_help="Configure the memory for KDUMP mechanism")
@click.argument('kdump_memory', metavar='<kdump_memory>', required=True)
@pass_db
Expand All @@ -74,13 +79,13 @@ def kdump_memory(db, kdump_memory):
check_kdump_table_existence(kdump_table)

db.cfgdb.mod_entry("KDUMP", "config", {"memory": kdump_memory})
click.echo("KDUMP configuration changes may require a reboot to take effect.")
click.echo("Save SONiC configuration using 'config save' before issuing the reboot command.")

echo_reboot_warning()

#
# 'num_dumps' command ('sudo config keump num_dumps ...')
# 'num_dumps' command ('sudo config kdump num_dumps ...')
#


@kdump.command(name="num_dumps", short_help="Configure the maximum dump files of KDUMP mechanism")
@click.argument('kdump_num_dumps', metavar='<kdump_num_dumps>', required=True, type=int)
@pass_db
Expand All @@ -90,3 +95,184 @@ def kdump_num_dumps(db, kdump_num_dumps):
check_kdump_table_existence(kdump_table)

db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps})
echo_reboot_warning()

#
# 'remote' command ('sudo config kdump remote ...')
#


@kdump.command(name="remote", short_help="Enable or Disable Kdump Remote")
@click.argument('action', required=True, type=click.Choice(['enable', 'disable'], case_sensitive=False))
@pass_db
def kdump_remote(db, action):
"""Enable or Disable Kdump Remote Mode"""
kdump_table = db.cfgdb.get_table("KDUMP")
check_kdump_table_existence(kdump_table)

current_remote_status = kdump_table.get("config", {}).get("remote", "false").lower()

if action.lower() == 'enable' and current_remote_status == 'true':
click.echo("Error: Kdump Remote Mode is already enabled.")
return
elif action.lower() == 'disable' and current_remote_status == 'false':
click.echo("Error: Kdump Remote Mode is already disabled.")
return

remote = 'true' if action.lower() == 'enable' else 'false'
db.cfgdb.mod_entry("KDUMP", "config", {"remote": remote})

file_path = Path('/etc/default/kdump-tools')
try:
# Read the content of the file
content = file_path.read_text()

if action.lower() == 'enable':
# Define replacement functions with capture groups for uncommenting
def uncomment_ssh(match):
return match.group(0).lstrip('#')

# Apply replacements using capture groups for uncommenting
new_content = re.sub(r"^\s*#?\s*SSH\s*=\s*.*$", uncomment_ssh, content, flags=re.MULTILINE)
new_content = re.sub(r"^\s*#?\s*SSH_KEY\s*=\s*.*$", uncomment_ssh, new_content, flags=re.MULTILINE)

# Write the updated content back to the file
file_path.write_text(new_content)
click.echo("Kdump Remote Mode Enabled")

elif action.lower() == 'disable':
# Define replacement functions with capture groups for commenting
def comment_ssh(match):
return f'#{match.group(0)}' if not match.group(0).startswith('#') else match.group(0)

# Apply replacements using capture groups for commenting
new_content = re.sub(r"^\s*\s*SSH\s*=\s*.*$", comment_ssh, content, flags=re.MULTILINE)
new_content = re.sub(r"^\s*\s*SSH_KEY\s*=\s*.*$", comment_ssh, new_content, flags=re.MULTILINE)

# Write the updated content back to the file
file_path.write_text(new_content)
click.echo("Kdump Remote Mode Disabled.")

except Exception as e:
click.echo(f"Error updating /etc/default/kdump-tools: {e}")

echo_reboot_warning()


#
# 'add' command ('sudo config kdump add ...')
#


@kdump.command(name="add", short_help="Add SSH connection string or SSH key path.")
@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path']))
@click.argument('value', metavar='<value>', required=True)
@pass_db
def add_kdump_item(db, item, value):
"""Add SSH connection string or SSH key path for kdump"""
kdump_table = db.cfgdb.get_table("KDUMP")
check_kdump_table_existence(kdump_table)

# Check if remote mode is enabled
remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower()
if remote_mode_enabled != "true":
click.echo("Error: Enable remote mode first.")
return

# Add item to config_db
db.cfgdb.mod_entry("KDUMP", "config", {item: value})

# Retrieve updated values from config_db
kdump_table = db.cfgdb.get_table("KDUMP")
ssh_string = kdump_table.get("config", {}).get("ssh_string", "")
ssh_path = kdump_table.get("config", {}).get("ssh_path", "")

file_path = Path('/etc/default/kdump-tools')
try:
# Read the content of the file
content = file_path.read_text()

# Check if SSH and SSH_KEY are uncommented and update them
ssh_uncommented = bool(re.search(r"^\s*SSH\s*=\s*.*$", content, flags=re.MULTILINE))
ssh_key_uncommented = bool(re.search(r"^\s*SSH_KEY\s*=\s*.*$", content, flags=re.MULTILINE))

if not ssh_uncommented or not ssh_key_uncommented:
click.echo("Error: Enable remote mode first.")
return

# Define replacement functions
def replace_ssh(match):
return f'SSH="{ssh_string}"' if ssh_string else match.group(0)

def replace_ssh_key(match):
return f'SSH_KEY="{ssh_path}"' if ssh_path else match.group(0)

# Apply replacements
new_content = re.sub(r"^\s*SSH\s*=\s*.*$", replace_ssh, content, flags=re.MULTILINE)
new_content = re.sub(r"^\s*SSH_KEY\s*=\s*.*$", replace_ssh_key, new_content, flags=re.MULTILINE)

# Write the updated content back to the file
file_path.write_text(new_content)
click.echo("Updated kdump configurations.")
except Exception as e:
click.echo(f"Error updating kdump configurations: {e}")

echo_reboot_warning()


@kdump.command(name="remove", short_help="Remove SSH connection string or SSH key path.")
@click.argument('item', type=click.Choice(['ssh_string', 'ssh_path']))
@pass_db
def remove_kdump_item(db, item):
"""Remove SSH connection string or SSH key path for kdump"""
kdump_table = db.cfgdb.get_table("KDUMP")
check_kdump_table_existence(kdump_table)

# Check if the item is already configured
existing_value = kdump_table.get("config", {}).get(item)
if not existing_value:
click.echo(f"Error: {item} is not configured.")
return

# Check if remote mode is enabled
remote_mode_enabled = kdump_table.get("config", {}).get("remote", "false").lower()
if remote_mode_enabled != "true":
click.echo("Error: Remote mode is not enabled.")
return

# Remove item from config_db
db.cfgdb.mod_entry("KDUMP", "config", {item: ""})

file_path = Path('/etc/default/kdump-tools')
try:
# Read the content of the file
content = file_path.read_text()

# Define replacement functions
def remove_ssh(match):
return 'SSH=""' if item == "ssh_string" else match.group(0)

def remove_ssh_key(match):
return 'SSH_KEY=""' if item == "ssh_path" else match.group(0)

# Check if SSH and SSH_KEY are commented
ssh_commented = bool(re.search(r"^\s*#\s*SSH\s*=\s*.*$", content, flags=re.MULTILINE))
ssh_key_commented = bool(re.search(r"^\s*#\s*SSH_KEY\s*=\s*.*$", content, flags=re.MULTILINE))

if ssh_commented and ssh_key_commented:
# Apply replacements to remove values
new_content = re.sub(r"^\s*#\s*SSH\s*=\s*.*$", remove_ssh, content, flags=re.MULTILINE)
new_content = re.sub(r"^\s*#\s*SSH_KEY\s*=\s*.*$", remove_ssh_key, new_content, flags=re.MULTILINE)
else:
# Apply replacements to remove values
new_content = re.sub(r"^\s*SSH\s*=\s*.*$", remove_ssh, content, flags=re.MULTILINE)
new_content = re.sub(r"^\s*SSH_KEY\s*=\s*.*$", remove_ssh_key, new_content, flags=re.MULTILINE)

# Write the updated content back to the file
file_path.write_text(new_content)
click.echo("Updated kdump configurations.")
except Exception as e:
click.echo(f"Error updating /etc/default/kdump-tools: {e}")

click.echo(f"{item} removed successfully.")
echo_reboot_warning()
2 changes: 1 addition & 1 deletion scripts/sonic-kdump-config
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,4 @@ def main():
sys.exit(0)

if __name__== "__main__":
main()
main()
8 changes: 8 additions & 0 deletions show/kdump.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def config():
num_files_config = get_kdump_config("num_dumps")
click.echo("Maximum number of Kdump files: {}".format(num_files_config))

# remote SSH configs
if get_kdump_config("remote") == "true":
ssh_conn_str = get_kdump_config("ssh_string")
click.echo("Kdump ssh connection string: {}".format(ssh_conn_str))

ssh_prv_key = get_kdump_config("ssh_path")
click.echo("Kdump private key path: {}".format(ssh_prv_key))


def get_kdump_core_files():
"""Retrieves the kernel core dump files from directory '/var/crash/'.
Expand Down
Loading
Loading