74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
|
import socket
|
||
|
|
||
|
from flask import Flask
|
||
|
from flask_kerberos import init_kerberos
|
||
|
from zope import component
|
||
|
|
||
|
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)
|
||
|
|
||
|
if app.config.get('TESTING') or app.config.get('ENV') == 'development':
|
||
|
# TODO: create test config class
|
||
|
cfg = Config()
|
||
|
else:
|
||
|
cfg = Config()
|
||
|
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)
|
||
|
|
||
|
# Only instantiate FileService is this host has NFS no_root_squash
|
||
|
if hostname == cfg.get('fs_root_host'):
|
||
|
file_srv = FileService()
|
||
|
component.provideUtility(file_srv, IFileService)
|
||
|
|
||
|
# Only offer mailman API if this host is running Mailman
|
||
|
if hostname == cfg.get('mailman_host'):
|
||
|
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)
|
||
|
|
||
|
@app.route('/ping')
|
||
|
def ping():
|
||
|
"""Health check"""
|
||
|
return 'pong\n'
|
||
|
|
||
|
return app
|