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

Clean up init_device fails and make it more consistent #358

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions src/cnaas_nms/db/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@
peer_hostnames.append(intf.data["neighbor"])
return peer_hostnames

def reset_uplink_interfaces(self, session):
intfs = (

Check warning on line 264 in src/cnaas_nms/db/device.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/device.py#L264

Added line #L264 was not covered by tests
session.query(Interface)
.filter(Interface.device == self)
.filter(Interface.configtype == InterfaceConfigType.ACCESS_UPLINK)
.all()
)
intf: Interface = Interface()
for intf in intfs:
intf.configtype = InterfaceConfigType.ACCESS_AUTO
if intf.data:
intf.data = {}
session.commit()

Check warning on line 275 in src/cnaas_nms/db/device.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/db/device.py#L270-L275

Added lines #L270 - L275 were not covered by tests

def get_mlag_peer(self, session) -> Optional[Device]:
intfs = (
session.query(Interface)
Expand Down
39 changes: 25 additions & 14 deletions src/cnaas_nms/devicehandler/init_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ipaddress import IPv4Address, IPv4Interface, ip_interface
from typing import List, Optional, Union

import napalm.base.exceptions
import yaml
from apscheduler.job import Job
from netmiko.exceptions import ReadTimeout as NMReadTimeout
Expand Down Expand Up @@ -112,6 +113,8 @@
configuration=task.host["config"],
dry_run=False,
)
except (napalm.base.exceptions.ReplaceConfigException, napalm.base.exceptions.CommitError) as e:
raise InitError("Device {} did not commit new base management config: {}".format(task.host.name, str(e)))

Check warning on line 117 in src/cnaas_nms/devicehandler/init_device.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/devicehandler/init_device.py#L117

Added line #L117 was not covered by tests
except Exception:
task.run(task=napalm_get, getters=["facts"])
if not task.results[-1].failed:
Expand Down Expand Up @@ -374,6 +377,24 @@
schedule_mlag_peer_init(mlag_peer_id, mlag_peer_new_hostname, uplink_hostnames, scheduled_by)


def cleanup_init_step1_result(nrresult: List[Union[Result, MultiResult]]) -> List[Union[Result, MultiResult]]:
res: Union[Result, MultiResult]
for res in nrresult:
# These tasks are supposed to get connection timeouts etc, setting them
# to failed=False will keep job history clean and cause less confusion
if res.name in ["Push base management config", "push_base_management", "napalm_get"]:
res.failed = False
res.result = ""
if res.name == "ztp_device_cert" and not api_settings.VERIFY_TLS_DEVICE:
if type(res) is Result:
res.failed = False

Check warning on line 390 in src/cnaas_nms/devicehandler/init_device.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/devicehandler/init_device.py#L390

Added line #L390 was not covered by tests
elif type(res) is MultiResult:
for sres in res:
if type(sres) is Result:
sres.failed = False
return nrresult


@job_wrapper
def init_access_device_step1(
device_id: int,
Expand Down Expand Up @@ -585,6 +606,7 @@
reserved_ips = session.query(ReservedIP).filter(ReservedIP.device == dev).all()
for reserved_ip in reserved_ips:
session.delete(reserved_ip)
dev.reset_uplink_interfaces(session)

Check warning on line 609 in src/cnaas_nms/devicehandler/init_device.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/devicehandler/init_device.py#L609

Added line #L609 was not covered by tests
return NornirJobResult(nrresult=nrresult)

dev.management_ip = device_variables["mgmt_ip"]
Expand Down Expand Up @@ -625,20 +647,7 @@
if mlag_peer_id and mlag_peer_new_hostname:
schedule_mlag_peer_init(mlag_peer_id, mlag_peer_new_hostname, uplink_hostnames, scheduled_by)

res: Union[Result, MultiResult]
for res in nrresult[hostname]:
# These tasks are supposed to get connection timeouts etc, setting them
# to failed=False will keep job history clean and cause less confusion
if res.name in ["Push base management config", "push_base_management", "napalm_get"]:
res.failed = False
res.result = ""
if res.name == "ztp_device_cert" and not api_settings.VERIFY_TLS_DEVICE:
if type(res) is Result:
res.failed = False
elif type(res) is MultiResult:
for sres in res:
if type(sres) is Result:
sres.failed = False
nrresult[hostname] = cleanup_init_step1_result(nrresult[hostname])

return NornirJobResult(nrresult=nrresult, next_job_id=next_job_id)

Expand Down Expand Up @@ -801,6 +810,8 @@

logger.info("Init step 2 for {} scheduled as job # {}".format(new_hostname, next_job_id))

nrresult[hostname] = cleanup_init_step1_result(nrresult[hostname])

Check warning on line 813 in src/cnaas_nms/devicehandler/init_device.py

View check run for this annotation

Codecov / codecov/patch

src/cnaas_nms/devicehandler/init_device.py#L813

Added line #L813 was not covered by tests

return NornirJobResult(nrresult=nrresult, next_job_id=next_job_id)


Expand Down
Loading