1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

fix app directory validation

This commit is contained in:
checktheroads
2020-02-15 11:01:16 -07:00
parent d4e5f9b66e
commit 25aff69cf6
3 changed files with 46 additions and 38 deletions

View File

@@ -37,15 +37,13 @@ POSSIBILITY OF SUCH DAMAGE.
"""
# Standard Library
import os
import sys
import getpass
from pathlib import Path
# Third Party
import uvloop
# Project
from hyperglass.util import set_app_path
from hyperglass.constants import METADATA
try:
@@ -59,39 +57,7 @@ else:
_style = "plaintext"
stackprinter.set_excepthook(style=_style)
config_path = None
_CONFIG_PATHS = (Path.home() / "hyperglass", Path("/etc/hyperglass/"))
for path in _CONFIG_PATHS:
try:
if not isinstance(path, Path):
path = Path(path)
if path.exists():
tmp = path / "test.tmp"
tmp.touch()
if tmp.exists():
config_path = path
tmp.unlink()
break
except Exception:
config_path = None
if config_path is None:
raise RuntimeError(
"""
No configuration directories were determined to both exist and be readable
by hyperglass. hyperglass is running as user '{un}' (UID '{uid}'), and tried to access
the following directories:
{dir}""".format(
un=getpass.getuser(),
uid=os.getuid(),
dir="\n".join([" - " + str(p) for p in _CONFIG_PATHS]),
)
)
os.environ["hyperglass_directory"] = str(config_path)
set_app_path()
uvloop.install()

View File

@@ -14,7 +14,7 @@ from aiofile import AIOFile
from pydantic import ValidationError
# Project
from hyperglass.util import log, check_path
from hyperglass.util import log, check_path, set_app_path
from hyperglass.constants import (
CREDIT,
LOG_LEVELS,
@@ -31,6 +31,8 @@ from hyperglass.configuration.models import routers as _routers
from hyperglass.configuration.models import commands as _commands
from hyperglass.configuration.markdown import get_markdown
set_app_path()
CONFIG_PATH = Path(os.environ["hyperglass_directory"])
log.info("Configuration directory: {d}", d=str(CONFIG_PATH))

View File

@@ -365,7 +365,7 @@ async def build_frontend( # noqa: C901
Compare repository's static assets with build directory's
assets. If the contents don't match, re-copy the files.
"""
asset_dir = Path(__file__).parent.parent / "assets"
asset_dir = Path(__file__).parent.parent / "images"
target_dir = app_path / "static" / "images"
comparison = dircmp(asset_dir, target_dir, ignore=[".DS_Store"])
@@ -381,3 +381,43 @@ async def build_frontend( # noqa: C901
raise RuntimeError(str(e))
return True
def set_app_path(required=False):
"""Find app directory and set value to environment variable."""
import os
from pathlib import Path
from getpass import getuser
matched_path = None
config_paths = (Path.home() / "hyperglass", Path("/etc/hyperglass/"))
for path in config_paths:
try:
if path.exists():
tmp = path / "test.tmp"
tmp.touch()
if tmp.exists():
matched_path = path
tmp.unlink()
break
except Exception:
matched_path = None
if required and matched_path is None:
# Only raise an error if required is True
raise RuntimeError(
"""
No configuration directories were determined to both exist and be readable
by hyperglass. hyperglass is running as user '{un}' (UID '{uid}'), and tried
to access the following directories:
{dir}""".format(
un=getuser(),
uid=os.getuid(),
dir="\n".join([" - " + str(p) for p in config_paths]),
)
)
os.environ["hyperglass_directory"] = str(matched_path)
return True