canaille-globuzma/canaille/app/commands.py

67 lines
1.5 KiB
Python
Raw Normal View History

import functools
2021-10-13 08:12:44 +00:00
import sys
2021-10-13 07:38:44 +00:00
2021-12-20 22:57:27 +00:00
import click
2021-10-13 07:38:44 +00:00
from flask import current_app
2021-12-20 22:57:27 +00:00
from flask.cli import with_appcontext
from canaille.backends import Backend
def with_backendcontext(func):
@functools.wraps(func)
def _func(*args, **kwargs):
if not current_app.config["TESTING"]: # pragma: no cover
with Backend.instance.session():
result = func(*args, **kwargs)
2023-04-08 18:42:38 +00:00
else:
result = func(*args, **kwargs)
return result
return _func
@click.command()
2021-10-13 08:12:44 +00:00
@with_appcontext
@with_backendcontext
2021-10-13 08:12:44 +00:00
def check():
2024-05-12 09:33:22 +00:00
"""Test the configuration file.
Attempt to reach the database and the SMTP server with the provided
credentials.
"""
from canaille.app.configuration import ConfigurationException
from canaille.app.configuration import validate
2021-10-29 12:20:06 +00:00
2021-10-13 08:12:44 +00:00
try:
validate(current_app.config, validate_remote=True)
except ConfigurationException as exc:
print(exc)
sys.exit(1)
2021-11-08 17:09:05 +00:00
@click.command()
2021-11-08 17:09:05 +00:00
@with_appcontext
def install():
2024-05-12 09:33:22 +00:00
"""Installs canaille elements from the configuration.
For instance, depending on the configuration, this can generate OIDC
keys or install LDAP schemas.
"""
2023-04-09 13:52:55 +00:00
from canaille.app.configuration import ConfigurationException
from canaille.app.installation import install
2021-11-08 17:09:05 +00:00
try:
install(current_app.config)
2022-12-10 00:11:33 +00:00
except ConfigurationException as exc: # pragma: no cover
2021-11-08 17:09:05 +00:00
print(exc)
sys.exit(1)
def register(cli):
cli.add_command(check)
cli.add_command(install)