Commit b16c2eef authored by Lukas Burgey's avatar Lukas Burgey

Fix tracking of pending tasks

Especially adds pending credential states
parent 075de076
# Generated by Django 2.1.3 on 2018-11-22 22:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0025_auto_20181121_1149'),
]
operations = [
migrations.AddField(
model_name='deploymentstate',
name='pending',
field=models.BooleanField(default=False, editable=False),
),
]
......@@ -62,17 +62,19 @@ class Site(models.Model):
blank=True,
)
# tasks is what we send to the clients. the are the serialization of a deployment
@property
def pending_tasks(self):
tasks = {}
pending_items = self.state_items.filter(
state='deployment_pending',
) | self.state_items.filter(
state='removal_pending',
) | self.state_items.filter(
state='failed',
)
for item in pending_items.all():
dep_states = [
state
for state in self.state_items.all()
if state.is_pending or state.is_credential_pending
]
# make the tasks unique here
for item in dep_states:
tasks[item.parent.id] = item.parent
return tasks.values()
......@@ -319,6 +321,12 @@ class Deployment(models.Model):
def _set_target(self, target):
self.state_target = target
for credential_state in CredentialState.objects.filter(
target__parent=self,
):
credential_state.set_target(target)
LOGGER.debug(self.msg('Target changed to '+target))
self.save()
......@@ -387,6 +395,26 @@ class DeploymentState(models.Model):
blank=True,
)
@property
def is_pending(self):
# TODO
# pending because we are orphaned -> pending until removed everywhere
if self.parent is None:
return True
# pending because the state target is not reached
if self.parent.state_target != self.state:
return True
return False
@property
def is_credential_pending(self):
for credential_state in self.credential_states.all():
if credential_state.is_pending:
return True
return False
@property
def user_credentials(self):
return self.user.credentials
......@@ -454,9 +482,6 @@ class DeploymentState(models.Model):
LOGGER.info(self.msg('ignoring invalid state transition user_deploy'))
return
for credential_state in self.credential_states.all():
credential_state.set_target(DEPLOYED)
self._set_state(
DEPLOYMENT_PENDING,
publish=False, # the post response already contains the update
......@@ -488,9 +513,6 @@ class DeploymentState(models.Model):
self._set_state(NOT_DEPLOYED)
return
for credential_state in self.credential_states.all():
credential_state.set_target(DEPLOYED)
self._set_state(
REMOVAL_PENDING,
publish=False, # the post response already contains the update
......@@ -584,6 +606,13 @@ class DeploymentState(models.Model):
except CredentialState.DoesNotExist:
LOGGER.error('CredentialState.DoesNotExist in _set_state')
if (
state == DEPLOYMENT_PENDING or
state == REMOVAL_PENDING or
state == FAILED
):
self.pending = True
self.state = state
self.save()
LOGGER.debug(self.msg('State changed to '+self.state))
......@@ -638,6 +667,10 @@ class CredentialState(models.Model):
on_delete=models.CASCADE,
)
@property
def is_pending(self):
return self.state != self.state_target
@classmethod
def get_credential_state(cls, credential, target):
try:
......
......@@ -45,6 +45,7 @@ class CredentialStateSerializer(serializers.ModelSerializer):
'state',
'state_target',
'credential',
'is_pending',
]
......
......@@ -50,6 +50,8 @@ class DeploymentStateSerializer(serializers.ModelSerializer):
'vo',
'message',
'credential_states',
'is_credential_pending',
'is_pending',
]
......
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