Updated deploy mechanism

This commit is contained in:
Spitfireap
2024-06-26 17:53:13 +02:00
parent 610fe636c0
commit 18f7d22828
3 changed files with 62 additions and 39 deletions
-39
View File
@@ -8,14 +8,12 @@ import shutil
import string
import subprocess
import sys
import uuid
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
import dj_database_url
import django
from django.conf import settings
from django.core import management
from django.template import Context, Template
from django.utils.encoding import smart_str
@@ -304,40 +302,3 @@ class DeployCommand(Command):
if parsed_args.collectstatic:
self._exec_django_command("collectstatic", parsed_args.name, "--noinput")
self._exec_django_command("set_default_site", parsed_args.name, allowed_host)
base_frontend_dir = os.path.join(
os.path.dirname(__file__), "../../frontend_dist/"
)
if not os.path.exists(base_frontend_dir):
return
frontend_target_dir = "{}/frontend".format(parsed_args.name)
shutil.copytree(base_frontend_dir, frontend_target_dir)
frontend_path = getattr(settings, "NEW_ADMIN_URL", "new-admin")
redirect_uri = f"https://{allowed_host}/{frontend_path}/login/logged"
client_id = str(uuid.uuid4())
self._exec_django_command(
"createapplication",
parsed_args.name,
"--name",
"Modoboa frontend",
"--client-id",
client_id,
"--skip-authorization",
"--algorithm",
"RS256",
"--redirect-uris",
redirect_uri,
"public",
"authorization-code",
)
with open("{}/config.json".format(frontend_target_dir), "w") as fp:
fp.write(
f"""{{
"API_BASE_URL": "https://{allowed_host}/api/v2",
"OAUTH_AUTHORITY_URL": "https://{allowed_host}/api/o",
"OAUTH_CLIENT_ID": "{client_id}",
"OAUTH_REDIRECT_URI": "{redirect_uri}"
}}
"""
)
@@ -64,6 +64,7 @@ INSTALLED_APPS = (
'ckeditor',
'ckeditor_uploader',
'oauth2_provider',
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'drf_spectacular',
@@ -109,6 +110,7 @@ MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'x_forwarded_for.middleware.XForwardedForMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
@@ -214,6 +216,9 @@ OAUTH2_PROVIDER = {
'DEFAULT_SCOPES': ['openid', 'read', 'write'],
}
# If CORS fail, you might want to try to set it to True
#CORS_ORIGIN_ALLOW_ALL = False
# Rest framework settings
REST_FRAMEWORK = {
@@ -7,7 +7,11 @@ A management command to load Modoboa initial data.
"""
from functools import reduce
import os
import shutil
import uuid
from django.conf import settings
from django.contrib.auth.models import Group
from django.core.management import call_command
from django.core.management.base import BaseCommand
@@ -25,6 +29,9 @@ class Command(BaseCommand):
def add_arguments(self, parser):
"""Add extra arguments to command."""
parser.add_argument(
"--name", help="The name of your Modoboa instance", default="instance"
)
parser.add_argument(
"--admin-username",
default="admin",
@@ -83,6 +90,56 @@ class Command(BaseCommand):
else:
signals.initial_data_loaded.send(sender=self.__class__, extname=extname)
base_frontend_dir = os.path.join(
os.path.dirname(__file__), "../../frontend_dist/"
)
if os.path.exists(base_frontend_dir):
frontend_target_dir = "{}/frontend".format(options["name"])
shutil.copytree(base_frontend_dir, frontend_target_dir)
app_model = get_application_model()
allowed_host = settings.get("ALLOWED_HOSTS")
if allowed_host is not None:
allowed_host = allowed_host[0]
else:
allowed_host = input("What will be the hostname used to access Modoboa? ")
if not allowed_host:
allowed_host = "localhost"
frontend_path = settings.get("NEW_ADMIN_URL", "new-admin")
redirect_uri = f"https://{allowed_host}/{frontend_path}/login/logged"
client_id = str(uuid.uuid4())
frontend_application = app_model.objects.filter(name="frontend")
if not frontend_application.exists():
self._exec_django_command(
"createapplication",
"frontend",
"--name",
"Modoboa frontend",
"--client-id",
client_id,
"--skip-authorization",
"--algorithm",
"RS256",
"--redirect-uris",
redirect_uri,
"public",
"authorization-code",
)
else:
client_id = frontend_application.first().client_id
with open("{}/config.json".format(frontend_target_dir), "w") as fp:
fp.write(
f"""{{
"API_BASE_URL": "https://{allowed_host}/api/v2",
"OAUTH_AUTHORITY_URL": "https://{allowed_host}/api/o",
"OAUTH_CLIENT_ID": "{client_id}",
"OAUTH_REDIRECT_URI": "{redirect_uri}"
}}
"""
)
if options["extra_fixtures"]:
from modoboa.admin import factories