|
| 1 | +import json |
| 2 | +from typing import Any, Dict |
| 3 | + |
| 4 | +import boto3 |
| 5 | +from botocore.exceptions import ClientError |
| 6 | + |
| 7 | + |
| 8 | +dynamodb = boto3.resource("dynamodb") |
| 9 | +agent_events_table = dynamodb.Table("vllm-buildkite-agent-events") |
| 10 | +build_events_table = dynamodb.Table("vllm-buildkite-build-events") |
| 11 | +job_events_table = dynamodb.Table("vllm-buildkite-job-events") |
| 12 | + |
| 13 | + |
| 14 | +def save_agent_event(event_data: Dict[str, Any]) -> Dict[str, Any]: |
| 15 | + """ |
| 16 | + Save agent events to DynamoDB table. |
| 17 | +
|
| 18 | + Args: |
| 19 | + event_data: The agent event payload from Buildkite |
| 20 | +
|
| 21 | + Returns: |
| 22 | + Dict[str, Any]: Response containing status and result information |
| 23 | + """ |
| 24 | + try: |
| 25 | + agent = event_data.get("agent", {}) |
| 26 | + agent_id = agent.get("id", "") |
| 27 | + |
| 28 | + if not agent_id: |
| 29 | + return { |
| 30 | + "statusCode": 400, |
| 31 | + "body": json.dumps({"message": "Missing agent ID"}), |
| 32 | + } |
| 33 | + |
| 34 | + dynamo_key = agent_id |
| 35 | + item = {"dynamoKey": dynamo_key, **event_data} |
| 36 | + |
| 37 | + agent_events_table.put_item(Item=item) |
| 38 | + |
| 39 | + return { |
| 40 | + "statusCode": 200, |
| 41 | + "body": json.dumps( |
| 42 | + {"message": f"Agent event saved successfully with key: {dynamo_key}"} |
| 43 | + ), |
| 44 | + } |
| 45 | + |
| 46 | + except ClientError as e: |
| 47 | + return { |
| 48 | + "statusCode": 500, |
| 49 | + "body": json.dumps({"message": f"DynamoDB error: {str(e)}"}), |
| 50 | + } |
| 51 | + except Exception as e: |
| 52 | + return { |
| 53 | + "statusCode": 500, |
| 54 | + "body": json.dumps({"message": f"Error saving agent event: {str(e)}"}), |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +def save_build_event(event_data: Dict[str, Any]) -> Dict[str, Any]: |
| 59 | + """ |
| 60 | + Save build event to DynamoDB table. |
| 61 | +
|
| 62 | + Args: |
| 63 | + event_data: The build event payload from Buildkite |
| 64 | +
|
| 65 | + Returns: |
| 66 | + Dict[str, Any]: Response containing status and result information |
| 67 | + """ |
| 68 | + try: |
| 69 | + build = event_data.get("build", {}) |
| 70 | + repo_name = event_data.get("pipeline", {}).get("repository", "").split("/")[-1] |
| 71 | + pipeline_name = event_data.get("pipeline", {}).get("name", "") |
| 72 | + build_number = build.get("number", "") |
| 73 | + |
| 74 | + if not repo_name or not build_number: |
| 75 | + return { |
| 76 | + "statusCode": 400, |
| 77 | + "body": json.dumps( |
| 78 | + {"message": "Missing repository name or build number"} |
| 79 | + ), |
| 80 | + } |
| 81 | + |
| 82 | + # Buildkite build_number is only unique in a pipeline |
| 83 | + dynamo_key = f"{repo_name}/{pipeline_name}/{build_number}" |
| 84 | + |
| 85 | + item = {"dynamoKey": dynamo_key, **event_data} |
| 86 | + build_events_table.put_item(Item=item) |
| 87 | + |
| 88 | + return { |
| 89 | + "statusCode": 200, |
| 90 | + "body": json.dumps( |
| 91 | + {"message": f"Build event saved successfully with key: {dynamo_key}"} |
| 92 | + ), |
| 93 | + } |
| 94 | + |
| 95 | + except ClientError as e: |
| 96 | + return { |
| 97 | + "statusCode": 500, |
| 98 | + "body": json.dumps({"message": f"DynamoDB error: {str(e)}"}), |
| 99 | + } |
| 100 | + except Exception as e: |
| 101 | + return { |
| 102 | + "statusCode": 500, |
| 103 | + "body": json.dumps({"message": f"Error saving build event: {str(e)}"}), |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | +def save_job_event(event_data: Dict[str, Any]) -> Dict[str, Any]: |
| 108 | + """ |
| 109 | + Save job event to DynamoDB table. |
| 110 | +
|
| 111 | + Args: |
| 112 | + event_data: The job event payload from Buildkite |
| 113 | +
|
| 114 | + Returns: |
| 115 | + Dict[str, Any]: Response containing status and result information |
| 116 | + """ |
| 117 | + try: |
| 118 | + job = event_data.get("job", {}) |
| 119 | + repo_name = event_data.get("pipeline", {}).get("repository", "").split("/")[-1] |
| 120 | + job_id = job.get("id", "") |
| 121 | + |
| 122 | + if not repo_name or not job_id: |
| 123 | + return { |
| 124 | + "statusCode": 400, |
| 125 | + "body": json.dumps({"message": "Missing repository name or job ID"}), |
| 126 | + } |
| 127 | + |
| 128 | + dynamo_key = f"{repo_name}/{job_id}" |
| 129 | + |
| 130 | + item = {"dynamoKey": dynamo_key, **event_data} |
| 131 | + |
| 132 | + job_events_table.put_item(Item=item) |
| 133 | + |
| 134 | + return { |
| 135 | + "statusCode": 200, |
| 136 | + "body": json.dumps( |
| 137 | + {"message": f"Job event saved successfully with key: {dynamo_key}"} |
| 138 | + ), |
| 139 | + } |
| 140 | + |
| 141 | + except ClientError as e: |
| 142 | + return { |
| 143 | + "statusCode": 500, |
| 144 | + "body": json.dumps({"message": f"DynamoDB error: {str(e)}"}), |
| 145 | + } |
| 146 | + except Exception as e: |
| 147 | + return { |
| 148 | + "statusCode": 500, |
| 149 | + "body": json.dumps({"message": f"Error saving job event: {str(e)}"}), |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | +def lambda_handler(event: Dict[str, Any], context: Any) -> Dict[str, Any]: |
| 154 | + """ |
| 155 | + Main Lambda handler function for Buildkite webhook events. |
| 156 | +
|
| 157 | + Args: |
| 158 | + event: Contains the webhook payload from Buildkite |
| 159 | + context: Provides runtime information about the Lambda function |
| 160 | +
|
| 161 | + Returns: |
| 162 | + Dict[str, Any]: Response containing status and result information |
| 163 | + """ |
| 164 | + try: |
| 165 | + if event.get("body"): |
| 166 | + body = json.loads(event["body"]) |
| 167 | + else: |
| 168 | + body = event |
| 169 | + |
| 170 | + event_type = body.get("event") |
| 171 | + |
| 172 | + if not event_type: |
| 173 | + return { |
| 174 | + "statusCode": 400, |
| 175 | + "body": json.dumps( |
| 176 | + {"message": "Missing event type in webhook payload"} |
| 177 | + ), |
| 178 | + } |
| 179 | + |
| 180 | + if event_type.startswith("agent."): |
| 181 | + return save_agent_event(body) |
| 182 | + elif event_type.startswith("build."): |
| 183 | + return save_build_event(body) |
| 184 | + elif event_type.startswith("job."): |
| 185 | + return save_job_event(body) |
| 186 | + else: |
| 187 | + return { |
| 188 | + "statusCode": 400, |
| 189 | + "body": json.dumps( |
| 190 | + {"message": f"Unsupported event type: {event_type}"} |
| 191 | + ), |
| 192 | + } |
| 193 | + |
| 194 | + except json.JSONDecodeError as e: |
| 195 | + return { |
| 196 | + "statusCode": 400, |
| 197 | + "body": json.dumps({"message": f"Invalid JSON payload: {str(e)}"}), |
| 198 | + } |
| 199 | + except Exception as e: |
| 200 | + return { |
| 201 | + "statusCode": 500, |
| 202 | + "body": json.dumps({"message": f"Unexpected error: {str(e)}"}), |
| 203 | + } |
0 commit comments