#!/bin/bash ## Sequence: # 1. Cleanup directory # 2. Start cluster (leader) # Copy opensearch.yml to expected location, start process in background. Fetch its pid. # 3. Wait until cluster becomes reachable # 4. Repeat step 2 and 3 for follower cluster # 5. Setup replication # Create index in leader cluster, setup connection-name on follower, start replication for index, check status of replication # 6. Start ISM action --> Create policy, attach policy to index if [[ -z $OPENSEARCH_HOME ]]; then echo 'Please set $OPENSEARCH_HOME before proceeding further' exit 1 fi // Setting required params leaderOsProtocol="127.0.0.1:9200" followerOsProtocol="127.0.0.1:9201" runtimeBaseLocation="$OPENSEARCH_HOME" configBaseLocation="$runtimeBaseLocation/config" leaderConfigFile="opensearch-leader.yml" followerConfigFile="opensearch-follower.yml" function runCluster { clusterMode=$1 if [[ $clusterMode == "leader" ]] then echo "Starting leader cluster" osProtocol=$leaderOsProtocol cp $configBaseLocation/$leaderConfigFile $configBaseLocation/opensearch.yml elif [[ $clusterMode == "follower" ]]; then echo "Starting follower cluster" osProtocol=$followerOsProtocol cp $configBaseLocation/$followerConfigFile $configBaseLocation/opensearch.yml fi cd $runtimeBaseLocation ./bin/opensearch > /dev/null & opensearch_pid=$! echo "Process id - $opensearch_pid" while true; do os_status=`curl $osProtocol/_cluster/health?wait_for_status=yellow -o /dev/null -s -w "%{http_code}\n"` if [[ "$os_status" -eq 200 ]]; then echo "$clusterMode cluster is ready at `date +'%Y-%m-%d %H:%M:%S:%3N'`" break else echo "Waiting..." sleep 8 fi done echo "Exiting.." } function startReplication { echo "Setup connection name" curl -XPUT -H 'Content-Type: application/json' $followerOsProtocol/_cluster/settings?pretty -d '{ "persistent": { "cluster": { "remote": { "my-connection-alias": { "seeds": ["127.0.0.1:9300"] } } } } }' ## Creating index curl -X PUT $leaderOsProtocol/$index/_doc/1 -H 'Content-Type: application/json' -d' { "user" : "user1", "post_date" : "2023-12-12T11:15:12", "message" : "trying out Opensearch" }' curl $leaderOsProtocol/_cat/indices?v echo "Start replication" curl -XPUT -H 'Content-Type: application/json' $followerOsProtocol/_plugins/_replication/$followerIndex/_start?pretty -d '{ "leader_alias": "my-connection-alias", "leader_index": "'"$index"'" }' curl $followerOsProtocol/_cat/indices?v echo "Check status of replication" curl -XGET $followerOsProtocol/_plugins/_replication/$followerIndex/_status?pretty } function startIsmPolicy { curl -XPUT "$followerOsProtocol/_plugins/_ism/policies/sample-policy-unfollow" -H 'Content-Type: application/json' -d' {"policy":{"description":"Unfollow and delete indices older than 5m","default_state":"active","states":[{"name":"active","actions":[],"transitions":[{"state_name":"inactivestate","conditions":{"min_index_age":"5m"}}]},{"name":"inactivestate","actions":[{"unfollow":{}},{"delete":{}}],"transitions":[]}],"ism_template":{"index_patterns":["*log*"],"priority":100}}}' curl -XPOST "$followerOsProtocol/_plugins/_ism/add/*$index" -H 'Content-Type: application/json' -d' { "policy_id": "sample-policy-unfollow" }' curl -w "\n" $followerOsProtocol/_plugins/_ism/explain/$followerIndex } function cleanupBuildDir { echo $1 dir="$runtimeBaseLocation/ccrtest/$1" echo "Cleaning up $dir .." rm -rf $dir/logs/* rm -rf $dir/data/* } // Executing the steps cleanupBuildDir leader cleanupBuildDir follower runCluster "leader" sleep 5 runCluster "follower" index="log-test-01.07.2024" followerIndex="follower-"$index startReplication startIsmPolicy