Changeset View
Changeset View
Standalone View
Standalone View
swh/scheduler/api/server.py
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | for rule in app.url_map.iter_rules(): | ||||
rule=rule.rule, | rule=rule.rule, | ||||
description=getattr(SchedulerInterface, rule.endpoint).__doc__, | description=getattr(SchedulerInterface, rule.endpoint).__doc__, | ||||
) | ) | ||||
) | ) | ||||
# links is now a list of url, endpoint tuples | # links is now a list of url, endpoint tuples | ||||
return links | return links | ||||
def load_and_check_config(config_file, type="local"): | def load_and_check_config(config_path, type="local"): | ||||
"""Check the minimal configuration is set to run the api or raise an | """Check the minimal configuration is set to run the api or raise an | ||||
error explanation. | error explanation. | ||||
Args: | Args: | ||||
config_file (str): Path to the configuration file to load | config_path (str): Path to the configuration file to load | ||||
type (str): configuration type. For 'local' type, more | type (str): configuration type. For 'local' type, more | ||||
checks are done. | checks are done. | ||||
Raises: | Raises: | ||||
Error if the setup is not as expected | Error if the setup is not as expected | ||||
Returns: | Returns: | ||||
configuration as a dict | configuration as a dict | ||||
""" | """ | ||||
if not config_file: | if not config_path: | ||||
raise EnvironmentError("Configuration file must be defined") | raise EnvironmentError("Configuration file must be defined") | ||||
if not os.path.exists(config_file): | if not os.path.exists(config_path): | ||||
raise FileNotFoundError("Configuration file %s does not exist" % (config_file,)) | raise FileNotFoundError(f"Configuration file {config_path} does not exist") | ||||
cfg = config.read(config_file) | cfg = config.read(config_path) | ||||
vcfg = cfg.get("scheduler") | vcfg = cfg.get("scheduler") | ||||
if not vcfg: | if not vcfg: | ||||
raise KeyError("Missing '%scheduler' configuration") | raise KeyError("Missing '%scheduler' configuration") | ||||
if type == "local": | if type == "local": | ||||
cls = vcfg.get("cls") | cls = vcfg.get("cls") | ||||
if cls != "local": | if cls != "local": | ||||
raise ValueError( | raise ValueError( | ||||
"The scheduler backend can only be started with a 'local' " | "The scheduler backend can only be started with a 'local' " | ||||
"configuration" | "configuration" | ||||
) | ) | ||||
args = vcfg.get("args") | db = vcfg.get("db") | ||||
if not args: | |||||
raise KeyError("Invalid configuration; missing 'args' config entry") | |||||
db = args.get("db") | |||||
if not db: | if not db: | ||||
raise KeyError("Invalid configuration; missing 'db' config entry") | raise KeyError("Invalid configuration; missing 'db' config entry") | ||||
return cfg | return cfg | ||||
api_cfg = None | api_cfg = None | ||||
def make_app_from_configfile(): | def make_app_from_configfile(): | ||||
"""Run the WSGI app from the webserver, loading the configuration from | """Run the WSGI app from the webserver, loading the configuration from | ||||
a configuration file. | a configuration file. | ||||
SWH_CONFIG_FILENAME environment variable defines the | SWH_CONFIG_FILENAME environment variable defines the | ||||
configuration path to load. | configuration path to load. | ||||
""" | """ | ||||
global api_cfg | global api_cfg | ||||
if not api_cfg: | if not api_cfg: | ||||
config_file = os.environ.get("SWH_CONFIG_FILENAME") | config_path = os.environ.get("SWH_CONFIG_FILENAME") | ||||
api_cfg = load_and_check_config(config_file) | api_cfg = load_and_check_config(config_path) | ||||
app.config.update(api_cfg) | app.config.update(api_cfg) | ||||
handler = logging.StreamHandler() | handler = logging.StreamHandler() | ||||
app.logger.addHandler(handler) | app.logger.addHandler(handler) | ||||
return app | return app | ||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
print('Please use the "swh-scheduler api-server" command') | print('Please use the "swh-scheduler api-server" command') |