rabbitmq.py 3.93 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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')

17
18
19
    def get_uri(self, path):
        return '{}/{}'.format(self.api, path)

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    def rest_get(self, api_path):
        r = requests.get(
                self.get_uri(api_path),
                auth=self.auth)
        r.raise_for_status()
        return r.json()

    # send a rest call with path and data to the rest interface of
    # the rabbitmq instance
    def rest_put(self, api_path, data):
        r = requests.put(
                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)
        r.raise_for_status()
        return r

Lukas Burgey's avatar
Lukas Burgey committed
44
45
    def set_topic_permissions(self, site):
        username = site.client.username
46
        path = 'topic-permissions/{}/{}/'.format(
Lukas Burgey's avatar
Lukas Burgey committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
                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),
                }

69
        return self.rest_put(path, set_topic_permission_data)
Lukas Burgey's avatar
Lukas Burgey committed
70
71
72
73

    # set permissions for the user
    def set_permissions(self, site):
        username = site.client.username
74
        path = 'permissions/{}/{}/'.format(
Lukas Burgey's avatar
Lukas Burgey committed
75
76
77
78
79
80
81
82
83
84
                self.vhost,
                username,
                )
        permission = '^(amq\.gen.*|{})'.format(self.exchange)
        set_permission_data = {
                'configure': permission,
                'write': permission,
                'read': permission,
                }

85
        return self.rest_put(path, set_permission_data)
Lukas Burgey's avatar
Lukas Burgey committed
86
87
88
89

    # create user at the rabbitmq instance
    def create_user(self, site):
        username = site.client.username
90
        path = 'users/{}/'.format(username)
Lukas Burgey's avatar
Lukas Burgey committed
91
92
93
94
95

        user_creation_data = {
                'password': str(site.client.auth_token.key),
                'tags': '',
                }
96

97
        return self.rest_put(path, user_creation_data)
Lukas Burgey's avatar
Lukas Burgey committed
98
99
100
101

    # delete user at the rabbitmq instance
    def delete_user(self, site):
        username = site.client.username
102
        path = 'users/{}/'.format(username)
Lukas Burgey's avatar
Lukas Burgey committed
103

104
        return self.rest_del(path)
105
106

    # PUBLIC API
Lukas Burgey's avatar
Lukas Burgey committed
107
108

    def register_site(self, site):
109
110
        print('RabbitMQ: register: {} / {}'.format(
            site.name, site.client.username))
Lukas Burgey's avatar
Lukas Burgey committed
111
112
113
        self.create_user(site)
        self.set_permissions(site)
        self.set_topic_permissions(site)
114

115
116
117
118
119
    def update_site(self, site):
        print('RabbitMQ: update: {} / {}'.format(
            site.name, site.client.username))
        self.set_topic_permissions(site)

120
    def deregister_site(self, site):
121
122
        print('RabbitMQ: deregister: {} / {}'.format(
            site.name, site.client.username))
123
        self.delete_user(site)
124
125
126

    def is_client_connected(self, site):
        connections = self.rest_get("connections/")
Lukas Burgey's avatar
Lukas Burgey committed
127
128
129
130
        clients_for_site = [c
                            for c in connections
                            if c['user'] == site.client.username]
        return len(clients_for_site) > 0