Projet

Général

Profil

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

Benjamin Dauvergne, 09 septembre 2015 14:52

Télécharger (5,77 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 | 39 +++++++++++++++++++++++++++++++++++++++
 hobo/agent/common/__init__.py | 25 +++++++++++++++++++++++++
 hobo/agent/worker/celery.py   |  7 +++++++
 hobo/agent/worker/services.py | 21 +++++++++++++++++++++
 hobo/agent/worker/settings.py |  2 ++
 5 files changed, 94 insertions(+)
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, get_role_parenting_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(sender, instance, **kwargs):
18
    notify_agents({
19
        '@type': 'provision',
20
        'audience': get_audience(),
21
        'full': True,
22
        'objects': [
23
            {
24
                '@type': 'role',
25
                'uuid': role.uuid,
26
                'name': role.name,
27
                'slug': role.slug,
28
                'description': role.description,
29
            } for role in sender.objects.all()
30
        ]
31
    })
32

  
2 33

  
3 34
class Authentic2AgentConfig(AppConfig):
4 35
    name = 'hobo.agent.authentic2'
5 36
    label = 'authentic2_agent'
6 37
    verbose_name = 'Authentic2 Agent'
38

  
39
    def ready(self):
40
        Role = get_role_model()
41
        RoleParenting = get_role_parenting_model()
42
        post_save.connect(notify_roles, Role)
43
        post_delete.connect(notify_roles, Role)
44
        post_save.connect(notify_roles, RoleParenting)
45
        post_delete.connect(notify_roles, RoleParenting)
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
13 13
    CELERY_QUEUES=(Broadcast('broadcast_tasks'), )
14 14
)
15 15

  
16

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

  
21

  
22
@app.task(name='hobo-notify', bind=True, acks_late=True)
23
def hobo_notify(self, data):
24
    # do something with data
25
    services.notify(data)
hobo/agent/worker/services.py
55 55
                shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
56 56
        stdout = cmd_process.communicate(input=json.dumps(environment))
57 57

  
58
    @classmethod
59
    def notify(cls, data):
60
        pass
61

  
58 62

  
59 63
class Passerelle(BaseService):
60 64
    service_id = 'passerelle'
......
65 69
    service_id = 'wcs'
66 70
    service_manage_cmd = settings.WCS_MANAGE_COMMAND
67 71

  
72
    @classmethod
73
    def notify(cls, data):
74
        if not os.path.exists(settings.WCS_MANAGE_COMMAND):
75
            continue
76
        what = settings.WCS_MANAGE_COMMAND + ' notify -'
77
        cmd_process = subprocess.Popen(what, shell=True, stdin=subprocess.PIPE,
78
                                       stdout=subprocess.PIPE)
79
        cmd_process.communicate(input=json.dumps(data))
80
        if cmd_process.returncode != 0:
81
            raise RuntimeError('command "%s" failed' % what)
82

  
68 83

  
69 84
class Authentic(BaseService):
70 85
    service_id = 'authentic'
......
113 128
            logger.debug('skipping uptodate site: %r', service_obj)
114 129
            continue
115 130
        service_obj.execute(environment)
131

  
132
def notify(data):
133
    for klassname, service in globals().items():
134
        if not hasattr(service, 'notify'):
135
            continue
136
        service.notify(data)
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
-