|
1 |
| -import os |
| 1 | +import sys |
| 2 | + |
2 | 3 | import click
|
3 |
| -import utilities_common.cli as clicommon |
4 |
| -from swsscommon.swsscommon import ConfigDBConnector |
| 4 | +from utilities_common.cli import AbbreviationGroup, pass_db |
| 5 | + |
5 | 6 |
|
6 |
| -@click.group(cls=clicommon.AbbreviationGroup, name="kdump") |
| 7 | +# |
| 8 | +# 'kdump' group ('sudo config kdump ...') |
| 9 | +# |
| 10 | +@click.group(cls=AbbreviationGroup, name="kdump") |
7 | 11 | def kdump():
|
8 |
| - """ Configure kdump """ |
9 |
| - if os.geteuid() != 0: |
10 |
| - exit("Root privileges are required for this operation") |
11 |
| - |
12 |
| -@kdump.command() |
13 |
| -def disable(): |
14 |
| - """Disable kdump operation""" |
15 |
| - config_db = ConfigDBConnector() |
16 |
| - if config_db is not None: |
17 |
| - config_db.connect() |
18 |
| - config_db.mod_entry("KDUMP", "config", {"enabled": "false"}) |
19 |
| - |
20 |
| -@kdump.command() |
21 |
| -def enable(): |
22 |
| - """Enable kdump operation""" |
23 |
| - config_db = ConfigDBConnector() |
24 |
| - if config_db is not None: |
25 |
| - config_db.connect() |
26 |
| - config_db.mod_entry("KDUMP", "config", {"enabled": "true"}) |
27 |
| - |
28 |
| -@kdump.command() |
| 12 | + """Configure the KDUMP mechanism""" |
| 13 | + pass |
| 14 | + |
| 15 | + |
| 16 | +def check_kdump_table_existence(kdump_table): |
| 17 | + """Checks whether the 'KDUMP' table is configured in Config DB. |
| 18 | +
|
| 19 | + Args: |
| 20 | + kdump_table: A dictionary represents the key-value pair in sub-table |
| 21 | + of 'KDUMP'. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + None. |
| 25 | + """ |
| 26 | + if not kdump_table: |
| 27 | + click.echo("Unable to retrieve 'KDUMP' table from Config DB.") |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + if "config" not in kdump_table: |
| 31 | + click.echo("Unable to retrieve key 'config' from KDUMP table.") |
| 32 | + sys.exit(2) |
| 33 | + |
| 34 | + |
| 35 | +# |
| 36 | +# 'disable' command ('sudo config kdump disable') |
| 37 | +# |
| 38 | +@kdump.command(name="disable", short_help="Disable the KDUMP mechanism") |
| 39 | +@pass_db |
| 40 | +def kdump_disable(db): |
| 41 | + """Disable the KDUMP mechanism""" |
| 42 | + kdump_table = db.cfgdb.get_table("KDUMP") |
| 43 | + check_kdump_table_existence(kdump_table) |
| 44 | + |
| 45 | + db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "false"}) |
| 46 | + |
| 47 | + |
| 48 | +# |
| 49 | +# 'enable' command ('sudo config kdump enable') |
| 50 | +# |
| 51 | +@kdump.command(name="enable", short_help="Enable the KDUMP mechanism") |
| 52 | +@pass_db |
| 53 | +def kdump_enable(db): |
| 54 | + """Enable the KDUMP mechanism""" |
| 55 | + kdump_table = db.cfgdb.get_table("KDUMP") |
| 56 | + check_kdump_table_existence(kdump_table) |
| 57 | + |
| 58 | + db.cfgdb.mod_entry("KDUMP", "config", {"enabled": "true"}) |
| 59 | + |
| 60 | + |
| 61 | +# |
| 62 | +# 'memory' command ('sudo config kdump memory ...') |
| 63 | +# |
| 64 | +@kdump.command(name="memory", short_help="Configure the memory for KDUMP mechanism") |
29 | 65 | @click.argument('kdump_memory', metavar='<kdump_memory>', required=True)
|
30 |
| -def memory(kdump_memory): |
31 |
| - """Set memory allocated for kdump capture kernel""" |
32 |
| - config_db = ConfigDBConnector() |
33 |
| - if config_db is not None: |
34 |
| - config_db.connect() |
35 |
| - config_db.mod_entry("KDUMP", "config", {"memory": kdump_memory}) |
36 |
| - |
37 |
| -@kdump.command('num-dumps') |
| 66 | +@pass_db |
| 67 | +def kdump_memory(db, kdump_memory): |
| 68 | + """Reserve memory for kdump capture kernel""" |
| 69 | + kdump_table = db.cfgdb.get_table("KDUMP") |
| 70 | + check_kdump_table_existence(kdump_table) |
| 71 | + |
| 72 | + db.cfgdb.mod_entry("KDUMP", "config", {"memory": kdump_memory}) |
| 73 | + |
| 74 | + |
| 75 | +# |
| 76 | +# 'num_dumps' command ('sudo config keump num_dumps ...') |
| 77 | +# |
| 78 | +@kdump.command(name="num_dumps", short_help="Configure the maximum dump files of KDUMP mechanism") |
38 | 79 | @click.argument('kdump_num_dumps', metavar='<kdump_num_dumps>', required=True, type=int)
|
39 |
| -def num_dumps(kdump_num_dumps): |
40 |
| - """Set max number of dump files for kdump""" |
41 |
| - config_db = ConfigDBConnector() |
42 |
| - if config_db is not None: |
43 |
| - config_db.connect() |
44 |
| - config_db.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) |
| 80 | +@pass_db |
| 81 | +def kdump_num_dumps(db, kdump_num_dumps): |
| 82 | + """Set maximum number of dump files for kdump""" |
| 83 | + kdump_table = db.cfgdb.get_table("KDUMP") |
| 84 | + check_kdump_table_existence(kdump_table) |
| 85 | + |
| 86 | + db.cfgdb.mod_entry("KDUMP", "config", {"num_dumps": kdump_num_dumps}) |
0 commit comments