__init__.py 3.41 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1

2 3 4 5
from logging import getLogger

from django.db import models

6
# these imports are exports!
7
from ..auth.v1.models.vo import VO
8
from .brokers import RabbitMQInstance
9
from .users import User, SSHPublicKey
10 11 12 13 14 15

LOGGER = getLogger(__name__)


def questionnaire_default():
    return {}
16 17


18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
def credential_default():
    return {}


class Site(models.Model):
    client = models.OneToOneField(
        User,
        related_name='site',
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
    )
    name = models.CharField(
        max_length=150,
        unique=True,
    )
    description = models.TextField(
        max_length=300,
        blank=True,
    )

    @property
40 41
    def pending_deployments(self):
        deployments = {}
Lukas Burgey's avatar
Lukas Burgey committed
42

Lukas Burgey's avatar
Lukas Burgey committed
43
        # ignores orphaned deployment states
Lukas Burgey's avatar
Lukas Burgey committed
44 45
        dep_states = [
            state
46
            for state in self.states.all()
Lukas Burgey's avatar
Lukas Burgey committed
47 48
            if (
                state.is_pending or state.is_credential_pending
49
            ) and state.deployments.exists()
Lukas Burgey's avatar
Lukas Burgey committed
50 51
        ]

52
        # make the deployments unique here
53 54 55 56 57 58
        for state in dep_states:
            # we filter for deployments of the same state_target here
            # if we have multiple deployments per deployment state this causes only the
            # deployments with the same state_target to be pending
            for deployment in state.deployments.filter(state_target=state.state_target):
                deployments[deployment.id] = deployment
59

60
        return deployments.values()
61 62 63 64 65 66

    def __str__(self):
        return self.name


class Service(models.Model):
67

68 69 70 71
    name = models.CharField(
        max_length=150,
        unique=True,
    )
72

73 74 75 76 77 78
    site = models.ForeignKey(
        Site,
        related_name='services',
        on_delete=models.CASCADE,
    )

79 80 81 82
    description = models.TextField(
        max_length=300,
        blank=True,
    )
83 84 85

    vos = models.ManyToManyField(
        VO,
86 87 88 89
        related_name='services',
        blank=True,
    )

90
    @classmethod
91
    def get_service(cls, name, site, description='', vos=[]):
92
        try:
93 94 95 96 97 98 99 100 101 102
            service = cls.objects.get(
                name=name,
            )

            if service.description != description:
                service.description = description
                service.save()

            return service

103 104 105
        except cls.DoesNotExist:
            service = cls(
                name=name,
106
                site=site,
107 108 109 110
                description=description,
            )
            service.save()

111 112
            for vo in vos:
                service.vos.add(vo)
113 114 115

            return service

116 117 118
    def __str__(self):
        return self.name

Lukas Burgey's avatar
Lukas Burgey committed
119
    def remove_service(self):
Lukas Burgey's avatar
Lukas Burgey committed
120 121
        from . import deployments
        for deployment in deployments.Deployment.objects.filter(
Lukas Burgey's avatar
Lukas Burgey committed
122 123 124 125 126
            vo__services=self,
        ):
            deployment.service_removed(self)
        self.delete()

127 128
    def handle_vo_deployments(self):
        for vo in self.vos.all():
129

130
            # users that have vo deployments for this vo
131
            for user in User.objects.filter(
132
                    deployments__vo=vo,
133
            ).distinct():
134
                LOGGER.debug(user.msg('New service for vo. Adding to deployment'))
135 136

                try:
137
                    deployment = user.deployments.get(vo=vo)
138
                    deployment.service_added(self)
Lukas Burgey's avatar
Lukas Burgey committed
139
                except deployments.Deployment.DoesNotExist:
140
                    LOGGER.error('Inconsistency of vo deployment')
141
                    raise