Skip to content

Commit

Permalink
fixes issues with long running jobs and closing connection errors bb-…
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-Ricardo authored and kuznetsov andrei committed Oct 9, 2024
1 parent 45dbeff commit df64cbf
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 17 deletions.
22 changes: 13 additions & 9 deletions module/common/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def do_error_exit(log_text):

def get_relative_time(delta):
"""
https://stackoverflow.com/a/13756038
return a human readable string of a datetime object delta
Parameters
Expand All @@ -136,17 +137,20 @@ def get_relative_time(delta):
str: formatted string of time delta
"""

parts = [float(x) for x in str(delta).split(":")]

hour, minute, second = "{:1.0f}:{:1.0f}:{:1.2f}".format(*parts).split(":")
seconds = int(delta.total_seconds())
return_string = list()

if hour != "0":
return_string.append(f"{hour} hour%s" % plural(int(hour)))
if minute != "0":
return_string.append(f"{minute} minute%s" % plural(int(minute)))

return_string.append(f"{second} seconds")
periods = [
('day', 60 * 60 * 24),
('hour', 60 * 60),
('minute', 60),
('second', 1)
]

for period_name, period_seconds in periods:
if seconds >= period_seconds:
period_value, seconds = divmod(seconds, period_seconds)
return_string.append(f"{period_value} {period_name}{plural(period_value)}")

return ", ".join(return_string)

Expand Down
8 changes: 8 additions & 0 deletions module/netbox/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ def create_session(self):

return session

def finish(self):

# closing NetBox connection
try:
self.session.close()
except Exception as e:
log.error(f"unable to close NetBox connection: {e}")

def get_api_version(self):
"""
Perform a basic GET request to extract NetBox API version from header
Expand Down
4 changes: 2 additions & 2 deletions module/sources/check_redfish/import_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ def apply(self):
# parse inventory id to int as all NetBox ids are type integer
try:
inventory_id = int(inventory_id)
except ValueError:
log.error(f"Value for meta.inventory_id '{inventory_id}' must be an integer."
except (ValueError, TypeError):
log.error(f"Value for meta.inventory_id '{inventory_id}' must be an integer. "
f"Cannot use inventory_id to match device in NetBox.")

self.device_object = self.inventory.get_by_id(NBDevice, inventory_id)
Expand Down
4 changes: 4 additions & 0 deletions module/sources/common/source_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class SourceBase:
inventory = None
source_tag = None

# dummy function to implement a finish call for each source
def finish(self):
pass

def map_object_interfaces_to_current_interfaces(self, device_vm_object, interface_data_dict=None):
"""
Try to match current object interfaces to discovered ones. This will be done
Expand Down
26 changes: 20 additions & 6 deletions module/sources/vmware/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# For a copy, see file LICENSE.txt included in this
# repository or visit: <https://opensource.org/licenses/MIT>.

import atexit
import datetime
import pprint
import re
Expand Down Expand Up @@ -185,6 +184,7 @@ def __init__(self, name=None, settings=None, inventory=None):
log.info(f"Source '{name}' is currently disabled. Skipping")
return

self._sdk_instance = None
self.create_sdk_session()

if self.session is None:
Expand Down Expand Up @@ -412,15 +412,14 @@ def create_sdk_session(self):
try:
if self.proxy_host is not None and self.proxy_port is not None:
smart_stub = connect.SmartStubAdapter(**connection_params)
instance = vim.ServiceInstance('ServiceInstance', smart_stub)
content = instance.RetrieveContent()
self._sdk_instance = vim.ServiceInstance('ServiceInstance', smart_stub)
content = self._sdk_instance.RetrieveContent()
content.sessionManager.Login(self.username, self.password, None)
else:

instance = connect.SmartConnect(**connection_params)
self._sdk_instance = connect.SmartConnect(**connection_params)

atexit.register(connect.Disconnect, instance)
self.session = instance.RetrieveContent()
self.session = self._sdk_instance.RetrieveContent()

except vim.fault.InvalidLogin as e:
log.error(f"{def_exception_text} {e.msg}")
Expand Down Expand Up @@ -496,6 +495,21 @@ def create_api_session(self):

return True

def finish(self):

# closing tag session
if self._sdk_instance is not None:
try:
connect.Disconnect(self._sdk_instance)
except Exception as e:
log.error(f"unable to close vCenter SDK connection: {e}")

# closing SDK session
try:
del self.tag_session
except Exception as e:
log.error(f"unable to close vCenter API instance connection: {e}")

def apply(self):
"""
Main source handler method. This method is called for each source from "main" program
Expand Down
8 changes: 8 additions & 0 deletions netbox-sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ def main():
# prune orphaned objects from NetBox
nb_handler.prune_data()

# loop over sources and patch netbox data
for source in sources:
# closing all open connections
source.finish()

# closing NetBox connection
nb_handler.finish()

# finish
log.info("Completed NetBox Sync in %s" % get_relative_time(datetime.now() - start_time))

Expand Down

0 comments on commit df64cbf

Please sign in to comment.