From 354f789403d85fdd91d3fb20c45cdca1da072e60 Mon Sep 17 00:00:00 2001 From: checktheroads Date: Sat, 15 Feb 2020 20:02:47 -0700 Subject: [PATCH] add asset migration to setup process --- .tests/DockerfileUbuntu | 35 +++++++++++++++++-------------- hyperglass/cli/commands.py | 10 ++++++++- hyperglass/cli/util.py | 17 +++++++++++++++ hyperglass/util.py | 43 +++++++++++++++++++++++--------------- 4 files changed, 71 insertions(+), 34 deletions(-) diff --git a/.tests/DockerfileUbuntu b/.tests/DockerfileUbuntu index 6cd0b31..5b21b66 100644 --- a/.tests/DockerfileUbuntu +++ b/.tests/DockerfileUbuntu @@ -1,25 +1,28 @@ FROM ubuntu:bionic as base -# ENV LC_ALL=C.UTF-8 -# ENV LANG=C.UTF-8 +ENV LC_ALL=C.UTF-8 +ENV LANG=C.UTF-8 WORKDIR /tmp -RUN curl -sL https://deb.nodesource.com/setup_13.x | bash - \ +RUN apt-get update \ + && apt-get install -y git curl net-tools \ + && curl -sL https://deb.nodesource.com/setup_13.x | bash - \ && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \ && apt-get update \ - && apt-get install -y git curl net-tools python3 python3-pip python3-venv redis-server nodejs yarn \ + && apt-get install -y python3 python3-pip python3-venv redis-server nodejs yarn \ && curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3 \ - && git clone --single-branch --branch v1.0.0 --depth 1 https://github.com/checktheroads/hyperglass \ - && ln -s $(which python3) /usr/bin/python \ - && echo "Python Version: $(python --version)" \ - && echo "NodeJS Version: $(node --version)" \ - && echo "Yarn Version: $(yarn --version)" -ENV PATH=$PATH:$HOME/.poetry/bin + && python3 --version \ + && echo "NodeJS $(node --version)" \ + && echo "Yarn $(yarn --version)" \ + && git clone --single-branch --branch v1.0.0 --depth 1 https://github.com/checktheroads/hyperglass +ENV PATH=$PATH:/root/.poetry/bin +# ENV BASH_ENV=/etc/profile exec bash -FROM base -WORKDIR $HOME -RUN bash -c 'BASH_ENV=/etc/profile exec bash' \ - && $HOME/.poetry/bin/poetry install --no-ansi \ - && $HOME/.poetry/bin/poetry run hyperglass setup -d - && $HOME/.poetry/bin/poetry run hyperglass build-ui +FROM base as install +WORKDIR ./hyperglass +RUN poetry install --no-ansi + +FROM install as setup +WORKDIR ./hyperglass +RUN poetry run hyperglass setup -d EXPOSE 8001 CMD poetry run hyperglass start diff --git a/hyperglass/cli/commands.py b/hyperglass/cli/commands.py index 3b251d9..4fd1541 100644 --- a/hyperglass/cli/commands.py +++ b/hyperglass/cli/commands.py @@ -106,7 +106,13 @@ def generate_secret(length): ) def setup(unattended): """Define application directory, move example files, generate systemd service.""" - from hyperglass.cli.util import create_dir, move_files, make_systemd, write_to_file + from hyperglass.cli.util import ( + create_dir, + move_files, + make_systemd, + write_to_file, + migrate_static_assets, + ) user_path = Path.home() / "hyperglass" root_path = Path("/etc/hyperglass/") @@ -158,3 +164,5 @@ def setup(unattended): systemd_file = install_path / "hyperglass.service" systemd = make_systemd(user) write_to_file(systemd_file, systemd) + + migrate_static_assets(install_path) diff --git a/hyperglass/cli/util.py b/hyperglass/cli/util.py index 85eac09..5fe2573 100644 --- a/hyperglass/cli/util.py +++ b/hyperglass/cli/util.py @@ -358,3 +358,20 @@ def write_to_file(file, data): elif file.exists(): success("Wrote systemd file {f}", f=file) return True + + +def migrate_static_assets(app_path): + """Migrate app's static assets to app_path. + + Arguments: + app_path {Path} -- hyperglass runtime path + """ + from hyperglass.util import migrate_static_assets as _migrate + + migrated, msg, a, b = _migrate(app_path) + if not migrated: + callback = error + elif migrated: + callback = success + + callback(msg, a=a, b=b) diff --git a/hyperglass/util.py b/hyperglass/util.py index d729a31..3eec3e8 100644 --- a/hyperglass/util.py +++ b/hyperglass/util.py @@ -255,6 +255,31 @@ async def move_files(src, dst, files): # noqa: C901 return migrated +def migrate_static_assets(app_path): + """Compare repository's static assets with build directory's assets. + + If the contents don't match, re-copy the files. + """ + import shutil + from pathlib import Path + from filecmp import dircmp + + asset_dir = Path(__file__).parent.parent / "images" + target_dir = app_path / "static" / "images" + comparison = dircmp(asset_dir, target_dir, ignore=[".DS_Store"]) + + if not comparison.left_list == comparison.right_list: + shutil.copytree(asset_dir, target_dir) + if not comparison.left_list == comparison.right_list: + return ( + False, + "Files in {a} do not match files in {b}", + str(asset_dir), + str(target_dir), + ) + return (True, "Migrated assets from {a} to {b}", str(asset_dir), str(target_dir)) + + async def check_node_modules(): """Check if node_modules exists and has contents. @@ -346,8 +371,6 @@ async def build_frontend( # noqa: C901 """ import hashlib import tempfile - import shutil - from filecmp import dircmp from pathlib import Path from aiofile import AIOFile import ujson as json @@ -434,22 +457,8 @@ async def build_frontend( # noqa: C901 elif dev_mode and not force: log.debug("Running in developer mode, did not build new UI files") - """ - 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 / "images" - target_dir = app_path / "static" / "images" - comparison = dircmp(asset_dir, target_dir, ignore=[".DS_Store"]) + migrate_static_assets() - if not comparison.left_list == comparison.right_list: - shutil.copytree(asset_dir, target_dir) - if not comparison.left_list == comparison.right_list: - raise Exception( - "Files in '{a}' do not match files in '{b}'".format( - a=str(asset_dir), b=str(target_dir) - ) - ) except Exception as e: raise RuntimeError(str(e))