Commit 5abcfb5f authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Refactor rabbitmq interaction

parent ef2b577f
...@@ -4,11 +4,11 @@ from django.conf import settings ...@@ -4,11 +4,11 @@ from django.conf import settings
from django.dispatch import receiver, Signal from django.dispatch import receiver, Signal
from django.utils.timezone import make_aware from django.utils.timezone import make_aware
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from django.db.models.signals import post_save, m2m_changed from django.db.models.signals import post_save
# from django.db.models.signals import m2m_changed
from datetime import datetime from datetime import datetime
import requests from .rabbitmq import RabbitMQInstance
from requests.auth import HTTPBasicAuth
deployment_change = Signal(providing_args=['instance']) deployment_change = Signal(providing_args=['instance'])
...@@ -95,76 +95,12 @@ class Site(models.Model): ...@@ -95,76 +95,12 @@ class Site(models.Model):
@receiver(post_save, sender=Site) @receiver(post_save, sender=Site)
def register_client_at_rabbitmq(sender, instance=None, created=False, def register_at_rabbitmq(
**kwargs): sender, instance=None, created=False, **kwargs):
if not created: if not created:
return return
print('registerring client {}'.format(instance.client.username)) RabbitMQInstance().register_site(instance)
# %2f: url encoded '/' as this is the vhost we use
api = 'http://localhost:15672/api'
username = instance.client.username
vhost = '%2f'
exchange = 'deployments'
# guest only works on the localhost
auth = HTTPBasicAuth('guest', 'guest')
# create user
user_creation_uri = '{}/users/{}/'.format(
api,
username
)
user_creation_data = {
'password': str(instance.client.auth_token.key),
'tags': '',
}
r = requests.put(user_creation_uri, json=user_creation_data, auth=auth)
print('status_code {} {}'.format(r.status_code, r.text))
# set permissions for the user
set_permission_uri = '{}/permissions/{}/{}/'.format(
api,
vhost,
username,
)
permission = '^(amq\.gen.*|{})'.format(exchange)
set_permission_data = {
'configure': permission,
'write': permission,
'read': permission,
}
set_topic_permission_uri = '{}/topic-permissions/{}/{}/'.format(
api,
vhost,
username,
)
r = requests.put(set_permission_uri, json=set_permission_data, auth=auth)
print('status_code {} {}'.format(r.status_code, r.text))
# set permissions for the correct topics
# we construct a regex to match the services of the site
services = ''
omitBar = True
for service in instance.services.all():
prefix = '|'
if omitBar:
prefix = ''
omitBar = False
services = services + prefix + service.name
set_topic_permission_data = {
'exchange': exchange,
'write': '^$',
'read': '^service\.({})$'.format(services),
}
r = requests.put(
set_topic_permission_uri,
json=set_topic_permission_data, auth=auth)
print('status_code {} {}'.format(r.status_code, r.text))
class Service(models.Model): class Service(models.Model):
......
import requests
from requests.auth import HTTPBasicAuth
# clients are registerred at rabbitmq, when they are assigned to a site
# (because we only then know what services they provide)
class RabbitMQInstance:
def __init__(self):
self.api = 'http://localhost:15672/api'
# %2f: url encoded '/' as this is the vhost we use
self.vhost = '%2f'
self.exchange = 'deployments'
# guest only works on the localhost
self.auth = HTTPBasicAuth('guest', 'guest')
def set_topic_permissions(self, site):
username = site.client.username
set_topic_permission_uri = '{}/topic-permissions/{}/{}/'.format(
self.api,
self.vhost,
username,
)
# set permissions for the correct topics
# we construct a regex to match the services of the site
services = ''
omitBar = True
for service in site.services.all():
prefix = '|'
if omitBar:
prefix = ''
omitBar = False
services = services + prefix + service.name
set_topic_permission_data = {
'exchange': self.exchange,
'write': '^$',
'read': '^service\.({})$'.format(services),
}
# TODO might fail
r = requests.put(
set_topic_permission_uri,
json=set_topic_permission_data, auth=self.auth)
print('RabbitMQ: set_topic_permissions {}'.format(r.status_code))
# set permissions for the user
def set_permissions(self, site):
username = site.client.username
set_permission_uri = '{}/permissions/{}/{}/'.format(
self.api,
self.vhost,
username,
)
permission = '^(amq\.gen.*|{})'.format(self.exchange)
set_permission_data = {
'configure': permission,
'write': permission,
'read': permission,
}
# TODO might fail
r = requests.put(
set_permission_uri, json=set_permission_data, auth=self.auth)
print('RabbitMQ: set_permissions {}'.format(r.status_code))
# create user at the rabbitmq instance
def create_user(self, site):
username = site.client.username
user_creation_uri = '{}/users/{}/'.format(
self.api,
username
)
print('registerring client {}'.format(site.client.username))
user_creation_data = {
'password': str(site.client.auth_token.key),
'tags': '',
}
# TODO might fail
r = requests.put(
user_creation_uri, json=user_creation_data, auth=self.auth)
print('RabbitMQ: create_user {}'.format(r.status_code))
# delete user at the rabbitmq instance
def delete_user(self, site):
username = site.client.username
user_creation_uri = '{}/users/{}/'.format(
self.api,
username
)
print('registerring client {}'.format(site.client.username))
user_creation_data = {
'password': str(site.client.auth_token.key),
'tags': '',
}
# TODO might fail
r = requests.delete(
user_creation_uri, json=user_creation_data, auth=self.auth)
print('RabbitMQ: create_user {}'.format(r.status_code))
def register_site(self, site):
self.create_user(site)
self.set_permissions(site)
self.set_topic_permissions(site)
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