Commit 2717c6ae authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Switch to logging facility

parent 840bbec3
import logging import logging
logger = logging.getLogger(__name__) import sys
logger_name = 'fum_backend'
logger_dir = './logs/'
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
error_fh = logging.FileHandler(logger_dir + 'error.log')
error_fh.setLevel(logging.ERROR)
error_fh.setFormatter(formatter)
logger.addHandler(error_fh)
info_fh = logging.FileHandler(logger_dir + 'info.log')
info_fh.setLevel(logging.INFO)
info_fh.setFormatter(formatter)
logger.addHandler(info_fh)
debug_fh = logging.FileHandler(logger_dir + 'debug.log')
debug_fh.setLevel(logging.DEBUG)
debug_fh.setFormatter(formatter)
logger.addHandler(debug_fh)
# for the console
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.debug('logging initialized')
def log_exception(exception): def log_exception(exception):
......
...@@ -2,11 +2,13 @@ from django.conf import settings ...@@ -2,11 +2,13 @@ from django.conf import settings
from django.contrib.auth.models import AbstractUser, Group from django.contrib.auth.models import AbstractUser, Group
from django.db import models from django.db import models
from django.db.models.signals import post_save, pre_delete from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
import json import json
import pika import pika
import requests import requests
from .logging import logger
# clients are registerred at rabbitmq, when they are assigned to a site # clients are registerred at rabbitmq, when they are assigned to a site
...@@ -43,8 +45,8 @@ class RabbitMQInstance(models.Model): ...@@ -43,8 +45,8 @@ class RabbitMQInstance(models.Model):
def __str__(self): def __str__(self):
return self.host return self.host
def print(self, msg): def msg(self, msg):
print('[RabbitMQ:{}] {}'.format(self.host, msg)) return '[RabbitMQ:{}] {}'.format(self.host, msg)
@property @property
def api(self): def api(self):
...@@ -78,7 +80,7 @@ class RabbitMQInstance(models.Model): ...@@ -78,7 +80,7 @@ class RabbitMQInstance(models.Model):
) )
self.rabbitmq_connection = pika.BlockingConnection( self.rabbitmq_connection = pika.BlockingConnection(
rabbitmqconnection_properties) rabbitmqconnection_properties)
# self.print("Opened connection") logger.debug(self.msg('opened connection'))
return self.rabbitmq_connection return self.rabbitmq_connection
...@@ -91,7 +93,8 @@ class RabbitMQInstance(models.Model): ...@@ -91,7 +93,8 @@ class RabbitMQInstance(models.Model):
durable=True, durable=True,
exchange_type='topic') exchange_type='topic')
self.rabbitmq_channel.confirm_delivery() self.rabbitmq_channel.confirm_delivery()
# self.print("Opened channel") logger.debug(self.msg('opened channel'))
return self.rabbitmq_channel return self.rabbitmq_channel
def get_uri(self, path): def get_uri(self, path):
...@@ -186,20 +189,20 @@ class RabbitMQInstance(models.Model): ...@@ -186,20 +189,20 @@ class RabbitMQInstance(models.Model):
# PUBLIC API # PUBLIC API
def register_site(self, site): def register_site(self, site):
self.print('register: {} / {}'.format( logger.info(self.msg('register: {} / {}'.format(
site.name, site.client.username)) site.name, site.client.username)))
self.create_user(site) self.create_user(site)
self.set_permissions(site) self.set_permissions(site)
self.set_topic_permissions(site) self.set_topic_permissions(site)
def update_site(self, site): def update_site(self, site):
self.set_topic_permissions(site) self.set_topic_permissions(site)
self.print('updated permissions for {}'.format( logger.debug(self.msg('updated permissions for {}'.format(
print_client(site.client))) print_client(site.client))))
def deregister_site(self, site): def deregister_site(self, site):
self.print('deregistered {}'.format( logger.info(self.msg('deregistered {}'.format(
print_client(site.client))) print_client(site.client))))
def is_client_connected(self, site): def is_client_connected(self, site):
connections = self.rest_get("connections/") connections = self.rest_get("connections/")
...@@ -209,7 +212,7 @@ class RabbitMQInstance(models.Model): ...@@ -209,7 +212,7 @@ class RabbitMQInstance(models.Model):
return len(clients_for_site) > 0 return len(clients_for_site) > 0
def disconnect(self): def disconnect(self):
self.print("closing connection") logger.debug(self.msg('closing connection'))
self.connection.close() self.connection.close()
def service_routing_key(self, service): def service_routing_key(self, service):
...@@ -323,21 +326,19 @@ class SSHPublicKey(models.Model): ...@@ -323,21 +326,19 @@ class SSHPublicKey(models.Model):
editable=False, editable=False,
) )
def print(self, msg): def msg(self, msg):
print('[SSHPublicKey:{}] {}'.format(self, msg)) return '[SSHPublicKey:{}] {}'.format(self, msg)
# does not directly delete the key if the key is deployed or withdrawn # does not directly delete the key if the key is deployed or withdrawn
# somewhere # somewhere
# the receiver 'delete_withdrawn_ssh_key' does the actual deletion # the receiver 'delete_withdrawn_ssh_key' does the actual deletion
def delete_key(self): def delete_key(self):
if (not self.task_items.exists() if (not self.tasks.exists() and not self.deployments.exists()):
and not self.deployments.exists() logger.info(self.msg('Direct deletion of key'))
and not self.withdrawn_deployments.exists()):
self.print('Direct deletion of key')
self.delete() self.delete()
return return
self.print('Deletion of key started') logger.info(self.msg('Deletion of key started'))
self.deleted = True self.deleted = True
self.save() self.save()
...@@ -407,7 +408,8 @@ class Deployment(models.Model): ...@@ -407,7 +408,8 @@ class Deployment(models.Model):
# and delete outstanding tasks which are made obsolete by this task # and delete outstanding tasks which are made obsolete by this task
for withdrawal in self.withdrawals.filter(key=key): for withdrawal in self.withdrawals.filter(key=key):
withdrawal.delete_msg("now obsolete") logger.debug(withdrawal.msg('now obsolete'))
withdrawal.delete()
# generate task # generate task
task = DeploymentTask( task = DeploymentTask(
...@@ -416,7 +418,7 @@ class Deployment(models.Model): ...@@ -416,7 +418,7 @@ class Deployment(models.Model):
key=key, key=key,
) )
task.save() task.save()
task.print("generated") logger.info(task.msg('generated'))
# generate task items # generate task items
for site in self.service.site.all(): for site in self.service.site.all():
...@@ -425,7 +427,7 @@ class Deployment(models.Model): ...@@ -425,7 +427,7 @@ class Deployment(models.Model):
site=site, site=site,
) )
deploy.save() deploy.save()
deploy.print("generated") logger.debug(deploy.msg('generated'))
# publish the task # publish the task
task.publish() task.publish()
...@@ -439,7 +441,8 @@ class Deployment(models.Model): ...@@ -439,7 +441,8 @@ class Deployment(models.Model):
# and delete outstanding tasks which are made obsolete by this task # and delete outstanding tasks which are made obsolete by this task
for deploy in self.deploys.filter(key=key): for deploy in self.deploys.filter(key=key):
deploy.delete_msg("now obsolete") logger.debug(deploy.msg("now obsolete"))
deploy.delete()
# generate task # generate task
task = DeploymentTask( task = DeploymentTask(
...@@ -448,7 +451,7 @@ class Deployment(models.Model): ...@@ -448,7 +451,7 @@ class Deployment(models.Model):
key=key, key=key,
) )
task.save() task.save()
task.print("generated") logger.info(task.msg('generated'))
# generate task items # generate task items
for site in self.service.site.all(): for site in self.service.site.all():
...@@ -457,7 +460,7 @@ class Deployment(models.Model): ...@@ -457,7 +460,7 @@ class Deployment(models.Model):
site=site, site=site,
) )
withdrawal.save() withdrawal.save()
withdrawal.print("generated") logger.debug(withdrawal.msg('generated'))
# publish the task # publish the task
task.publish() task.publish()
...@@ -500,11 +503,8 @@ class DeploymentTask(models.Model): ...@@ -500,11 +503,8 @@ class DeploymentTask(models.Model):
self.action, self.action,
) )
def print(self, msg): def msg(self, msg):
print("[DeploymentTask:{}] {}".format( return '[DeploymentTask:{}] {}'.format(self, msg)
self,
msg,
))
def publish(self): def publish(self):
from .clientapi.serializers import DeploymentTaskSerializer from .clientapi.serializers import DeploymentTaskSerializer
...@@ -514,21 +514,18 @@ class DeploymentTask(models.Model): ...@@ -514,21 +514,18 @@ class DeploymentTask(models.Model):
self.service, self.service,
msg) msg)
# used for e.g. "done", "now obsolete"
def delete_msg(self, msg):
self.print(msg)
self.delete()
# the client acked the receipt and execution of the task for his site # the client acked the receipt and execution of the task for his site
def item_finished(self, site): def item_finished(self, site):
self.task_items.get(site=site).delete_msg("done") item = self.task_items.get(site=site)
logger.debug(item.msg('done'))
item.delete()
if not self.task_items.exists(): if not self.task_items.exists():
self.finished() self.finished()
# maintenance after all task items are done # maintenance after all task items are done
def finished(self): def finished(self):
self.print("done") logger.info(self.msg('done'))
# check if this was the final withdraw in a key deletion # check if this was the final withdraw in a key deletion
if self.action == 'withdraw': if self.action == 'withdraw':
...@@ -553,16 +550,8 @@ class DeploymentTaskItem(models.Model): ...@@ -553,16 +550,8 @@ class DeploymentTaskItem(models.Model):
self.site, self.site,
) )
def print(self, msg): def msg(self, msg):
print("[DeploymentTaskItem:{}] {}".format( return '[DeploymentTaskItem:{}] {}'.format(self, msg)
self,
msg,
))
# used for e.g. "done", "now obsolete"
def delete_msg(self, msg):
self.print(msg)
self.delete()
# #
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment