|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Deployment Monitor Hook |
| 4 | +# This hook monitors long-running deployment processes and notifies when complete |
| 5 | +# |
| 6 | +# Usage: Can be called after triggering a deployment to monitor its progress |
| 7 | +# |
| 8 | +# Configuration: |
| 9 | +# Set SLACK_WEBHOOK_URL environment variable or create .claude/hooks/config.sh |
| 10 | + |
| 11 | +set -e |
| 12 | + |
| 13 | +HOOK_NAME="deployment-monitor" |
| 14 | + |
| 15 | +# Load configuration if exists |
| 16 | +CONFIG_FILE="$(dirname "$0")/config.sh" |
| 17 | +if [ -f "$CONFIG_FILE" ]; then |
| 18 | + source "$CONFIG_FILE" |
| 19 | +fi |
| 20 | + |
| 21 | +# Function to check if a process is still running |
| 22 | +check_process() { |
| 23 | + local pid=$1 |
| 24 | + if ps -p "$pid" > /dev/null 2>&1; then |
| 25 | + return 0 # Process is running |
| 26 | + else |
| 27 | + return 1 # Process is not running |
| 28 | + fi |
| 29 | +} |
| 30 | + |
| 31 | +# Function to monitor deployment |
| 32 | +monitor_deployment() { |
| 33 | + local start_time=$(date +%s) |
| 34 | + local max_wait_time=3600 # 1 hour max |
| 35 | + |
| 36 | + echo "[$HOOK_NAME] Monitoring deployment process..." |
| 37 | + |
| 38 | + while [ $(($(date +%s) - start_time)) -lt $max_wait_time ]; do |
| 39 | + # Check if deployment is complete by looking for success indicators |
| 40 | + # This is a placeholder - adjust based on actual deployment markers |
| 41 | + |
| 42 | + # Sleep for 30 seconds before next check |
| 43 | + sleep 30 |
| 44 | + |
| 45 | + echo "[$HOOK_NAME] Deployment still in progress... (elapsed: $(($(date +%s) - start_time))s)" |
| 46 | + done |
| 47 | + |
| 48 | + echo "[$HOOK_NAME] Maximum wait time reached" |
| 49 | +} |
| 50 | + |
| 51 | +# Function to send Slack notification |
| 52 | +send_slack_notification() { |
| 53 | + local status=$1 |
| 54 | + local message=$2 |
| 55 | + local webhook_url=$3 |
| 56 | + |
| 57 | + if [ -z "$webhook_url" ]; then |
| 58 | + return 1 |
| 59 | + fi |
| 60 | + |
| 61 | + # Determine color based on status |
| 62 | + local color="good" |
| 63 | + local emoji=":white_check_mark:" |
| 64 | + if [[ "$status" == "FAILED" ]]; then |
| 65 | + color="danger" |
| 66 | + emoji=":x:" |
| 67 | + elif [[ "$status" == "COMPLETE" ]]; then |
| 68 | + color="good" |
| 69 | + emoji=":white_check_mark:" |
| 70 | + fi |
| 71 | + |
| 72 | + # Create JSON payload |
| 73 | + local payload=$(cat <<EOF |
| 74 | +{ |
| 75 | + "attachments": [ |
| 76 | + { |
| 77 | + "color": "$color", |
| 78 | + "title": "$emoji Maestro Deployment $status", |
| 79 | + "text": "$message", |
| 80 | + "footer": "Maestro Deployment Monitor", |
| 81 | + "ts": $(date +%s) |
| 82 | + } |
| 83 | + ] |
| 84 | +} |
| 85 | +EOF |
| 86 | +) |
| 87 | + |
| 88 | + # Send to Slack |
| 89 | + curl -X POST -H 'Content-type: application/json' \ |
| 90 | + --data "$payload" \ |
| 91 | + "$webhook_url" \ |
| 92 | + --silent --show-error |
| 93 | +} |
| 94 | + |
| 95 | +# Function to send notification |
| 96 | +notify_completion() { |
| 97 | + local status=$1 |
| 98 | + local message=$2 |
| 99 | + |
| 100 | + echo "" |
| 101 | + echo "==========================================" |
| 102 | + echo "[$HOOK_NAME] DEPLOYMENT $status" |
| 103 | + echo "Message: $message" |
| 104 | + echo "Time: $(date)" |
| 105 | + echo "==========================================" |
| 106 | + echo "" |
| 107 | + |
| 108 | + # Send Slack notification if webhook is configured |
| 109 | + if [ -n "$SLACK_WEBHOOK_URL" ]; then |
| 110 | + echo "[$HOOK_NAME] Sending Slack notification..." |
| 111 | + if send_slack_notification "$status" "$message" "$SLACK_WEBHOOK_URL"; then |
| 112 | + echo "[$HOOK_NAME] Slack notification sent successfully" |
| 113 | + else |
| 114 | + echo "[$HOOK_NAME] Failed to send Slack notification" |
| 115 | + fi |
| 116 | + fi |
| 117 | + |
| 118 | + # Also send system notification if available |
| 119 | + if command -v osascript &> /dev/null; then |
| 120 | + # macOS notification |
| 121 | + osascript -e "display notification \"$message\" with title \"Maestro Deployment $status\"" |
| 122 | + elif command -v notify-send &> /dev/null; then |
| 123 | + # Linux notification |
| 124 | + notify-send "Maestro Deployment $status" "$message" |
| 125 | + fi |
| 126 | +} |
| 127 | + |
| 128 | +# Main execution |
| 129 | +case "${1:-monitor}" in |
| 130 | + monitor) |
| 131 | + monitor_deployment |
| 132 | + ;; |
| 133 | + notify) |
| 134 | + notify_completion "${2:-COMPLETE}" "${3:-Deployment finished}" |
| 135 | + ;; |
| 136 | + *) |
| 137 | + echo "Usage: $0 {monitor|notify}" |
| 138 | + exit 1 |
| 139 | + ;; |
| 140 | +esac |
0 commit comments