|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +REQUEST_SIZE=${REQUEST_SIZE:-10000} |
| 4 | +TEST_PREFIX="dc-test-dc-v2-" |
| 5 | +PROD_PREFIX="dc-v2-" |
| 6 | + |
| 7 | +set -e |
| 8 | + |
| 9 | +update_test_index() { |
| 10 | + local index=$1 |
| 11 | + echo "Updating index: $index" |
| 12 | + TEST_INDEX=${TEST_PREFIX}${index} |
| 13 | + PROD_INDEX=${PROD_PREFIX}${index} |
| 14 | + |
| 15 | + echo -n "Retrieving test IDs..." |
| 16 | + ids=$(curl -s "http://localhost:9201/${TEST_INDEX}/_search?size=$REQUEST_SIZE&_source=id" | jq '[.hits.hits[]._id]') |
| 17 | + echo "Found $(echo "$ids" | jq 'length') IDs to update." |
| 18 | + |
| 19 | + echo "Building query..." |
| 20 | + query=$(jq -nc --argjson ids "$ids" --arg size "$REQUEST_SIZE" '{ "size": $size, "query": { "terms": { "id": $ids } } }') |
| 21 | + |
| 22 | + echo -n "Retrieving docs from production..." |
| 23 | + docs=$(curl -s -X POST "http://localhost:9202/${PROD_INDEX}/_search" -H 'Content-Type: application/json' -d "$query") |
| 24 | + echo "Retrieved $(echo "$docs" | jq '.hits.hits | length') documents." |
| 25 | + |
| 26 | + echo "Building bulk request..." |
| 27 | + timestamp=$(date +%s%3N) |
| 28 | + bulk=$(echo "$docs" | jq --arg test_index "$TEST_INDEX-$timestamp" -r ' |
| 29 | + .hits.hits[] | |
| 30 | + ({"index": {"_index": $test_index, "_id": ._id}} | tojson), |
| 31 | + (._source | tojson) |
| 32 | + ') |
| 33 | + bulk_file=$(mktemp) |
| 34 | + echo "$bulk" > "$bulk_file" |
| 35 | + |
| 36 | + echo "Getting index settings..." |
| 37 | + settings=$(curl -s "http://localhost:9202/${PROD_INDEX}" | jq '. | to_entries | .[0].value' | jq 'del(.aliases, .settings.index.creation_date, .settings.index.uuid, .settings.index.version, .settings.index.provided_name, .settings.index.default_pipeline)') |
| 38 | + |
| 39 | + if [[ -n "$UPDATE" ]]; then |
| 40 | + echo "Creating new index ${TEST_INDEX}-${timestamp}..." |
| 41 | + curl -s -X PUT "http://localhost:9201/${TEST_INDEX}-${timestamp}" -H 'Content-Type: application/json' -d "$settings" |
| 42 | + echo "" |
| 43 | + |
| 44 | + echo "Adding test data..." |
| 45 | + curl -s -X POST "http://localhost:9201/${TEST_INDEX}-${timestamp}/_bulk" \ |
| 46 | + -H 'Content-Type: application/x-ndjson' \ |
| 47 | + --data-binary "@$bulk_file" |
| 48 | + echo "" |
| 49 | + rm "$bulk_file" |
| 50 | + |
| 51 | + echo "Retargeting alias ${TEST_INDEX} to new index..." |
| 52 | + old_index=$(curl -s "http://localhost:9201/_alias/${TEST_INDEX}" | jq -r 'keys[0]') |
| 53 | + |
| 54 | + curl -s -X POST 'http://localhost:9201/_aliases' -H 'Content-Type: application/json' -d @- <<EOF |
| 55 | + { |
| 56 | + "actions": [ |
| 57 | + { "remove": { "index": "$old_index", "alias": "${TEST_INDEX}" } }, |
| 58 | + { "add": { "index": "${TEST_INDEX}-${timestamp}", "alias": "${TEST_INDEX}" } } |
| 59 | + ] |
| 60 | + } |
| 61 | +EOF |
| 62 | + echo "" |
| 63 | + else |
| 64 | + mkdir -p test-data |
| 65 | + echo "$bulk" > test-data/$index.ndjson |
| 66 | + echo "$settings" > test-data/$index-settings.json |
| 67 | + echo "Dry run complete. Re-run with UPDATE=1 to perform the update." |
| 68 | + fi |
| 69 | +} |
| 70 | + |
| 71 | +setup() { |
| 72 | + echo "Setting up..." |
| 73 | + PORT=9201 AWS_PROFILE=staging es-proxy start |
| 74 | + PORT=9202 AWS_PROFILE=production es-proxy start |
| 75 | +} |
| 76 | + |
| 77 | +cleanup() { |
| 78 | + echo "Cleaning up..." |
| 79 | + PORT=9201 AWS_PROFILE=staging es-proxy stop |
| 80 | + PORT=9202 AWS_PROFILE=production es-proxy stop |
| 81 | +} |
| 82 | +trap cleanup EXIT |
| 83 | +setup |
| 84 | +for index in collection work file-set; do |
| 85 | + update_test_index $index |
| 86 | +done |
0 commit comments