2020-04-13 01:06:03 -07:00
|
|
|
"""Gunicorn Config File."""
|
|
|
|
|
|
|
|
# Standard Library
|
|
|
|
import sys
|
|
|
|
import shutil
|
2021-09-15 00:57:45 -07:00
|
|
|
import typing as t
|
2021-09-15 18:25:37 -07:00
|
|
|
import asyncio
|
2020-04-13 01:06:03 -07:00
|
|
|
import platform
|
|
|
|
|
|
|
|
# Third Party
|
2021-09-15 18:25:37 -07:00
|
|
|
from gunicorn.arbiter import Arbiter # type: ignore
|
2021-09-07 22:58:39 -07:00
|
|
|
from gunicorn.app.base import BaseApplication # type: ignore
|
2020-04-13 01:06:03 -07:00
|
|
|
|
2021-01-18 14:35:10 -07:00
|
|
|
# Local
|
2021-09-26 16:50:25 -07:00
|
|
|
from .log import CustomGunicornLogger, log, setup_lib_logging
|
2021-09-13 14:10:50 -07:00
|
|
|
from .plugins import (
|
|
|
|
InputPluginManager,
|
|
|
|
OutputPluginManager,
|
|
|
|
register_plugin,
|
|
|
|
init_builtin_plugins,
|
|
|
|
)
|
2021-01-18 14:35:10 -07:00
|
|
|
from .constants import MIN_NODE_VERSION, MIN_PYTHON_VERSION, __version__
|
|
|
|
from .util.frontend import get_node_version
|
2020-04-13 01:06:03 -07:00
|
|
|
|
2021-01-18 14:35:10 -07:00
|
|
|
# Ensure the Python version meets the minimum requirements.
|
2020-04-13 01:06:03 -07:00
|
|
|
pretty_version = ".".join(tuple(str(v) for v in MIN_PYTHON_VERSION))
|
|
|
|
if sys.version_info < MIN_PYTHON_VERSION:
|
|
|
|
raise RuntimeError(f"Python {pretty_version}+ is required.")
|
|
|
|
|
2021-01-18 14:35:10 -07:00
|
|
|
# Ensure the NodeJS version meets the minimum requirements.
|
2021-07-03 23:02:14 -07:00
|
|
|
node_major, _, __ = get_node_version()
|
2020-10-10 21:13:28 -07:00
|
|
|
|
2021-07-03 23:02:14 -07:00
|
|
|
if node_major != MIN_NODE_VERSION:
|
2021-09-15 00:57:45 -07:00
|
|
|
raise RuntimeError(f"NodeJS {MIN_NODE_VERSION!s}+ is required.")
|
2021-01-18 14:35:10 -07:00
|
|
|
|
|
|
|
|
|
|
|
# Local
|
2021-09-15 00:57:45 -07:00
|
|
|
from .util import cpu_count
|
|
|
|
from .state import use_state
|
|
|
|
from .settings import Settings
|
2021-09-17 18:15:59 -07:00
|
|
|
from .configuration import init_user_config
|
2021-01-28 23:02:25 -07:00
|
|
|
from .util.frontend import build_frontend
|
2020-04-13 01:06:03 -07:00
|
|
|
|
|
|
|
|
2021-01-28 23:02:25 -07:00
|
|
|
async def build_ui() -> bool:
|
|
|
|
"""Perform a UI build prior to starting the application."""
|
2021-09-15 00:57:45 -07:00
|
|
|
state = use_state()
|
2020-04-13 01:06:03 -07:00
|
|
|
await build_frontend(
|
2021-09-15 00:57:45 -07:00
|
|
|
dev_mode=Settings.dev_mode,
|
2021-09-15 18:25:37 -07:00
|
|
|
dev_url=Settings.dev_url,
|
|
|
|
prod_url=Settings.prod_url,
|
2021-09-15 00:57:45 -07:00
|
|
|
params=state.ui_params,
|
|
|
|
app_path=Settings.app_path,
|
2020-04-13 01:06:03 -07:00
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-09-26 11:39:46 -07:00
|
|
|
def register_all_plugins() -> None:
|
2021-09-11 17:55:27 -07:00
|
|
|
"""Validate and register configured plugins."""
|
|
|
|
|
2021-09-26 11:39:46 -07:00
|
|
|
state = use_state()
|
|
|
|
|
2021-09-13 14:10:50 -07:00
|
|
|
# Register built-in plugins.
|
|
|
|
init_builtin_plugins()
|
|
|
|
|
2021-09-26 11:39:46 -07:00
|
|
|
failures = ()
|
|
|
|
|
|
|
|
# Register external directive-based plugins (defined in directives).
|
|
|
|
for plugin_file, directives in state.devices.directive_plugins().items():
|
|
|
|
failures += register_plugin(plugin_file, directives=directives)
|
|
|
|
|
|
|
|
# Register external global/common plugins (defined in config).
|
|
|
|
for plugin_file in state.params.common_plugins():
|
|
|
|
failures += register_plugin(plugin_file, common=True)
|
|
|
|
|
|
|
|
for failure in failures:
|
|
|
|
log.warning(
|
|
|
|
"Plugin {!r} is not a valid hyperglass plugin and was not registered", failure,
|
|
|
|
)
|
2021-09-11 17:55:27 -07:00
|
|
|
|
|
|
|
|
|
|
|
def unregister_all_plugins() -> None:
|
|
|
|
"""Unregister all plugins."""
|
|
|
|
for manager in (InputPluginManager, OutputPluginManager):
|
|
|
|
manager().reset()
|
|
|
|
|
|
|
|
|
2021-09-17 12:08:38 -07:00
|
|
|
def on_starting(server: "Arbiter") -> None:
|
2020-04-13 01:06:03 -07:00
|
|
|
"""Gunicorn pre-start tasks."""
|
|
|
|
|
|
|
|
python_version = platform.python_version()
|
|
|
|
required = ".".join((str(v) for v in MIN_PYTHON_VERSION))
|
2021-09-15 00:57:45 -07:00
|
|
|
log.debug("Python {} detected ({} required)", python_version, required)
|
|
|
|
|
2021-09-26 11:39:46 -07:00
|
|
|
register_all_plugins()
|
2020-04-13 01:06:03 -07:00
|
|
|
|
2021-09-15 18:25:37 -07:00
|
|
|
asyncio.run(build_ui())
|
2020-04-13 01:06:03 -07:00
|
|
|
|
|
|
|
log.success(
|
2021-09-15 00:57:45 -07:00
|
|
|
"Started hyperglass {} on http://{} with {!s} workers",
|
|
|
|
__version__,
|
|
|
|
Settings.bind(),
|
|
|
|
server.app.cfg.settings["workers"].value,
|
2020-04-13 01:06:03 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-17 18:15:59 -07:00
|
|
|
def on_exit(_: t.Any) -> None:
|
2020-04-13 01:06:03 -07:00
|
|
|
"""Gunicorn shutdown tasks."""
|
2020-04-15 02:12:01 -07:00
|
|
|
|
|
|
|
log.critical("Stopping hyperglass {}", __version__)
|
|
|
|
|
2021-09-15 00:57:45 -07:00
|
|
|
state = use_state()
|
|
|
|
if not Settings.dev_mode:
|
|
|
|
state.clear()
|
2020-04-15 02:12:01 -07:00
|
|
|
|
2021-09-11 17:55:27 -07:00
|
|
|
unregister_all_plugins()
|
2020-04-13 01:06:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
class HyperglassWSGI(BaseApplication):
|
|
|
|
"""Custom gunicorn app."""
|
|
|
|
|
2021-09-17 12:08:38 -07:00
|
|
|
def __init__(self: "HyperglassWSGI", app: str, options: t.Dict[str, t.Any]):
|
2020-04-13 01:06:03 -07:00
|
|
|
"""Initialize custom WSGI."""
|
|
|
|
self.application = app
|
|
|
|
self.options = options or {}
|
|
|
|
super().__init__()
|
|
|
|
|
2021-09-17 12:08:38 -07:00
|
|
|
def load_config(self: "HyperglassWSGI"):
|
2020-04-13 01:06:03 -07:00
|
|
|
"""Load gunicorn config."""
|
|
|
|
config = {
|
|
|
|
key: value
|
|
|
|
for key, value in self.options.items()
|
|
|
|
if key in self.cfg.settings and value is not None
|
|
|
|
}
|
2020-04-15 02:12:01 -07:00
|
|
|
|
2020-04-13 01:06:03 -07:00
|
|
|
for key, value in config.items():
|
|
|
|
self.cfg.set(key.lower(), value)
|
|
|
|
|
2021-09-17 12:08:38 -07:00
|
|
|
def load(self: "HyperglassWSGI"):
|
2020-04-13 01:06:03 -07:00
|
|
|
"""Load gunicorn app."""
|
|
|
|
return self.application
|
|
|
|
|
|
|
|
|
2021-09-17 12:08:38 -07:00
|
|
|
def start(*, log_level: str, workers: int, **kwargs) -> None:
|
2020-04-13 01:06:03 -07:00
|
|
|
"""Start hyperglass via gunicorn."""
|
|
|
|
|
|
|
|
HyperglassWSGI(
|
2021-09-12 18:27:33 -07:00
|
|
|
app="hyperglass.api:app",
|
2020-04-13 01:06:03 -07:00
|
|
|
options={
|
|
|
|
"preload": True,
|
2021-09-15 00:57:45 -07:00
|
|
|
"errorlog": "-",
|
|
|
|
"accesslog": "-",
|
2020-04-13 01:06:03 -07:00
|
|
|
"workers": workers,
|
|
|
|
"on_exit": on_exit,
|
2021-09-15 00:57:45 -07:00
|
|
|
"loglevel": log_level,
|
|
|
|
"bind": Settings.bind(),
|
|
|
|
"on_starting": on_starting,
|
|
|
|
"command": shutil.which("gunicorn"),
|
2021-09-26 16:50:25 -07:00
|
|
|
"logger_class": CustomGunicornLogger,
|
2021-09-15 00:57:45 -07:00
|
|
|
"worker_class": "uvicorn.workers.UvicornWorker",
|
2020-10-11 15:39:15 -07:00
|
|
|
"logconfig_dict": {"formatters": {"generic": {"format": "%(message)s"}}},
|
2020-04-13 01:06:03 -07:00
|
|
|
**kwargs,
|
|
|
|
},
|
|
|
|
).run()
|
|
|
|
|
|
|
|
|
2021-09-27 01:40:49 -07:00
|
|
|
def run(_workers: int = None):
|
|
|
|
"""Run hyperglass."""
|
2021-09-15 00:57:45 -07:00
|
|
|
try:
|
2021-09-17 18:15:59 -07:00
|
|
|
init_user_config()
|
2021-09-17 12:08:38 -07:00
|
|
|
|
|
|
|
log.debug("System settings: {!r}", Settings)
|
|
|
|
|
|
|
|
workers, log_level = 1, "DEBUG"
|
|
|
|
|
|
|
|
if Settings.debug is False:
|
|
|
|
workers, log_level = cpu_count(2), "WARNING"
|
|
|
|
|
2021-09-27 01:40:49 -07:00
|
|
|
if _workers is not None:
|
|
|
|
workers = _workers
|
|
|
|
|
2021-09-17 12:08:38 -07:00
|
|
|
setup_lib_logging(log_level)
|
|
|
|
start(log_level=log_level, workers=workers)
|
2021-09-15 00:57:45 -07:00
|
|
|
except Exception as error:
|
2021-09-15 18:25:37 -07:00
|
|
|
# Handle app exceptions.
|
2021-09-15 00:57:45 -07:00
|
|
|
if not Settings.dev_mode:
|
|
|
|
state = use_state()
|
|
|
|
state.clear()
|
|
|
|
log.info("Cleared Redis cache")
|
|
|
|
unregister_all_plugins()
|
|
|
|
raise error
|
2021-09-15 18:25:37 -07:00
|
|
|
except SystemExit:
|
|
|
|
# Handle Gunicorn exit.
|
|
|
|
sys.exit(4)
|
2021-09-27 01:40:49 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
run()
|