Commit 00cd417a authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Add the DeploymentUpdateModel

parent 8a0bb0d0
......@@ -26,4 +26,4 @@ def publish_deployment(sender, instance=None, created=False, **kwargs):
site,
instance.service,
))
site.client_updated()
site.ack_update(service=instance.service)
......@@ -58,39 +58,32 @@ class Site(models.Model):
)
name = models.CharField(max_length=150, unique=True)
description = models.TextField(max_length=300, blank=True)
last_fetch = models.DateTimeField(
default=make_aware(datetime.utcfromtimestamp(0)),
editable=False)
def __str__(self):
return self.name
def client_updated(self):
print("Client for site {} got updated".format(self.name))
self.last_fetch = make_aware(datetime.now())
self.save()
def ack_update(self, service=None):
if service is None:
for du in self.deployment_updates.all():
du.delete()
else:
for deployment_update in self.deployment_updates.all():
if deployment_update.service.name == service.name:
deployment_update.delete()
def clientapi_get_deployments(self, all=False, filter=None):
def service_deployments(service):
if all:
ds = (
service.deployments
# only oidcuser are supposed to have deployments
.filter(user__user_type='oidcuser')
)
return service.deployments.all()
else:
ds = (
service.deployments
# only oidcuser are supposed to have deployments
.filter(user__user_type='oidcuser')
.filter(last_change__gte=self.last_fetch)
)
for deployment in ds:
# TODO replace this optimism with an acknowledgement
deployment.client_updated()
ds = [deployment_update.deployment
for deployment_update
in self.deployment_updates.all()
if deployment_update.service.name == service.name]
return ds
# TODO is this apropriate?
self.ack_update(service=service)
return ds
if filter is not None:
services = [s
......@@ -104,10 +97,8 @@ class Site(models.Model):
for service
in services}
# TODO we expect the client to get the update here
self.client_updated()
# deployments of the services of this site
# changed deployments for this site
# filtered by service name using filter parameter
return deployments
......@@ -139,7 +130,7 @@ class Service(models.Model):
blank=True)
def __str__(self):
return self.name + '@' + str(list(self.site.all()))
return self.name
class SSHPublicKey(models.Model):
......@@ -221,6 +212,18 @@ class Deployment(models.Model):
def changed(self):
print("Deployment {} changed".format(self))
# delete old deployment_updates and generate new ones
for du in self.deployment_updates.all():
du.delete()
for site in self.service.site.all():
du = DeploymentUpdate(
site=site,
deployment=self,
)
du.save()
self.last_change = make_aware(datetime.now())
self.save()
......@@ -244,7 +247,11 @@ class Deployment(models.Model):
self.changed()
self.send_change()
def client_updated(self):
def update(self):
# if there are still remaining updates we do not change anything
if self.deployment_updates.exists():
return
withdrawn_keys = list(self.ssh_keys_to_withdraw.all())
# the client has withdrawn the keys so we can empty the list
......@@ -257,3 +264,25 @@ class Deployment(models.Model):
def send_change(self):
deployment_change.send(sender=self.__class__, instance=self)
class DeploymentUpdate(models.Model):
deployment = models.ForeignKey(
Deployment,
related_name='deployment_updates',
)
site = models.ForeignKey(
Site,
related_name='deployment_updates',
)
@property
def user(self):
return self.deployment.user
@property
def service(self):
return self.deployment.service
def __str__(self):
return str(self.user) + ':' + str(self.service) + '@' + str(self.site)
......@@ -124,7 +124,7 @@ class RabbitMQInstance:
def is_client_connected(self, site):
connections = self.rest_get("connections/")
client_connections = [c
for c in connections
if c['user'] == site.client.username]
return len(client_connections) > 0
clients_for_site = [c
for c in connections
if c['user'] == site.client.username]
return len(clients_for_site) > 0
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