__init__.py 3.35 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()
47
            if state.is_pending and state.deployments.exists()
Lukas Burgey's avatar
Lukas Burgey committed
48 49
        ]

50
        # make the deployments unique here
51 52 53 54 55 56
        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
57

58
        return deployments.values()
59 60 61 62 63 64

    def __str__(self):
        return self.name


class Service(models.Model):
65

66 67 68 69
    name = models.CharField(
        max_length=150,
        unique=True,
    )
70

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

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

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

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

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

            return service

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

109 110
            for vo in vos:
                service.vos.add(vo)
111 112 113

            return service

114 115 116
    def __str__(self):
        return self.name

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

125 126
    def handle_vo_deployments(self):
        for vo in self.vos.all():
127

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

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