Projet

Général

Profil

0001-add-new-agent-task-to-provision-objects-to-tenants-f.patch

Benjamin Dauvergne, 09 septembre 2015 09:29

Télécharger (5,48 ko)

Voir les différences:

Subject: [PATCH] add new agent task to provision objects to tenants (fixes
 #8217)

First use is to connect it to post_save, post_delete signal on Role
model of authentic, to propagate roles to tenants.
 hobo/agent/authentic2/apps.py | 51 +++++++++++++++++++++++++++++++++++++++++++
 hobo/agent/common/__init__.py | 25 +++++++++++++++++++++
 hobo/agent/worker/celery.py   |  8 +++++++
 hobo/agent/worker/notify.py   | 17 +++++++++++++++
 hobo/agent/worker/settings.py |  2 ++
 5 files changed, 103 insertions(+)
 create mode 100644 hobo/agent/worker/notify.py
hobo/agent/authentic2/apps.py
1 1
from django.apps import AppConfig
2
from django.db.models import post_save, post_delete
3

  
4
from django_rbac.utils import get_role_model
5

  
6
from hobo.agent.common import notify_agents
7
from authentic2.utils import to_list
8
from authentic2.saml.models import LibertyProvider
9

  
10

  
11
@to_list
12
def get_audience():
13
    for provider in LibertyProvider.objects.all():
14
        yield provider.entity_id
15

  
16

  
17
def notify_roles_post_save(sender, instance, created, **kwargs):
18
    notify_agents({
19
        '@type': 'provision',
20
        'audience': get_audience(),
21
        'objects': [
22
            {
23
                '@type': 'role',
24
                'uuid': instance.uuid,
25
                'name': instance.name,
26
                'slug': instance.slug,
27
                'description': instance.description,
28
            }
29
        ]
30
    })
31

  
32

  
33
def notify_roles_post_delete(sender, instance, **kwargs):
34
    notify_agents({
35
        '@type': 'deprovision',
36
        'audience': get_audience(),
37
        'objects': [
38
            {
39
                '@type': 'role',
40
                'uuid': instance.uuid,
41
                'name': instance.name,
42
                'slug': instance.slug,
43
                'description': instance.description,
44
            }
45
        ]
46
    })
47

  
2 48

  
3 49
class Authentic2AgentConfig(AppConfig):
4 50
    name = 'hobo.agent.authentic2'
5 51
    label = 'authentic2_agent'
6 52
    verbose_name = 'Authentic2 Agent'
53

  
54
    def ready(self):
55
        Role = get_role_model()
56
        post_save.connect(notify_roles_post_save, Role)
57
        post_delete.connect(notify_roles_post_delete, Role)
hobo/agent/common/__init__.py
1
from celery import Celery
2
from kombu.common import Broadcast
3

  
4
from django.conf import settings
5
from django.db import connection
6

  
7

  
8
def notify_agents(data):
9
    '''Send notifications to all other tenants'''
10
    notification = {
11
        'tenant': connection.get_tenant().domain_url,
12
        'data': data,
13
    }
14
    with Celery('hobo', broker=settings.BROKER_URL) as app:
15
        app.conf.update(
16
            CELERY_TASK_SERIALIZER='json',
17
            CELERY_ACCEPT_CONTENT=['json'],
18
            CELERY_RESULT_SERIALIZER='json',
19
            CELERY_QUEUES=(Broadcast('broadcast_tasks'), )
20
        )
21
        # see called method in hobo.agent.worker.celery
22
        app.send_task('hobo-notify',
23
                      (notification,),
24
                      expires=settings.BROKER_TASK_EXPIRES,
25
                      queue='broadcast_tasks')
hobo/agent/worker/celery.py
4 4
from kombu.common import Broadcast
5 5
from . import settings
6 6
from . import services
7
from . import notify
7 8

  
8 9
app = Celery('hobo', broker=settings.BROKER_URL)
9 10
app.conf.update(
......
13 14
    CELERY_QUEUES=(Broadcast('broadcast_tasks'), )
14 15
)
15 16

  
17

  
16 18
@app.task(name='hobo-deploy', bind=True)
17 19
def deploy(self, environment):
18 20
    services.deploy(environment)
21

  
22

  
23
@app.task(name='hobo-notify', bind=True, acks_late=True)
24
def hobo_notify(self, data):
25
    # do something with data
26
    notify.notify(data)
hobo/agent/worker/notify.py
1
import os.path
2
import subprocess
3
import json
4

  
5
from . import settings
6

  
7

  
8
def notify(data):
9
    for command in settings.NOTIFY_COMMANDS:
10
        if not os.path.exists(command):
11
            continue
12
        what = command + ' notify -'
13
        cmd_process = subprocess.Popen(what, shell=True, stdin=subprocess.PIPE,
14
                                       stdout=subprocess.PIPE)
15
        cmd_process.communicate(input=json.dumps(data))
16
        if cmd_process.returncode != 0:
17
            raise RuntimeError('command "%s" failed' % what)
hobo/agent/worker/settings.py
24 24
FARGO_MANAGE_COMMAND = '/usr/bin/fargo-manage'
25 25
WELCO_MANAGE_COMMAND = '/usr/bin/welco-manage'
26 26

  
27
NOTIFY_COMMANDS = ['/usr/sbin/wcsctl']
28

  
27 29
local_settings_file = os.environ.get('HOBO_AGENT_SETTINGS_FILE',
28 30
    os.path.join(os.path.dirname(__file__), 'local_settings.py'))
29 31
if os.path.exists(local_settings_file):
30
-