Skip to content

Commit

Permalink
undeploy command
Browse files Browse the repository at this point in the history
  • Loading branch information
masci committed Dec 29, 2023
1 parent 7abed2f commit 4eaad83
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/hayhooks/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -18,3 +19,4 @@ def hayhooks():
hayhooks.add_command(run)
hayhooks.add_command(deploy)
hayhooks.add_command(status)
hayhooks.add_command(undeploy)
19 changes: 19 additions & 0 deletions src/hayhooks/cli/undeploy/__init__.py
Original file line number Diff line number Diff line change
@@ -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`")
3 changes: 2 additions & 1 deletion src/hayhooks/server/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
23 changes: 23 additions & 0 deletions src/hayhooks/server/handlers/undeploy.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions src/hayhooks/server/pipelines/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 4eaad83

Please sign in to comment.