|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Initialize Google Cloud Pub/Sub emulator with topics and subscriptions for Maestro. |
| 4 | +
|
| 5 | +This script creates the necessary topics and subscriptions for the Maestro server |
| 6 | +to communicate with agents using Pub/Sub. |
| 7 | +
|
| 8 | +Environment Variables: |
| 9 | + PUBSUB_EMULATOR_HOST: The emulator host (default: localhost:8085) |
| 10 | + PUBSUB_PROJECT_ID: The GCP project ID (default: maestro-test) |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | +from google.cloud import pubsub_v1 |
| 16 | +from google.api_core import exceptions |
| 17 | + |
| 18 | + |
| 19 | +def init_server_topics_and_subscriptions(project_id: str): |
| 20 | + """Initialize topics and subscriptions for the Maestro server.""" |
| 21 | + publisher = pubsub_v1.PublisherClient() |
| 22 | + subscriber = pubsub_v1.SubscriberClient() |
| 23 | + |
| 24 | + # Topics to create |
| 25 | + topics = ['sourceevents', 'sourcebroadcast', 'agentevents', 'agentbroadcast'] |
| 26 | + |
| 27 | + print("Creating topics...") |
| 28 | + for topic_name in topics: |
| 29 | + topic_path = publisher.topic_path(project_id, topic_name) |
| 30 | + try: |
| 31 | + publisher.create_topic(request={"name": topic_path}) |
| 32 | + print(f" ✓ Created topic: {topic_name}") |
| 33 | + except exceptions.AlreadyExists: |
| 34 | + print(f" - Topic already exists: {topic_name}") |
| 35 | + except Exception as e: |
| 36 | + print(f" ✗ Error creating topic {topic_name}: {e}", file=sys.stderr) |
| 37 | + return False |
| 38 | + |
| 39 | + # Server subscriptions to create (name:topic:filter) |
| 40 | + subscriptions = [ |
| 41 | + ('agentevents-maestro', 'agentevents', 'attributes.ce-originalsource="maestro"'), |
| 42 | + ('agentbroadcast-maestro', 'agentbroadcast', '') |
| 43 | + ] |
| 44 | + |
| 45 | + print("\nCreating server subscriptions...") |
| 46 | + for sub_name, topic_name, filter_expr in subscriptions: |
| 47 | + subscription_path = subscriber.subscription_path(project_id, sub_name) |
| 48 | + topic_path = publisher.topic_path(project_id, topic_name) |
| 49 | + try: |
| 50 | + if filter_expr: |
| 51 | + subscriber.create_subscription( |
| 52 | + request={"name": subscription_path, "topic": topic_path, "filter": filter_expr} |
| 53 | + ) |
| 54 | + print(f" ✓ Created subscription: {sub_name} (filtered by {filter_expr})") |
| 55 | + else: |
| 56 | + subscriber.create_subscription( |
| 57 | + request={"name": subscription_path, "topic": topic_path} |
| 58 | + ) |
| 59 | + print(f" ✓ Created subscription: {sub_name}") |
| 60 | + except exceptions.AlreadyExists: |
| 61 | + print(f" - Subscription already exists: {sub_name}") |
| 62 | + except Exception as e: |
| 63 | + print(f" ✗ Error creating subscription {sub_name}: {e}", file=sys.stderr) |
| 64 | + return False |
| 65 | + |
| 66 | + return True |
| 67 | + |
| 68 | + |
| 69 | +def init_agent_subscriptions(project_id: str, consumer_name: str): |
| 70 | + """Initialize subscriptions for a Maestro agent.""" |
| 71 | + publisher = pubsub_v1.PublisherClient() |
| 72 | + subscriber = pubsub_v1.SubscriberClient() |
| 73 | + |
| 74 | + # Agent subscriptions to create: (subscription_name, topic_name, filter) |
| 75 | + subscriptions = [ |
| 76 | + ( |
| 77 | + f'sourceevents-{consumer_name}', |
| 78 | + 'sourceevents', |
| 79 | + f'attributes.ce-clustername="{consumer_name}"' |
| 80 | + ), |
| 81 | + ( |
| 82 | + f'sourcebroadcast-{consumer_name}', |
| 83 | + 'sourcebroadcast', |
| 84 | + '' # No filter for broadcast |
| 85 | + ) |
| 86 | + ] |
| 87 | + |
| 88 | + print(f"\nCreating agent subscriptions for consumer '{consumer_name}'...") |
| 89 | + for sub_name, topic_name, filter_expr in subscriptions: |
| 90 | + subscription_path = subscriber.subscription_path(project_id, sub_name) |
| 91 | + topic_path = publisher.topic_path(project_id, topic_name) |
| 92 | + |
| 93 | + try: |
| 94 | + if filter_expr: |
| 95 | + subscriber.create_subscription( |
| 96 | + request={ |
| 97 | + "name": subscription_path, |
| 98 | + "topic": topic_path, |
| 99 | + "filter": filter_expr |
| 100 | + } |
| 101 | + ) |
| 102 | + print(f" ✓ Created subscription: {sub_name} (filtered)") |
| 103 | + else: |
| 104 | + subscriber.create_subscription( |
| 105 | + request={ |
| 106 | + "name": subscription_path, |
| 107 | + "topic": topic_path |
| 108 | + } |
| 109 | + ) |
| 110 | + print(f" ✓ Created subscription: {sub_name}") |
| 111 | + except exceptions.AlreadyExists: |
| 112 | + print(f" - Subscription already exists: {sub_name}") |
| 113 | + except Exception as e: |
| 114 | + print(f" ✗ Error creating subscription {sub_name}: {e}", file=sys.stderr) |
| 115 | + return False |
| 116 | + |
| 117 | + return True |
| 118 | + |
| 119 | + |
| 120 | +def main(): |
| 121 | + project_id = os.getenv('PUBSUB_PROJECT_ID', 'maestro-test') |
| 122 | + emulator_host = os.getenv('PUBSUB_EMULATOR_HOST', 'localhost:8085') |
| 123 | + consumer_name = os.getenv('CONSUMER_NAME', '') |
| 124 | + |
| 125 | + print(f"Initializing Pub/Sub emulator at {emulator_host}") |
| 126 | + print(f"Project ID: {project_id}") |
| 127 | + |
| 128 | + # Initialize server topics and subscriptions |
| 129 | + if not init_server_topics_and_subscriptions(project_id): |
| 130 | + print("\n✗ Failed to initialize server topics and subscriptions", file=sys.stderr) |
| 131 | + sys.exit(1) |
| 132 | + |
| 133 | + # Initialize agent subscriptions if consumer name is provided |
| 134 | + if consumer_name: |
| 135 | + if not init_agent_subscriptions(project_id, consumer_name): |
| 136 | + print(f"\n✗ Failed to initialize agent subscriptions for {consumer_name}", file=sys.stderr) |
| 137 | + sys.exit(1) |
| 138 | + |
| 139 | + print("\n✓ Pub/Sub emulator initialized successfully!") |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == '__main__': |
| 143 | + try: |
| 144 | + main() |
| 145 | + except Exception as e: |
| 146 | + print(f"✗ Unexpected error: {e}", file=sys.stderr) |
| 147 | + sys.exit(1) |
0 commit comments