2019-06-07 18:33:49 -07:00
|
|
|
"""
|
|
|
|
https://github.com/checktheroads/hyperglass
|
|
|
|
Guncorn configuration
|
|
|
|
"""
|
2019-06-11 13:39:40 -07:00
|
|
|
import os
|
2019-06-15 11:35:55 -07:00
|
|
|
import shutil
|
2019-05-11 23:22:27 -07:00
|
|
|
import multiprocessing
|
2019-06-11 13:39:40 -07:00
|
|
|
from logzero import logger
|
2019-05-11 23:22:27 -07:00
|
|
|
|
|
|
|
command = "/usr/local/bin/gunicorn"
|
2019-05-28 14:14:42 -07:00
|
|
|
pythonpath = "/opt/hyperglass"
|
2019-05-11 23:22:27 -07:00
|
|
|
bind = "[::1]:8001"
|
2019-05-28 14:14:42 -07:00
|
|
|
preload = True
|
2019-05-28 00:53:45 -07:00
|
|
|
workers = multiprocessing.cpu_count() * 2
|
2019-05-11 23:22:27 -07:00
|
|
|
user = "www-data"
|
2019-05-11 23:28:13 -07:00
|
|
|
timeout = 60
|
2019-05-28 14:14:42 -07:00
|
|
|
keepalive = 10
|
|
|
|
|
2019-06-11 13:39:40 -07:00
|
|
|
# Prometheus Multiprocessing directory, set as environment variable
|
2019-06-15 12:42:28 -07:00
|
|
|
prometheus_multiproc_dir = "/tmp/hyperglass_prometheus"
|
2019-06-11 13:39:40 -07:00
|
|
|
|
|
|
|
|
|
|
|
def on_starting(server): # pylint: disable=unused-argument
|
|
|
|
"""Pre-startup Gunicorn Tasks"""
|
2019-06-15 12:42:28 -07:00
|
|
|
# Renders Jinja2 -> Sass, compiles Sass -> CSS prior to worker load
|
2019-06-11 13:39:40 -07:00
|
|
|
try:
|
|
|
|
import hyperglass.render
|
|
|
|
|
|
|
|
hyperglass.render.css()
|
|
|
|
print(1)
|
|
|
|
except ImportError as error_exception:
|
|
|
|
logger.error(f"Exception occurred:\n{error_exception}")
|
2019-06-15 12:42:28 -07:00
|
|
|
# Verify Redis is running
|
|
|
|
try:
|
|
|
|
import hyperglass.configuration
|
|
|
|
import redis
|
|
|
|
|
|
|
|
config = hyperglass.configuration.params()
|
|
|
|
|
|
|
|
redis_config = {
|
|
|
|
"host": config["general"]["redis_host"],
|
|
|
|
"port": config["general"]["redis_port"],
|
|
|
|
"charset": "utf-8",
|
|
|
|
"decode_responses": True,
|
|
|
|
"db": config["features"]["cache"]["redis_id"],
|
|
|
|
}
|
|
|
|
r_cache = redis.Redis(**redis_config)
|
|
|
|
if r_cache.set("testkey", "testvalue", ex=1):
|
|
|
|
logger.debug("Redis is working properly")
|
|
|
|
except (redis.exceptions.ConnectionError):
|
|
|
|
logger.error("Redis is not running")
|
|
|
|
raise EnvironmentError("Redis is not running")
|
2019-06-11 22:28:30 -07:00
|
|
|
# Prometheus multiprocessing directory
|
2019-06-15 12:42:28 -07:00
|
|
|
if os.path.exists(prometheus_multiproc_dir):
|
|
|
|
shutil.rmtree(prometheus_multiproc_dir)
|
|
|
|
else:
|
|
|
|
os.mkdir(prometheus_multiproc_dir)
|
2019-06-11 13:39:40 -07:00
|
|
|
os.environ["prometheus_multiproc_dir"] = prometheus_multiproc_dir
|
|
|
|
|
|
|
|
|
|
|
|
def worker_exit(server, worker): # pylint: disable=unused-argument
|
|
|
|
"""Prometheus multiprocessing WSGI support"""
|
|
|
|
from prometheus_client import multiprocess
|
|
|
|
|
|
|
|
multiprocess.mark_process_dead(worker.pid)
|
|
|
|
|
2019-05-28 14:14:42 -07:00
|
|
|
|
2019-06-11 13:39:40 -07:00
|
|
|
def on_exit(server):
|
2019-06-15 12:51:03 -07:00
|
|
|
"""Pre-shutdown Gunicorn Tasks"""
|
2019-06-15 12:42:28 -07:00
|
|
|
if os.path.exists(prometheus_multiproc_dir):
|
|
|
|
shutil.rmtree(prometheus_multiproc_dir)
|