Commit e56d2a38 authored by Lukas Burgey's avatar Lukas Burgey

Extend deployment so it can also handle groups instead of services

parent 428f1341
......@@ -63,7 +63,8 @@ class DeploymentStateSerializer(serializers.ModelSerializer):
class DeploymentSerializer(serializers.ModelSerializer):
service = ServiceSerializer()
ssh_keys = backend_serializers.SSHPublicKeySerializer(many=True)
services = ServiceSerializer(many=True)
ssh_keys = backend_serializers.SSHPublicKeyRefSerializer(many=True)
#ssh_keys_to_remove = backend_serializers.SSHPublicKeySerializer(many=True)
states = DeploymentStateSerializer(many=True)
......@@ -71,6 +72,8 @@ class DeploymentSerializer(serializers.ModelSerializer):
model = models.Deployment
fields = [
'service',
'services',
'group',
'ssh_keys',
#'ssh_keys_to_remove',
'states',
......
import logging
from django.contrib.auth.models import Group
from django.shortcuts import get_object_or_404
from django.contrib.auth import logout
from rest_framework import views
......@@ -103,25 +104,33 @@ class DeploymentView(views.APIView):
if (
'type' not in request.data or
'key' not in request.data or
'service' not in request.data
(
'service' not in request.data and
'group' not in request.data
)
):
LOGGER.error('Deployment api got invalid request %s', request.data)
return _api_error_response(
"request misses fields (should have: 'type', 'key', and 'service')"
"request misses fields (should have: 'type', 'key', and ('service' or 'group'))"
)
request_type = request.data['type']
request_service = models.Service.objects.get(
id=request.data['service'],
)
request_key = models.SSHPublicKey.objects.get(
id=request.data['key'],
)
deployment = models.Deployment.get_deployment(
request.user,
request_service,
)
deployment = None
if 'group' in request.data:
deployment = models.Deployment.get_deployment(
request.user,
group=Group.objects.get(id=request.data['group']),
)
elif 'service' in request.data:
deployment = models.Deployment.get_deployment(
request.user,
service=models.Service.objects.get(id=request.data['service']),
)
if request_type == 'add':
deployment.deploy_key(request_key)
......
......@@ -191,6 +191,13 @@ class RabbitMQInstance(SingletonModel):
msg,
)
def publish_by_group(self, group, msg):
self._publish(
'groups',
group.name,
msg,
)
def publish_by_site(self, site, msg):
self._publish(
'sites',
......@@ -564,12 +571,19 @@ class Deployment(models.Model):
null=True,
)
group = models.ForeignKey(
Service,
related_name='groups_deployments',
Group,
related_name='deployments',
on_delete=models.CASCADE,
null=True,
blank=True,
)
# only used when group is set and service is not set
@property
def services(self):
if self.group is not None:
return self.group.services.all()
return None
service = models.ForeignKey(
Service,
related_name='deployments',
......@@ -601,14 +615,24 @@ class Deployment(models.Model):
user=user,
group=group,
)
else:
raise Exception("need either a group or service for the deployment")
if query.exists():
return query.first()
deployment = cls(
user=user,
service=service,
)
deployment = None
if service is not None:
deployment = cls(
user=user,
service=service,
)
elif group is not None:
deployment = cls(
user=user,
group=group,
)
deployment.save()
LOGGER.debug(deployment.msg('created'))
return deployment
......@@ -725,11 +749,15 @@ class DeploymentState(models.Model):
def service(self):
return self.deployment.service
@property
def group(self):
return self.deployment.group
@property
def target_reached(self):
return self.state_target == self.state
# get a deployment for a user/service.
# get a state for a user/service.
# if it does not exist it is created
@classmethod
def get_state(cls, deployment, key):
......@@ -750,12 +778,21 @@ class DeploymentState(models.Model):
LOGGER.debug(state.msg('created'))
# generate state items
for site in deployment.service.site.all():
deploy = DeploymentStateItem(
parent=state,
site=site,
)
deploy.save()
if deployment.service is not None:
for site in deployment.service.site.all():
deploy = DeploymentStateItem(
parent=state,
site=site,
)
deploy.save()
elif deployment.group is not None:
for service in deployment.group.services.all():
for site in service.site.all():
deploy = DeploymentStateItem(
parent=state,
site=site,
)
deploy.save()
return state
......@@ -779,10 +816,19 @@ class DeploymentState(models.Model):
from .clientapi.serializers import DeploymentStateSerializer
msg = json.dumps(DeploymentStateSerializer(self).data)
RabbitMQInstance.load().publish_by_service(
self.service,
msg,
)
if self.service is not None:
RabbitMQInstance.load().publish_by_service(
self.service,
msg,
)
elif self.group is not None:
RabbitMQInstance.load().publish_by_group(
self.group,
msg,
)
else:
LOGGER.error("Deployment as neither a group or a service")
# update the state of the remote webpage
def publish_to_user(self):
if self.user is not None:
......
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