Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
feudal
feudalBackend
Commits
5abcfb5f
Commit
5abcfb5f
authored
Dec 11, 2017
by
Lukas Burgey
Browse files
Refactor rabbitmq interaction
parent
ef2b577f
Changes
2
Hide whitespace changes
Inline
Side-by-side
django_backend/backend/models.py
View file @
5abcfb5f
...
...
@@ -4,11 +4,11 @@ from django.conf import settings
from
django.dispatch
import
receiver
,
Signal
from
django.utils.timezone
import
make_aware
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
import
requests
from
requests.auth
import
HTTPBasicAuth
from
.rabbitmq
import
RabbitMQInstance
deployment_change
=
Signal
(
providing_args
=
[
'instance'
])
...
...
@@ -95,76 +95,12 @@ class Site(models.Model):
@
receiver
(
post_save
,
sender
=
Site
)
def
register_
client_
at_rabbitmq
(
sender
,
instance
=
None
,
created
=
False
,
**
kwargs
):
def
register_at_rabbitmq
(
sender
,
instance
=
None
,
created
=
False
,
**
kwargs
):
if
not
created
:
return
print
(
'registerring client {}'
.
format
(
instance
.
client
.
username
))
# %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
))
RabbitMQInstance
().
register_site
(
instance
)
class
Service
(
models
.
Model
):
...
...
django_backend/backend/rabbitmq.py
0 → 100644
View file @
5abcfb5f
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment