|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Initialize Google Cloud Pub/Sub emulator with topics and subscriptions for Maestro. |
| 5 | +# |
| 6 | +# This script creates the necessary topics and subscriptions for the Maestro server |
| 7 | +# to communicate with agents using Pub/Sub. |
| 8 | +# |
| 9 | +# Environment Variables: |
| 10 | +# PUBSUB_EMULATOR_HOST: The emulator host (default: localhost:8085) |
| 11 | +# PUBSUB_PROJECT_ID: The GCP project ID (default: maestro-test) |
| 12 | +# CONSUMER_NAME: Optional consumer name for agent subscriptions |
| 13 | + |
| 14 | +PROJECT_ID="${PUBSUB_PROJECT_ID:-maestro-test}" |
| 15 | +EMULATOR_HOST="${PUBSUB_EMULATOR_HOST:-localhost:8085}" |
| 16 | +CONSUMER_NAME="${CONSUMER_NAME:-}" |
| 17 | + |
| 18 | +# Base URL for Pub/Sub emulator API |
| 19 | +BASE_URL="http://${EMULATOR_HOST}/v1" |
| 20 | + |
| 21 | +echo "Initializing Pub/Sub emulator at ${EMULATOR_HOST}" |
| 22 | +echo "Project ID: ${PROJECT_ID}" |
| 23 | + |
| 24 | +# Wait for emulator to be ready |
| 25 | +echo "Verifying emulator connectivity..." |
| 26 | +max_attempts=30 |
| 27 | +attempt=0 |
| 28 | +while [ $attempt -lt $max_attempts ]; do |
| 29 | + if curl -s --connect-timeout 2 --max-time 5 "http://${EMULATOR_HOST}" > /dev/null 2>&1; then |
| 30 | + echo "Emulator responded! Waiting for API to be fully ready..." |
| 31 | + sleep 2 |
| 32 | + echo "Emulator is ready!" |
| 33 | + break |
| 34 | + fi |
| 35 | + attempt=$((attempt + 1)) |
| 36 | + if [ $attempt -eq $max_attempts ]; then |
| 37 | + echo "Error: Could not connect to Pub/Sub emulator at http://${EMULATOR_HOST}" >&2 |
| 38 | + echo "Please ensure the emulator is running and accessible." >&2 |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + sleep 1 |
| 42 | +done |
| 43 | + |
| 44 | +# Function to create a topic |
| 45 | +create_topic() { |
| 46 | + local topic_name=$1 |
| 47 | + local topic_path="projects/${PROJECT_ID}/topics/${topic_name}" |
| 48 | + |
| 49 | + response=$(curl -s --connect-timeout 5 --max-time 10 -w "\n%{http_code}" -X PUT "${BASE_URL}/${topic_path}" 2>&1) |
| 50 | + http_code=$(echo "$response" | tail -n1) |
| 51 | + |
| 52 | + if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then |
| 53 | + echo " ✓ Created topic: ${topic_name}" |
| 54 | + return 0 |
| 55 | + elif [ "$http_code" -eq 409 ]; then |
| 56 | + echo " - Topic already exists: ${topic_name}" |
| 57 | + return 0 |
| 58 | + else |
| 59 | + echo " ✗ Error creating topic ${topic_name}: HTTP ${http_code}" >&2 |
| 60 | + echo " URL: ${BASE_URL}/${topic_path}" >&2 |
| 61 | + echo "$response" | head -n-1 >&2 |
| 62 | + return 1 |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +# Function to create a subscription |
| 67 | +create_subscription() { |
| 68 | + local sub_name=$1 |
| 69 | + local topic_name=$2 |
| 70 | + local filter_expr=$3 |
| 71 | + |
| 72 | + local sub_path="projects/${PROJECT_ID}/subscriptions/${sub_name}" |
| 73 | + local topic_path="projects/${PROJECT_ID}/topics/${topic_name}" |
| 74 | + |
| 75 | + # Build JSON payload using jq for proper JSON escaping |
| 76 | + local payload |
| 77 | + if [ -n "$filter_expr" ]; then |
| 78 | + payload=$(jq -n \ |
| 79 | + --arg topic "${topic_path}" \ |
| 80 | + --arg filter "${filter_expr}" \ |
| 81 | + '{topic: $topic, filter: $filter}') |
| 82 | + else |
| 83 | + payload=$(jq -n \ |
| 84 | + --arg topic "${topic_path}" \ |
| 85 | + '{topic: $topic}') |
| 86 | + fi |
| 87 | + |
| 88 | + response=$(curl -s --connect-timeout 5 --max-time 10 -w "\n%{http_code}" -X PUT \ |
| 89 | + -H "Content-Type: application/json" \ |
| 90 | + -d "$payload" \ |
| 91 | + "${BASE_URL}/${sub_path}" 2>&1) |
| 92 | + http_code=$(echo "$response" | tail -n1) |
| 93 | + |
| 94 | + if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then |
| 95 | + if [ -n "$filter_expr" ]; then |
| 96 | + echo " ✓ Created subscription: ${sub_name} (filtered by ${filter_expr})" |
| 97 | + else |
| 98 | + echo " ✓ Created subscription: ${sub_name}" |
| 99 | + fi |
| 100 | + return 0 |
| 101 | + elif [ "$http_code" -eq 409 ]; then |
| 102 | + echo " - Subscription already exists: ${sub_name}" |
| 103 | + return 0 |
| 104 | + else |
| 105 | + echo " ✗ Error creating subscription ${sub_name}: HTTP ${http_code}" >&2 |
| 106 | + echo " URL: ${BASE_URL}/${sub_path}" >&2 |
| 107 | + echo "$response" | head -n-1 >&2 |
| 108 | + return 1 |
| 109 | + fi |
| 110 | +} |
| 111 | + |
| 112 | +# Create topics |
| 113 | +echo "Creating topics..." |
| 114 | +topics=(sourceevents sourcebroadcast agentevents agentbroadcast) |
| 115 | +for topic in "${topics[@]}"; do |
| 116 | + if ! create_topic "$topic"; then |
| 117 | + echo "" >&2 |
| 118 | + echo "✗ Failed to initialize server topics" >&2 |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | +done |
| 122 | + |
| 123 | +# Create server subscriptions |
| 124 | +echo "" |
| 125 | +echo "Creating server subscriptions..." |
| 126 | +if ! create_subscription "agentevents-maestro" "agentevents" 'attributes."ce-originalsource"="maestro"'; then |
| 127 | + echo "" >&2 |
| 128 | + echo "✗ Failed to initialize server subscriptions" >&2 |
| 129 | + exit 1 |
| 130 | +fi |
| 131 | + |
| 132 | +if ! create_subscription "agentbroadcast-maestro" "agentbroadcast" ""; then |
| 133 | + echo "" >&2 |
| 134 | + echo "✗ Failed to initialize server subscriptions" >&2 |
| 135 | + exit 1 |
| 136 | +fi |
| 137 | + |
| 138 | +# Create agent subscriptions if consumer name is provided |
| 139 | +if [ -n "$CONSUMER_NAME" ]; then |
| 140 | + echo "" |
| 141 | + echo "Creating agent subscriptions for consumer '${CONSUMER_NAME}'..." |
| 142 | + |
| 143 | + if ! create_subscription "sourceevents-${CONSUMER_NAME}" "sourceevents" "attributes.\"ce-clustername\"=\"${CONSUMER_NAME}\""; then |
| 144 | + echo "" >&2 |
| 145 | + echo "✗ Failed to initialize agent subscriptions for ${CONSUMER_NAME}" >&2 |
| 146 | + exit 1 |
| 147 | + fi |
| 148 | + |
| 149 | + if ! create_subscription "sourcebroadcast-${CONSUMER_NAME}" "sourcebroadcast" ""; then |
| 150 | + echo "" >&2 |
| 151 | + echo "✗ Failed to initialize agent subscriptions for ${CONSUMER_NAME}" >&2 |
| 152 | + exit 1 |
| 153 | + fi |
| 154 | +fi |
| 155 | + |
| 156 | +echo "" |
| 157 | +echo "✓ Pub/Sub emulator initialized successfully!" |
0 commit comments