|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function to display help |
| 4 | +show_help() { |
| 5 | + echo "Usage: $0 [option] [license]" |
| 6 | + echo "Options:" |
| 7 | + echo " up - Start the clusters and apply the license" |
| 8 | + echo " down - Shut down the clusters" |
| 9 | + echo " help - Display this help message" |
| 10 | +} |
| 11 | + |
| 12 | +# Function to apply the license to a cluster |
| 13 | +apply_license() { |
| 14 | + local port=$1 |
| 15 | + local license="$2" |
| 16 | + local response_file=$(mktemp) |
| 17 | + local http_code |
| 18 | + http_code=$(curl -s -o "$response_file" -w "%{http_code}" -X PUT "http://localhost:$port/_license?pretty" -H "Content-Type: application/json" -d "$license") |
| 19 | + |
| 20 | + if [ "$http_code" -ne 200 ]; then |
| 21 | + echo "Failed to apply license to cluster on port $port. HTTP status code: $http_code" |
| 22 | + echo "Error response: $(cat "$response_file")" |
| 23 | + rm "$response_file" |
| 24 | + exit 1 |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +# Function to shut down the clusters |
| 29 | +shutdown_clusters() { |
| 30 | + docker compose --project-directory docker --profile ccr down |
| 31 | + echo "Clusters shut down." |
| 32 | +} |
| 33 | + |
| 34 | +# Check for options |
| 35 | +case "$1" in |
| 36 | + up) |
| 37 | + |
| 38 | + # Get the directory of the current script |
| 39 | + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 40 | + |
| 41 | + # Start the clusters |
| 42 | + docker compose --project-directory docker --profile ccr up -d |
| 43 | + |
| 44 | + # Wait for both clusters to be online |
| 45 | + export ES_PORT=9208 |
| 46 | + "$SCRIPT_DIR/poll-for-es" |
| 47 | + export ES_PORT=9209 |
| 48 | + "$SCRIPT_DIR/poll-for-es" |
| 49 | + |
| 50 | + # Apply the license to both clusters |
| 51 | + LICENSE=$2 |
| 52 | + if [ -z "$LICENSE" ]; then |
| 53 | + echo "License key is required as the second argument." |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + |
| 57 | + echo "Applying license to cluster on port 9208..." |
| 58 | + apply_license 9208 "$LICENSE" |
| 59 | + echo "Applying license to cluster on port 9209..." |
| 60 | + apply_license 9209 "$LICENSE" |
| 61 | + echo "License applied to both clusters." |
| 62 | + |
| 63 | + # Set up the remote connection between the clusters |
| 64 | + curl -X PUT "http://localhost:9209/_cluster/settings" -H "Content-Type: application/json" -d '{ |
| 65 | + "persistent": { |
| 66 | + "cluster": { |
| 67 | + "remote": { |
| 68 | + "cluster_one": { |
| 69 | + "seeds": ["es8.13:9300"] |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + }' |
| 75 | + |
| 76 | + echo "Clusters setup completed." |
| 77 | + ;; |
| 78 | + down) |
| 79 | + shutdown_clusters |
| 80 | + ;; |
| 81 | + help) |
| 82 | + show_help |
| 83 | + ;; |
| 84 | + *) |
| 85 | + echo "Invalid option: $1" |
| 86 | + show_help |
| 87 | + exit 1 |
| 88 | + ;; |
| 89 | +esac |
0 commit comments