Commit 221a1896 authored by Lukas Burgey's avatar Lukas Burgey

Fix task fetch

parent ab06774f
......@@ -47,11 +47,23 @@ class DeploymentsSerializer(serializers.Serializer):
)
class DeploymentStateSerializer(serializers.Serializer):
id = serializers.IntegerField()
state_target = serializers.CharField()
class DeploymentStateSerializer(serializers.ModelSerializer):
user = UserSerializer()
key = backend_serializers.SSHPublicKeySerializer()
service = ServiceSerializer()
group = backend_serializers.GroupSerializer()
class Meta:
model = models.DeploymentState
fields = [
'state_target',
'id',
'service',
'group',
'key',
'user',
]
class SiteSerializer(serializers.Serializer):
......
......@@ -321,8 +321,6 @@ class User(AbstractUser):
@classmethod
def construct_from_userinfo(cls, userinfo, idp):
LOGGER.debug('Constructing User from:\n%s', userinfo)
if 'sub' not in userinfo:
raise Exception('Missing attribute in userinfo: sub')
sub = userinfo['sub']
......@@ -490,7 +488,18 @@ class Site(models.Model):
@property
def pending_tasks(self):
return [item.parent for item in self.state_items.all()]
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():
tasks[item.parent.id] = item.parent
return tasks.values()
def __str__(self):
return self.name
......@@ -822,25 +831,29 @@ class DeploymentState(models.Model):
deployment=deployment,
key=key,
)
state = None
if query.exists():
return query.first()
# create new state if not
state = cls(
deployment=deployment,
key=key,
)
state.save()
LOGGER.debug(state.msg('created'))
if len(query) == 1:
state = query.first()
else:
raise Exception("more than one state for a key")
else:
# create new state if not
state = cls(
deployment=deployment,
key=key,
)
state.save()
LOGGER.debug(state.msg('created'))
# generate state items
if deployment.service is not None:
for site in deployment.service.site.all():
deploy = DeploymentStateItem(
DeploymentStateItem.get_state_item(
parent=state,
site=site,
)
deploy.save()
service=deployment.service,
).save()
elif deployment.group is not None:
# every site which provides a service for group
for site in deployment.sites:
......@@ -848,12 +861,11 @@ class DeploymentState(models.Model):
groups=deployment.group,
site=site,
):
deploy = DeploymentStateItem(
DeploymentStateItem.get_state_item(
parent=state,
site=site,
service=service,
)
deploy.save()
).save()
return state
......@@ -938,8 +950,6 @@ class DeploymentStateItem(models.Model):
Service,
related_name='state_items',
on_delete=models.CASCADE,
null=True,
blank=True,
)
state = models.CharField(
max_length=50,
......@@ -957,7 +967,6 @@ class DeploymentStateItem(models.Model):
default=credential_default,
)
@property
def user(self):
return self.parent.user
......@@ -970,17 +979,40 @@ class DeploymentStateItem(models.Model):
def group(self):
return self.parent.group
# TODO this should not be needed
@property
def services(self):
return self.parent.services.filter(groups=self.group)
if self.group is None:
return [self.service]
@classmethod
def get_state_item(cls, parent=None, site=None, service=None):
query = parent.state_items.filter(
site=site,
service=service,
)
if query.exists():
if len(query) == 1:
return query.first()
else:
raise Exception("ambiguous DeploymentStateItem")
else:
item = cls(
parent=parent,
site=site,
service=service,
)
return item
# STATE transitions
# user: deployment requested
def user_deploy(self):
if self.state == 'removal_pending':
self._set_state('deployed')
# TODO this is now valid
return
if self.state == 'deployed':
LOGGER.info(self.msg('ignoring invalid state transition user_deploy'))
return
......@@ -992,17 +1024,18 @@ class DeploymentStateItem(models.Model):
# user: removal requested
def user_remove(self):
if self.state == 'not_deployed':
LOGGER.info(self.msg('ignoring invalid state transition user_remove'))
return
if (
self.state == 'deployment_pending'
or self.state == 'questionnaire'
):
# TODO this is not valid
self._set_state('not_deployed')
return
if self.state == 'not_deployed':
LOGGER.info(self.msg('ignoring invalid state transition user_remove'))
return
self._set_state(
'removal_pending',
publish=False, # the post response already contains the update
......
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