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