From f7b2b08834a665980f85e135172b23a544bdc9d6 Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Mon, 29 Jul 2024 20:23:29 -0400 Subject: [PATCH] persist backend --- Makefile | 21 ++++++----- README.md | 6 ++++ wis2-grep-management/docker/entrypoint.sh | 2 +- .../wis2_grep/backend/base.py | 10 ++++++ .../wis2_grep/backend/elastic.py | 6 +++- wis2-grep-management/wis2_grep/loader.py | 35 +++++++++++++++---- 6 files changed, 63 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index ea77198..a144032 100644 --- a/Makefile +++ b/Makefile @@ -24,22 +24,25 @@ DOCKER_COMPOSE_ARGS=--project-name wis2-grep --file docker-compose.yml --file do build: docker compose $(DOCKER_COMPOSE_ARGS) build +force-build: + docker compose $(DOCKER_COMPOSE_ARGS) build --no-cache + up: docker compose $(DOCKER_COMPOSE_ARGS) up --detach -dev: - docker compose $(DOCKER_COMPOSE_ARGS) --file docker-compose.dev.yml up - -login: - docker exec -it wis2-grep-management /bin/bash - down: docker compose $(DOCKER_COMPOSE_ARGS) down restart: down up -force-build: - docker compose $(DOCKER_COMPOSE_ARGS) build --no-cache +login: + docker exec -it wis2-grep-management /bin/bash + +dev: + docker compose $(DOCKER_COMPOSE_ARGS) --file docker-compose.dev.yml up + +reinit-backend: + docker exec -it wis2-grep-management sh -c "wis2-grep setup --force" logs: docker compose $(DOCKER_COMPOSE_ARGS) logs --follow @@ -51,4 +54,4 @@ clean: rm: docker volume rm $(shell docker volume ls --filter name=wis2-grep -q) -.PHONY: build up dev login down restart force-build logs rm clean +.PHONY: build up dev login down restart reinit-backend force-build logs rm clean diff --git a/README.md b/README.md index 46a64dd..9531e68 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,9 @@ pywis-pubsub schema sync # setup backend wis2-grep setup +# setup backend (force reinitialization of backend) +wis2-grep setup --force + # teardown backend wis2-grep teardown @@ -136,6 +139,9 @@ make force-build make up # API is up at http://localhost +# reinitialize backend +make reinit-backend + # start all containers in dev mode make dev # API is up at http://localhost diff --git a/wis2-grep-management/docker/entrypoint.sh b/wis2-grep-management/docker/entrypoint.sh index 603a628..6fa3d5d 100755 --- a/wis2-grep-management/docker/entrypoint.sh +++ b/wis2-grep-management/docker/entrypoint.sh @@ -34,7 +34,7 @@ echo "Caching WNM schema" pywis-pubsub schema sync echo "Setting up notification message backend" -wis2-grep setup --yes +wis2-grep setup echo "END /entrypoint.sh" exec "$@" diff --git a/wis2-grep-management/wis2_grep/backend/base.py b/wis2-grep-management/wis2_grep/backend/base.py index 1961ccb..edcc3a9 100644 --- a/wis2-grep-management/wis2_grep/backend/base.py +++ b/wis2-grep-management/wis2_grep/backend/base.py @@ -61,6 +61,16 @@ def save(self, message: dict) -> None: raise NotImplementedError() + @abstractmethod + def exists(self) -> bool: + """ + Querying whether backend exists + + :returns: `bool` of whether backend exists + """ + + raise NotImplementedError() + @abstractmethod def message_exists(self, identifier: str) -> bool: """ diff --git a/wis2-grep-management/wis2_grep/backend/elastic.py b/wis2-grep-management/wis2_grep/backend/elastic.py index 1cd6df1..650cc7d 100644 --- a/wis2-grep-management/wis2_grep/backend/elastic.py +++ b/wis2-grep-management/wis2_grep/backend/elastic.py @@ -110,7 +110,6 @@ def __init__(self, defs): self.es = Elasticsearch(**settings) def setup(self) -> None: - self.teardown() LOGGER.debug(f'Creating index {self.index_name}') self.es.indices.create(index=self.index_name, body=self.ES_SETTINGS) @@ -123,6 +122,11 @@ def save(self, message: dict) -> None: LOGGER.debug(f"Indexing message {message['id']}") self.es.index(index=self.index_name, id=message['id'], body=message) + def exists(self) -> bool: + LOGGER.debug('Checking whether backend exists') + + return self.es.indices.exists(index=self.index_name) + def message_exists(self, identifier: str) -> bool: LOGGER.debug(f'Querying Replay API for id {identifier}') try: diff --git a/wis2-grep-management/wis2_grep/loader.py b/wis2-grep-management/wis2_grep/loader.py index 97a66fd..3703d1a 100644 --- a/wis2-grep-management/wis2_grep/loader.py +++ b/wis2-grep-management/wis2_grep/loader.py @@ -92,19 +92,42 @@ def __repr__(self): @click.command() @click.pass_context +@click.option('--force', '-f', 'force', is_flag=True, default=False, + help='Force reinitialization of backend') @click.option('--yes', '-y', 'bypass', is_flag=True, default=False, help='Bypass permission prompts') @cli_options.OPTION_VERBOSITY -def setup(ctx, bypass, verbosity='NOTSET'): +def setup(ctx, force, bypass, verbosity='NOTSET'): """Create Global Replay Service backend""" - if not bypass: - if not click.confirm('Create Global Replay Service backend? This will overwrite existing collections', abort=True): # noqa - return - backend = BACKENDS[BACKEND_TYPE]({'connection': BACKEND_CONNECTION}) LOGGER.debug(f'Backend: {backend}') - backend.setup() + + if backend.exists(): + if not force: + click.echo('Backend already exists') + return + else: + if bypass: + click.echo('Reinitializing backend') + backend.teardown() + backend.setup() + else: + msg = ('Recreate backend? This will delete all metadata ' + 'and delete/setup/reinitialize the backend.') + + if not click.confirm(msg, abort=True): + click.echo('Not reinitializing backend') + return + else: + click.echo('Reinitializing backend') + backend.teardown() + backend.setup() + else: + click.echo('Setting up backend') + backend.setup() + + click.echo('Done') @click.command()