pyceo/ceod/api/app_factory.py

85 lines
2.8 KiB
Python
Raw Normal View History

2021-08-03 19:19:33 -04:00
import importlib.resources
import os
2021-07-24 17:09:10 -04:00
import socket
from flask import Flask
from flask_kerberos import init_kerberos
from zope import component
2021-08-03 19:19:33 -04:00
from .error_handlers import register_error_handlers
2021-07-24 17:09:10 -04:00
from ceo_common.interfaces import IConfig, IKerberosService, ILDAPService, IFileService, \
IMailmanService, IMailService, IUWLDAPService, IHTTPClient
from ceo_common.model import Config, HTTPClient, RemoteMailmanService
from ceod.model import KerberosService, LDAPService, FileService, \
MailmanService, MailService, UWLDAPService
def create_app(flask_config={}):
app = Flask(__name__)
app.config.from_mapping(flask_config)
2021-08-03 19:19:33 -04:00
if app.config.get('ENV') == 'development' and 'CEOD_CONFIG' not in os.environ:
2021-08-04 13:15:06 -04:00
with importlib.resources.path('tests_common', 'ceod_dev.ini') as p:
2021-08-03 19:19:33 -04:00
config_file = p.__fspath__()
2021-07-24 17:09:10 -04:00
else:
2021-08-03 19:19:33 -04:00
config_file = None
cfg = Config(config_file)
2021-07-24 17:09:10 -04:00
component.provideUtility(cfg, IConfig)
init_kerberos(app, service='ceod')
hostname = socket.gethostname()
# Only ceod_admin_host has the ceod/admin key in its keytab
if hostname == cfg.get('ceod_admin_host'):
krb_srv = KerberosService(cfg.get('ldap_admin_principal'))
from ceod.api import members
app.register_blueprint(members.bp, url_prefix='/api/members')
else:
fqdn = socket.getfqdn()
krb_srv = KerberosService(f'ceod/{fqdn}')
component.provideUtility(krb_srv, IKerberosService)
# Any host can use LDAPService, but only ceod_admin_host can write
ldap_srv = LDAPService()
component.provideUtility(ldap_srv, ILDAPService)
http_client = HTTPClient()
component.provideUtility(http_client, IHTTPClient)
2021-08-03 19:19:33 -04:00
# Only instantiate FileService if this host has NFS no_root_squash
# If admin_host and fs_root_host become separate, we will need
# to create a RemoteFileService
if hostname == cfg.get('ceod_fs_root_host'):
2021-07-24 17:09:10 -04:00
file_srv = FileService()
component.provideUtility(file_srv, IFileService)
# Only offer mailman API if this host is running Mailman
2021-08-03 19:19:33 -04:00
if hostname == cfg.get('ceod_mailman_host'):
2021-07-24 17:09:10 -04:00
mailman_srv = MailmanService()
component.provideUtility(mailman_srv, IMailmanService)
from ceod.api import mailman
app.register_blueprint(mailman.bp, url_prefix='/api/mailman')
else:
mailman_srv = RemoteMailmanService()
component.provideUtility(mailman_srv, IMailmanService)
mail_srv = MailService()
component.provideUtility(mail_srv, IMailService)
uwldap_srv = UWLDAPService()
component.provideUtility(uwldap_srv, IUWLDAPService)
2021-08-02 23:20:11 -04:00
from ceod.api import uwldap
app.register_blueprint(uwldap.bp, url_prefix='/api/uwldap')
2021-08-03 19:19:33 -04:00
register_error_handlers(app)
2021-07-24 17:09:10 -04:00
@app.route('/ping')
def ping():
"""Health check"""
return 'pong\n'
return app