From 0c2c045fd2a9466fe6b0d9689b3a7b9bbf034408 Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Fri, 7 Feb 2025 19:12:20 -0600 Subject: [PATCH] host-online: Accept cfgpol branch in form data In order to support testing new policy on a development branch, the _POST /host/online_ hook now accepts an optional `branch` parameter. The value of this parameter is passed to the host provisioner via the host info message published on the message queue. In this way, new machines can request a specific branch of the policy, providing a method for automated testing prior to merging the branch in to the main development line. How hosts themselves know what branch to request is of course another matter, and will depend on how they are provisioned, whether they are physical or virtual, etc. --- dch_webhooks.py | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/dch_webhooks.py b/dch_webhooks.py index 4006440..44bf4e1 100644 --- a/dch_webhooks.py +++ b/dch_webhooks.py @@ -609,24 +609,29 @@ async def start_ansible_job(): raise Exception(r.read()) -async def publish_host_info(hostname: str, sshkeys: str): +async def publish_host_info( + hostname: str, sshkeys: str, branch: Optional[str] +): + data = { + 'hostname': hostname, + 'sshkeys': sshkeys, + } + if branch: + data['branch'] = branch await context.amqp.connect() await context.amqp.queue_declare(HOST_INFO_QUEUE, durable=True) context.amqp.publish( exchange='', routing_key=HOST_INFO_QUEUE, - body=json.dumps( - { - 'hostname': hostname, - 'sshkeys': sshkeys, - }, - ).encode('utf-8'), + body=json.dumps(data).encode('utf-8'), ) -async def handle_host_online(hostname: str, sshkeys: str): +async def handle_host_online( + hostname: str, sshkeys: str, branch: Optional[str] +): try: - await publish_host_info(hostname, sshkeys) + await publish_host_info(hostname, sshkeys, branch) except asyncio.CancelledError: raise except Exception: @@ -721,5 +726,9 @@ async def jenkins_notify(request: fastapi.Request) -> None: status_code=fastapi.status.HTTP_202_ACCEPTED, response_class=fastapi.responses.PlainTextResponse, ) -async def host_online(hostname: str = Form(), sshkeys: str = Form()) -> None: - asyncio.create_task(handle_host_online(hostname, sshkeys)) +async def host_online( + hostname: str = Form(), + sshkeys: str = Form(), + branch: Optional[str] = Form(None), +) -> None: + asyncio.create_task(handle_host_online(hostname, sshkeys, branch))