Commit c0173efa authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Change the API a little bit

parent 2991bcf8
# we don't need to deserialize, so we do not implement the abstract methods # we don't need to deserialize, so we do not implement the abstract methods
# pylint: disable=abstract-method # pylint: disable=abstract-method
from django_mysql.models import JSONField
from rest_framework import serializers from rest_framework import serializers
from .. import models, serializers as backend_serializers from .. import models, serializers as backend_serializers
...@@ -13,10 +14,11 @@ class ServiceSerializer(serializers.ModelSerializer): ...@@ -13,10 +14,11 @@ class ServiceSerializer(serializers.ModelSerializer):
class UserSerializer(serializers.ModelSerializer): class UserSerializer(serializers.ModelSerializer):
groups = backend_serializers.GroupSerializer(many=True) groups = backend_serializers.GroupSerializer(many=True)
userinfo = JSONField()
class Meta: class Meta:
model = models.User model = models.User
fields = ['sub', 'email', 'groups'] fields = ['email', 'groups', 'userinfo']
class DeploymentSerializer(serializers.ModelSerializer): class DeploymentSerializer(serializers.ModelSerializer):
......
...@@ -2,7 +2,7 @@ from django.conf.urls import url ...@@ -2,7 +2,7 @@ from django.conf.urls import url
from . import views from . import views
URLPATTERNS = [ URLPATTERNS = [
url(r'^deployments/', views.DeploymentsView.as_view()), url(r'^deployments', views.DeploymentsView.as_view()),
url(r'^config/', views.ConfigurationView.as_view()), url(r'^config', views.ConfigurationView.as_view()),
url(r'^ack/(?P<task_id>\d+)/', views.AckView.as_view()), url(r'^ack/(?P<task_id>\d+)', views.AckView.as_view()),
] ]
...@@ -20,10 +20,31 @@ class ServiceSerializer(serializers.ModelSerializer): ...@@ -20,10 +20,31 @@ class ServiceSerializer(serializers.ModelSerializer):
exclude = [] exclude = []
class DeploymentTaskItemSerializer(serializers.ModelSerializer):
service = ServiceSerializer()
key = backend_serializers.SSHPublicKeySerializerB()
site = SiteSerializer()
class Meta:
model = models.DeploymentTaskItem
fields = [
'action',
'key',
'service',
'site',
]
class DeploymentTaskSerializer(serializers.ModelSerializer): class DeploymentTaskSerializer(serializers.ModelSerializer):
key = backend_serializers.SSHPublicKeySerializerB()
service = ServiceSerializer()
class Meta: class Meta:
model = models.DeploymentTask model = models.DeploymentTask
fields = ['action', 'key'] fields = [
'action',
'key',
'service',
]
class DeploymentSerializer(serializers.Serializer): class DeploymentSerializer(serializers.Serializer):
...@@ -51,11 +72,21 @@ class UserSerializer(serializers.ModelSerializer): ...@@ -51,11 +72,21 @@ class UserSerializer(serializers.ModelSerializer):
deployments = DeploymentSerializer(many=True) deployments = DeploymentSerializer(many=True)
auth_groups = backend_serializers.AuthGroupSerializer(many=True) auth_groups = backend_serializers.AuthGroupSerializer(many=True)
deployment_tasks = DeploymentTaskSerializer(many=True) deployment_tasks = DeploymentTaskSerializer(many=True)
deployment_task_items = DeploymentTaskItemSerializer(many=True)
class Meta: class Meta:
model = models.User model = models.User
fields = ['id', 'email', 'userinfo', 'ssh_keys', 'groups', 'deployments', 'auth_groups', fields = [
'deployment_tasks'] 'id',
'email',
'userinfo',
'ssh_keys',
'groups',
'deployments',
'auth_groups',
'deployment_tasks',
'deployment_task_items',
]
class ClientSerializer(serializers.HyperlinkedModelSerializer): class ClientSerializer(serializers.HyperlinkedModelSerializer):
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
import json import json
import logging import logging
import pika import pika
from pika.exceptions import ConnectionClosed
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
from django.contrib.auth.models import AbstractUser, Group from django.contrib.auth.models import AbstractUser, Group
from django.core.cache import cache from django.core.cache import cache
...@@ -127,9 +128,14 @@ class RabbitMQInstance(SingletonModel): ...@@ -127,9 +128,14 @@ class RabbitMQInstance(SingletonModel):
@property @property
def _channel(self): def _channel(self):
channel = self._connection.channel() try:
channel.confirm_delivery() channel = self._connection.channel()
return channel channel.confirm_delivery()
return channel
except ConnectionClosed as exception:
LOGGER.error(self.msg('ConnectionClosed: ' + str(exception)))
self._init_connection()
return self._channel
def _publish(self, exchange, routing_key, body): def _publish(self, exchange, routing_key, body):
channel = self._channel channel = self._channel
...@@ -647,6 +653,7 @@ class DeploymentTask(models.Model): ...@@ -647,6 +653,7 @@ class DeploymentTask(models.Model):
deploy = DeploymentTaskItem( deploy = DeploymentTaskItem(
task=task, task=task,
site=site, site=site,
user=deployment.user
) )
deploy.save() deploy.save()
LOGGER.debug(deploy.msg('generated')) LOGGER.debug(deploy.msg('generated'))
...@@ -674,6 +681,7 @@ class DeploymentTask(models.Model): ...@@ -674,6 +681,7 @@ class DeploymentTask(models.Model):
deploy = DeploymentTaskItem( deploy = DeploymentTaskItem(
task=task, task=task,
site=site, site=site,
user=deployment.user
) )
deploy.save() deploy.save()
LOGGER.debug(deploy.msg('generated')) LOGGER.debug(deploy.msg('generated'))
...@@ -736,7 +744,11 @@ class DeploymentTask(models.Model): ...@@ -736,7 +744,11 @@ class DeploymentTask(models.Model):
if self.action == 'withdraw': if self.action == 'withdraw':
self.key.try_final_deletion() self.key.try_final_deletion()
message = 'Completed: ' + str(self.action) + ' ' + str(self.key) + ' '+ str(self.site) message = 'Completed: {} {} @ {}'.format(
self.action,
self.key,
self.service,
)
from .frontend.views import user_state_dict from .frontend.views import user_state_dict
content = { content = {
...@@ -760,6 +772,23 @@ class DeploymentTaskItem(models.Model): ...@@ -760,6 +772,23 @@ class DeploymentTaskItem(models.Model):
related_name='task_items', related_name='task_items',
on_delete=models.CASCADE, on_delete=models.CASCADE,
) )
user = models.ForeignKey(
User,
related_name='deployment_task_items',
on_delete=models.CASCADE,
)
@property
def service(self):
return self.task.service
@property
def key(self):
return self.task.key
@property
def action(self):
return self.deployment.key
def __str__(self): def __str__(self):
return "{}@{}#{}".format( return "{}@{}#{}".format(
......
...@@ -22,3 +22,9 @@ class SSHPublicKeySerializer(serializers.ModelSerializer): ...@@ -22,3 +22,9 @@ class SSHPublicKeySerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = SSHPublicKey model = SSHPublicKey
fields = ['id', 'name', 'key'] fields = ['id', 'name', 'key']
class SSHPublicKeySerializerB(serializers.ModelSerializer):
class Meta:
model = SSHPublicKey
fields = ['id', 'name']
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