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

200 lines
5.1 KiB
Python
Raw Normal View History

2020-01-21 01:08:28 -07:00
"""CLI Command definitions."""
2020-02-03 02:35:11 -07:00
# Standard Library
2020-02-15 12:18:03 -07:00
import sys
2020-01-21 01:08:28 -07:00
from pathlib import Path
2020-02-03 02:35:11 -07:00
# Third Party
from click import group, option, help_option
2020-01-21 01:08:28 -07:00
2020-02-03 02:35:11 -07:00
# Project
2020-04-13 01:06:03 -07:00
from hyperglass.util import cpu_count
2020-10-05 12:13:03 -07:00
2020-10-11 13:14:57 -07:00
# Local
2021-02-10 00:42:33 -07:00
from .echo import error, label, success, warning, cmd_help
2020-10-05 12:13:03 -07:00
from .util import build_ui
from .static import LABEL, CLI_HELP, E
from .installer import Installer
2020-10-05 12:13:03 -07:00
from .formatting import HelpColorsGroup, HelpColorsCommand, random_colors
2020-01-21 01:08:28 -07:00
# Define working directory
WORKING_DIR = Path(__file__).parent
2020-02-15 12:18:03 -07:00
supports_color = "utf" in sys.getfilesystemencoding().lower()
2020-01-21 01:08:28 -07:00
2020-02-16 00:51:31 -07:00
def _print_version(ctx, param, value):
# Project
2020-02-16 00:51:31 -07:00
from hyperglass import __version__
if not value or ctx.resilient_parsing:
return
label("hyperglass version: {v}", v=__version__)
ctx.exit()
@group(
2020-01-21 01:08:28 -07:00
cls=HelpColorsGroup,
help=CLI_HELP,
2020-02-16 00:51:31 -07:00
context_settings={"help_option_names": ["-h", "--help"], "color": supports_color},
2020-01-21 01:08:28 -07:00
help_headers_color=LABEL,
help_options_custom_colors=random_colors(
2020-10-05 12:13:03 -07:00
"build-ui", "start", "secret", "setup", "system-info", "clear-cache"
),
2020-01-21 01:08:28 -07:00
)
2020-02-16 00:51:31 -07:00
@option(
"-v",
"--version",
is_flag=True,
callback=_print_version,
expose_value=False,
is_eager=True,
help=cmd_help(E.NUMBERS, "hyperglass version", supports_color),
)
@help_option(
2021-09-12 15:09:24 -07:00
"-h", "--help", help=cmd_help(E.FOLDED_HANDS, "Show this help message", supports_color),
2020-02-16 00:51:31 -07:00
)
2020-01-21 01:08:28 -07:00
def hg():
"""Initialize Click Command Group."""
pass
2021-09-12 15:09:24 -07:00
@hg.command("build-ui", help=cmd_help(E.BUTTERFLY, "Create a new UI build", supports_color))
@option("-t", "--timeout", required=False, default=180, help="Timeout in seconds")
def build_frontend(timeout):
"""Create a new UI build."""
return build_ui(timeout)
2020-01-21 01:08:28 -07:00
2020-06-02 01:43:29 -07:00
@hg.command( # noqa: C901
2020-01-21 01:08:28 -07:00
"start",
2020-02-15 12:18:03 -07:00
help=cmd_help(E.ROCKET, "Start web server", supports_color),
2020-01-21 01:08:28 -07:00
cls=HelpColorsCommand,
2020-04-13 01:06:03 -07:00
help_options_custom_colors=random_colors("-b", "-d", "-w"),
2020-01-21 01:08:28 -07:00
)
@option("-b", "--build", is_flag=True, help="Render theme & build frontend assets")
2020-04-13 01:06:03 -07:00
@option(
"-d",
"--direct",
is_flag=True,
default=False,
help="Start hyperglass directly instead of through process manager",
)
@option(
"-w",
"--workers",
type=int,
required=False,
default=0,
help=f"Number of workers. By default, calculated from CPU cores [{cpu_count(2)}]",
)
def start(build, direct, workers): # noqa: C901
2020-01-21 01:08:28 -07:00
"""Start web server and optionally build frontend assets."""
# Project
from hyperglass.api import start as uvicorn_start
from hyperglass.main import start
2020-04-13 01:06:03 -07:00
kwargs = {}
if workers != 0:
kwargs["workers"] = workers
2020-01-21 01:08:28 -07:00
2020-06-02 01:43:29 -07:00
try:
if build:
2021-02-06 00:19:04 -07:00
build_complete = build_ui(timeout=180)
2020-06-02 01:43:29 -07:00
if build_complete and not direct:
start(**kwargs)
elif build_complete and direct:
uvicorn_start(**kwargs)
2020-01-21 01:08:28 -07:00
2020-06-02 01:43:29 -07:00
if not build and not direct:
2020-04-13 01:06:03 -07:00
start(**kwargs)
2020-06-02 01:43:29 -07:00
elif not build and direct:
2020-04-13 01:06:03 -07:00
uvicorn_start(**kwargs)
2021-02-10 00:42:33 -07:00
except (KeyboardInterrupt, SystemExit) as err:
error_message = str(err)
if (len(error_message)) > 1:
warning(str(err))
2020-10-11 15:39:15 -07:00
error("Stopping hyperglass due to keyboard interrupt.")
2020-01-21 01:08:28 -07:00
@hg.command(
"secret",
2020-02-15 12:18:03 -07:00
help=cmd_help(E.LOCK, "Generate agent secret", supports_color),
2020-01-21 01:08:28 -07:00
cls=HelpColorsCommand,
help_options_custom_colors=random_colors("-l"),
)
2021-09-12 15:09:24 -07:00
@option("-l", "--length", "length", default=32, help="Number of characters [default: 32]")
2020-01-21 01:08:28 -07:00
def generate_secret(length):
"""Generate secret for hyperglass-agent.
Arguments:
length {int} -- Length of secret
"""
# Standard Library
2020-01-21 01:08:28 -07:00
import secrets
gen_secret = secrets.token_urlsafe(length)
label("Secret: {s}", s=gen_secret)
@hg.command(
2020-02-14 22:54:31 -07:00
"setup",
2020-02-15 12:18:03 -07:00
help=cmd_help(E.TOOLBOX, "Run the setup wizard", supports_color),
2020-02-14 22:54:31 -07:00
cls=HelpColorsCommand,
help_options_custom_colors=random_colors("-d"),
)
@option(
"-d",
"--use-defaults",
"unattended",
default=False,
is_flag=True,
help="Use hyperglass defaults (requires no input)",
)
2020-02-14 22:54:31 -07:00
def setup(unattended):
"""Define application directory, move example files, generate systemd service."""
installer = Installer(unattended=unattended)
installer.install()
success(
"""Completed hyperglass installation.
2021-07-06 22:05:10 -07:00
After adding your {devices} file, you should run the {build_cmd} command.""", # noqa: E501
devices="devices.yaml",
build_cmd="hyperglass build-ui",
)
@hg.command(
"system-info",
2021-09-12 15:09:24 -07:00
help=cmd_help(E.THERMOMETER, " Get system information for a bug report", supports_color),
cls=HelpColorsCommand,
)
def get_system_info():
"""Get CPU, Memory, Disk, Python, & hyperglass version."""
# Project
from hyperglass.cli.util import system_info
system_info()
2020-10-05 12:13:03 -07:00
@hg.command(
"clear-cache",
help=cmd_help(E.SOAP, "Clear the Redis cache", supports_color),
cls=HelpColorsCommand,
)
def clear_cache():
"""Clear the Redis Cache."""
# Project
from hyperglass.util import sync_clear_redis_cache
try:
sync_clear_redis_cache()
success("Cleared Redis Cache")
except RuntimeError as err:
error(str(err))