add clear-cache CLI command

This commit is contained in:
checktheroads 2020-10-05 12:13:03 -07:00
parent 0f3f4bd56b
commit d9fa00fd75
3 changed files with 46 additions and 8 deletions

View file

@ -11,10 +11,11 @@ from click import group, option, confirm, help_option
# Project
from hyperglass.util import cpu_count
from hyperglass.cli.echo import error, label, cmd_help
from hyperglass.cli.util import build_ui
from hyperglass.cli.static import LABEL, CLI_HELP, E
from hyperglass.cli.formatting import HelpColorsGroup, HelpColorsCommand, random_colors
from .echo import error, label, success, cmd_help
from .util import build_ui
from .static import LABEL, CLI_HELP, E
from .formatting import HelpColorsGroup, HelpColorsCommand, random_colors
# Define working directory
WORKING_DIR = Path(__file__).parent
@ -38,7 +39,7 @@ def _print_version(ctx, param, value):
context_settings={"help_option_names": ["-h", "--help"], "color": supports_color},
help_headers_color=LABEL,
help_options_custom_colors=random_colors(
"build-ui", "start", "secret", "setup", "system-info"
"build-ui", "start", "secret", "setup", "system-info", "clear-cache"
),
)
@option(
@ -240,7 +241,7 @@ def setup(unattended):
@hg.command(
"system-info",
help=cmd_help(
E.THERMOMETER, " Get System Information for a Bug report", supports_color
E.THERMOMETER, " Get system information for a bug report", supports_color
),
cls=HelpColorsCommand,
)
@ -250,3 +251,20 @@ def get_system_info():
from hyperglass.cli.util import system_info
system_info()
@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))

View file

@ -46,6 +46,7 @@ class Emoji:
CLAMP = "\U0001F5DC "
BOOKS = "\U0001F4DA "
THERMOMETER = "\U0001F321 "
SOAP = "\U0001F9FC "
WS = Char(" ")

View file

@ -219,6 +219,19 @@ async def clear_redis_cache(db: int, config: Dict) -> bool:
return True
def sync_clear_redis_cache() -> None:
"""Clear the Redis cache."""
# Project
from hyperglass.cache import SyncCache
from hyperglass.configuration import REDIS_CONFIG, params
try:
cache = SyncCache(db=params.cache.database, **REDIS_CONFIG)
cache.clear()
except BaseException as err:
raise RuntimeError from err
async def move_files(src, dst, files): # noqa: C901
"""Move iterable of files from source to destination.
@ -768,6 +781,7 @@ def format_listen_address(listen_address: Union[IPv4Address, IPv6Address, str])
fmt = str(listen_address)
if isinstance(listen_address, str):
# Standard Library
from ipaddress import ip_address
try:
@ -866,6 +880,7 @@ def get_cache_env():
def make_repr(_class):
"""Create a user-friendly represention of an object."""
# Standard Library
from asyncio import iscoroutine
def _process_attrs(_dir):
@ -890,9 +905,12 @@ def make_repr(_class):
def validate_nos(nos):
"""Validate device NOS is supported."""
from hyperglass.constants import DRIVER_MAP
# Third Party
from netmiko.ssh_dispatcher import CLASS_MAPPER_BASE
# Project
from hyperglass.constants import DRIVER_MAP
result = (False, None)
all_nos = {*DRIVER_MAP.keys(), *CLASS_MAPPER_BASE.keys()}
@ -932,7 +950,8 @@ def validation_error_message(*errors: Dict) -> str:
def resolve_hostname(hostname: str) -> Generator:
"""Resolve a hostname via DNS/hostfile."""
from socket import getaddrinfo, gaierror
# Standard Library
from socket import gaierror, getaddrinfo
log.debug("Ensuring '{}' is resolvable...", hostname)