environment: store local hobo info in Hobo model (#60572)

This commit is contained in:
Emmanuel Cazenave 2022-01-13 17:56:23 +01:00
parent 206193e357
commit 00f897753b
6 changed files with 120 additions and 25 deletions

View File

@ -24,7 +24,7 @@ class Command(hobo_deploy.Command):
if me.get('secondary'):
# check we are currently processing the primary hobo, we don't
# want to create secondary services on every branches.
if Hobo.objects.filter(secondary=False).count() == 0:
if Hobo.objects.filter(secondary=False, local=False).count() == 0:
return
is_primary_hobo = True
# on the primary hobo, notify other primary services, this will

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2.24 on 2022-02-15 10:33
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('environment', '0021_base_url_validators'),
]
operations = [
migrations.AddField(
model_name='hobo',
name='local',
field=models.BooleanField(default=False),
),
]

View File

@ -0,0 +1,43 @@
# Generated by Django 2.2.24 on 2022-02-15 10:36
from django.db import connection, migrations
from django.urls import reverse
from hobo.environment.utils import get_local_key
def populate_local_hobo(apps, schema_editor):
Hobo = apps.get_model('environment', 'Hobo')
try:
Hobo.objects.get(local=True)
return
except Hobo.DoesNotExist:
pass
if hasattr(connection, 'get_tenant'):
build_absolute_uri = getattr(connection.get_tenant(), 'build_absolute_uri', None)
if build_absolute_uri:
Hobo.objects.create(
secret_key=get_local_key(build_absolute_uri('/')),
title='Hobo',
slug='hobo',
base_url=build_absolute_uri(reverse('home')),
secondary=False,
local=True,
)
def clean_local_hobo(apps, schema_editor):
Hobo = apps.get_model('environment', 'Hobo')
Hobo.objects.filter(local=True).delete()
class Migration(migrations.Migration):
dependencies = [
('environment', '0022_local_hobo'),
]
operations = [
migrations.RunPython(populate_local_hobo, clean_local_hobo),
]

View File

@ -459,6 +459,11 @@ class Hobo(ServiceBase):
service_label = _('Deployment Server')
service_default_slug = 'hobo'
# historically an hobo instance did not store information about itself,
# it only stored info about other hobo's.
# now we also store info about an instance on the instance itself, via local=True
local = models.BooleanField(default=False)
def get_admin_zones(self):
return [Zone(self.title, 'hobo', self.get_base_url_path())]
@ -466,7 +471,19 @@ class Hobo(ServiceBase):
return self.get_base_url_path() + 'accounts/mellon/metadata/'
def get_backoffice_menu_url(self):
return None
return self.get_base_url_path() + 'menu.json'
def as_dict(self):
res = super().as_dict()
del res['local']
if self.local:
del res['id']
del res['secret_key']
del res['secondary']
del res['service-label']
del res['template_name']
del res['variables']
return res
class BiJoe(ServiceBase):

View File

@ -28,11 +28,14 @@ from hobo.profile.utils import get_profile_dict
def get_installed_services():
from .models import AVAILABLE_SERVICES
from .models import AVAILABLE_SERVICES, Hobo
installed_services = []
for available_service in AVAILABLE_SERVICES:
installed_services.extend(available_service.objects.all())
if available_service is Hobo:
installed_services.extend(available_service.objects.filter(local=False))
else:
installed_services.extend(available_service.objects.all())
return installed_services
@ -46,27 +49,39 @@ def get_local_key(url):
return KnownServices.shared_secret(secret1, secret2)[:40]
def get_or_create_local_hobo():
from .models import Hobo
try:
hobo = Hobo.objects.get(local=True)
except Hobo.DoesNotExist:
build_absolute_uri = None
if hasattr(connection, 'get_tenant') and hasattr(connection.get_tenant(), 'build_absolute_uri'):
build_absolute_uri = connection.get_tenant().build_absolute_uri
else:
request = StoreRequestMiddleware.get_request()
if request:
build_absolute_uri = request.build_absolute_uri
if not build_absolute_uri:
return None
hobo = Hobo.objects.create(
secret_key=get_local_key(build_absolute_uri('/')),
title='Hobo',
slug='hobo',
base_url=build_absolute_uri(reverse('home')),
secondary=False,
local=True,
)
return hobo
def get_local_hobo_dict():
build_absolute_uri = None
if hasattr(connection, 'get_tenant') and hasattr(connection.get_tenant(), 'build_absolute_uri'):
build_absolute_uri = connection.get_tenant().build_absolute_uri
else:
request = StoreRequestMiddleware.get_request()
if request:
build_absolute_uri = request.build_absolute_uri
if not build_absolute_uri:
return None
# if there's a known base url hobo can advertise itself.
return {
'secret_key': get_local_key(build_absolute_uri('/')),
'service-id': 'hobo',
'title': 'Hobo',
'slug': 'hobo',
'base_url': build_absolute_uri(reverse('home')),
'saml-sp-metadata-url': build_absolute_uri(reverse('mellon_metadata')),
'backoffice-menu-url': build_absolute_uri(reverse('menu_json')),
'provisionning-url': build_absolute_uri('/__provision__/'),
}
hobo = get_or_create_local_hobo()
if hobo:
return hobo.as_dict()
return None
def get_installed_services_dict():

View File

@ -88,7 +88,9 @@ def test_multipublik(tenants, mocker):
# (no service creation recursion)
HoboDeployCommand().handle(hobo2.base_url, get_hobo_json_filename(hobo1))
with tenant_context(hobo2):
assert Hobo.objects.filter().count() == 2
# +1 on hobo since we last checked because the local hobo
# is not stored in DB until the first hobo_json is generated
assert Hobo.objects.filter().count() == 3
assert Hobo.objects.filter(secondary=True).count() == 2
assert Combo.objects.filter().count() == 2
assert Combo.objects.filter(secondary=True).count() == 1