0001-add-new-agent-task-to-provision-objects-to-tenants-f.patch
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 |
cmd = settings.service_manage_cmd + ' notify -' |
|
61 |
try: |
|
62 |
cmd_process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, |
|
63 |
stdout=subprocess.PIPE) |
|
64 |
except OSError: |
|
65 |
return |
|
66 |
cmd_process.communicate(input=json.dumps(data)) |
|
67 |
if cmd_process.returncode != 0: |
|
68 |
raise RuntimeError('command "%s" failed' % what) |
|
69 | ||
58 | 70 | |
59 | 71 |
class Passerelle(BaseService): |
60 | 72 |
service_id = 'passerelle' |
... | ... | |
113 | 125 |
logger.debug('skipping uptodate site: %r', service_obj) |
114 | 126 |
continue |
115 | 127 |
service_obj.execute(environment) |
128 | ||
129 |
def notify(data): |
|
130 |
for klassname, service in globals().items(): |
|
131 |
if not hasattr(service, 'notify'): |
|
132 |
continue |
|
133 |
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 |
- |