From a3d9cf4e54bc2696ca0e7d89d5b73bcdec9635bb Mon Sep 17 00:00:00 2001 From: checktheroads Date: Mon, 18 Jan 2021 14:35:10 -0700 Subject: [PATCH] check the NodeJS version on startup, closes #105 --- .flake8 | 2 +- hyperglass/constants.py | 2 ++ hyperglass/main.py | 25 ++++++++++++++++++------- hyperglass/util/frontend.py | 21 +++++++++++++++++++++ 4 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 hyperglass/util/frontend.py diff --git a/.flake8 b/.flake8 index 54cab92..7f0a5e6 100644 --- a/.flake8 +++ b/.flake8 @@ -16,7 +16,7 @@ per-file-ignores= # Disable unused import warning for modules hyperglass/*/__init__.py:F401 hyperglass/models/*/__init__.py:F401 -ignore=W503,C0330,R504,D202,S403,S301 +ignore=W503,C0330,R504,D202,S403,S301,S404 select=B, BLK, C, D, E, F, I, II, N, P, PIE, S, R, W disable-noqa=False hang-closing=False diff --git a/hyperglass/constants.py b/hyperglass/constants.py index 4178e36..1d561f3 100644 --- a/hyperglass/constants.py +++ b/hyperglass/constants.py @@ -13,6 +13,8 @@ METADATA = (__name__, __version__, __author__, __copyright__, __license__) MIN_PYTHON_VERSION = (3, 6) +MIN_NODE_VERSION = 14 + TARGET_FORMAT_SPACE = ("huawei", "huawei_vrpv8") TARGET_JUNIPER_ASPATH = ("juniper", "juniper_junos") diff --git a/hyperglass/main.py b/hyperglass/main.py index 51c4b4b..020fcdd 100644 --- a/hyperglass/main.py +++ b/hyperglass/main.py @@ -12,18 +12,27 @@ from gunicorn.arbiter import Arbiter from gunicorn.app.base import BaseApplication from gunicorn.glogging import Logger -# Project -from hyperglass.log import log, setup_lib_logging -from hyperglass.constants import MIN_PYTHON_VERSION, __version__ +# Local +from .log import log, setup_lib_logging +from .constants import MIN_NODE_VERSION, MIN_PYTHON_VERSION, __version__ +from .util.frontend import get_node_version +# Ensure the Python version meets the minimum requirements. 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.") -# Project -from hyperglass.cache import SyncCache +# Ensure the NodeJS version meets the minimum requirements. +node_version = get_node_version() -from hyperglass.configuration import ( # isort:skip +if node_version != MIN_NODE_VERSION: + raise RuntimeError(f"NodeJS {MIN_NODE_VERSION}+ is required.") + + +# Local +from .cache import SyncCache + +from .configuration import ( # isort:skip params, URL_DEV, URL_PROD, @@ -31,12 +40,14 @@ from hyperglass.configuration import ( # isort:skip REDIS_CONFIG, frontend_params, ) -from hyperglass.util import ( # isort:skip +from .util import ( # isort:skip cpu_count, build_frontend, clear_redis_cache, format_listen_address, ) + + from hyperglass.compat._asyncio import aiorun # isort:skip if params.debug: diff --git a/hyperglass/util/frontend.py b/hyperglass/util/frontend.py new file mode 100644 index 0000000..2ce792c --- /dev/null +++ b/hyperglass/util/frontend.py @@ -0,0 +1,21 @@ +"""Utility functions for frontend-related tasks.""" + +# Standard Library +import shutil +import subprocess + + +def get_node_version() -> int: + """Get the system's NodeJS version.""" + node_path = shutil.which("node") + + raw_version = subprocess.check_output( # noqa: S603 + [node_path, "--version"] + ).decode() + + # Node returns the version as 'v14.5.0', for example. Remove the v. + version = raw_version.replace("v", "") + # Parse the version parts. + major, minor, patch = version.split(".") + + return int(major)