forked from sonic-net/sonic-mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_golden_config_db.py
More file actions
384 lines (321 loc) · 16.5 KB
/
Copy pathgenerate_golden_config_db.py
File metadata and controls
384 lines (321 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python
# This ansible module is for generate golden_config_db.json
# Currently, only enable dhcp_server feature and generated related configuration in MX device
# which has dhcp_server feature.
import copy
from jinja2 import Template
import logging
import json
import re
from ansible.module_utils.basic import AnsibleModule
from sonic_py_common import device_info, multi_asic
from ansible.module_utils.smartswitch_utils import smartswitch_hwsku_config
DOCUMENTATION = '''
module: generate_golden_config_db.py
author: Yaqiang Zhu (yaqiangzhu@microsoft.com)
short_description: Generate golden_config_db.json
Description:
When load_minigraph, SONiC support to use parameter --override_config to add configuration via
golden_config_db.json. This module is to generate required /etc/sonic/golden_config_db.json
Input:
topo_name: Name of current topo
'''
GOLDEN_CONFIG_DB_PATH = "/etc/sonic/golden_config_db.json"
TEMP_DHCP_SERVER_CONFIG_PATH = "/tmp/dhcp_server.json"
TEMP_SMARTSWITCH_CONFIG_PATH = "/tmp/smartswitch.json"
DUMMY_QUOTA = "dummy_single_quota"
MACSEC_PROFILE_PATH = '/tmp/profile.json'
GOLDEN_CONFIG_TEMPLATE = 'golden_config_db_t2.j2'
GOLDEN_CONFIG_TEMPLATE_PATH = '/tmp/golden_config_db_t2.j2'
DNS_CONFIG_PATH = '/tmp/dns_config.json'
logger = logging.getLogger(__name__)
class GenerateGoldenConfigDBModule(object):
def __init__(self):
self.module = AnsibleModule(argument_spec=dict(topo_name=dict(required=True, type='str'),
port_index_map=dict(require=False, type='dict', default=None),
macsec_profile=dict(require=False, type='str', default=None),
num_asics=dict(require=False, type='int', default=1)),
supports_check_mode=True)
self.topo_name = self.module.params['topo_name']
self.port_index_map = self.module.params['port_index_map']
self.macsec_profile = self.module.params['macsec_profile']
self.num_asics = self.module.params['num_asics']
def generate_mgfx_golden_config_db(self):
rc, out, err = self.module.run_command("sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data")
if rc != 0:
self.module.fail_json(msg="Failed to get config from minigraph: {}".format(err))
# Generate config table from init_cfg.ini
ori_config_db = json.loads(out)
golden_config_db = {}
if "DEVICE_METADATA" in ori_config_db:
golden_config_db["DEVICE_METADATA"] = ori_config_db["DEVICE_METADATA"]
if ("localhost" in golden_config_db["DEVICE_METADATA"] and
"default_pfcwd_status" in golden_config_db["DEVICE_METADATA"]["localhost"]):
golden_config_db["DEVICE_METADATA"]["localhost"]["default_pfcwd_status"] = "disable"
if self.topo_name == "mx":
golden_config_db.update(self.generate_mx_golden_config_db())
return json.dumps(golden_config_db, indent=4)
def generate_mx_golden_config_db(self):
"""
If FEATURE table in init_cfg.json contains dhcp_server, enable it.
And add dhcp_server related configuration
"""
rc, out, err = self.module.run_command("sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data")
if rc != 0:
self.module.fail_json(msg="Failed to get config from minigraph: {}".format(err))
# Generate FEATURE table from init_cfg.ini
ori_config_db = json.loads(out)
if "FEATURE" not in ori_config_db or "dhcp_server" not in ori_config_db["FEATURE"]:
return "{}"
ori_config_db["FEATURE"]["dhcp_server"]["state"] = "enabled"
gold_config_db = {
"FEATURE": copy.deepcopy(ori_config_db["FEATURE"]),
"PORT": copy.deepcopy(ori_config_db["PORT"])
}
# Generate dhcp_server related configuration
rc, out, err = self.module.run_command("cat {}".format(TEMP_DHCP_SERVER_CONFIG_PATH))
if rc != 0:
self.module.fail_json(msg="Failed to get dhcp_server config: {}".format(err))
if self.port_index_map is None or self.port_index_map == {}:
self.module.fail_json(msg="port_index_map is missing")
dhcp_server_config_obj = json.loads(out)
# Update DHCP_SERVER_IPV4_PORT based on port index map
dhcp_server_port_config = {}
for key, value in dhcp_server_config_obj["DHCP_SERVER_IPV4_PORT"].items():
splits = key.split("|")
new_key = "{}|{}".format(splits[0], self.port_index_map[splits[1]])
dhcp_server_port_config[new_key] = value
dhcp_server_config_obj["DHCP_SERVER_IPV4_PORT"] = dhcp_server_port_config
gold_config_db.update(dhcp_server_config_obj)
return gold_config_db
def check_version_for_bmp(self):
output_version = device_info.get_sonic_version_info()
build_version = output_version['build_version']
if re.match(r'^(\d{6})', build_version):
version_number = int(re.findall(r'\d{6}', build_version)[0])
if version_number < 202411:
return False
elif re.match(r'^internal-(\d{6})', build_version):
internal_version_number = int(re.findall(r'\d{6}', build_version)[0])
if internal_version_number < 202411:
return False
else:
return True
return True
def get_config_from_minigraph(self):
rc, out, err = self.module.run_command("sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data")
if rc != 0:
self.module.fail_json(msg="Failed to get config from minigraph: {}".format(err))
return out
def get_multiasic_feature_config(self):
rc, out, err = self.module.run_command("show runningconfiguration all")
if rc != 0:
self.module.fail_json(msg="Failed to get config from runningconfiguration: {}".format(err))
return out
def overwrite_feature_golden_config_db_multiasic(self, config, feature_key):
full_config = json.loads(config)
if config == "{}" or "FEATURE" not in config["localhost"]:
# need dump running config FEATURE + selected feature
gold_config_db = json.loads(self.get_multiasic_feature_config())
else:
# need existing config + selected feature
gold_config_db = full_config
feature_data = {
feature_key: {
"auto_restart": "enabled",
"check_up_status": "false",
"delayed": "False",
"has_global_scope": "False",
"has_per_asic_scope": "True",
"high_mem_alert": "disabled",
"set_owner": "local",
"state": "enabled",
"support_syslog_rate_limit": "false"
}
}
for namespace, ns_data in gold_config_db.items():
if "FEATURE" in ns_data:
feature_section = ns_data["FEATURE"]
feature_section.update(feature_data)
ns_data["FEATURE"] = feature_section
return json.dumps(gold_config_db, indent=4)
def overwrite_feature_golden_config_db_singleasic(self, config, feature_key):
full_config = config
onlyFeature = config == "{}" # FEATURE needs special handling since it does not support incremental update.
if config == "{}":
full_config = self.get_config_from_minigraph()
ori_config_db = json.loads(full_config)
if "FEATURE" not in ori_config_db:
full_config = self.get_config_from_minigraph()
feature_config_db = json.loads(full_config)
ori_config_db["FEATURE"] = feature_config_db.get("FEATURE", {})
# Append the specified feature section to the original "FEATURE" section
ori_config_db.setdefault("FEATURE", {}).setdefault(feature_key, {}).update({
"auto_restart": "enabled",
"check_up_status": "false",
"delayed": "False",
"has_global_scope": "True",
"has_per_asic_scope": "False",
"high_mem_alert": "disabled",
"set_owner": "local",
"state": "enabled",
"support_syslog_rate_limit": "false"
})
# Create the gold_config_db dictionary with both "FEATURE" and the specified feature section
if onlyFeature:
gold_config_db = {
"FEATURE": copy.deepcopy(ori_config_db["FEATURE"])
}
else:
gold_config_db = ori_config_db
return json.dumps(gold_config_db, indent=4)
def generate_smartswitch_golden_config_db(self):
rc, out, err = self.module.run_command("sonic-cfggen -H -m -j /etc/sonic/init_cfg.json --print-data")
if rc != 0:
self.module.fail_json(msg="Failed to get config from minigraph: {}".format(err))
# Generate FEATURE table from init_cfg.ini
ori_config_db = json.loads(out)
if "DEVICE_METADATA" not in ori_config_db or "localhost" not in ori_config_db["DEVICE_METADATA"]:
return "{}"
ori_config_db["DEVICE_METADATA"]["localhost"]["subtype"] = "SmartSwitch"
hwsku = ori_config_db["DEVICE_METADATA"]["localhost"].get("hwsku", None)
if "FEATURE" not in ori_config_db \
or "dhcp_server" not in ori_config_db["FEATURE"] \
or "dhcp_relay" not in ori_config_db["FEATURE"]:
return "{}"
ori_config_db["FEATURE"]["dhcp_server"]["state"] = "enabled"
ori_config_db["FEATURE"]["dhcp_relay"]["state"] = "enabled"
# Generate INTERFACE table for EthernetBPXX
if "PORT" not in ori_config_db or "INTERFACE" not in ori_config_db:
return "{}"
if hwsku not in smartswitch_hwsku_config:
return "{}"
if "DPUS" not in ori_config_db:
ori_config_db["DPUS"] = {}
if "CHASSIS_MODULE" not in ori_config_db:
ori_config_db["CHASSIS_MODULE"] = {}
if "DHCP_SERVER_IPV4_PORT" not in ori_config_db:
ori_config_db["DHCP_SERVER_IPV4_PORT"] = {}
hwsku_config = smartswitch_hwsku_config[hwsku]
for i in range(smartswitch_hwsku_config[hwsku]["dpu_num"]):
if "base" in hwsku_config and "step" in hwsku_config:
port_key = hwsku_config["port_key"].format(hwsku_config["base"] + i * hwsku_config["step"])
else:
port_key = hwsku_config["port_key"].format(i)
if "interface_key" in hwsku_config:
interface_key = hwsku_config["interface_key"].format(i, i)
dpu_key = hwsku_config["dpu_key"].format(i)
if port_key in ori_config_db["PORT"]:
ori_config_db["PORT"][port_key]["admin_status"] = "up"
if "interface_key" in hwsku_config:
ori_config_db["INTERFACE"][port_key] = {}
ori_config_db["INTERFACE"][interface_key] = {}
ori_config_db["CHASSIS_MODULE"]["DPU{}".format(i)] = {"admin_status": "up"}
if dpu_key not in ori_config_db["DPUS"]:
ori_config_db["DPUS"][dpu_key] = {}
ori_config_db["DPUS"][dpu_key]["midplane_interface"] = dpu_key
key = "bridge-midplane|dpu{}".format(i)
if key not in ori_config_db["DHCP_SERVER_IPV4_PORT"]:
ori_config_db["DHCP_SERVER_IPV4_PORT"][key] = {}
ori_config_db["DHCP_SERVER_IPV4_PORT"][key]["ips"] = ["169.254.200.{}".format(i + 1)]
mid_plane_bridge_config = {
"GLOBAL": {
"bridge": "bridge-midplane",
"ip_prefix": "169.254.200.254/24"
}
}
ori_config_db["MID_PLANE_BRIDGE"] = mid_plane_bridge_config
dhcp_server_ipv4_config = {
"DHCP_SERVER_IPV4": {
"bridge-midplane": {
"gateway": "169.254.200.254",
"lease_time": "600000000",
"mode": "PORT",
"netmask": "255.255.255.0",
"state": "enabled"
}
}
}
ori_config_db["DHCP_SERVER_IPV4"] = dhcp_server_ipv4_config["DHCP_SERVER_IPV4"]
gold_config_db = {
"DEVICE_METADATA": copy.deepcopy(ori_config_db["DEVICE_METADATA"]),
"FEATURE": copy.deepcopy(ori_config_db["FEATURE"]),
"INTERFACE": copy.deepcopy(ori_config_db["INTERFACE"]),
"PORT": copy.deepcopy(ori_config_db["PORT"]),
"CHASSIS_MODULE": copy.deepcopy(ori_config_db["CHASSIS_MODULE"]),
"DPUS": copy.deepcopy(ori_config_db["DPUS"]),
"DHCP_SERVER_IPV4_PORT": copy.deepcopy(ori_config_db["DHCP_SERVER_IPV4_PORT"]),
"MID_PLANE_BRIDGE": copy.deepcopy(ori_config_db["MID_PLANE_BRIDGE"]),
"DHCP_SERVER_IPV4": copy.deepcopy(ori_config_db["DHCP_SERVER_IPV4"])
}
# Generate dhcp_server related configuration
rc, out, err = self.module.run_command("cat {}".format(TEMP_SMARTSWITCH_CONFIG_PATH))
if rc != 0:
self.module.fail_json(msg="Failed to get smartswitch config: {}".format(err))
smartswitch_config_obj = json.loads(out)
gold_config_db.update(smartswitch_config_obj)
return json.dumps(gold_config_db, indent=4)
def generate_t2_golden_config_db(self):
with open(MACSEC_PROFILE_PATH) as f:
macsec_profiles = json.load(f)
profile = macsec_profiles.get(self.macsec_profile)
if profile:
profile['macsec_profile'] = self.macsec_profile
# Update the profile context with the asic count
profile['asic_cnt'] = self.num_asics
def safe_open_template(template_path):
with open(template_path) as template_file:
return Template(template_file.read())
# Render the template using the profile
rendered_json = safe_open_template(GOLDEN_CONFIG_TEMPLATE_PATH).render(profile)
return rendered_json
def update_dns_config(self, config):
# Generate dns_server related configuration
rc, out, err = self.module.run_command("cat {}".format(DNS_CONFIG_PATH))
if rc != 0:
self.module.fail_json(msg="Failed to get dns config: {}".format(err))
try:
dns_config_obj = json.loads(out)
except json.JSONDecodeError:
self.module.fail_json(msg="Invalid JSON in DNS config: {}".format(out))
if "DNS_NAMESERVER" in dns_config_obj:
ori_config_db = json.loads(config)
if multi_asic.is_multi_asic():
for key, value in ori_config_db.items():
value.update(dns_config_obj)
else:
ori_config_db.update(dns_config_obj)
return json.dumps(ori_config_db, indent=4)
else:
return config
def generate(self):
# topo check
if self.topo_name == "mx" or "m0" in self.topo_name:
config = self.generate_mgfx_golden_config_db()
self.module.run_command("sudo rm -f {}".format(TEMP_DHCP_SERVER_CONFIG_PATH))
elif self.topo_name in ["t1-smartswitch-ha", "t1-28-lag", "smartswitch-t1"]:
config = self.generate_smartswitch_golden_config_db()
self.module.run_command("sudo rm -f {}".format(TEMP_SMARTSWITCH_CONFIG_PATH))
elif "t2" in self.topo_name and self.macsec_profile:
config = self.generate_t2_golden_config_db()
self.module.run_command("sudo rm -f {}".format(MACSEC_PROFILE_PATH))
self.module.run_command("sudo rm -f {}".format(GOLDEN_CONFIG_TEMPLATE_PATH))
else:
config = "{}"
# update dns config
config = self.update_dns_config(config)
# To enable bmp feature when the image version is >= 202411 and the device is not supervisor
# Note: the Chassis supervisor is not holding any BGP sessions so the BMP feature is not needed
if self.check_version_for_bmp() is True and device_info.is_supervisor() is False:
if multi_asic.is_multi_asic():
config = self.overwrite_feature_golden_config_db_multiasic(config, "bmp")
else:
config = self.overwrite_feature_golden_config_db_singleasic(config, "bmp")
with open(GOLDEN_CONFIG_DB_PATH, "w") as temp_file:
temp_file.write(config)
self.module.exit_json(change=True, msg="Success to generate golden_config_db.json")
def main():
generate_golden_config_db = GenerateGoldenConfigDBModule()
generate_golden_config_db.generate()
if __name__ == '__main__':
main()