Skip to content

Commit

Permalink
persist backend
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Jul 30, 2024
1 parent 212642c commit f7b2b08
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 17 deletions.
21 changes: 12 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion wis2-grep-management/docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
10 changes: 10 additions & 0 deletions wis2-grep-management/wis2_grep/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
6 changes: 5 additions & 1 deletion wis2-grep-management/wis2_grep/backend/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down
35 changes: 29 additions & 6 deletions wis2-grep-management/wis2_grep/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f7b2b08

Please sign in to comment.