diff --git a/src/hayhooks/cli/__init__.py b/src/hayhooks/cli/__init__.py index 29d18a6..e90b7c5 100644 --- a/src/hayhooks/cli/__init__.py +++ b/src/hayhooks/cli/__init__.py @@ -7,6 +7,7 @@ from hayhooks.cli.run import run from hayhooks.cli.deploy import deploy from hayhooks.cli.status import status +from hayhooks.cli.undeploy import undeploy @click.group(context_settings={"help_option_names": ["-h", "--help"]}, invoke_without_command=True) @@ -18,3 +19,4 @@ def hayhooks(): hayhooks.add_command(run) hayhooks.add_command(deploy) hayhooks.add_command(status) +hayhooks.add_command(undeploy) diff --git a/src/hayhooks/cli/undeploy/__init__.py b/src/hayhooks/cli/undeploy/__init__.py new file mode 100644 index 0000000..8558a17 --- /dev/null +++ b/src/hayhooks/cli/undeploy/__init__.py @@ -0,0 +1,19 @@ +from pathlib import Path + +import click +import requests +from requests import ConnectionError + + +@click.command() +@click.argument('pipeline_name') +def undeploy(pipeline_name): + try: + resp = requests.post(f"http://localhost:1416/undeploy/{pipeline_name}") + + if resp.status_code >= 400: + click.echo(f"Cannot undeploy pipeline: {resp.json().get('detail')}") + else: + click.echo(f"Pipeline successfully undeployed") + except ConnectionError: + click.echo("Hayhooks server is not responding. To start one, run `hayooks run`") diff --git a/src/hayhooks/server/handlers/__init__.py b/src/hayhooks/server/handlers/__init__.py index 4821b88..50a7a47 100644 --- a/src/hayhooks/server/handlers/__init__.py +++ b/src/hayhooks/server/handlers/__init__.py @@ -1,5 +1,6 @@ from hayhooks.server.handlers import deploy from hayhooks.server.handlers import status +from hayhooks.server.handlers import undeploy -__all__ = ["deploy", "status"] +__all__ = ["deploy", "status", "undeploy"] diff --git a/src/hayhooks/server/handlers/undeploy.py b/src/hayhooks/server/handlers/undeploy.py new file mode 100644 index 0000000..491cf79 --- /dev/null +++ b/src/hayhooks/server/handlers/undeploy.py @@ -0,0 +1,23 @@ +from typing import cast + +from fastapi import HTTPException +from fastapi.routing import APIRoute +from hayhooks.server import app +from hayhooks.server.pipelines import registry + + +@app.post("/undeploy/{pipeline_name}") +async def deploy(pipeline_name: str): + if pipeline_name not in registry.get_names(): + raise HTTPException(status_code=404) + + new_routes = [] + for route in app.router.routes: + route = cast(APIRoute, route) + if route.name != pipeline_name: + new_routes.append(route) + + app.router.routes = new_routes + app.openapi_schema = None + app.setup() + registry.remove(pipeline_name) diff --git a/src/hayhooks/server/pipelines/registry.py b/src/hayhooks/server/pipelines/registry.py index 25f4dec..860eea8 100644 --- a/src/hayhooks/server/pipelines/registry.py +++ b/src/hayhooks/server/pipelines/registry.py @@ -21,6 +21,10 @@ def add(self, name: str, source: str) -> Pipeline: return self._pipelines[name] + def remove(self, name: str): + if name in self._pipelines: + del self._pipelines[name] + def get(self, name: str) -> Optional[Pipeline]: return self._pipelines.get(name)