Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions changes/366.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed Sync Devices and Sync Network Data jobs not releasing DB connections.
35 changes: 35 additions & 0 deletions development/Dockerfile-fakenos
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Fakenos dockerfile for local development

# Accepts a desired Python version as build argument, default to 3.12
ARG PYTHON_VER="3.12"

FROM docker.io/python:${PYTHON_VER}-slim-bookworm

WORKDIR /usr/src/fakenos

RUN apt-get update && apt-get install -y --no-install-recommends git && rm -rf /var/lib/apt/lists/*

RUN pip install git+https://github.com/fakenos/fakenos.git

# NOTE: fakenos does not recognize .yml files, must use .yaml
RUN cat <<EOF > /usr/src/fakenos/fakenos_inventory.yaml
hosts:
fakerouter:
username: admin
password: admin
platform: cisco_ios
server:
plugin: "ParamikoSshServer"
configuration:
address: "0.0.0.0"
timeout: 1
port: 22
EOF

ARG PYTHON_VER="3.12"

# NOTE: fakenos does not correctly handle absolute paths
# NOTE: stop using fakenos
Comment thread
smk4664 marked this conversation as resolved.
Outdated
WORKDIR /usr/local/lib/python${PYTHON_VER}/site-packages

CMD [ "fakenos", "--inventory", "/usr/src/fakenos/fakenos_inventory.yaml" ]
28 changes: 28 additions & 0 deletions development/docker-compose.fakenos.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
services:
fakerouter1:
build:
args:
PYTHON_VER: "${PYTHON_VER}"
context: "../"
dockerfile: "development/Dockerfile-fakenos"
image: "nautobot-device-onboarding/fakenos:py${PYTHON_VER}"
pull_policy: "never"
expose:
- "22"
fakerouter2: &fakerouter
image: "nautobot-device-onboarding/fakenos:py${PYTHON_VER}"
pull_policy: "never"
expose:
- "22"
fakerouter3: *fakerouter
fakerouter4: *fakerouter
fakerouter5: *fakerouter
fakerouter6: *fakerouter
fakerouter7: *fakerouter
fakerouter8: *fakerouter
fakerouter9: *fakerouter
fakerouter10: *fakerouter
fakerouter11: *fakerouter
fakerouter12: *fakerouter
fakerouter13: *fakerouter
7 changes: 6 additions & 1 deletion nautobot_device_onboarding/nornir_plays/command_getter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
get_git_repo_parser_path,
load_files_with_precedence,
)
from nautobot_device_onboarding.utils.helper import check_for_required_file, format_log_message
from nautobot_device_onboarding.utils.helper import (
check_for_required_file,
close_threaded_db_connections,
format_log_message,
)

PARSER_DIR = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)), "parsers"))

Expand Down Expand Up @@ -114,6 +118,7 @@ def _get_commands_to_run(yaml_parsed_info, sync_vlans, sync_vrfs, sync_cables, s
return deduplicate_command_list(all_commands)


@close_threaded_db_connections
def netmiko_send_commands(task: Task, command_getter_yaml_data: Dict, command_getter_job: str, logger, nautobot_job):
"""Run commands specified in PLATFORM_COMMAND_MAP."""
if not task.host.platform:
Expand Down
2 changes: 2 additions & 0 deletions nautobot_device_onboarding/nornir_plays/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from nautobot_device_onboarding.nornir_plays.formatter import extract_show_data
from nautobot_device_onboarding.nornir_plays.schemas import NETWORK_DATA_SCHEMA, NETWORK_DEVICES_SCHEMA
from nautobot_device_onboarding.utils.helper import close_threaded_db_connections


class CommandGetterProcessor(BaseLoggingProcessor):
Expand All @@ -29,6 +30,7 @@ def task_instance_started(self, task: Task, host: Host) -> None:
"network_driver": host.platform,
}

@close_threaded_db_connections
def task_instance_completed(self, task: Task, host: Host, result: MultiResult) -> None:
"""Processor for logging and data processing on task completed.

Expand Down
13 changes: 13 additions & 0 deletions nautobot_device_onboarding/utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import socket

import netaddr
from django.db import connections
from nautobot.dcim.filters import DeviceFilterSet
from nautobot.dcim.models import Device
from netaddr.core import AddrFormatError
Expand Down Expand Up @@ -119,3 +120,15 @@ def check_for_required_file(directory, filename):
return False
except FileNotFoundError:
return False


def close_threaded_db_connections(func):
"""Decorator to close database connections in threaded tasks."""

def inner(*args, **kwargs):
try:
func(*args, **kwargs)
finally:
connections.close_all()

return inner