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

[minigraph] Consume golden_config_db.json while loading minigraph #2140

Merged
merged 3 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 67 additions & 2 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from collections import OrderedDict
from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat
from minigraph import parse_device_desc_xml
from minigraph import parse_device_desc_xml, minigraph_encoder
from natsort import natsorted
from portconfig import get_child_ports
from socket import AF_INET, AF_INET6
Expand All @@ -27,7 +27,7 @@
from utilities_common.intf_filter import parse_interface_in_filter
from utilities_common import bgp_util
import utilities_common.cli as clicommon
from utilities_common.general import load_db_config
from utilities_common.general import load_db_config, load_module_from_source

from .utils import log

Expand Down Expand Up @@ -99,6 +99,9 @@
QUEUE_RANGE = click.IntRange(min=0, max=255)
GRE_TYPE_RANGE = click.IntRange(min=0, max=65535)

# Load sonic-cfggen from source since /usr/local/bin/sonic-cfggen does not have .py extension.
sonic_cfggen = load_module_from_source('sonic_cfggen', '/usr/local/bin/sonic-cfggen')
Copy link
Contributor

@qiluo-msft qiluo-msft Apr 26, 2022

Choose a reason for hiding this comment

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

load_module_from_source

We can move some useful functions to an official python module (ref: src/sonic-config-engine/setup.py) so it will be easy to reuse. This ask is in low priority, you can check this after all others resolved. #Pending


#
# Helper functions
#
Expand Down Expand Up @@ -1619,6 +1622,9 @@ def load_minigraph(db, no_service_restart):
cfggen_namespace_option = " -n {}".format(namespace)
clicommon.run_command(db_migrator + ' -o set_version' + cfggen_namespace_option)

# Load golden_config_db.json
load_golden_config('/etc/sonic/golden_config_db.json')
wen587 marked this conversation as resolved.
Show resolved Hide resolved
wen587 marked this conversation as resolved.
Show resolved Hide resolved

# We first run "systemctl reset-failed" to remove the "failed"
# status from all services before we attempt to restart them
if not no_service_restart:
Expand Down Expand Up @@ -1667,6 +1673,65 @@ def load_port_config(config_db, port_config_path):
port_name), display_cmd=True)
return


def load_golden_config(golden_config_path):
if not os.path.isfile(golden_config_path):
return
wen587 marked this conversation as resolved.
Show resolved Hide resolved

# Override configDB with golden config
clicommon.run_command('config override-config-table {}'.format(
wen587 marked this conversation as resolved.
Show resolved Hide resolved
golden_config_path), display_cmd=True)
return


#
# 'override-config-table' command ('config override-config-table ...')
#
@config.command('override-config-table')
@click.argument('golden-config-db', required=True)
@click.option(
'--dry_run', type=click.STRING,
wen587 marked this conversation as resolved.
Show resolved Hide resolved
wen587 marked this conversation as resolved.
Show resolved Hide resolved
help="Dry run, writes config to the given file"
)
@clicommon.pass_db
def override_config_table(db, golden_config_db, dry_run):
"""Override current configDB with golden config."""

try:
# Load golden config json
golden_config_input = read_json_file(golden_config_db)
except Exception:
wen587 marked this conversation as resolved.
Show resolved Hide resolved
click.secho("Bad format: json file broken", fg='magenta')
sys.exit(1)

# Validate if the input is dict
if not isinstance(golden_config_input, dict):
click.secho("Bad format: golden_config_db is not a dict", fg='magenta')
sys.exit(1)

config_db = db.cfgdb

if dry_run:
# Read config from configDB
current_config = config_db.get_config()
# Serialize to the same format as json input
sonic_cfggen.FormatConverter.to_serialized(current_config)
# Override current config with golden config
for table in golden_config_input:
current_config[table] = golden_config_input[table]
with open(dry_run, "w+") as f:
json.dump(current_config, f, sort_keys=True, indent=4, cls=minigraph_encoder)
else:
# Deserialized golden config to DB recognized format
sonic_cfggen.FormatConverter.to_deserialized(golden_config_input)
# Delete table from DB then mod_config to apply golden config
click.echo("Removing configDB overriden table first ...")
for table in golden_config_input:
config_db.delete_table(table)
click.echo("Writing golden config to configDB ...")
wen587 marked this conversation as resolved.
Show resolved Hide resolved
wen587 marked this conversation as resolved.
Show resolved Hide resolved
data = sonic_cfggen.FormatConverter.output_to_db(golden_config_input)
config_db.mod_config(data)
wen587 marked this conversation as resolved.
Show resolved Hide resolved

#
# 'hostname' command
#
Expand Down
113 changes: 113 additions & 0 deletions tests/config_override_input/empty_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"running_config": {
"ACL_TABLE": {
"DATAACL": {
"policy_desc": "DATAACL",
"ports": [
"Ethernet4"
],
"stage": "ingress",
"type": "L3"
},
"NTP_ACL": {
"policy_desc": "NTP_ACL",
"services": [
"NTP"
],
"stage": "ingress",
"type": "CTRLPLANE"
}
},
"AUTO_TECHSUPPORT_FEATURE": {
"bgp": {
"rate_limit_interval": "600",
"state": "enabled"
},
"database": {
"rate_limit_interval": "600",
"state": "enabled"
}
},
"PORT": {
"Ethernet4": {
"admin_status": "up",
"alias": "fortyGigE0/4",
"description": "Servers0:eth0",
"index": "1",
"lanes": "29,30,31,32",
"mtu": "9100",
"pfc_asym": "off",
"speed": "40000",
"tpid": "0x8100"
},
"Ethernet8": {
"admin_status": "up",
"alias": "fortyGigE0/8",
"description": "Servers1:eth0",
"index": "2",
"lanes": "33,34,35,36",
"mtu": "9100",
"pfc_asym": "off",
"speed": "40000",
"tpid": "0x8100"
}
}
},
"golden_config": {

},
"expected_config": {
"ACL_TABLE": {
"DATAACL": {
"policy_desc": "DATAACL",
"ports": [
"Ethernet4"
],
"stage": "ingress",
"type": "L3"
},
"NTP_ACL": {
"policy_desc": "NTP_ACL",
"services": [
"NTP"
],
"stage": "ingress",
"type": "CTRLPLANE"
}
},
"AUTO_TECHSUPPORT_FEATURE": {
"bgp": {
"rate_limit_interval": "600",
"state": "enabled"
},
"database": {
"rate_limit_interval": "600",
"state": "enabled"
}
},
"PORT": {
"Ethernet4": {
"admin_status": "up",
"alias": "fortyGigE0/4",
"description": "Servers0:eth0",
"index": "1",
"lanes": "29,30,31,32",
"mtu": "9100",
"pfc_asym": "off",
"speed": "40000",
"tpid": "0x8100"
},
"Ethernet8": {
"admin_status": "up",
"alias": "fortyGigE0/8",
"description": "Servers1:eth0",
"index": "2",
"lanes": "33,34,35,36",
"mtu": "9100",
"pfc_asym": "off",
"speed": "40000",
"tpid": "0x8100"
}
}
}
}
122 changes: 122 additions & 0 deletions tests/config_override_input/full_config_override.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
{
"running_config": {
"ACL_TABLE": {
"DATAACL": {
"policy_desc": "DATAACL",
"ports": [
"Ethernet4"
],
"stage": "ingress",
"type": "L3"
},
"NTP_ACL": {
"policy_desc": "NTP_ACL",
"services": [
"NTP"
],
"stage": "ingress",
"type": "CTRLPLANE"
}
},
"AUTO_TECHSUPPORT_FEATURE": {
"bgp": {
"rate_limit_interval": "600",
"state": "enabled"
},
"database": {
"rate_limit_interval": "600",
"state": "enabled"
}
},
"PORT": {
"Ethernet4": {
"admin_status": "up",
"alias": "fortyGigE0/4",
"description": "Servers0:eth0",
"index": "1",
"lanes": "29,30,31,32",
"mtu": "9100",
"pfc_asym": "off",
"speed": "40000",
"tpid": "0x8100"
},
"Ethernet8": {
"admin_status": "up",
"alias": "fortyGigE0/8",
"description": "Servers1:eth0",
"index": "2",
"lanes": "33,34,35,36",
"mtu": "9100",
"pfc_asym": "off",
"speed": "40000",
"tpid": "0x8100"
}
}
},
"golden_config": {
"ACL_TABLE": {
"EVERFLOWV6": {
"policy_desc": "EVERFLOWV6",
"ports": [
"Ethernet12"
],
"stage": "ingress",
"type": "MIRRORV6"
}
},
"AUTO_TECHSUPPORT_FEATURE": {
"bgp": {
"state": "disabled"
},
"database": {
"state": "disabled"
}
},
"PORT": {
"Ethernet12": {
"admin_status": "up",
"alias": "fortyGigE0/12",
"description": "Servers2:eth0",
"index": "3",
"lanes": "37,38,39,40",
"mtu": "9100",
"pfc_asym": "off",
"speed": "40000",
"tpid": "0x8100"
}
}
},
"expected_config": {
"ACL_TABLE": {
"EVERFLOWV6": {
"policy_desc": "EVERFLOWV6",
"ports": [
"Ethernet12"
],
"stage": "ingress",
"type": "MIRRORV6"
}
},
"AUTO_TECHSUPPORT_FEATURE": {
"bgp": {
"state": "disabled"
},
"database": {
"state": "disabled"
}
},
"PORT": {
"Ethernet12": {
"admin_status": "up",
"alias": "fortyGigE0/12",
"description": "Servers2:eth0",
"index": "3",
"lanes": "37,38,39,40",
"mtu": "9100",
"pfc_asym": "off",
"speed": "40000",
"tpid": "0x8100"
}
}
}
}
Loading