pyceo/ceod/api/app_factory.py

148 lines
4.9 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 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, IDatabaseService, \
ICloudStackService, ICloudResourceManager, IKubernetesService, IVHostManager, \
IContainerRegistryService, IClubWebHostingService
2021-07-24 17:09:10 -04:00
from ceo_common.model import Config, HTTPClient, RemoteMailmanService
2021-08-21 02:27:33 -04:00
from ceod.api.spnego import init_spnego
2021-07-24 17:09:10 -04:00
from ceod.model import KerberosService, LDAPService, FileService, \
MailmanService, MailService, UWLDAPService, CloudStackService, \
CloudResourceManager, KubernetesService, VHostManager, \
ContainerRegistryService, ClubWebHostingService
from ceod.db import MySQLService, PostgreSQLService
2021-07-24 17:09:10 -04:00
def create_app(flask_config={}):
app = Flask(__name__)
app.config.from_mapping(flask_config)
2021-08-13 20:11:56 -04:00
if not app.config.get('TESTING'):
register_services(app)
cfg = component.getUtility(IConfig)
2021-08-21 02:27:33 -04:00
init_spnego('ceod')
2021-08-13 20:11:56 -04:00
hostname = socket.gethostname()
# Only ceod_admin_host should serve the /api/members endpoints because
# it needs to run kadmin
if hostname == cfg.get('ceod_admin_host'):
from ceod.api import members
app.register_blueprint(members.bp, url_prefix='/api/members')
2021-08-18 19:48:17 -04:00
# Only offer mailman API if this host is running Mailman
2021-08-13 20:11:56 -04:00
if hostname == cfg.get('ceod_mailman_host'):
from ceod.api import mailman
app.register_blueprint(mailman.bp, url_prefix='/api/mailman')
if hostname == cfg.get('ceod_database_host'):
from ceod.api import database
app.register_blueprint(database.bp, url_prefix='/api/db')
if hostname == cfg.get('ceod_cloud_host'):
from ceod.api import cloud
app.register_blueprint(cloud.bp, url_prefix='/api/cloud')
if hostname == cfg.get('ceod_webhosting_host'):
from ceod.api import webhosting
app.register_blueprint(webhosting.bp, url_prefix='/api/webhosting')
2021-08-18 19:48:17 -04:00
from ceod.api import groups
app.register_blueprint(groups.bp, url_prefix='/api/groups')
from ceod.api import positions
app.register_blueprint(positions.bp, url_prefix='/api/positions')
2021-08-13 20:11:56 -04:00
from ceod.api import uwldap
app.register_blueprint(uwldap.bp, url_prefix='/api/uwldap')
register_error_handlers(app)
@app.route('/ping')
def ping():
"""Health check"""
return 'pong\n'
return app
def register_services(app):
# Config
2021-08-03 19:19:33 -04:00
if app.config.get('ENV') == 'development' and 'CEOD_CONFIG' not in os.environ:
2021-08-13 20:11:56 -04:00
with importlib.resources.path('tests', '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)
hostname = socket.gethostname()
# KerberosService
krb_srv = KerberosService()
2021-07-24 17:09:10 -04:00
component.provideUtility(krb_srv, IKerberosService)
2021-08-13 20:11:56 -04:00
# LDAPService
2021-07-24 17:09:10 -04:00
ldap_srv = LDAPService()
component.provideUtility(ldap_srv, ILDAPService)
2021-08-13 20:11:56 -04:00
# HTTPService
2021-07-24 17:09:10 -04:00
http_client = HTTPClient()
component.provideUtility(http_client, IHTTPClient)
2021-08-13 20:11:56 -04:00
# FileService
2021-08-03 19:19:33 -04:00
if hostname == cfg.get('ceod_fs_root_host'):
2021-07-24 17:09:10 -04:00
file_srv = FileService()
component.provideUtility(file_srv, IFileService)
2021-08-13 20:11:56 -04:00
# MailmanService
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()
else:
mailman_srv = RemoteMailmanService()
2021-08-13 20:11:56 -04:00
component.provideUtility(mailman_srv, IMailmanService)
2021-07-24 17:09:10 -04:00
2021-08-13 20:11:56 -04:00
# MailService
2021-07-24 17:09:10 -04:00
mail_srv = MailService()
component.provideUtility(mail_srv, IMailService)
2021-08-13 20:11:56 -04:00
# UWLDAPService
2021-07-24 17:09:10 -04:00
uwldap_srv = UWLDAPService()
component.provideUtility(uwldap_srv, IUWLDAPService)
# ClubWebHostingService
if hostname == cfg.get('ceod_webhosting_host'):
webhosting_srv = ClubWebHostingService()
component.provideUtility(webhosting_srv, IClubWebHostingService)
# MySQLService, PostgreSQLService
if hostname == cfg.get('ceod_database_host'):
mysql_srv = MySQLService()
component.provideUtility(mysql_srv, IDatabaseService, 'mysql')
psql_srv = PostgreSQLService()
component.provideUtility(psql_srv, IDatabaseService, 'postgresql')
# all of the cloud services
if hostname == cfg.get('ceod_cloud_host'):
cloudstack_srv = CloudStackService()
component.provideUtility(cloudstack_srv, ICloudStackService)
cloud_mgr = CloudResourceManager()
component.provideUtility(cloud_mgr, ICloudResourceManager)
vhost_mgr = VHostManager()
component.provideUtility(vhost_mgr, IVHostManager)
k8s_srv = KubernetesService()
component.provideUtility(k8s_srv, IKubernetesService)
reg_srv = ContainerRegistryService()
component.provideUtility(reg_srv, IContainerRegistryService)