Commit dd386be8 authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Reindented code

parent 65251eb0
......@@ -14,33 +14,32 @@ from .logging import logger
# 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(
max_length=150,
default='localhost',
)
max_length=150,
default='localhost',
)
exchange = models.CharField(
max_length=150,
default='deployments',
)
max_length=150,
default='deployments',
)
port = models.IntegerField(
default=15672
)
default=15672
)
path = models.CharField(
max_length=150,
default='api',
)
max_length=150,
default='api',
)
username = models.CharField(
max_length=150,
default='guest',
)
max_length=150,
default='guest',
)
password = models.CharField(
max_length=150,
default='guest',
)
max_length=150,
default='guest',
)
enabled = models.BooleanField(
default=False,
)
default=False,
)
def __str__(self):
return self.host
......@@ -51,17 +50,17 @@ class RabbitMQInstance(models.Model):
@property
def api(self):
return 'http://{}:{}/{}'.format(
self.host,
self.port,
self.path,
)
self.host,
self.port,
self.path,
)
@property
def auth(self):
return HTTPBasicAuth(
self.username,
self.password
)
self.username,
self.password
)
@property
def vhost(self):
......@@ -75,11 +74,12 @@ class RabbitMQInstance(models.Model):
def connection(self):
if self.rabbitmq_connection is None:
rabbitmqconnection_properties = pika.ConnectionParameters(
host=self.host,
ssl=True,
)
host=self.host,
ssl=True,
)
self.rabbitmq_connection = pika.BlockingConnection(
rabbitmqconnection_properties)
rabbitmqconnection_properties
)
logger.debug(self.msg('opened connection'))
return self.rabbitmq_connection
......@@ -89,9 +89,9 @@ class RabbitMQInstance(models.Model):
if self.rabbitmq_channel is None:
self.rabbitmq_channel = self.connection.channel()
self.rabbitmq_channel.exchange_declare(
exchange=self.exchange,
durable=True,
exchange_type='topic')
exchange=self.exchange,
durable=True,
exchange_type='topic')
self.rabbitmq_channel.confirm_delivery()
logger.debug(self.msg('opened channel'))
......@@ -102,8 +102,8 @@ class RabbitMQInstance(models.Model):
def rest_get(self, api_path):
r = requests.get(
self.get_uri(api_path),
auth=self.auth)
self.get_uri(api_path),
auth=self.auth)
r.raise_for_status()
return r.json()
......@@ -111,25 +111,25 @@ class RabbitMQInstance(models.Model):
# the rabbitmq instance
def rest_put(self, api_path, data):
r = requests.put(
self.get_uri(api_path),
json=data,
auth=self.auth)
self.get_uri(api_path),
json=data,
auth=self.auth)
r.raise_for_status()
return r
def rest_del(self, api_path):
r = requests.delete(
self.get_uri(api_path),
auth=self.auth)
self.get_uri(api_path),
auth=self.auth)
r.raise_for_status()
return r
def set_topic_permissions(self, site):
username = site.client.username
path = 'topic-permissions/{}/{}/'.format(
self.vhost,
username,
)
self.vhost,
username,
)
# set permissions for the correct topics
# we construct a regex to match the services of the site
......@@ -144,10 +144,10 @@ class RabbitMQInstance(models.Model):
services = services + prefix + service.name
set_topic_permission_data = {
'exchange': self.exchange,
'write': '^$',
'read': '^service\.({})$'.format(services),
}
'exchange': self.exchange,
'write': '^$',
'read': '^service\.({})$'.format(services),
}
return self.rest_put(path, set_topic_permission_data)
......@@ -155,15 +155,15 @@ class RabbitMQInstance(models.Model):
def set_permissions(self, site):
username = site.client.username
path = 'permissions/{}/{}/'.format(
self.vhost,
username,
)
self.vhost,
username,
)
permission = '^(amq\.gen.*|{})'.format(self.exchange)
set_permission_data = {
'configure': permission,
'write': permission,
'read': permission,
}
'configure': permission,
'write': permission,
'read': permission,
}
return self.rest_put(path, set_permission_data)
......@@ -173,9 +173,9 @@ class RabbitMQInstance(models.Model):
path = 'users/{}/'.format(username)
user_creation_data = {
'password': str(site.client.auth_token.key),
'tags': '',
}
'password': str(site.client.auth_token.key),
'tags': '',
}
return self.rest_put(path, user_creation_data)
......@@ -222,31 +222,30 @@ class RabbitMQInstance(models.Model):
def publish_by_service(self, service, msg):
return self.channel.basic_publish(
exchange=self.exchange,
routing_key=self.service_routing_key(service),
body=msg,
properties=pika.BasicProperties(
delivery_mode=1,
),
)
exchange=self.exchange,
routing_key=self.service_routing_key(service),
body=msg,
properties=pika.BasicProperties(
delivery_mode=1,
),
)
# TODO dirty
def rabbitmq_instance():
return RabbitMQInstance.objects.get(enabled=True)
class User(AbstractUser):
TYPE_CHOICES = (
('apiclient', 'API-Client'),
('oidcuser', 'OIDC User'),
('admin', 'Admin'),
)
('apiclient', 'API-Client'),
('oidcuser', 'OIDC User'),
('admin', 'Admin'),
)
user_type = models.CharField(
max_length=20,
choices=TYPE_CHOICES,
default='oidcuser',
)
max_length=20,
choices=TYPE_CHOICES,
default='oidcuser',
)
sub = models.CharField(max_length=150, blank=True, null=True)
password = models.CharField(max_length=150, blank=True, null=True)
......@@ -312,20 +311,20 @@ class User(AbstractUser):
def construct_user(user_info):
return User(
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'],
)
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'],
)
class Site(models.Model):
client = models.OneToOneField(
User,
related_name='site',
)
User,
related_name='site',
)
name = models.CharField(max_length=150, unique=True)
description = models.TextField(max_length=300, blank=True)
......@@ -344,30 +343,36 @@ class Service(models.Model):
name = models.CharField(max_length=150, unique=True)
description = models.TextField(max_length=300, blank=True)
site = models.ManyToManyField(
Site,
related_name='services')
Site,
related_name='services')
groups = models.ManyToManyField(
Group,
related_name='services',
blank=True)
Group,
related_name='services',
blank=True)
def __str__(self):
return self.name
class SSHPublicKey(models.Model):
name = models.CharField(max_length=150, unique=True)
key = models.TextField(max_length=1000)
name = models.CharField(
max_length=150,
unique=True,
)
key = models.TextField(
max_length=1000
)
# hidden field at the user
user = models.ForeignKey(
User,
related_name='_ssh_keys')
User,
related_name='_ssh_keys',
)
# has the user triggered the deletion of this key
deleted = models.BooleanField(
default=False,
editable=False,
)
default=False,
editable=False,
)
def msg(self, msg):
return '[SSHPublicKey:{}] {}'.format(self, msg)
......@@ -412,27 +417,28 @@ class SSHPublicKey(models.Model):
# The DeploymentTaskItem track the acknowledgements from the clients
class Deployment(models.Model):
user = models.ForeignKey(
User,
related_name='deployments',
on_delete=models.CASCADE,
)
User,
related_name='deployments',
on_delete=models.CASCADE,
)
service = models.ForeignKey(
Service,
related_name='deployments',
on_delete=models.CASCADE,
)
Service,
related_name='deployments',
on_delete=models.CASCADE,
)
ssh_keys = models.ManyToManyField(
SSHPublicKey,
related_name='deployments',
blank=True,
)
SSHPublicKey,
related_name='deployments',
blank=True,
)
ssh_keys_to_withdraw = models.ManyToManyField(
SSHPublicKey,
related_name='withdrawn_deployments',
blank=True,
)
SSHPublicKey,
related_name='withdrawn_deployments',
blank=True,
)
is_active = models.BooleanField(
default=True)
default=True,
)
@property
def withdrawals(self):
......@@ -485,19 +491,19 @@ class Deployment(models.Model):
# generate task
task = DeploymentTask(
action='deploy',
deployment=self,
key=key,
)
action='deploy',
deployment=self,
key=key,
)
task.save()
logger.debug(task.msg('generated'))
# generate task items
for site in self.service.site.all():
deploy = DeploymentTaskItem(
task=task,
site=site,
)
task=task,
site=site,
)
deploy.save()
logger.debug(deploy.msg('generated'))
......@@ -512,19 +518,19 @@ class Deployment(models.Model):
# generate task
task = DeploymentTask(
action='withdraw',
deployment=self,
key=key,
)
action='withdraw',
deployment=self,
key=key,
)
task.save()
logger.debug(task.msg('generated'))
# generate task items
for site in self.service.site.all():
withdrawal = DeploymentTaskItem(
task=task,
site=site,
)
task=task,
site=site,
)
withdrawal.save()
logger.debug(withdrawal.msg('generated'))
......@@ -562,23 +568,23 @@ class Deployment(models.Model):
class DeploymentTask(models.Model):
ACTION_CHOICES = (
('deploy', 'deploy'),
('withdraw', 'withdraw'),
)
('deploy', 'deploy'),
('withdraw', 'withdraw'),
)
action = models.CharField(
max_length=10,
choices=ACTION_CHOICES,
)
max_length=10,
choices=ACTION_CHOICES,
)
key = models.ForeignKey(
SSHPublicKey,
related_name='tasks',
on_delete=models.CASCADE,
)
SSHPublicKey,
related_name='tasks',
on_delete=models.CASCADE,
)
deployment = models.ForeignKey(
Deployment,
related_name='tasks',
on_delete=models.CASCADE,
)
Deployment,
related_name='tasks',
on_delete=models.CASCADE,
)
@property
def user(self):
......@@ -590,11 +596,11 @@ class DeploymentTask(models.Model):
def __str__(self):
return "{}:{}:{} - {}".format(
self.deployment.service,
self.deployment.user,
self.key,
self.action,
)
self.deployment.service,
self.deployment.user,
self.key,
self.action,
)
def msg(self, msg):
return '[DeploymentTask:{}] {}'.format(self, msg)
......@@ -604,8 +610,9 @@ class DeploymentTask(models.Model):
msg = json.dumps(DeploymentTaskSerializer(self).data)
rabbitmq_instance().publish_by_service(
self.service,
msg)
self.service,
msg,
)
# the client acked the receipt and execution of the task for his site
def item_finished(self, site):
......@@ -628,21 +635,21 @@ class DeploymentTask(models.Model):
class DeploymentTaskItem(models.Model):
task = models.ForeignKey(
DeploymentTask,
related_name='task_items',
on_delete=models.CASCADE,
)
DeploymentTask,
related_name='task_items',
on_delete=models.CASCADE,
)
site = models.ForeignKey(
Site,
related_name='task_items',
on_delete=models.CASCADE,
)
Site,
related_name='task_items',
on_delete=models.CASCADE,
)
def __str__(self):
return "{}@{}".format(
self.task,
self.site,
)
self.task,
self.site,
)
def msg(self, msg):
return '[DeploymentTaskItem:{}] {}'.format(self, msg)
......@@ -659,8 +666,7 @@ def create_auth_token(sender, instance=None, created=False, **kwargs):
@receiver(post_save, sender=Site)
def register_at_rabbitmq(
sender, instance=None, created=False, **kwargs):
def register_at_rabbitmq(sender, instance=None, created=False, **kwargs):
if not created:
return
......
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