models.py 19.3 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
from .logging import setup_logger
Lukas Burgey's avatar
Lukas Burgey committed
16

Lukas Burgey's avatar
Lukas Burgey committed
17
18
19
# we start the logger here
setup_logger()
LOGGER = logging.getLogger(__name__)
20

Lukas Burgey's avatar
Lukas Burgey committed
21

Lukas Burgey's avatar
Lukas Burgey committed
22
23
24
25
# 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
26
27
28
        max_length=150,
        default='localhost',
    )
Lukas Burgey's avatar
Lukas Burgey committed
29
    exchange = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
30
31
32
        max_length=150,
        default='deployments',
    )
Lukas Burgey's avatar
Lukas Burgey committed
33
    port = models.IntegerField(
Lukas Burgey's avatar
Lukas Burgey committed
34
35
        default=15672
    )
Lukas Burgey's avatar
Lukas Burgey committed
36
    path = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
37
38
39
        max_length=150,
        default='api',
    )
Lukas Burgey's avatar
Lukas Burgey committed
40
    username = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
41
42
43
        max_length=150,
        default='guest',
    )
Lukas Burgey's avatar
Lukas Burgey committed
44
    password = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
45
46
47
        max_length=150,
        default='guest',
    )
Lukas Burgey's avatar
Lukas Burgey committed
48
49
    is_active = models.BooleanField(
        default=True,
Lukas Burgey's avatar
Lukas Burgey committed
50
    )
Lukas Burgey's avatar
Lukas Burgey committed
51
52
53
54

    def __str__(self):
        return self.host

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

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

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

69
70
71
72
73
74
75
76
    # singletons
    rabbitmq_connection = None
    rabbitmq_channel = None

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

        return self.rabbitmq_connection

    @property
    def channel(self):
        if self.rabbitmq_channel is None:
            self.rabbitmq_channel = self.connection.channel()
            self.rabbitmq_channel.exchange_declare(
Lukas Burgey's avatar
Lukas Burgey committed
92
93
94
                exchange=self.exchange,
                durable=True,
                exchange_type='topic')
95
            self.rabbitmq_channel.confirm_delivery()
Lukas Burgey's avatar
Lukas Burgey committed
96
            LOGGER.debug(self.msg('opened channel'))
Lukas Burgey's avatar
Lukas Burgey committed
97

98
99
        return self.rabbitmq_channel

Lukas Burgey's avatar
Lukas Burgey committed
100
    def get_uri(self, path):
Lukas Burgey's avatar
Lukas Burgey committed
101
102
103
104
105
106
107
        api = 'http://{}:{}/{}'.format(
            self.host,
            self.port,
            self.path,
        )

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

    def rest_get(self, api_path):
Lukas Burgey's avatar
Lukas Burgey committed
110
        req = requests.get(
Lukas Burgey's avatar
Lukas Burgey committed
111
112
            self.get_uri(api_path),
            auth=self.auth)
Lukas Burgey's avatar
Lukas Burgey committed
113
114
        req.raise_for_status()
        return req.json()
Lukas Burgey's avatar
Lukas Burgey committed
115
116
117
118

    # 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
119
        req = requests.put(
Lukas Burgey's avatar
Lukas Burgey committed
120
121
122
            self.get_uri(api_path),
            json=data,
            auth=self.auth)
Lukas Burgey's avatar
Lukas Burgey committed
123
124
        req.raise_for_status()
        return req
Lukas Burgey's avatar
Lukas Burgey committed
125
126

    def rest_del(self, api_path):
Lukas Burgey's avatar
Lukas Burgey committed
127
        req = requests.delete(
Lukas Burgey's avatar
Lukas Burgey committed
128
129
            self.get_uri(api_path),
            auth=self.auth)
Lukas Burgey's avatar
Lukas Burgey committed
130
131
        req.raise_for_status()
        return req
Lukas Burgey's avatar
Lukas Burgey committed
132
133
134
135

    def set_topic_permissions(self, site):
        username = site.client.username
        path = 'topic-permissions/{}/{}/'.format(
Lukas Burgey's avatar
Lukas Burgey committed
136
137
138
            self.vhost,
            username,
        )
Lukas Burgey's avatar
Lukas Burgey committed
139
140
141
142

        # 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
143
        omit_bar = True
Lukas Burgey's avatar
Lukas Burgey committed
144
145
        for service in site.services.all():
            prefix = '|'
Lukas Burgey's avatar
Lukas Burgey committed
146
            if omit_bar:
Lukas Burgey's avatar
Lukas Burgey committed
147
                prefix = ''
Lukas Burgey's avatar
Lukas Burgey committed
148
                omit_bar = False
Lukas Burgey's avatar
Lukas Burgey committed
149
150
151
152

            services = services + prefix + service.name

        set_topic_permission_data = {
Lukas Burgey's avatar
Lukas Burgey committed
153
154
            'exchange': self.exchange,
            'write': '^$',
Lukas Burgey's avatar
Lukas Burgey committed
155
            'read': r'^service\.({})$'.format(services),
Lukas Burgey's avatar
Lukas Burgey committed
156
        }
Lukas Burgey's avatar
Lukas Burgey committed
157
158
159
160
161
162
163

        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
164
165
166
            self.vhost,
            username,
        )
Lukas Burgey's avatar
Lukas Burgey committed
167
        permission = r'^(amq\.gen.*|{})'.format(self.exchange)
Lukas Burgey's avatar
Lukas Burgey committed
168
        set_permission_data = {
Lukas Burgey's avatar
Lukas Burgey committed
169
170
171
172
            'configure': permission,
            'write': permission,
            'read': permission,
        }
Lukas Burgey's avatar
Lukas Burgey committed
173
174
175
176
177
178
179
180
181

        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
182
183
184
            'password': str(site.client.auth_token.key),
            'tags': '',
        }
Lukas Burgey's avatar
Lukas Burgey committed
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

        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
201
        LOGGER.info(self.msg('registered {}'.format(site.client)))
Lukas Burgey's avatar
Lukas Burgey committed
202
203
204

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

    def deregister_site(self, site):
Lukas Burgey's avatar
Lukas Burgey committed
208
        LOGGER.info(self.msg('deregistered {}'.format(site.client)))
Lukas Burgey's avatar
Lukas Burgey committed
209
210
211
212
213
214
215
216
217

    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
218
        LOGGER.debug(self.msg('closing connection'))
Lukas Burgey's avatar
Lukas Burgey committed
219
220
221
222
223
224
225
        self.connection.close()

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

226
227
    def publish_by_service(self, service, msg):
        return self.channel.basic_publish(
Lukas Burgey's avatar
Lukas Burgey committed
228
229
230
231
232
233
234
            exchange=self.exchange,
            routing_key=self.service_routing_key(service),
            body=msg,
            properties=pika.BasicProperties(
                delivery_mode=1,
            ),
        )
Lukas Burgey's avatar
Lukas Burgey committed
235
236


Lukas Burgey's avatar
Lukas Burgey committed
237
238
239
240
241
def service_routing_key(service):
    return 'service.' + service.name


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


Lukas Burgey's avatar
Lukas Burgey committed
244
class User(AbstractUser):
245
    TYPE_CHOICES = (
Lukas Burgey's avatar
Lukas Burgey committed
246
247
248
249
        ('apiclient', 'API-Client'),
        ('oidcuser', 'OIDC User'),
        ('admin', 'Admin'),
    )
250
    user_type = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
251
252
253
254
        max_length=20,
        choices=TYPE_CHOICES,
        default='oidcuser',
    )
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    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
271

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

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

290
291
292
293
294
295
296
    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
297
            LOGGER.info(self.msg('Deleting'))
298
299
300
301
302
303
304

            # 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):
305
        if self._is_active:
Lukas Burgey's avatar
Lukas Burgey committed
306
            LOGGER.error(self.msg('already activated'))
307
308
309
310
            return

        if self.user_type == 'oidcuser':
            self.is_active = True
311
            self._is_active = True
312
313
314
315
316
            self.save()

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

Lukas Burgey's avatar
Lukas Burgey committed
317
            LOGGER.info(self.msg('activated'))
318
319
320

    # oidcuser: withdraw all credentials
    def deactivate(self):
321
        if not self._is_active:
Lukas Burgey's avatar
Lukas Burgey committed
322
            LOGGER.error(self.msg('already deactivated'))
323
324
325
326
            return

        if self.user_type == 'oidcuser':
            self.is_active = False
327
            self._is_active = False
328
329
330
331
332
            self.save()

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

Lukas Burgey's avatar
Lukas Burgey committed
333
            LOGGER.info(self.msg('deactivated'))
334
335


Lukas Burgey's avatar
Lukas Burgey committed
336
337
def construct_user(user_info):
    return User(
Lukas Burgey's avatar
Lukas Burgey committed
338
339
340
341
342
343
344
        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
345
346
347


class Site(models.Model):
348
    client = models.OneToOneField(
Lukas Burgey's avatar
Lukas Burgey committed
349
350
351
        User,
        related_name='site',
    )
Lukas Burgey's avatar
Lukas Burgey committed
352
353
354
355
356
357
    name = models.CharField(max_length=150, unique=True)
    description = models.TextField(max_length=300, blank=True)

    def __str__(self):
        return self.name

358
359
360
361
362
363
    # 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
364

Lukas Burgey's avatar
Lukas Burgey committed
365
366
367
368

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

    def __str__(self):
Lukas Burgey's avatar
Lukas Burgey committed
378
        return self.name
Lukas Burgey's avatar
Lukas Burgey committed
379
380
381


class SSHPublicKey(models.Model):
Lukas Burgey's avatar
Lukas Burgey committed
382
383
384
385
386
387
388
    name = models.CharField(
        max_length=150,
        unique=True,
    )
    key = models.TextField(
        max_length=1000
    )
Lukas Burgey's avatar
Lukas Burgey committed
389
    # hidden field at the user
Lukas Burgey's avatar
Lukas Burgey committed
390
    user = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
391
392
393
        User,
        related_name='_ssh_keys',
    )
Lukas Burgey's avatar
Lukas Burgey committed
394

Lukas Burgey's avatar
Lukas Burgey committed
395
396
    # has the user triggered the deletion of this key
    deleted = models.BooleanField(
Lukas Burgey's avatar
Lukas Burgey committed
397
398
399
        default=False,
        editable=False,
    )
400

Lukas Burgey's avatar
Lukas Burgey committed
401
402
    def msg(self, msg):
        return '[SSHPublicKey:{}] {}'.format(self, msg)
403

404
405
406
    # does not directly delete the key if the key is deployed or withdrawn
    # somewhere
    # the receiver 'delete_withdrawn_ssh_key' does the actual deletion
407
    def delete_key(self):
Lukas Burgey's avatar
Lukas Burgey committed
408
        if (not self.tasks.exists() and not self.deployments.exists()):
Lukas Burgey's avatar
Lukas Burgey committed
409
            LOGGER.info(self.msg('Direct deletion of key'))
410
411
412
            self.delete()
            return

Lukas Burgey's avatar
Lukas Burgey committed
413
        LOGGER.info(self.msg('Deletion of key started'))
414
415
416
        self.deleted = True
        self.save()

Lukas Burgey's avatar
Lukas Burgey committed
417
        # delete implies withdrawing the key from all clients
418
419
420
        for deployment in self.deployments.all():
            deployment.withdraw_key(self)

Lukas Burgey's avatar
Lukas Burgey committed
421
422
    # 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
423
        if (self.deleted and not self.tasks.exists()):
Lukas Burgey's avatar
Lukas Burgey committed
424
            LOGGER.info(self.msg(
Lukas Burgey's avatar
Lukas Burgey committed
425
                'All clients have withdrawn this key. Final deletion'))
Lukas Burgey's avatar
Lukas Burgey committed
426
427
428
            self.delete()
            return

Lukas Burgey's avatar
Lukas Burgey committed
429
    def __str__(self):
Lukas Burgey's avatar
Lukas Burgey committed
430
431
        if self.deleted:
            return "DELETED: {}".format(self.name)
Lukas Burgey's avatar
Lukas Burgey committed
432
433
434
        return self.name


435
# Deployment describes the credential state per user as it is supposed to be
436
437
438
439
#
# (exception: if is_active=False the ssh_keys contain the keys to be deployed
# if the deployment is reactivated)
#
440
441
# 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
442
443
class Deployment(models.Model):
    user = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
444
445
446
447
        User,
        related_name='deployments',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
448
    service = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
449
450
451
452
        Service,
        related_name='deployments',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
453
    ssh_keys = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
454
455
456
457
        SSHPublicKey,
        related_name='deployments',
        blank=True,
    )
458
    ssh_keys_to_withdraw = models.ManyToManyField(
Lukas Burgey's avatar
Lukas Burgey committed
459
460
461
462
        SSHPublicKey,
        related_name='withdrawn_deployments',
        blank=True,
    )
463
    is_active = models.BooleanField(
Lukas Burgey's avatar
Lukas Burgey committed
464
465
        default=True,
    )
466

467
468
469
    @property
    def withdrawals(self):
        return self.tasks.filter(action='withdraw')
Lukas Burgey's avatar
Lukas Burgey committed
470

471
472
473
    @property
    def deploys(self):
        return self.tasks.filter(action='deploy')
Lukas Burgey's avatar
Lukas Burgey committed
474

475
476
    def __str__(self):
        return '{}:{}'.format(self.service, self.user)
477

478
479
    def msg(self, msg):
        return '[Deployment:{}] {}'.format(self, msg)
480

481
482
483
    # deploy credentials which were deployed prior to deactivation
    def activate(self):
        if self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
484
            LOGGER.error(self.msg('already active'))
485
486
            return

Lukas Burgey's avatar
Lukas Burgey committed
487
        LOGGER.debug(self.msg(str(self.ssh_keys.all())))
488
489
490
491
492
        for key in self.ssh_keys.all():
            self._deploy_key(key)

        self.is_active = True
        self.save()
Lukas Burgey's avatar
Lukas Burgey committed
493
        LOGGER.info(self.msg('activated'))
494
495
496
497

    # withdraw all credentials
    def deactivate(self):
        if not self.is_active:
Lukas Burgey's avatar
Lukas Burgey committed
498
            LOGGER.error(self.msg('already deactivated'))
499
500
501
            return

        self.is_active = False
502
        self.save()
503

504
505
506
        for key in self.ssh_keys.all():
            self._withdraw_key(key)

Lukas Burgey's avatar
Lukas Burgey committed
507
        LOGGER.info(self.msg('deactivated'))
508
509
510
511

    # only deploy the key
    def _deploy_key(self, key):
        # delete outstanding tasks which are made obsolete by this task
512
        for withdrawal in self.withdrawals.filter(key=key):
Lukas Burgey's avatar
Lukas Burgey committed
513
            LOGGER.debug(withdrawal.msg('now obsolete'))
Lukas Burgey's avatar
Lukas Burgey committed
514
            withdrawal.delete()
515
516
517

        # generate task
        task = DeploymentTask(
Lukas Burgey's avatar
Lukas Burgey committed
518
519
520
521
            action='deploy',
            deployment=self,
            key=key,
        )
522
        task.save()
Lukas Burgey's avatar
Lukas Burgey committed
523
        LOGGER.debug(task.msg('generated'))
524
525
526
527

        # generate task items
        for site in self.service.site.all():
            deploy = DeploymentTaskItem(
Lukas Burgey's avatar
Lukas Burgey committed
528
529
530
                task=task,
                site=site,
            )
531
            deploy.save()
Lukas Burgey's avatar
Lukas Burgey committed
532
            LOGGER.debug(deploy.msg('generated'))
533
534
535

        # publish the task
        task.publish()
536

537
538
    def _withdraw_key(self, key):
        # delete outstanding tasks which are made obsolete by this task
539
        for deploy in self.deploys.filter(key=key):
Lukas Burgey's avatar
Lukas Burgey committed
540
            LOGGER.debug(deploy.msg("now obsolete"))
Lukas Burgey's avatar
Lukas Burgey committed
541
            deploy.delete()
Lukas Burgey's avatar
Lukas Burgey committed
542

543
544
        # generate task
        task = DeploymentTask(
Lukas Burgey's avatar
Lukas Burgey committed
545
546
547
548
            action='withdraw',
            deployment=self,
            key=key,
        )
549
        task.save()
Lukas Burgey's avatar
Lukas Burgey committed
550
        LOGGER.debug(task.msg('generated'))
Lukas Burgey's avatar
Lukas Burgey committed
551

552
553
554
        # generate task items
        for site in self.service.site.all():
            withdrawal = DeploymentTaskItem(
Lukas Burgey's avatar
Lukas Burgey committed
555
556
557
                task=task,
                site=site,
            )
558
            withdrawal.save()
Lukas Burgey's avatar
Lukas Burgey committed
559
            LOGGER.debug(withdrawal.msg('generated'))
560

561
562
        # publish the task
        task.publish()
Lukas Burgey's avatar
Lukas Burgey committed
563

564
565
566
    # 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
567
            LOGGER.error(self.msg('cannot deploy while deactivated'))
568
569
570
571
572
573
574
575
576
577
578
579
580
            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
581
            LOGGER.error(self.msg('cannot withdraw while deactivated'))
582
583
584
585
586
587
588
589
590
591
            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
592

593
594
class DeploymentTask(models.Model):
    ACTION_CHOICES = (
Lukas Burgey's avatar
Lukas Burgey committed
595
596
597
        ('deploy', 'deploy'),
        ('withdraw', 'withdraw'),
    )
598
    action = models.CharField(
Lukas Burgey's avatar
Lukas Burgey committed
599
600
601
        max_length=10,
        choices=ACTION_CHOICES,
    )
602
    key = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
603
604
605
606
        SSHPublicKey,
        related_name='tasks',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
607
    deployment = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
608
609
610
611
        Deployment,
        related_name='tasks',
        on_delete=models.CASCADE,
    )
Lukas Burgey's avatar
Lukas Burgey committed
612
613
614
615
616
617
618
619
620

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

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

621
    def __str__(self):
622
        return "{}:{}:{} - {}".format(
Lukas Burgey's avatar
Lukas Burgey committed
623
624
625
626
627
            self.deployment.service,
            self.deployment.user,
            self.key,
            self.action,
        )
628

Lukas Burgey's avatar
Lukas Burgey committed
629
630
    def msg(self, msg):
        return '[DeploymentTask:{}] {}'.format(self, msg)
631
632
633
634
635

    def publish(self):
        from .clientapi.serializers import DeploymentTaskSerializer
        msg = json.dumps(DeploymentTaskSerializer(self).data)

Lukas Burgey's avatar
Lukas Burgey committed
636
        RABBITMQ_INSTANCE.publish_by_service(
Lukas Burgey's avatar
Lukas Burgey committed
637
638
639
            self.service,
            msg,
        )
640
641
642

    # 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
643
        item = self.task_items.get(site=site)
Lukas Burgey's avatar
Lukas Burgey committed
644
        LOGGER.debug(item.msg('done'))
Lukas Burgey's avatar
Lukas Burgey committed
645
        item.delete()
646
647
648
649
650
651

        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
652
        LOGGER.info(self.msg('done'))
Lukas Burgey's avatar
Lukas Burgey committed
653
        self.delete()
654
655
656
657
658
659
660
661

        # 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
662
663
664
665
        DeploymentTask,
        related_name='task_items',
        on_delete=models.CASCADE,
    )
666
    site = models.ForeignKey(
Lukas Burgey's avatar
Lukas Burgey committed
667
668
669
670
        Site,
        related_name='task_items',
        on_delete=models.CASCADE,
    )
671

Lukas Burgey's avatar
Lukas Burgey committed
672
    def __str__(self):
673
        return "{}@{}".format(
Lukas Burgey's avatar
Lukas Burgey committed
674
675
676
            self.task,
            self.site,
        )
677

Lukas Burgey's avatar
Lukas Burgey committed
678
679
    def msg(self, msg):
        return '[DeploymentTaskItem:{}] {}'.format(self, msg)
680

Lukas Burgey's avatar
Lukas Burgey committed
681

682
683
684
#
# RECEIVERS
#
Lukas Burgey's avatar
Lukas Burgey committed
685
686
687
688
689
690
691
692

@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
693
def register_at_rabbitmq(sender, instance=None, created=False, **kwargs):
Lukas Burgey's avatar
Lukas Burgey committed
694
695
696
697
698
699
700
    if not created:
        return

    RabbitMQInstance().register_site(instance)


@receiver(pre_delete, sender=Site)
Lukas Burgey's avatar
Lukas Burgey committed
701
def deregister_at_rabbitmq(sender, instance=None, **kwargs):
Lukas Burgey's avatar
Lukas Burgey committed
702
    RabbitMQInstance().deregister_site(instance)
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720


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

    if not instance.is_active and instance._is_active:
        instance.deactivate()


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

    if instance.is_active and not instance._is_active:
        instance.activate()