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

Switch to logging facility

parent 840bbec3
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):
......
......@@ -2,11 +2,13 @@ from django.conf import settings
from django.contrib.auth.models import AbstractUser, Group
from django.db import models
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from requests.auth import HTTPBasicAuth
from rest_framework.authtoken.models import Token
import json
import pika
import requests
from .logging import logger
# clients are registerred at rabbitmq, when they are assigned to a site
......@@ -43,8 +45,8 @@ class RabbitMQInstance(models.Model):
def __str__(self):
return self.host
def print(self, msg):
print('[RabbitMQ:{}] {}'.format(self.host, msg))
def msg(self, msg):
return '[RabbitMQ:{}] {}'.format(self.host, msg)
@property
def api(self):
......@@ -78,7 +80,7 @@ class RabbitMQInstance(models.Model):
)
self.rabbitmq_connection = pika.BlockingConnection(
rabbitmqconnection_properties)
# self.print("Opened connection")
logger.debug(self.msg('opened connection'))
return self.rabbitmq_connection
......@@ -91,7 +93,8 @@ class RabbitMQInstance(models.Model):
durable=True,
exchange_type='topic')
self.rabbitmq_channel.confirm_delivery()
# self.print("Opened channel")
logger.debug(self.msg('opened channel'))
return self.rabbitmq_channel
def get_uri(self, path):
......@@ -186,20 +189,20 @@ class RabbitMQInstance(models.Model):
# PUBLIC API
def register_site(self, site):
self.print('register: {} / {}'.format(
site.name, site.client.username))
logger.info(self.msg('register: {} / {}'.format(
site.name, site.client.username)))
self.create_user(site)
self.set_permissions(site)
self.set_topic_permissions(site)
def update_site(self, site):
self.set_topic_permissions(site)
self.print('updated permissions for {}'.format(
print_client(site.client)))
logger.debug(self.msg('updated permissions for {}'.format(
print_client(site.client))))
def deregister_site(self, site):
self.print('deregistered {}'.format(
print_client(site.client)))
logger.info(self.msg('deregistered {}'.format(
print_client(site.client))))
def is_client_connected(self, site):
connections = self.rest_get("connections/")
......@@ -209,7 +212,7 @@ class RabbitMQInstance(models.Model):
return len(clients_for_site) > 0
def disconnect(self):
self.print("closing connection")
logger.debug(self.msg('closing connection'))
self.connection.close()
def service_routing_key(self, service):
......@@ -323,21 +326,19 @@ class SSHPublicKey(models.Model):
editable=False,
)
def print(self, msg):
print('[SSHPublicKey:{}] {}'.format(self, msg))
def msg(self, msg):
return '[SSHPublicKey:{}] {}'.format(self, msg)
# does not directly delete the key if the key is deployed or withdrawn
# somewhere
# the receiver 'delete_withdrawn_ssh_key' does the actual deletion
def delete_key(self):
if (not self.task_items.exists()
and not self.deployments.exists()
and not self.withdrawn_deployments.exists()):
self.print('Direct deletion of key')
if (not self.tasks.exists() and not self.deployments.exists()):
logger.info(self.msg('Direct deletion of key'))
self.delete()
return
self.print('Deletion of key started')
logger.info(self.msg('Deletion of key started'))
self.deleted = True
self.save()
......@@ -407,7 +408,8 @@ class Deployment(models.Model):
# and delete outstanding tasks which are made obsolete by this task
for withdrawal in self.withdrawals.filter(key=key):
withdrawal.delete_msg("now obsolete")
logger.debug(withdrawal.msg('now obsolete'))
withdrawal.delete()
# generate task
task = DeploymentTask(
......@@ -416,7 +418,7 @@ class Deployment(models.Model):
key=key,
)
task.save()
task.print("generated")
logger.info(task.msg('generated'))
# generate task items
for site in self.service.site.all():
......@@ -425,7 +427,7 @@ class Deployment(models.Model):
site=site,
)
deploy.save()
deploy.print("generated")
logger.debug(deploy.msg('generated'))
# publish the task
task.publish()
......@@ -439,7 +441,8 @@ class Deployment(models.Model):
# and delete outstanding tasks which are made obsolete by this task
for deploy in self.deploys.filter(key=key):
deploy.delete_msg("now obsolete")
logger.debug(deploy.msg("now obsolete"))
deploy.delete()
# generate task
task = DeploymentTask(
......@@ -448,7 +451,7 @@ class Deployment(models.Model):
key=key,
)
task.save()
task.print("generated")
logger.info(task.msg('generated'))
# generate task items
for site in self.service.site.all():
......@@ -457,7 +460,7 @@ class Deployment(models.Model):
site=site,
)
withdrawal.save()
withdrawal.print("generated")
logger.debug(withdrawal.msg('generated'))
# publish the task
task.publish()
......@@ -500,11 +503,8 @@ class DeploymentTask(models.Model):
self.action,
)
def print(self, msg):
print("[DeploymentTask:{}] {}".format(
self,
msg,
))
def msg(self, msg):
return '[DeploymentTask:{}] {}'.format(self, msg)
def publish(self):
from .clientapi.serializers import DeploymentTaskSerializer
......@@ -514,21 +514,18 @@ class DeploymentTask(models.Model):
self.service,
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
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():
self.finished()
# maintenance after all task items are done
def finished(self):
self.print("done")
logger.info(self.msg('done'))
# check if this was the final withdraw in a key deletion
if self.action == 'withdraw':
......@@ -553,16 +550,8 @@ class DeploymentTaskItem(models.Model):
self.site,
)
def print(self, msg):
print("[DeploymentTaskItem:{}] {}".format(
self,
msg,
))
# used for e.g. "done", "now obsolete"
def delete_msg(self, msg):
self.print(msg)
self.delete()
def msg(self, msg):
return '[DeploymentTaskItem:{}] {}'.format(self, msg)
#
......
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