181 lines
5.7 KiB
Python
181 lines
5.7 KiB
Python
# hobo - portal to configure and deploy applications
|
|
# Copyright (C) 2015-2019 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import hashlib
|
|
|
|
from django.conf import settings
|
|
from django.urls import reverse
|
|
from django.db import connection, transaction
|
|
from django.utils.six.moves.urllib.parse import urlparse
|
|
from django.utils.encoding import force_text
|
|
|
|
from hobo.middleware.utils import StoreRequestMiddleware
|
|
from hobo.multitenant.settings_loaders import KnownServices
|
|
from hobo.profile.utils import get_profile_dict
|
|
|
|
|
|
def get_installed_services():
|
|
from .models import AVAILABLE_SERVICES
|
|
installed_services = []
|
|
for available_service in AVAILABLE_SERVICES:
|
|
installed_services.extend(available_service.objects.all())
|
|
return installed_services
|
|
|
|
|
|
def get_operational_services():
|
|
return [x for x in get_installed_services() if x.is_operational()]
|
|
|
|
|
|
def get_local_key(url):
|
|
secret1 = force_text(settings.SECRET_KEY)
|
|
secret2 = url
|
|
return KnownServices.shared_secret(secret1, secret2)[:40]
|
|
|
|
|
|
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__/'),
|
|
}
|
|
|
|
|
|
def get_installed_services_dict():
|
|
from .models import Variable
|
|
hobo_service = []
|
|
hobo_dict = get_local_hobo_dict()
|
|
if hobo_dict:
|
|
hobo_service.append(hobo_dict)
|
|
return {
|
|
'services': hobo_service + [x.as_dict() for x in get_installed_services()],
|
|
'variables': {v.name: v.json for v in Variable.objects.filter(service_pk__isnull=True)}
|
|
}
|
|
|
|
|
|
class Zone:
|
|
title = None
|
|
zone_icon_id = None
|
|
href = None
|
|
|
|
def __init__(self, title, zone_icon_id, href):
|
|
self.title = title
|
|
self.zone_icon_id = zone_icon_id
|
|
self.href = href
|
|
|
|
|
|
def get_variable(name):
|
|
from .models import Variable
|
|
try:
|
|
variable = Variable.objects.get(name=name)
|
|
except Variable.DoesNotExist:
|
|
variable = Variable(name=name, auto=True)
|
|
return variable
|
|
|
|
|
|
def set_variable(name, value):
|
|
from .models import Variable
|
|
variable, created = Variable.objects.get_or_create(
|
|
name=name, defaults={'auto': True})
|
|
variable.value = value
|
|
variable.save()
|
|
|
|
|
|
def create_base_url(hostname, service):
|
|
"""
|
|
Distinguish mutualised domains (matching a "-" in the first part of the netloc)
|
|
from the normal scenario with a dedicated parent domain.
|
|
"""
|
|
ph = urlparse(hostname)
|
|
parts = ph.netloc.split('.')
|
|
if '-' in parts[0]:
|
|
netloc = '%s-%s.%s' % (service, parts[0].split('-')[1], '.'.join(parts[1:]))
|
|
else:
|
|
netloc = '%s.%s' % (service, '.'.join(parts[1:]))
|
|
return '%s://%s' % (ph.scheme, netloc)
|
|
|
|
|
|
def get_setting_variable(setting_name, label=None, service=None):
|
|
from .models import Variable, ContentType
|
|
|
|
kwargs = {
|
|
'name': 'SETTING_' + setting_name,
|
|
}
|
|
if service:
|
|
kwargs['service_type'] = ContentType.objects.get_for_model(service)
|
|
kwargs['service_pk'] = service.pk
|
|
else:
|
|
kwargs['service_type__isnull'] = True
|
|
kwargs['service_pk__isnull'] = True
|
|
|
|
try:
|
|
variable = Variable.objects.get(**kwargs)
|
|
except Variable.DoesNotExist:
|
|
variable = Variable(
|
|
name='SETTING_' + setting_name,
|
|
label=label or '',
|
|
service=service,
|
|
auto=True)
|
|
|
|
return variable
|
|
|
|
|
|
def export_parameters():
|
|
from .models import Variable
|
|
|
|
variables = []
|
|
for var in Variable.objects.filter(service_pk__isnull=True):
|
|
variables.append({
|
|
'name': var.name,
|
|
'label': var.label,
|
|
'value': var.value,
|
|
'auto': var.auto})
|
|
parameters = {'variables': variables}
|
|
parameters.update(get_profile_dict())
|
|
return parameters
|
|
|
|
|
|
def import_parameters(parameters):
|
|
from .models import Variable
|
|
from hobo.profile.models import AttributeDefinition
|
|
|
|
with transaction.atomic():
|
|
for variables in parameters.get('variables', []):
|
|
obj, created = Variable.objects.get_or_create(name=variables['name'])
|
|
for key, value in variables.items():
|
|
setattr(obj, key, value)
|
|
obj.save()
|
|
|
|
for fields in parameters.get('profile', {}).get('fields', []):
|
|
obj, created = AttributeDefinition.objects.get_or_create(name=fields['name'])
|
|
for key, value in fields.items():
|
|
setattr(obj, key, value)
|
|
obj.save()
|