models.py 19.7 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
4
5
6
7
8
# django senders need their arguments
# pylint: disable=unused-argument

import json
import logging
import requests
from requests.auth import HTTPBasicAuth
import pika
Lukas Burgey's avatar
Lukas Burgey committed
9
from django.conf import settings
Lukas Burgey's avatar
Lukas Burgey committed
10
11
from django.contrib.auth.models import AbstractUser, Group
from django.db import models
12
from django.db.models.signals import post_save, pre_delete
Lukas Burgey's avatar
Lukas Burgey committed
13
from django.dispatch import receiver
Lukas Burgey's avatar
Lukas Burgey committed
14
from rest_framework.authtoken.models import Token
Lukas Burgey's avatar
Lukas Burgey committed
15

Lukas Burgey's avatar
Lukas Burgey committed
16
LOGGER = logging.getLogger(__name__)
17

Lukas Burgey's avatar
Lukas Burgey committed
18

Lukas Burgey's avatar
Lukas Burgey committed
19
20
21
22
# clients are registerred at rabbitmq, when they are assigned to a site
# (because we only then know what services they provide)
class RabbitMQInstance(models.Model):
    host = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
23
24
25
        max_length=150,
        default='localhost',
    )
Lukas Burgey's avatar
Lukas Burgey committed
26
    exchange = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
27
28
29
        max_length=150,
        default='deployments',
    )
Lukas Burgey's avatar
Lukas Burgey committed
30
    port = models.IntegerField(
Lukas Burgey's avatar
Lukas Burgey committed
31
32
        default=15672
    )
Lukas Burgey's avatar
Lukas Burgey committed
33
    path = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
34
35
36
        max_length=150,
        default='api',
    )
Lukas Burgey's avatar
Lukas Burgey committed
37
    username = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
38
39
40
        max_length=150,
        default='guest',
    )
Lukas Burgey's avatar
Lukas Burgey committed
41
    password = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
42
43
44
        max_length=150,
        default='guest',
    )
Lukas Burgey's avatar
Lukas Burgey committed
45
46
    is_active = models.BooleanField(
        default=True,
Lukas Burgey's avatar
Lukas Burgey committed
47
    )
Lukas Burgey's avatar
Lukas Burgey committed
48
49
50
51

    def __str__(self):
        return self.host

Lukas Burgey's avatar
Lukas Burgey committed
52
53
    def msg(self, msg):
        return '[RabbitMQ:{}] {}'.format(self.host, msg)
Lukas Burgey's avatar
Lukas Burgey committed
54
55
56
57

    @property
    def auth(self):
        return HTTPBasicAuth(
Lukas Burgey's avatar
Lukas Burgey committed
58
59
60
            self.username,
            self.password
        )
Lukas Burgey's avatar
Lukas Burgey committed
61
62
63
64
65

    @property
    def vhost(self):
        return '%2f'

66
67
68
69
70
71
    # singletons
    rabbitmq_connection = None
    rabbitmq_channel = None

    @property
    def connection(self):
72
73
74
75
76
        if (
                self.rabbitmq_connection is None
                or self.rabbitmq_connection.is_closed
                or self.rabbitmq_connection.is_closing
        ):
77
            rabbitmqconnection_properties = pika.ConnectionParameters(
Lukas Burgey's avatar
Lukas Burgey committed
78
79
80
                host=self.host,
                ssl=True,
            )
81
            self.rabbitmq_connection = pika.BlockingConnection(
Lukas Burgey's avatar
Lukas Burgey committed
82
83
                rabbitmqconnection_properties
            )
Lukas Burgey's avatar
Lukas Burgey committed
84
            LOGGER.debug(self.msg('opened connection'))
85
86
87
88
89

        return self.rabbitmq_connection

    @property
    def channel(self):
90
91
92
93
94
        if (
                self.rabbitmq_channel is None
                or self.rabbitmq_channel.is_closed
                or self.rabbitmq_channel.is_closing
        ):
95
96
            self.rabbitmq_channel = self.connection.channel()
            self.rabbitmq_channel.exchange_declare(
Lukas Burgey's avatar
Lukas Burgey committed
97
98
99
                exchange=self.exchange,
                durable=True,
                exchange_type='topic')
100
            self.rabbitmq_channel.confirm_delivery()
Lukas Burgey's avatar
Lukas Burgey committed
101
            LOGGER.debug(self.msg('opened channel'))
Lukas Burgey's avatar
Lukas Burgey committed
102

103
104
        return self.rabbitmq_channel

Lukas Burgey's avatar
Lukas Burgey committed
105
    def get_uri(self, path):
Lukas Burgey's avatar
Lukas Burgey committed
106
107
108
109
110
111
112
        api = 'http://{}:{}/{}'.format(
            self.host,
            self.port,
            self.path,
        )

        return '{}/{}'.format(api, path)
Lukas Burgey's avatar
Lukas Burgey committed
113
114

    def rest_get(self, api_path):
Lukas Burgey's avatar
Lukas Burgey committed
115
        req = requests.get(
Lukas Burgey's avatar
Lukas Burgey committed
116
117
            self.get_uri(api_path),
            auth=self.auth)
Lukas Burgey's avatar
Lukas Burgey committed
118
119
        req.raise_for_status()
        return req.json()
Lukas Burgey's avatar
Lukas Burgey committed
120
121
122
123

    # send a rest call with path and data to the rest interface of
    # the rabbitmq instance
    def rest_put(self, api_path, data):
Lukas Burgey's avatar
Lukas Burgey committed
124
        req = requests.put(
Lukas Burgey's avatar
Lukas Burgey committed
125
126
127
            self.get_uri(api_path),
            json=data,
            auth=self.auth)
Lukas Burgey's avatar
Lukas Burgey committed
128
129
        req.raise_for_status()
        return req
Lukas Burgey's avatar
Lukas Burgey committed
130
131

    def rest_del(self, api_path):
Lukas Burgey's avatar
Lukas Burgey committed
132
        req = requests.delete(
Lukas Burgey's avatar
Lukas Burgey committed
133
134
            self.get_uri(api_path),
            auth=self.auth)
Lukas Burgey's avatar
Lukas Burgey committed
135
136
        req.raise_for_status()
        return req
Lukas Burgey's avatar
Lukas Burgey committed
137
138
139
140

    def set_topic_permissions(self, site):
        username = site.client.username
        path = 'topic-permissions/{}/{}/'.format(
Lukas Burgey's avatar
Lukas Burgey committed
141
142
143
            self.vhost,
            username,
        )
Lukas Burgey's avatar
Lukas Burgey committed
144
145
146
147

        # set permissions for the correct topics
        # we construct a regex to match the services of the site
        services = ''
Lukas Burgey's avatar
Lukas Burgey committed
148
        omit_bar = True
Lukas Burgey's avatar
Lukas Burgey committed
149
150
        for service in site.services.all():
            prefix = '|'
Lukas Burgey's avatar
Lukas Burgey committed
151
            if omit_bar:
Lukas Burgey's avatar
Lukas Burgey committed
152
                prefix = ''
Lukas Burgey's avatar
Lukas Burgey committed
153
                omit_bar = False
Lukas Burgey's avatar
Lukas Burgey committed
154
155
156
157

            services = services + prefix + service.name

        set_topic_permission_data = {
Lukas Burgey's avatar
Lukas Burgey committed
158
159
            'exchange': self.exchange,
            'write': '^$',
Lukas Burgey's avatar
Lukas Burgey committed
160
            'read': r'^service\.({})$'.format(services),
Lukas Burgey's avatar
Lukas Burgey committed
161
        }
Lukas Burgey's avatar
Lukas Burgey committed
162
163
164
165
166
167
168

        return self.rest_put(path, set_topic_permission_data)

    # set permissions for the user
    def set_permissions(self, site):
        username = site.client.username
        path = 'permissions/{}/{}/'.format(
Lukas Burgey's avatar
Lukas Burgey committed
169
170
171
            self.vhost,
            username,
        )
Lukas Burgey's avatar
Lukas Burgey committed
172
        permission = r'^(amq\.gen.*|{})'.format(self.exchange)
Lukas Burgey's avatar
Lukas Burgey committed
173
        set_permission_data = {
Lukas Burgey's avatar
Lukas Burgey committed
174
175
176
177
            'configure': permission,
            'write': permission,
            'read': permission,
        }
Lukas Burgey's avatar
Lukas Burgey committed
178
179
180
181
182
183
184
185
186

        return self.rest_put(path, set_permission_data)

    # create user at the rabbitmq instance
    def create_user(self, site):
        username = site.client.username
        path = 'users/{}/'.format(username)

        user_creation_data = {
Lukas Burgey's avatar
Lukas Burgey committed
187
188
189
            'password': str(site.client.auth_token.key),
            'tags': '',
        }
Lukas Burgey's avatar
Lukas Burgey committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

        return self.rest_put(path, user_creation_data)

    # delete user at the rabbitmq instance
    def delete_user(self, site):
        username = site.client.username
        path = 'users/{}/'.format(username)

        return self.rest_del(path)

    # PUBLIC API

    def register_site(self, site):
        self.create_user(site)
        self.set_permissions(site)
        self.set_topic_permissions(site)
Lukas Burgey's avatar
Lukas Burgey committed
206
        LOGGER.info(self.msg('registered {}'.format(site.client)))
Lukas Burgey's avatar
Lukas Burgey committed
207
208
209

    def update_site(self, site):
        self.set_topic_permissions(site)
Lukas Burgey's avatar
Lukas Burgey committed
210
        LOGGER.info(self.msg('updated permissions for {}'.format(site.client)))
Lukas Burgey's avatar
Lukas Burgey committed
211
212

    def deregister_site(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
213
        LOGGER.info(self.msg('deregistered {}'.format(site.client)))
Lukas Burgey's avatar
Lukas Burgey committed
214
215
216
217
218
219
220
221
222

    def is_client_connected(self, site):
        connections = self.rest_get("connections/")
        clients_for_site = [c
                            for c in connections
                            if c['user'] == site.client.username]
        return len(clients_for_site) > 0

    def disconnect(self):
Lukas Burgey's avatar
Lukas Burgey committed
223
        LOGGER.debug(self.msg('closing connection'))
Lukas Burgey's avatar
Lukas Burgey committed
224
225
226
227
228
229
230
        self.connection.close()

    def online_clients(self, service):
        return [site
                for site in service.site.all()
                if self.is_client_connected(site)]

231
232
    def publish_by_service(self, service, msg):
        return self.channel.basic_publish(
Lukas Burgey's avatar
Lukas Burgey committed
233
            exchange=self.exchange,
234
            routing_key=service.routing_key,
Lukas Burgey's avatar
Lukas Burgey committed
235
236
237
238
239
            body=msg,
            properties=pika.BasicProperties(
                delivery_mode=1,
            ),
        )
Lukas Burgey's avatar
Lukas Burgey committed
240
241


Lukas Burgey's avatar
Lukas Burgey committed
242
RABBITMQ_INSTANCE = RabbitMQInstance.objects.filter(is_active=True).first()
Lukas Burgey's avatar
Lukas Burgey committed
243
244


Lukas Burgey's avatar
Lukas Burgey committed
245
class User(AbstractUser):
246
    TYPE_CHOICES = (
Lukas Burgey's avatar
Lukas Burgey committed
247
248
249
250
        ('apiclient', 'API-Client'),
        ('oidcuser', 'OIDC User'),
        ('admin', 'Admin'),
    )
251
    user_type = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
252
253
254
255
        max_length=20,
        choices=TYPE_CHOICES,
        default='oidcuser',
    )
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    sub = models.CharField(
        max_length=150,
        blank=True,
        null=True,
    )
    password = models.CharField(
        max_length=150,
        blank=True,
        null=True,
    )
    # the real state of the user
    # (self.is_active is the supposed state of the user)
    _is_active = models.BooleanField(
        default=True,
        editable=False,
    )
Lukas Burgey's avatar
Lukas Burgey committed
272

Lukas Burgey's avatar
Lukas Burgey committed
273
    # we hide deleted keys here
274
    # the full list of ssh keys is self._ssh_keys
Lukas Burgey's avatar
Lukas Burgey committed
275
276
277
278
    @property
    def ssh_keys(self):
        return self._ssh_keys.filter(deleted=False)

279
280
281
282
    @property
    def is_active_at_clients(self):
        return self._is_active

283
284
285
    def __str__(self):
        if self.user_type == 'admin':
            return 'ADMIN {}'.format(self.username)
Lukas Burgey's avatar
Lukas Burgey committed
286
        elif self.user_type == 'oidcuser':
287
288
289
            if not self.is_active:
                return 'DEACTIVATED USER {}'.format(self.username)
            return 'USER {}'.format(self.username)
Lukas Burgey's avatar
Lukas Burgey committed
290
        elif self.user_type == 'apiclient':
291
            return 'APICLIENT {}@{}'.format(self.username, self.site)
Lukas Burgey's avatar
Lukas Burgey committed
292
293
        else:
            raise Exception()
Lukas Burgey's avatar
Lukas Burgey committed
294

295
296
297
298
299
300
301
    def msg(self, msg):
        return '[{}] {}'.format(self, msg)

    # oidcuser: withdraw and delete all credentials and delete the user
    def remove(self):
        if self.user_type == 'oidcuser':
            self.deactivate()
Lukas Burgey's avatar
Lukas Burgey committed
302
            LOGGER.info(self.msg('Deleting'))
303
304
305
306
307
308
309

            # TODO: deleting the user brings problems:
            # the deletion cascades down to DeploymentTask and DeploymentTaskItem
            # but these need to be conserved so all clients withdrawals can be tracked
            self.delete()

    def activate(self):
310
        if self._is_active:
Lukas Burgey's avatar
Lukas Burgey committed
311
            LOGGER.error(self.msg('already activated'))
312
313
314
315
            return

        if self.user_type == 'oidcuser':
            self.is_active = True
316
            self._is_active = True
317
318
319
320
321
            self.save()

            for dep in self.deployments.all():
                dep.activate()

Lukas Burgey's avatar
Lukas Burgey committed
322
            LOGGER.info(self.msg('activated'))
323
324
325

    # oidcuser: withdraw all credentials
    def deactivate(self):
326
        if not self._is_active:
Lukas Burgey's avatar
Lukas Burgey committed
327
            LOGGER.error(self.msg('already deactivated'))
328
329
330
331
            return

        if self.user_type == 'oidcuser':
            self.is_active = False
332
            self._is_active = False
333
334
335
336
337
            self.save()

            for dep in self.deployments.all():
                dep.deactivate()

Lukas Burgey's avatar
Lukas Burgey committed
338
            LOGGER.info(self.msg('deactivated'))
339
340


Lukas Burgey's avatar
Lukas Burgey committed
341
342
def construct_user(user_info):
    return User(
Lukas Burgey's avatar
Lukas Burgey committed
343
344
345
346
347
348
349
        sub=user_info['sub'],
        name=user_info['name'],
        first_name=user_info['given_name'],
        last_name=user_info['family_name'],
        email=user_info['email'],
        username=user_info['email'],
    )
Lukas Burgey's avatar
Lukas Burgey committed
350
351
352


class Site(models.Model):
353
    client = models.OneToOneField(
Lukas Burgey's avatar
Lukas Burgey committed
354
355
356
        User,
        related_name='site',
    )
Lukas Burgey's avatar
Lukas Burgey committed
357
358
359
360
361
362
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)

    def __str__(self):
        return self.name

363
364
365
366
367
368
    # tasks which are still to be executed on this site
    @property
    def tasks(self):
        return [item.task
                for item
                in self.task_items.all()]
Lukas Burgey's avatar
Lukas Burgey committed
369

Lukas Burgey's avatar
Lukas Burgey committed
370
371
372
373

class Service(models.Model):
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)
374
    site = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
375
376
        Site,
        related_name='services')
Lukas Burgey's avatar
Lukas Burgey committed
377
    groups = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
378
379
380
        Group,
        related_name='services',
        blank=True)
Lukas Burgey's avatar
Lukas Burgey committed
381

382
383
384
385
    @property
    def routing_key(self):
        return 'service.{}'.format(self.name)

Lukas Burgey's avatar
Lukas Burgey committed
386
    def __str__(self):
Lukas Burgey's avatar
Lukas Burgey committed
387
        return self.name
Lukas Burgey's avatar
Lukas Burgey committed
388
389
390


class SSHPublicKey(models.Model):
Lukas Burgey's avatar
Lukas Burgey committed
391
392
393
394
395
396
397
    name = models.CharField(
        max_length=150,
        unique=True,
    )
    key = models.TextField(
        max_length=1000
    )
Lukas Burgey's avatar
Lukas Burgey committed
398
    # hidden field at the user
Lukas Burgey's avatar
Lukas Burgey committed
399
    user = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
400
401
402
        User,
        related_name='_ssh_keys',
    )
Lukas Burgey's avatar
Lukas Burgey committed
403

Lukas Burgey's avatar
Lukas Burgey committed
404
405
    # has the user triggered the deletion of this key
    deleted = models.BooleanField(
Lukas Burgey's avatar
Lukas Burgey committed
406
407
408
        default=False,
        editable=False,
    )
409

Lukas Burgey's avatar
Lukas Burgey committed
410
411
    def msg(self, msg):
        return '[SSHPublicKey:{}] {}'.format(self, msg)
412

413
414
415
    # does not directly delete the key if the key is deployed or withdrawn
    # somewhere
    # the receiver 'delete_withdrawn_ssh_key' does the actual deletion
416
    def delete_key(self):
Lukas Burgey's avatar
Lukas Burgey committed
417
        if (not self.tasks.exists() and not self.deployments.exists()):
Lukas Burgey's avatar
Lukas Burgey committed
418
            LOGGER.info(self.msg('Direct deletion of key'))
419
420
421
            self.delete()
            return

Lukas Burgey's avatar
Lukas Burgey committed
422
        LOGGER.info(self.msg('Deletion of key started'))
423
424
425
        self.deleted = True
        self.save()

Lukas Burgey's avatar
Lukas Burgey committed
426
        # delete implies withdrawing the key from all clients
427
428
429
        for deployment in self.deployments.all():
            deployment.withdraw_key(self)

Lukas Burgey's avatar
Lukas Burgey committed
430
431
    # when a key is withdrawn by a client we try to finally delete it
    def try_final_deletion(self):
Lukas Burgey's avatar
Lukas Burgey committed
432
        if (self.deleted and not self.tasks.exists()):
Lukas Burgey's avatar
Lukas Burgey committed
433
            LOGGER.info(self.msg(
Lukas Burgey's avatar
Lukas Burgey committed
434
                'All clients have withdrawn this key. Final deletion'))
Lukas Burgey's avatar
Lukas Burgey committed
435
436
437
            self.delete()
            return

Lukas Burgey's avatar
Lukas Burgey committed
438
    def __str__(self):
Lukas Burgey's avatar
Lukas Burgey committed
439
440
        if self.deleted:
            return "DELETED: {}".format(self.name)
Lukas Burgey's avatar
Lukas Burgey committed
441
442
443
        return self.name


444
# Deployment describes the credential state per user as it is supposed to be
445
446
447
448
#
# (exception: if is_active=False the ssh_keys contain the keys to be deployed
# if the deployment is reactivated)
#
449
450
# DeploymentTask is what is sent to the clients via rabbitmq
# The DeploymentTaskItem track the acknowledgements from the clients
Lukas Burgey's avatar
Lukas Burgey committed
451
452
class Deployment(models.Model):
    user = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
453
454
455
456
        User,
        related_name='deployments',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
457
    service = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
458
459
460
461
        Service,
        related_name='deployments',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
462
    ssh_keys = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
463
464
465
466
        SSHPublicKey,
        related_name='deployments',
        blank=True,
    )
467
    ssh_keys_to_withdraw = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
468
469
470
471
        SSHPublicKey,
        related_name='withdrawn_deployments',
        blank=True,
    )
472
    is_active = models.BooleanField(
Lukas Burgey's avatar
Lukas Burgey committed
473
474
        default=True,
    )
475

476
477
478
    @property
    def withdrawals(self):
        return self.tasks.filter(action='withdraw')
Lukas Burgey's avatar
Lukas Burgey committed
479

480
481
482
    @property
    def deploys(self):
        return self.tasks.filter(action='deploy')
Lukas Burgey's avatar
Lukas Burgey committed
483

484
485
    def __str__(self):
        return '{}:{}'.format(self.service, self.user)
486

487
488
    def msg(self, msg):
        return '[Deployment:{}] {}'.format(self, msg)
489

490
491
492
    # deploy credentials which were deployed prior to deactivation
    def activate(self):
        if self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
493
            LOGGER.error(self.msg('already active'))
494
495
            return

Lukas Burgey's avatar
Lukas Burgey committed
496
        LOGGER.debug(self.msg(str(self.ssh_keys.all())))
497
498
499
500
501
        for key in self.ssh_keys.all():
            self._deploy_key(key)

        self.is_active = True
        self.save()
Lukas Burgey's avatar
Lukas Burgey committed
502
        LOGGER.info(self.msg('activated'))
503
504
505
506

    # withdraw all credentials
    def deactivate(self):
        if not self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
507
            LOGGER.error(self.msg('already deactivated'))
508
509
510
            return

        self.is_active = False
511
        self.save()
512

513
514
515
        for key in self.ssh_keys.all():
            self._withdraw_key(key)

Lukas Burgey's avatar
Lukas Burgey committed
516
        LOGGER.info(self.msg('deactivated'))
517
518
519
520

    # only deploy the key
    def _deploy_key(self, key):
        # delete outstanding tasks which are made obsolete by this task
521
        for withdrawal in self.withdrawals.filter(key=key):
Lukas Burgey's avatar
Lukas Burgey committed
522
            LOGGER.debug(withdrawal.msg('now obsolete'))
Lukas Burgey's avatar
Lukas Burgey committed
523
            withdrawal.delete()
524
525
526

        # generate task
        task = DeploymentTask(
Lukas Burgey's avatar
Lukas Burgey committed
527
528
529
530
            action='deploy',
            deployment=self,
            key=key,
        )
531
        task.save()
Lukas Burgey's avatar
Lukas Burgey committed
532
        LOGGER.debug(task.msg('generated'))
533
534
535
536

        # generate task items
        for site in self.service.site.all():
            deploy = DeploymentTaskItem(
Lukas Burgey's avatar
Lukas Burgey committed
537
538
539
                task=task,
                site=site,
            )
540
            deploy.save()
Lukas Burgey's avatar
Lukas Burgey committed
541
            LOGGER.debug(deploy.msg('generated'))
542
543
544

        # publish the task
        task.publish()
545

546
547
    def _withdraw_key(self, key):
        # delete outstanding tasks which are made obsolete by this task
548
        for deploy in self.deploys.filter(key=key):
Lukas Burgey's avatar
Lukas Burgey committed
549
            LOGGER.debug(deploy.msg("now obsolete"))
Lukas Burgey's avatar
Lukas Burgey committed
550
            deploy.delete()
Lukas Burgey's avatar
Lukas Burgey committed
551

552
553
        # generate task
        task = DeploymentTask(
Lukas Burgey's avatar
Lukas Burgey committed
554
555
556
557
            action='withdraw',
            deployment=self,
            key=key,
        )
558
        task.save()
Lukas Burgey's avatar
Lukas Burgey committed
559
        LOGGER.debug(task.msg('generated'))
Lukas Burgey's avatar
Lukas Burgey committed
560

561
562
563
        # generate task items
        for site in self.service.site.all():
            withdrawal = DeploymentTaskItem(
Lukas Burgey's avatar
Lukas Burgey committed
564
565
566
                task=task,
                site=site,
            )
567
            withdrawal.save()
Lukas Burgey's avatar
Lukas Burgey committed
568
            LOGGER.debug(withdrawal.msg('generated'))
569

570
571
        # publish the task
        task.publish()
Lukas Burgey's avatar
Lukas Burgey committed
572

573
574
575
    # deploy key and track changes in the key lists
    def deploy_key(self, key):
        if not self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
576
            LOGGER.error(self.msg('cannot deploy while deactivated'))
577
578
579
580
581
582
583
584
585
586
587
588
589
            raise Exception('deployment deactivated')

        self.ssh_keys.add(key)

        if key in self.ssh_keys_to_withdraw.all():
            self.ssh_keys_to_withdraw.remove(key)
        self.save()

        self._deploy_key(key)

    # withdraw key and track changes in the key lists
    def withdraw_key(self, key):
        if not self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
590
            LOGGER.error(self.msg('cannot withdraw while deactivated'))
591
592
593
594
595
596
597
598
599
600
            raise Exception('deployment deactivated')

        self.ssh_keys.remove(key)

        # keys which are to be withdrawn by the clients
        self.ssh_keys_to_withdraw.add(key)
        self.save()

        self._withdraw_key(key)

Lukas Burgey's avatar
Lukas Burgey committed
601

602
603
class DeploymentTask(models.Model):
    ACTION_CHOICES = (
Lukas Burgey's avatar
Lukas Burgey committed
604
605
606
        ('deploy', 'deploy'),
        ('withdraw', 'withdraw'),
    )
607
    action = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
608
609
610
        max_length=10,
        choices=ACTION_CHOICES,
    )
611
    key = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
612
613
614
615
        SSHPublicKey,
        related_name='tasks',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
616
    deployment = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
617
618
619
620
        Deployment,
        related_name='tasks',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
621
622
623
624
625
626
627
628
629

    @property
    def user(self):
        return self.deployment.user

    @property
    def service(self):
        return self.deployment.service

630
    def __str__(self):
631
        return "{}:{}:{} - {}".format(
Lukas Burgey's avatar
Lukas Burgey committed
632
633
634
635
636
            self.deployment.service,
            self.deployment.user,
            self.key,
            self.action,
        )
637

Lukas Burgey's avatar
Lukas Burgey committed
638
639
    def msg(self, msg):
        return '[DeploymentTask:{}] {}'.format(self, msg)
640
641

    def publish(self):
642
        # FIXME mitigating circular dependencies here
643
644
645
        from .clientapi.serializers import DeploymentTaskSerializer
        msg = json.dumps(DeploymentTaskSerializer(self).data)

Lukas Burgey's avatar
Lukas Burgey committed
646
        RABBITMQ_INSTANCE.publish_by_service(
Lukas Burgey's avatar
Lukas Burgey committed
647
648
649
            self.service,
            msg,
        )
650
651
652

    # the client acked the receipt and execution of the task for his site
    def item_finished(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
653
        item = self.task_items.get(site=site)
Lukas Burgey's avatar
Lukas Burgey committed
654
        LOGGER.debug(item.msg('done'))
Lukas Burgey's avatar
Lukas Burgey committed
655
        item.delete()
656
657
658
659
660
661

        if not self.task_items.exists():
            self.finished()

    # maintenance after all task items are done
    def finished(self):
Lukas Burgey's avatar
Lukas Burgey committed
662
        LOGGER.info(self.msg('done'))
Lukas Burgey's avatar
Lukas Burgey committed
663
        self.delete()
664
665
666
667
668
669
670
671

        # check if this was the final withdraw in a key deletion
        if self.action == 'withdraw':
            self.key.try_final_deletion()


class DeploymentTaskItem(models.Model):
    task = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
672
673
674
675
        DeploymentTask,
        related_name='task_items',
        on_delete=models.CASCADE,
    )
676
    site = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
677
678
679
680
        Site,
        related_name='task_items',
        on_delete=models.CASCADE,
    )
681

Lukas Burgey's avatar
Lukas Burgey committed
682
    def __str__(self):
683
        return "{}@{}".format(
Lukas Burgey's avatar
Lukas Burgey committed
684
685
686
            self.task,
            self.site,
        )
687

Lukas Burgey's avatar
Lukas Burgey committed
688
689
    def msg(self, msg):
        return '[DeploymentTaskItem:{}] {}'.format(self, msg)
690

Lukas Burgey's avatar
Lukas Burgey committed
691

692
693
694
#
# RECEIVERS
#
Lukas Burgey's avatar
Lukas Burgey committed
695
696
697
698
699
700
701
702

@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if instance.user_type == 'apiclient' and created:
        Token.objects.create(user=instance)


@receiver(post_save, sender=Site)
Lukas Burgey's avatar
Lukas Burgey committed
703
def register_at_rabbitmq(sender, instance=None, created=False, **kwargs):
Lukas Burgey's avatar
Lukas Burgey committed
704
705
706
707
708
709
710
    if not created:
        return

    RabbitMQInstance().register_site(instance)


@receiver(pre_delete, sender=Site)
Lukas Burgey's avatar
Lukas Burgey committed
711
def deregister_at_rabbitmq(sender, instance=None, **kwargs):
Lukas Burgey's avatar
Lukas Burgey committed
712
    RabbitMQInstance().deregister_site(instance)
713
714
715
716
717
718
719


@receiver(post_save, sender=User)
def deactivate_user(sender, instance=None, created=False, **kwargs):
    if created:
        return

720
    if not instance.is_active and instance.is_active_at_clients:
721
722
723
724
725
726
727
728
        instance.deactivate()


@receiver(post_save, sender=User)
def activate_user(sender, instance=None, created=False, **kwargs):
    if created:
        return

729
    if instance.is_active and not instance.is_active_at_clients:
730
        instance.activate()