osism 0.20250621.0__py3-none-any.whl → 0.20250628.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- osism/api.py +264 -94
- osism/commands/apply.py +2 -2
- osism/commands/manage.py +251 -0
- osism/commands/reconciler.py +8 -1
- osism/commands/validate.py +1 -1
- osism/tasks/conductor/ironic.py +15 -17
- osism/tasks/conductor/sonic/config_generator.py +133 -41
- osism/tasks/conductor/sonic/constants.py +1 -0
- osism/tasks/conductor/sonic/interface.py +167 -16
- osism/tasks/conductor/sonic/sync.py +26 -1
- osism/tasks/reconciler.py +12 -2
- {osism-0.20250621.0.dist-info → osism-0.20250628.0.dist-info}/METADATA +4 -4
- {osism-0.20250621.0.dist-info → osism-0.20250628.0.dist-info}/RECORD +21 -24
- {osism-0.20250621.0.dist-info → osism-0.20250628.0.dist-info}/entry_points.txt +1 -2
- osism-0.20250628.0.dist-info/pbr.json +1 -0
- osism/actions/__init__.py +0 -1
- osism/core/__init__.py +0 -1
- osism/plugins/__init__.py +0 -1
- osism-0.20250621.0.dist-info/pbr.json +0 -1
- /osism/{core → data}/enums.py +0 -0
- /osism/{core → data}/playbooks.py +0 -0
- {osism-0.20250621.0.dist-info → osism-0.20250628.0.dist-info}/WHEEL +0 -0
- {osism-0.20250621.0.dist-info → osism-0.20250628.0.dist-info}/licenses/AUTHORS +0 -0
- {osism-0.20250621.0.dist-info → osism-0.20250628.0.dist-info}/licenses/LICENSE +0 -0
- {osism-0.20250621.0.dist-info → osism-0.20250628.0.dist-info}/top_level.txt +0 -0
osism/commands/manage.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# SPDX-License-Identifier: Apache-2.0
|
2
2
|
|
3
|
+
import json
|
4
|
+
import os
|
5
|
+
from datetime import datetime
|
3
6
|
from re import findall
|
4
7
|
from urllib.parse import urljoin
|
5
8
|
|
@@ -7,10 +10,12 @@ from cliff.command import Command
|
|
7
10
|
import docker
|
8
11
|
from jinja2 import Template
|
9
12
|
from loguru import logger
|
13
|
+
import paramiko
|
10
14
|
import requests
|
11
15
|
|
12
16
|
from osism.data import TEMPLATE_IMAGE_CLUSTERAPI, TEMPLATE_IMAGE_OCTAVIA
|
13
17
|
from osism.tasks import openstack, ansible, handle_task
|
18
|
+
from osism import utils
|
14
19
|
|
15
20
|
SUPPORTED_CLUSTERAPI_K8S_IMAGES = ["1.31", "1.32", "1.33"]
|
16
21
|
|
@@ -384,3 +389,249 @@ class Dnsmasq(Command):
|
|
384
389
|
)
|
385
390
|
|
386
391
|
return handle_task(task, wait, format="log", timeout=300)
|
392
|
+
|
393
|
+
|
394
|
+
class Sonic(Command):
|
395
|
+
def get_parser(self, prog_name):
|
396
|
+
parser = super(Sonic, self).get_parser(prog_name)
|
397
|
+
parser.add_argument(
|
398
|
+
"hostname", type=str, help="Hostname of the SONiC switch to manage"
|
399
|
+
)
|
400
|
+
parser.add_argument(
|
401
|
+
"--reload",
|
402
|
+
action="store_true",
|
403
|
+
help="Execute config reload after config load to restart services",
|
404
|
+
)
|
405
|
+
return parser
|
406
|
+
|
407
|
+
def take_action(self, parsed_args):
|
408
|
+
hostname = parsed_args.hostname
|
409
|
+
reload_config = parsed_args.reload
|
410
|
+
today = datetime.now().strftime("%Y%m%d")
|
411
|
+
|
412
|
+
try:
|
413
|
+
# Get device from NetBox - try by name first, then by inventory_hostname
|
414
|
+
device = utils.nb.dcim.devices.get(name=hostname)
|
415
|
+
if not device:
|
416
|
+
# Try to find by inventory_hostname custom field
|
417
|
+
devices = utils.nb.dcim.devices.filter(cf_inventory_hostname=hostname)
|
418
|
+
if devices:
|
419
|
+
device = devices[0] # Take the first match
|
420
|
+
logger.info(f"Device found by inventory_hostname: {device.name}")
|
421
|
+
else:
|
422
|
+
logger.error(
|
423
|
+
f"Device {hostname} not found in NetBox (searched by name and inventory_hostname)"
|
424
|
+
)
|
425
|
+
return 1
|
426
|
+
|
427
|
+
# Get device configuration from local_context_data
|
428
|
+
if (
|
429
|
+
not hasattr(device, "local_context_data")
|
430
|
+
or not device.local_context_data
|
431
|
+
):
|
432
|
+
logger.error(f"Device {hostname} has no local_context_data in NetBox")
|
433
|
+
return 1
|
434
|
+
|
435
|
+
config_context = device.local_context_data
|
436
|
+
|
437
|
+
# Save config context to local /tmp directory
|
438
|
+
config_context_file = f"/tmp/config_db_{hostname}_{today}.json"
|
439
|
+
try:
|
440
|
+
with open(config_context_file, "w") as f:
|
441
|
+
json.dump(config_context, f, indent=2)
|
442
|
+
logger.info(f"Config context saved to {config_context_file}")
|
443
|
+
except Exception as e:
|
444
|
+
logger.error(f"Failed to save config context: {e}")
|
445
|
+
return 1
|
446
|
+
|
447
|
+
# Extract SSH connection details
|
448
|
+
ssh_host = None
|
449
|
+
ssh_username = None
|
450
|
+
|
451
|
+
# Try to get SSH details from config context
|
452
|
+
if "management" in config_context:
|
453
|
+
mgmt = config_context["management"]
|
454
|
+
if "ip" in mgmt:
|
455
|
+
ssh_host = mgmt["ip"]
|
456
|
+
if "username" in mgmt:
|
457
|
+
ssh_username = mgmt["username"]
|
458
|
+
|
459
|
+
# Fallback: try to get OOB IP from NetBox
|
460
|
+
if not ssh_host:
|
461
|
+
from osism.tasks.conductor.netbox import get_device_oob_ip
|
462
|
+
|
463
|
+
oob_result = get_device_oob_ip(device)
|
464
|
+
if oob_result:
|
465
|
+
ssh_host = oob_result[0]
|
466
|
+
|
467
|
+
if not ssh_host:
|
468
|
+
logger.error(f"No SSH host found for device {hostname}")
|
469
|
+
return 1
|
470
|
+
|
471
|
+
if not ssh_username:
|
472
|
+
ssh_username = "admin" # Default SONiC username
|
473
|
+
|
474
|
+
# SSH private key path
|
475
|
+
ssh_key_path = "/ansible/secrets/id_rsa.operator"
|
476
|
+
|
477
|
+
if not os.path.exists(ssh_key_path):
|
478
|
+
logger.error(f"SSH private key not found at {ssh_key_path}")
|
479
|
+
return 1
|
480
|
+
|
481
|
+
logger.info(
|
482
|
+
f"Connecting to {hostname} ({ssh_host}) to backup SONiC configuration"
|
483
|
+
)
|
484
|
+
|
485
|
+
# Create SSH connection
|
486
|
+
ssh = paramiko.SSHClient()
|
487
|
+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
488
|
+
|
489
|
+
try:
|
490
|
+
# Connect with private key
|
491
|
+
ssh.connect(
|
492
|
+
hostname=ssh_host,
|
493
|
+
username=ssh_username,
|
494
|
+
key_filename=ssh_key_path,
|
495
|
+
timeout=30,
|
496
|
+
)
|
497
|
+
|
498
|
+
# Generate backup filename with date and increment on switch
|
499
|
+
base_backup_path = f"/home/admin/config_db_{hostname}_{today}"
|
500
|
+
backup_filename = f"{base_backup_path}_1.json"
|
501
|
+
|
502
|
+
# Find next available filename on switch
|
503
|
+
x = 1
|
504
|
+
while True:
|
505
|
+
check_cmd = f"ls {base_backup_path}_{x}.json 2>/dev/null"
|
506
|
+
stdin, stdout, stderr = ssh.exec_command(check_cmd)
|
507
|
+
if stdout.read().decode().strip() == "":
|
508
|
+
backup_filename = f"{base_backup_path}_{x}.json"
|
509
|
+
break
|
510
|
+
x += 1
|
511
|
+
|
512
|
+
logger.info(
|
513
|
+
f"Backing up current configuration on switch to {backup_filename}"
|
514
|
+
)
|
515
|
+
|
516
|
+
# Backup current configuration on switch
|
517
|
+
backup_cmd = f"sudo cp /etc/sonic/config_db.json {backup_filename}"
|
518
|
+
stdin, stdout, stderr = ssh.exec_command(backup_cmd)
|
519
|
+
exit_status = stdout.channel.recv_exit_status()
|
520
|
+
|
521
|
+
if exit_status != 0:
|
522
|
+
error_msg = stderr.read().decode()
|
523
|
+
logger.error(
|
524
|
+
f"Failed to backup configuration on switch: {error_msg}"
|
525
|
+
)
|
526
|
+
return 1
|
527
|
+
|
528
|
+
logger.info("Configuration backed up successfully on switch")
|
529
|
+
|
530
|
+
# Upload local config context to switch /tmp directory
|
531
|
+
switch_config_file = f"/tmp/config_db_{hostname}_current.json"
|
532
|
+
logger.info(
|
533
|
+
f"Uploading config context to {switch_config_file} on switch"
|
534
|
+
)
|
535
|
+
|
536
|
+
# Use SFTP to upload the config context file
|
537
|
+
sftp = ssh.open_sftp()
|
538
|
+
try:
|
539
|
+
sftp.put(config_context_file, switch_config_file)
|
540
|
+
logger.info(
|
541
|
+
f"Config context successfully uploaded to {switch_config_file} on switch"
|
542
|
+
)
|
543
|
+
except Exception as e:
|
544
|
+
logger.error(f"Failed to upload config context to switch: {e}")
|
545
|
+
return 1
|
546
|
+
finally:
|
547
|
+
sftp.close()
|
548
|
+
|
549
|
+
# Load and apply the new configuration
|
550
|
+
logger.info("Loading and applying new configuration on switch")
|
551
|
+
|
552
|
+
load_cmd = f"sudo config load -y {switch_config_file}"
|
553
|
+
stdin, stdout, stderr = ssh.exec_command(load_cmd)
|
554
|
+
exit_status = stdout.channel.recv_exit_status()
|
555
|
+
|
556
|
+
if exit_status != 0:
|
557
|
+
error_msg = stderr.read().decode()
|
558
|
+
logger.error(f"Failed to load configuration: {error_msg}")
|
559
|
+
return 1
|
560
|
+
|
561
|
+
logger.info("Configuration loaded and applied successfully")
|
562
|
+
|
563
|
+
# Optionally reload configuration to restart services
|
564
|
+
config_operations_successful = True
|
565
|
+
if reload_config:
|
566
|
+
logger.info("Reloading configuration to restart services")
|
567
|
+
|
568
|
+
reload_cmd = "sudo config reload -y"
|
569
|
+
stdin, stdout, stderr = ssh.exec_command(reload_cmd)
|
570
|
+
exit_status = stdout.channel.recv_exit_status()
|
571
|
+
|
572
|
+
if exit_status != 0:
|
573
|
+
error_msg = stderr.read().decode()
|
574
|
+
logger.error(f"Failed to reload configuration: {error_msg}")
|
575
|
+
config_operations_successful = False
|
576
|
+
else:
|
577
|
+
logger.info("Configuration reloaded successfully")
|
578
|
+
|
579
|
+
# Save configuration only if load (and optionally reload) were successful
|
580
|
+
if config_operations_successful:
|
581
|
+
logger.info("Saving configuration to persist changes")
|
582
|
+
|
583
|
+
save_cmd = "sudo config save -y"
|
584
|
+
stdin, stdout, stderr = ssh.exec_command(save_cmd)
|
585
|
+
exit_status = stdout.channel.recv_exit_status()
|
586
|
+
|
587
|
+
if exit_status != 0:
|
588
|
+
error_msg = stderr.read().decode()
|
589
|
+
logger.error(f"Failed to save configuration: {error_msg}")
|
590
|
+
return 1
|
591
|
+
|
592
|
+
logger.info("Configuration saved successfully")
|
593
|
+
else:
|
594
|
+
logger.warning("Skipping config save due to reload failure")
|
595
|
+
|
596
|
+
# Delete the temporary configuration file
|
597
|
+
logger.info(f"Cleaning up temporary file {switch_config_file}")
|
598
|
+
|
599
|
+
delete_cmd = f"rm {switch_config_file}"
|
600
|
+
stdin, stdout, stderr = ssh.exec_command(delete_cmd)
|
601
|
+
exit_status = stdout.channel.recv_exit_status()
|
602
|
+
|
603
|
+
if exit_status != 0:
|
604
|
+
error_msg = stderr.read().decode()
|
605
|
+
logger.warning(f"Failed to delete temporary file: {error_msg}")
|
606
|
+
else:
|
607
|
+
logger.info("Temporary file deleted successfully")
|
608
|
+
|
609
|
+
logger.info("SONiC configuration management completed successfully")
|
610
|
+
logger.info(f"- Config context saved locally to: {config_context_file}")
|
611
|
+
if reload_config and config_operations_successful:
|
612
|
+
logger.info("- Configuration loaded, reloaded, and saved on switch")
|
613
|
+
elif config_operations_successful:
|
614
|
+
logger.info("- Configuration loaded and saved on switch")
|
615
|
+
else:
|
616
|
+
logger.info(
|
617
|
+
"- Configuration loaded on switch (save skipped due to reload failure)"
|
618
|
+
)
|
619
|
+
logger.info(f"- Backup created on switch: {backup_filename}")
|
620
|
+
|
621
|
+
return 0
|
622
|
+
|
623
|
+
except paramiko.AuthenticationException:
|
624
|
+
logger.error(f"Authentication failed for {ssh_host}")
|
625
|
+
return 1
|
626
|
+
except paramiko.SSHException as e:
|
627
|
+
logger.error(f"SSH connection failed: {e}")
|
628
|
+
return 1
|
629
|
+
except Exception as e:
|
630
|
+
logger.error(f"Unexpected error during SSH operations: {e}")
|
631
|
+
return 1
|
632
|
+
finally:
|
633
|
+
ssh.close()
|
634
|
+
|
635
|
+
except Exception as e:
|
636
|
+
logger.error(f"Error managing SONiC device {hostname}: {e}")
|
637
|
+
return 1
|
osism/commands/reconciler.py
CHANGED
@@ -38,13 +38,20 @@ class Sync(Command):
|
|
38
38
|
type=int,
|
39
39
|
help="Timeout for a scheduled task that has not been executed yet",
|
40
40
|
)
|
41
|
+
parser.add_argument(
|
42
|
+
"--flush-cache",
|
43
|
+
default=False,
|
44
|
+
help="Flush cache before running sync",
|
45
|
+
action="store_true",
|
46
|
+
)
|
41
47
|
return parser
|
42
48
|
|
43
49
|
def take_action(self, parsed_args):
|
44
50
|
wait = not parsed_args.no_wait
|
45
51
|
task_timeout = parsed_args.task_timeout
|
52
|
+
flush_cache = parsed_args.flush_cache
|
46
53
|
|
47
|
-
t = reconciler.run.delay(publish=wait)
|
54
|
+
t = reconciler.run.delay(publish=wait, flush_cache=flush_cache)
|
48
55
|
if wait:
|
49
56
|
logger.info(
|
50
57
|
f"Task {t.task_id} (sync inventory) is running in background. Output coming soon."
|
osism/commands/validate.py
CHANGED
osism/tasks/conductor/ironic.py
CHANGED
@@ -235,23 +235,21 @@ def sync_ironic(request_id, get_ironic_parameters, force_update=False):
|
|
235
235
|
details=False, attributes=dict(node_uuid=node["uuid"])
|
236
236
|
)
|
237
237
|
# NOTE: Baremetal ports are only required for (i)pxe boot
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
port
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
else:
|
254
|
-
node_ports.remove(port[0])
|
238
|
+
for port_attributes in ports_attributes:
|
239
|
+
port_attributes.update({"node_id": node["uuid"]})
|
240
|
+
port = [
|
241
|
+
port
|
242
|
+
for port in node_ports
|
243
|
+
if port_attributes["address"].upper() == port["address"].upper()
|
244
|
+
]
|
245
|
+
if not port:
|
246
|
+
osism_utils.push_task_output(
|
247
|
+
request_id,
|
248
|
+
f"Creating baremetal port with MAC address {port_attributes['address']} for {device.name}\n",
|
249
|
+
)
|
250
|
+
openstack.baremetal_port_create(port_attributes)
|
251
|
+
else:
|
252
|
+
node_ports.remove(port[0])
|
255
253
|
for node_port in node_ports:
|
256
254
|
# NOTE: Delete remaining ports not found in NetBox
|
257
255
|
osism_utils.push_task_output(
|
@@ -29,7 +29,6 @@ from .interface import (
|
|
29
29
|
from .connections import (
|
30
30
|
get_connected_interfaces,
|
31
31
|
get_connected_device_for_sonic_interface,
|
32
|
-
get_device_bgp_neighbors_via_loopback,
|
33
32
|
)
|
34
33
|
from .cache import get_cached_device_interfaces
|
35
34
|
|
@@ -305,7 +304,7 @@ def _add_port_configurations(
|
|
305
304
|
interface_speed = int(port_speed) if port_speed else None
|
306
305
|
is_breakout_port = port_name in breakout_info["breakout_ports"]
|
307
306
|
correct_alias = convert_sonic_interface_to_alias(
|
308
|
-
port_name, interface_speed, is_breakout_port
|
307
|
+
port_name, interface_speed, is_breakout_port, port_config
|
309
308
|
)
|
310
309
|
|
311
310
|
# Use master port index for breakout ports
|
@@ -457,7 +456,7 @@ def _add_missing_breakout_ports(
|
|
457
456
|
# Generate correct alias (breakout port always gets subport notation)
|
458
457
|
interface_speed = int(port_speed)
|
459
458
|
correct_alias = convert_sonic_interface_to_alias(
|
460
|
-
port_name, interface_speed, is_breakout=True
|
459
|
+
port_name, interface_speed, is_breakout=True, port_config=port_config
|
461
460
|
)
|
462
461
|
|
463
462
|
# Use master port index for breakout ports
|
@@ -608,11 +607,6 @@ def _add_bgp_configurations(
|
|
608
607
|
"v6only": "true",
|
609
608
|
}
|
610
609
|
|
611
|
-
# Add additional BGP_NEIGHBOR configuration using Loopback0 IP addresses
|
612
|
-
_add_loopback_bgp_neighbors(
|
613
|
-
config, device, portchannel_info, connected_interfaces, device_as_mapping
|
614
|
-
)
|
615
|
-
|
616
610
|
|
617
611
|
def _get_connected_device_for_interface(device, interface_name):
|
618
612
|
"""Get the connected device for a given interface name.
|
@@ -652,10 +646,40 @@ def _determine_peer_type(local_device, connected_device, device_as_mapping=None)
|
|
652
646
|
connected_as = None
|
653
647
|
if device_as_mapping and connected_device.id in device_as_mapping:
|
654
648
|
connected_as = device_as_mapping[connected_device.id]
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
649
|
+
else:
|
650
|
+
# If connected device is not in device_as_mapping, check if it's a spine/superspine
|
651
|
+
# and calculate AS for its group
|
652
|
+
if connected_device.role and connected_device.role.slug in [
|
653
|
+
"spine",
|
654
|
+
"superspine",
|
655
|
+
]:
|
656
|
+
# Import here to avoid circular imports
|
657
|
+
from .bgp import calculate_minimum_as_for_group
|
658
|
+
from .connections import find_interconnected_devices
|
659
|
+
|
660
|
+
# Get all devices to find the group
|
661
|
+
all_devices = list(
|
662
|
+
utils.nb.dcim.devices.filter(role=["spine", "superspine"])
|
663
|
+
)
|
664
|
+
spine_groups = find_interconnected_devices(
|
665
|
+
all_devices, ["spine", "superspine"]
|
666
|
+
)
|
667
|
+
|
668
|
+
# Find which group the connected device belongs to
|
669
|
+
for group in spine_groups:
|
670
|
+
if any(dev.id == connected_device.id for dev in group):
|
671
|
+
connected_as = calculate_minimum_as_for_group(group)
|
672
|
+
if connected_as:
|
673
|
+
logger.debug(
|
674
|
+
f"Calculated AS {connected_as} for connected spine/superspine device {connected_device.name}"
|
675
|
+
)
|
676
|
+
break
|
677
|
+
|
678
|
+
# Fallback to calculating from IPv4 if still no AS
|
679
|
+
if not connected_as and connected_device.primary_ip4:
|
680
|
+
connected_as = calculate_local_asn_from_ipv4(
|
681
|
+
str(connected_device.primary_ip4.address)
|
682
|
+
)
|
659
683
|
|
660
684
|
# Compare AS numbers
|
661
685
|
if local_as and connected_as and local_as == connected_as:
|
@@ -670,30 +694,91 @@ def _determine_peer_type(local_device, connected_device, device_as_mapping=None)
|
|
670
694
|
return "external" # Default to external on error
|
671
695
|
|
672
696
|
|
673
|
-
def
|
674
|
-
|
675
|
-
|
676
|
-
|
697
|
+
def _get_ntp_server_for_device(device):
|
698
|
+
"""Get single NTP server IP for a SONiC device based on OOB connection to metalbox.
|
699
|
+
|
700
|
+
Returns the IP address of the metalbox device interface that is connected to the
|
701
|
+
OOB switch. If VLANs are used, returns the IP of the VLAN interface where the
|
702
|
+
SONiC switch management interface (eth0) has access.
|
703
|
+
|
704
|
+
Args:
|
705
|
+
device: SONiC device object
|
706
|
+
|
707
|
+
Returns:
|
708
|
+
str: IP address of the NTP server or None if not found
|
709
|
+
"""
|
677
710
|
try:
|
678
|
-
# Get
|
679
|
-
|
680
|
-
|
681
|
-
|
711
|
+
# Get the OOB IP configuration for this SONiC device
|
712
|
+
oob_ip_result = get_device_oob_ip(device)
|
713
|
+
if not oob_ip_result:
|
714
|
+
logger.debug(f"No OOB IP found for device {device.name}")
|
715
|
+
return None
|
682
716
|
|
683
|
-
|
684
|
-
|
717
|
+
oob_ip, prefix_len = oob_ip_result
|
718
|
+
logger.debug(f"Device {device.name} has OOB IP {oob_ip}/{prefix_len}")
|
685
719
|
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
device_as_mapping,
|
691
|
-
)
|
720
|
+
# Find the network/subnet that contains this OOB IP
|
721
|
+
from ipaddress import IPv4Network, IPv4Address
|
722
|
+
|
723
|
+
device_network = IPv4Network(f"{oob_ip}/{prefix_len}", strict=False)
|
692
724
|
|
693
|
-
|
725
|
+
# Get all metalbox devices
|
726
|
+
metalbox_devices = utils.nb.dcim.devices.filter(role="metalbox")
|
727
|
+
|
728
|
+
for metalbox in metalbox_devices:
|
729
|
+
logger.debug(f"Checking metalbox device {metalbox.name} for NTP server")
|
730
|
+
|
731
|
+
# Get all interfaces on this metalbox
|
732
|
+
interfaces = utils.nb.dcim.interfaces.filter(device_id=metalbox.id)
|
733
|
+
|
734
|
+
for interface in interfaces:
|
735
|
+
# Skip management-only interfaces
|
736
|
+
if hasattr(interface, "mgmt_only") and interface.mgmt_only:
|
737
|
+
continue
|
738
|
+
|
739
|
+
# Check both physical interfaces and VLAN interfaces (SVIs)
|
740
|
+
# VLAN interfaces are typically named "Vlan123" for VLAN ID 123
|
741
|
+
is_vlan_interface = (
|
742
|
+
hasattr(interface, "type")
|
743
|
+
and interface.type
|
744
|
+
and interface.type.value == "virtual"
|
745
|
+
and interface.name.startswith("Vlan")
|
746
|
+
)
|
747
|
+
|
748
|
+
# Get IP addresses for this interface
|
749
|
+
ip_addresses = utils.nb.ipam.ip_addresses.filter(
|
750
|
+
assigned_object_id=interface.id,
|
751
|
+
)
|
752
|
+
|
753
|
+
for ip_addr in ip_addresses:
|
754
|
+
if ip_addr.address:
|
755
|
+
# Extract IP address without prefix
|
756
|
+
ip_only = ip_addr.address.split("/")[0]
|
757
|
+
|
758
|
+
# Check if it's IPv4 and in the same network as the SONiC device
|
759
|
+
try:
|
760
|
+
metalbox_ip = IPv4Address(ip_only)
|
761
|
+
if metalbox_ip in device_network:
|
762
|
+
interface_type = (
|
763
|
+
"VLAN interface"
|
764
|
+
if is_vlan_interface
|
765
|
+
else "interface"
|
766
|
+
)
|
767
|
+
logger.info(
|
768
|
+
f"Found NTP server {ip_only} on metalbox {metalbox.name} "
|
769
|
+
f"{interface_type} {interface.name} for SONiC device {device.name}"
|
770
|
+
)
|
771
|
+
return ip_only
|
772
|
+
except ValueError:
|
773
|
+
# Skip non-IPv4 addresses
|
774
|
+
continue
|
775
|
+
|
776
|
+
logger.warning(f"No suitable NTP server found for SONiC device {device.name}")
|
777
|
+
return None
|
694
778
|
|
695
779
|
except Exception as e:
|
696
|
-
logger.warning(f"Could not
|
780
|
+
logger.warning(f"Could not determine NTP server for device {device.name}: {e}")
|
781
|
+
return None
|
697
782
|
|
698
783
|
|
699
784
|
def _get_ntp_servers():
|
@@ -755,20 +840,27 @@ def _get_ntp_servers():
|
|
755
840
|
|
756
841
|
|
757
842
|
def _add_ntp_configuration(config, device):
|
758
|
-
"""Add NTP_SERVER configuration to device config.
|
759
|
-
try:
|
760
|
-
ntp_servers = _get_ntp_servers()
|
843
|
+
"""Add NTP_SERVER configuration to device config.
|
761
844
|
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
845
|
+
Each SONiC switch gets exactly one NTP server - the IP address of the
|
846
|
+
metalbox device interface connected to the OOB switch.
|
847
|
+
"""
|
848
|
+
try:
|
849
|
+
# Get the specific NTP server for this device
|
850
|
+
ntp_server_ip = _get_ntp_server_for_device(device)
|
851
|
+
|
852
|
+
if ntp_server_ip:
|
853
|
+
# Add single NTP server configuration
|
854
|
+
config["NTP_SERVER"][ntp_server_ip] = {
|
855
|
+
"maxpoll": "10",
|
856
|
+
"minpoll": "6",
|
857
|
+
"prefer": "false",
|
858
|
+
}
|
859
|
+
logger.info(
|
860
|
+
f"Added NTP server {ntp_server_ip} to SONiC device {device.name}"
|
769
861
|
)
|
770
862
|
else:
|
771
|
-
logger.
|
863
|
+
logger.warning(f"No NTP server found for SONiC device {device.name}")
|
772
864
|
|
773
865
|
except Exception as e:
|
774
866
|
logger.warning(f"Could not add NTP configuration to device {device.name}: {e}")
|