Skip to content

Commit ff75793

Browse files
committed
feat: CLI healthcheck to avoid install curl to container
1 parent 36031c8 commit ff75793

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/rmmtxauthz/console.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import logging
44
import json
5+
import asyncio
56

67
import click
8+
import aiohttp
79
from libadvian.logging import init_logging
810

911
from rmmtxauthz import __version__
@@ -48,6 +50,34 @@ def dump_openapi(ctx: click.Context) -> None:
4850
ctx.exit(0)
4951

5052

53+
@cli_group.command(name="healthcheck")
54+
@click.option("--host", default="localhost", help="The host to connect to")
55+
@click.option("--port", default=8005, help="The port to connect to")
56+
@click.option("--timeout", default=2.0, help="The timeout in seconds")
57+
@click.pass_context
58+
def do_http_healthcheck(ctx: click.Context, host: str, port: int, timeout: float) -> None:
59+
"""
60+
Do a GET request to the healthcheck api and dump results to stdout
61+
"""
62+
63+
async def doit() -> int:
64+
"""The actual work"""
65+
nonlocal host, port, timeout
66+
if "://" not in host:
67+
host = f"http://{host}"
68+
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=timeout)) as session:
69+
async with session.get(f"{host}:{port}/api/v1/healthcheck") as resp:
70+
if resp.status != 200:
71+
return int(resp.status)
72+
payload = await resp.json()
73+
click.echo(json.dumps(payload))
74+
if not payload["healthy"]:
75+
return 1
76+
return 0
77+
78+
ctx.exit(asyncio.get_event_loop().run_until_complete(doit()))
79+
80+
5181
def rmmtxauthz_cli() -> None:
5282
"""Backend for the IoT devices"""
5383
init_logging(logging.WARNING)

0 commit comments

Comments
 (0)