Skip to content

Commit afe1122

Browse files
Merge branch 'master' into storm_control
2 parents e4389e5 + 6ab1c51 commit afe1122

File tree

9 files changed

+1069
-40
lines changed

9 files changed

+1069
-40
lines changed

config/main.py

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from collections import OrderedDict
1717
from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat
18-
from minigraph import parse_device_desc_xml
18+
from minigraph import parse_device_desc_xml, minigraph_encoder
1919
from natsort import natsorted
2020
from portconfig import get_child_ports
2121
from socket import AF_INET, AF_INET6
@@ -27,7 +27,7 @@
2727
from utilities_common.intf_filter import parse_interface_in_filter
2828
from utilities_common import bgp_util
2929
import utilities_common.cli as clicommon
30-
from utilities_common.general import load_db_config
30+
from utilities_common.general import load_db_config, load_module_from_source
3131
import utilities_common.multi_asic as multi_asic_util
3232

3333
from .utils import log
@@ -72,6 +72,7 @@
7272
DEFAULT_CONFIG_YANG_FILE = '/etc/sonic/config_yang.json'
7373
NAMESPACE_PREFIX = 'asic'
7474
INTF_KEY = "interfaces"
75+
DEFAULT_GOLDEN_CONFIG_DB_FILE = '/etc/sonic/golden_config_db.json'
7576

7677
INIT_CFG_FILE = '/etc/sonic/init_cfg.json'
7778

@@ -100,6 +101,9 @@
100101
QUEUE_RANGE = click.IntRange(min=0, max=255)
101102
GRE_TYPE_RANGE = click.IntRange(min=0, max=65535)
102103

104+
# Load sonic-cfggen from source since /usr/local/bin/sonic-cfggen does not have .py extension.
105+
sonic_cfggen = load_module_from_source('sonic_cfggen', '/usr/local/bin/sonic-cfggen')
106+
103107
#
104108
# Helper functions
105109
#
@@ -1692,6 +1696,10 @@ def load_minigraph(db, no_service_restart):
16921696
cfggen_namespace_option = " -n {}".format(namespace)
16931697
clicommon.run_command(db_migrator + ' -o set_version' + cfggen_namespace_option)
16941698

1699+
# Load golden_config_db.json
1700+
if os.path.isfile(DEFAULT_GOLDEN_CONFIG_DB_FILE):
1701+
override_config_by(DEFAULT_GOLDEN_CONFIG_DB_FILE)
1702+
16951703
# We first run "systemctl reset-failed" to remove the "failed"
16961704
# status from all services before we attempt to restart them
16971705
if not no_service_restart:
@@ -1740,6 +1748,65 @@ def load_port_config(config_db, port_config_path):
17401748
port_name), display_cmd=True)
17411749
return
17421750

1751+
1752+
def override_config_by(golden_config_path):
1753+
# Override configDB with golden config
1754+
clicommon.run_command('config override-config-table {}'.format(
1755+
golden_config_path), display_cmd=True)
1756+
return
1757+
1758+
1759+
#
1760+
# 'override-config-table' command ('config override-config-table ...')
1761+
#
1762+
@config.command('override-config-table')
1763+
@click.argument('input-config-db', required=True)
1764+
@click.option(
1765+
'--dry-run', is_flag=True, default=False,
1766+
help='test out the command without affecting config state'
1767+
)
1768+
@clicommon.pass_db
1769+
def override_config_table(db, input_config_db, dry_run):
1770+
"""Override current configDB with input config."""
1771+
1772+
try:
1773+
# Load golden config json
1774+
config_input = read_json_file(input_config_db)
1775+
except Exception as e:
1776+
click.secho("Bad format: json file broken. {}".format(str(e)),
1777+
fg='magenta')
1778+
sys.exit(1)
1779+
1780+
# Validate if the input is dict
1781+
if not isinstance(config_input, dict):
1782+
click.secho("Bad format: input_config_db is not a dict",
1783+
fg='magenta')
1784+
sys.exit(1)
1785+
1786+
config_db = db.cfgdb
1787+
1788+
if dry_run:
1789+
# Read config from configDB
1790+
current_config = config_db.get_config()
1791+
# Serialize to the same format as json input
1792+
sonic_cfggen.FormatConverter.to_serialized(current_config)
1793+
# Override current config with golden config
1794+
for table in config_input:
1795+
current_config[table] = config_input[table]
1796+
print(json.dumps(current_config, sort_keys=True,
1797+
indent=4, cls=minigraph_encoder))
1798+
else:
1799+
# Deserialized golden config to DB recognized format
1800+
sonic_cfggen.FormatConverter.to_deserialized(config_input)
1801+
# Delete table from DB then mod_config to apply golden config
1802+
click.echo("Removing configDB overriden table first ...")
1803+
for table in config_input:
1804+
config_db.delete_table(table)
1805+
click.echo("Overriding input config to configDB ...")
1806+
data = sonic_cfggen.FormatConverter.output_to_db(config_input)
1807+
config_db.mod_config(data)
1808+
click.echo("Overriding completed. No service is restarted.")
1809+
17431810
#
17441811
# 'hostname' command
17451812
#

scripts/sonic-kdump-config

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ See the License for the specific language governing permissions and
1717
limitations under the License.
1818
'''
1919

20-
import sys
2120
import argparse
22-
import shlex
21+
import json
2322
import os
23+
import shlex
24+
import sys
25+
import syslog
2426
import subprocess
25-
import json
27+
2628
from swsscommon.swsscommon import ConfigDBConnector
2729

2830
aboot_cfg_template ="/host/image-%s/kernel-cmdline"
@@ -437,7 +439,7 @@ def get_kdump_config_json(config_param):
437439
# @param verbose If True, the function will display a few additinal information
438440
# @param image The image on which kdump settings are changed
439441
# @return True if the grub/cmdline cfg has changed, and False if it has not
440-
def cmd_kdump_enable(verbose, image=get_current_image()):
442+
def cmd_kdump_enable(verbose, image):
441443

442444
kdump_enabled = get_kdump_administrative_mode()
443445
memory = get_kdump_memory()
@@ -460,75 +462,76 @@ def cmd_kdump_enable(verbose, image=get_current_image()):
460462
# @param image The image on which kdump settings are changed
461463
# @return True if the grub/cmdline cfg has changed, and False if it has not
462464
def cmd_kdump_config_next(verbose):
463-
return cmd_kdump_enable(verbose, image=get_next_image())
465+
image = get_next_image()
466+
return cmd_kdump_enable(verbose, image)
464467

465-
## Disable kdump
466-
#
467-
# @param verbose If True, the function will display a few additional information
468-
# @return True if the grub/cmdline cfg has changed, and False if it has not
469-
def kdump_disable(verbose, kdump_enabled, memory, num_dumps, image, cmdline_file):
468+
469+
def kdump_disable(verbose, image, booter_config_file_path):
470+
"""Unloads Kdump kernel and remove parameter `crashkernel=*` from configuration file of
471+
kernel boot loader.
472+
473+
Args:
474+
image: A string represents SONiC image version.
475+
booter_config_file_path: A string represents path of kernel boot loader configuration file.
476+
477+
Returns:
478+
changes: If kernel booter configuration file was changed, returns True; Otherwise, returns
479+
False.
480+
"""
470481
write_use_kdump(0)
471482

472483
if verbose:
473484
print("Disabling kdump for image=[%s]\n" % image)
474-
lines = [line.rstrip('\n') for line in open(cmdline_file)]
485+
486+
try:
487+
with open(booter_config_file_path) as config_file_handler:
488+
lines = [line.rstrip('\n') for line in config_file_handler]
489+
except OSError as sys_err:
490+
syslog.syslog(syslog.LOG_ERR, "Failed to open kernel booter configuration file: '{}', Error is: '{}'!"
491+
.format(booter_config_file_path, sys_err))
492+
return False
493+
475494
img_index = locate_image(lines, "loop=image-"+image)
476495

477496
changed = False
497+
478498
crash_kernel_mem = search_for_crash_kernel(lines[img_index])
479499
if crash_kernel_mem is None:
480500
print("kdump is already disabled")
481501
else:
482502
lines[img_index] = lines[img_index].replace("crashkernel="+crash_kernel_mem, "")
483503
changed = True
484504
if verbose:
485-
print("Removed [%s] in %s" % ("crashkernel="+crash_kernel_mem, cmdline_file))
505+
print("Removed [%s] in %s" % ("crashkernel="+crash_kernel_mem, booter_config_file_path))
486506

487507
if changed:
488-
rewrite_cfg(lines, cmdline_file)
508+
rewrite_cfg(lines, booter_config_file_path)
489509

490510
if not os.path.exists('/etc/sonic/config_db.json'):
491511
print_err("Startup configuration not found, Kdump configuration is not saved")
492512
return False
493513

494-
current_img = get_current_image();
495-
if verbose:
496-
print("Current image=[%s]\n" % current_img)
497-
lines = [line.rstrip('\n') for line in open(grub_cfg)]
498-
current_img_index = locate_image(lines, "loop=image-"+current_img)
499-
500-
changed = False
501-
curr_crash_kernel_mem = search_for_crash_kernel(lines[current_img_index])
502-
if curr_crash_kernel_mem is None:
503-
print("Kdump is already disabled")
504-
else:
505-
lines[current_img_index] = lines[current_img_index].replace("crashkernel="+curr_crash_kernel_mem, "")
506-
changed = True
507-
if verbose:
508-
print("Removed [%s] in grub.cfg" % ("crashkernel="+curr_crash_kernel_mem))
509-
510-
if changed:
511-
rewrite_grub_cfg(lines, grub_cfg)
512-
513514
return changed
514515

516+
515517
## Command: Disable kdump
516518
#
517519
# @param verbose If True, the function will display a few additional information
518-
# @param image The image on which kdump settings are changed
519-
def cmd_kdump_disable(verbose, image=get_current_image()):
520+
def cmd_kdump_disable(verbose):
520521

522+
image = get_current_image()
521523
kdump_enabled = get_kdump_administrative_mode()
522524
memory = get_kdump_memory()
523525
num_dumps = get_kdump_num_dumps()
526+
524527
if verbose:
525528
print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled, memory, num_dumps))
526529

527530
if os.path.exists(grub_cfg):
528-
return kdump_disable(verbose, kdump_enabled, memory, num_dumps, image, grub_cfg)
531+
return kdump_disable(verbose, image, grub_cfg)
529532
elif open(machine_cfg, 'r').read().find('aboot_platform') >= 0:
530533
aboot_cfg = aboot_cfg_template % image
531-
return kdump_disable(verbose, kdump_enabled, memory, num_dumps, image, aboot_cfg)
534+
return kdump_disable(verbose, image, aboot_cfg)
532535
else:
533536
print("Feature not supported on this platform")
534537
return False
@@ -549,7 +552,8 @@ def cmd_kdump_memory(verbose, memory):
549552
memory_in_db = get_kdump_memory()
550553
memory_in_json = get_kdump_config_json("memory")
551554
if memory != crash_kernel_in_cmdline or memory != memory_in_db or memory != memory_in_json:
552-
cmd_kdump_enable(verbose)
555+
image = get_current_image()
556+
cmd_kdump_enable(verbose, image)
553557
print("Kdump updated memory will be only operational after the system reboots")
554558
else:
555559
num_dumps = get_kdump_num_dumps()
@@ -635,7 +639,8 @@ def main():
635639
changed = False
636640
try:
637641
if options.enable:
638-
changed = cmd_kdump_enable(options.verbose)
642+
image = get_current_image()
643+
changed = cmd_kdump_enable(options.verbose, image)
639644
elif options.config_next:
640645
changed = cmd_kdump_config_next(options.verbose)
641646
elif options.disable:
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
{
2+
"running_config": {
3+
"ACL_TABLE": {
4+
"DATAACL": {
5+
"policy_desc": "DATAACL",
6+
"ports": [
7+
"Ethernet4"
8+
],
9+
"stage": "ingress",
10+
"type": "L3"
11+
},
12+
"NTP_ACL": {
13+
"policy_desc": "NTP_ACL",
14+
"services": [
15+
"NTP"
16+
],
17+
"stage": "ingress",
18+
"type": "CTRLPLANE"
19+
}
20+
},
21+
"AUTO_TECHSUPPORT_FEATURE": {
22+
"bgp": {
23+
"rate_limit_interval": "600",
24+
"state": "enabled"
25+
},
26+
"database": {
27+
"rate_limit_interval": "600",
28+
"state": "enabled"
29+
}
30+
},
31+
"PORT": {
32+
"Ethernet4": {
33+
"admin_status": "up",
34+
"alias": "fortyGigE0/4",
35+
"description": "Servers0:eth0",
36+
"index": "1",
37+
"lanes": "29,30,31,32",
38+
"mtu": "9100",
39+
"pfc_asym": "off",
40+
"speed": "40000",
41+
"tpid": "0x8100"
42+
},
43+
"Ethernet8": {
44+
"admin_status": "up",
45+
"alias": "fortyGigE0/8",
46+
"description": "Servers1:eth0",
47+
"index": "2",
48+
"lanes": "33,34,35,36",
49+
"mtu": "9100",
50+
"pfc_asym": "off",
51+
"speed": "40000",
52+
"tpid": "0x8100"
53+
}
54+
}
55+
},
56+
"golden_config": {
57+
58+
},
59+
"expected_config": {
60+
"ACL_TABLE": {
61+
"DATAACL": {
62+
"policy_desc": "DATAACL",
63+
"ports": [
64+
"Ethernet4"
65+
],
66+
"stage": "ingress",
67+
"type": "L3"
68+
},
69+
"NTP_ACL": {
70+
"policy_desc": "NTP_ACL",
71+
"services": [
72+
"NTP"
73+
],
74+
"stage": "ingress",
75+
"type": "CTRLPLANE"
76+
}
77+
},
78+
"AUTO_TECHSUPPORT_FEATURE": {
79+
"bgp": {
80+
"rate_limit_interval": "600",
81+
"state": "enabled"
82+
},
83+
"database": {
84+
"rate_limit_interval": "600",
85+
"state": "enabled"
86+
}
87+
},
88+
"PORT": {
89+
"Ethernet4": {
90+
"admin_status": "up",
91+
"alias": "fortyGigE0/4",
92+
"description": "Servers0:eth0",
93+
"index": "1",
94+
"lanes": "29,30,31,32",
95+
"mtu": "9100",
96+
"pfc_asym": "off",
97+
"speed": "40000",
98+
"tpid": "0x8100"
99+
},
100+
"Ethernet8": {
101+
"admin_status": "up",
102+
"alias": "fortyGigE0/8",
103+
"description": "Servers1:eth0",
104+
"index": "2",
105+
"lanes": "33,34,35,36",
106+
"mtu": "9100",
107+
"pfc_asym": "off",
108+
"speed": "40000",
109+
"tpid": "0x8100"
110+
}
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)