from django.conf import settings
import logging
from django.core.cache import cache
+import os
+
+cwd = os.getcwd()
+
+
+LOG_FILENAME = os.path.join(settings.LOG_FILE_LOCATION, 'celery_jobs.log')
+
+#FORMAT = '%(asctime)s %(levelname)s: %(message)s'
+#logging.basicConfig(format=FORMAT)
+formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s')
-FORMAT = '%(asctime)s %(levelname)s: %(message)s'
-logging.basicConfig(format=FORMAT)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
+handler = logging.FileHandler(LOG_FILENAME)
+handler.setFormatter(formatter)
+logger.addHandler(handler)
+
+def fod_unknown_host_cb(host, fingerprint):
+ return True
class Retriever(object):
def __init__(self, device=settings.NETCONF_DEVICE, username=settings.NETCONF_USER, password=settings.NETCONF_PASS, filter=settings.ROUTES_FILTER, route_name=None, xml=None):
self.filter = settings.ROUTE_FILTER%route_name
def fetch_xml(self):
- with manager.connect(host=self.device, port=830, username=self.username, password=self.password) as m:
+ with manager.connect(host=self.device, port=830, username=self.username, password=self.password, unknown_host_cb=fod_unknown_host_cb) as m:
xmlconfig = m.get_config(source='running', filter=('subtree',self.filter)).data_xml
return xmlconfig
def fetch_device(self):
device = cache.get("device")
+ logger.info("[CACHE] hit! got device")
if device:
return device
else:
device = self.proccess_xml()
if device.routing_options:
- cache.set("device", device)
+ cache.set("device", device, 3600)
+ logger.info("[CACHE] miss, setting device")
return device
else:
return False
commit_confirmed_is_successful = False
commit_is_successful = False
if configuration:
- with manager.connect(host=self.device, port=830, username=self.username, password=self.password) as m:
+ with manager.connect(host=self.device, port=830, username=self.username, password=self.password, unknown_host_cb=fod_unknown_host_cb) as m:
assert(":candidate" in m.server_capabilities)
with m.locked(target='candidate'):
m.discard_changes()
logger.info("Successfully committed @ %s" % self.device)
newconfig = m.get_config(source='running', filter=('subtree',settings.ROUTES_FILTER)).data_xml
retrieve = Retriever(xml=newconfig)
- cache.set("device", retrieve.proccess_xml())
+ logger.info("[CACHE] caching device configuration")
+ cache.set("device", retrieve.proccess_xml(), 3600)
if not commit_is_successful:
raise Exception()