Projet

Général

Profil

0001-environment-store-local-hobo-info-in-Hobo-model-6057.patch

Emmanuel Cazenave, 15 février 2022 15:44

Télécharger (8,3 ko)

Voir les différences:

Subject: [PATCH] environment: store local hobo info in Hobo model (#60572)

 .../hobo/management/commands/hobo_deploy.py   |  2 +-
 .../environment/migrations/0022_local_hobo.py | 18 ++++++
 .../migrations/0023_populate_local_hobo.py    | 43 ++++++++++++++
 hobo/environment/models.py                    | 16 ++++-
 hobo/environment/utils.py                     | 59 ++++++++++++-------
 tests_multipublik/test_multipublik.py         |  4 +-
 6 files changed, 117 insertions(+), 25 deletions(-)
 create mode 100644 hobo/environment/migrations/0022_local_hobo.py
 create mode 100644 hobo/environment/migrations/0023_populate_local_hobo.py
hobo/agent/hobo/management/commands/hobo_deploy.py
24 24
            if me.get('secondary'):
25 25
                # check we are currently processing the primary hobo, we don't
26 26
                # want to create secondary services on every branches.
27
                if Hobo.objects.filter(secondary=False).count() == 0:
27
                if Hobo.objects.filter(secondary=False, local=False).count() == 0:
28 28
                    return
29 29
                is_primary_hobo = True
30 30
                # on the primary hobo, notify other primary services, this will
hobo/environment/migrations/0022_local_hobo.py
1
# Generated by Django 2.2.24 on 2022-02-15 10:33
2

  
3
from django.db import migrations, models
4

  
5

  
6
class Migration(migrations.Migration):
7

  
8
    dependencies = [
9
        ('environment', '0021_base_url_validators'),
10
    ]
11

  
12
    operations = [
13
        migrations.AddField(
14
            model_name='hobo',
15
            name='local',
16
            field=models.BooleanField(default=False),
17
        ),
18
    ]
hobo/environment/migrations/0023_populate_local_hobo.py
1
# Generated by Django 2.2.24 on 2022-02-15 10:36
2

  
3
from django.db import connection, migrations
4
from django.urls import reverse
5

  
6
from hobo.environment.utils import get_local_key
7

  
8

  
9
def populate_local_hobo(apps, schema_editor):
10
    Hobo = apps.get_model('environment', 'Hobo')
11
    try:
12
        Hobo.objects.get(local=True)
13
        return
14
    except Hobo.DoesNotExist:
15
        pass
16

  
17
    if hasattr(connection, 'get_tenant'):
18
        build_absolute_uri = getattr(connection.get_tenant(), 'build_absolute_uri', None)
19
        if build_absolute_uri:
20
            Hobo.objects.create(
21
                secret_key=get_local_key(build_absolute_uri('/')),
22
                title='Hobo',
23
                slug='hobo',
24
                base_url=build_absolute_uri(reverse('home')),
25
                secondary=False,
26
                local=True,
27
            )
28

  
29

  
30
def clean_local_hobo(apps, schema_editor):
31
    Hobo = apps.get_model('environment', 'Hobo')
32
    Hobo.objects.filter(local=True).delete()
33

  
34

  
35
class Migration(migrations.Migration):
36

  
37
    dependencies = [
38
        ('environment', '0022_local_hobo'),
39
    ]
40

  
41
    operations = [
42
        migrations.RunPython(populate_local_hobo, clean_local_hobo),
43
    ]
hobo/environment/models.py
459 459
        service_label = _('Deployment Server')
460 460
        service_default_slug = 'hobo'
461 461

  
462
    local = models.BooleanField(default=False)
463

  
462 464
    def get_admin_zones(self):
463 465
        return [Zone(self.title, 'hobo', self.get_base_url_path())]
464 466

  
......
466 468
        return self.get_base_url_path() + 'accounts/mellon/metadata/'
467 469

  
468 470
    def get_backoffice_menu_url(self):
469
        return None
471
        return self.get_base_url_path() + 'menu.json'
472

  
473
    def as_dict(self):
474
        res = super().as_dict()
475
        del res['local']
476
        if self.local:
477
            del res['id']
478
            del res['secret_key']
479
            del res['secondary']
480
            del res['service-label']
481
            del res['template_name']
482
            del res['variables']
483
        return res
470 484

  
471 485

  
472 486
class BiJoe(ServiceBase):
hobo/environment/utils.py
28 28

  
29 29

  
30 30
def get_installed_services():
31
    from .models import AVAILABLE_SERVICES
31
    from .models import AVAILABLE_SERVICES, Hobo
32 32

  
33 33
    installed_services = []
34 34
    for available_service in AVAILABLE_SERVICES:
35
        installed_services.extend(available_service.objects.all())
35
        if available_service is Hobo:
36
            installed_services.extend(available_service.objects.filter(local=False))
37
        else:
38
            installed_services.extend(available_service.objects.all())
36 39
    return installed_services
37 40

  
38 41

  
......
46 49
    return KnownServices.shared_secret(secret1, secret2)[:40]
47 50

  
48 51

  
52
def get_or_create_local_hobo():
53
    from .models import Hobo
54

  
55
    try:
56
        hobo = Hobo.objects.get(local=True)
57
    except Hobo.DoesNotExist:
58
        build_absolute_uri = None
59
        if hasattr(connection, 'get_tenant') and hasattr(connection.get_tenant(), 'build_absolute_uri'):
60
            build_absolute_uri = connection.get_tenant().build_absolute_uri
61
        else:
62
            request = StoreRequestMiddleware.get_request()
63
            if request:
64
                build_absolute_uri = request.build_absolute_uri
65
        if not build_absolute_uri:
66
            return None
67

  
68
        hobo = Hobo.objects.create(
69
            secret_key=get_local_key(build_absolute_uri('/')),
70
            title='Hobo',
71
            slug='hobo',
72
            base_url=build_absolute_uri(reverse('home')),
73
            secondary=False,
74
            local=True,
75
        )
76

  
77
    return hobo
78

  
79

  
49 80
def get_local_hobo_dict():
50
    build_absolute_uri = None
51
    if hasattr(connection, 'get_tenant') and hasattr(connection.get_tenant(), 'build_absolute_uri'):
52
        build_absolute_uri = connection.get_tenant().build_absolute_uri
53
    else:
54
        request = StoreRequestMiddleware.get_request()
55
        if request:
56
            build_absolute_uri = request.build_absolute_uri
57
    if not build_absolute_uri:
58
        return None
59
    # if there's a known base url hobo can advertise itself.
60
    return {
61
        'secret_key': get_local_key(build_absolute_uri('/')),
62
        'service-id': 'hobo',
63
        'title': 'Hobo',
64
        'slug': 'hobo',
65
        'base_url': build_absolute_uri(reverse('home')),
66
        'saml-sp-metadata-url': build_absolute_uri(reverse('mellon_metadata')),
67
        'backoffice-menu-url': build_absolute_uri(reverse('menu_json')),
68
        'provisionning-url': build_absolute_uri('/__provision__/'),
69
    }
81
    hobo = get_or_create_local_hobo()
82
    if hobo:
83
        return hobo.as_dict()
84
    return None
70 85

  
71 86

  
72 87
def get_installed_services_dict():
tests_multipublik/test_multipublik.py
88 88
    # (no service creation recursion)
89 89
    HoboDeployCommand().handle(hobo2.base_url, get_hobo_json_filename(hobo1))
90 90
    with tenant_context(hobo2):
91
        assert Hobo.objects.filter().count() == 2
91
        # +1 on hobo since we last checked because the local hobo
92
        # is not stored in DB until the first hobo_json is generated
93
        assert Hobo.objects.filter().count() == 3
92 94
        assert Hobo.objects.filter(secondary=True).count() == 2
93 95
        assert Combo.objects.filter().count() == 2
94 96
        assert Combo.objects.filter(secondary=True).count() == 1
95
-