|
2 | 2 |
|
3 | 3 | import logging
|
4 | 4 | import json
|
| 5 | +import asyncio |
5 | 6 |
|
6 | 7 | import click
|
| 8 | +import aiohttp |
7 | 9 | from libadvian.logging import init_logging
|
8 | 10 |
|
9 | 11 | from rmmtxauthz import __version__
|
@@ -48,6 +50,34 @@ def dump_openapi(ctx: click.Context) -> None:
|
48 | 50 | ctx.exit(0)
|
49 | 51 |
|
50 | 52 |
|
| 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 | + |
51 | 81 | def rmmtxauthz_cli() -> None:
|
52 | 82 | """Backend for the IoT devices"""
|
53 | 83 | init_logging(logging.WARNING)
|
|
0 commit comments