Skip to content

Instantly share code, notes, and snippets.

@jinnko
Last active November 1, 2023 09:36
Show Gist options
  • Save jinnko/78072a77c8302390ff0830963492e9f4 to your computer and use it in GitHub Desktop.
Save jinnko/78072a77c8302390ff0830963492e9f4 to your computer and use it in GitHub Desktop.
Script to open elasticsearch indices, allow them to complete verification or shard redistribution, then close the index.
#!/bin/dash -e
# vim:ts=2 sw=2 sts=2 expandtab:
# How long to wait for an opened index to become green
TIMEOUT=3600
DELAY=5
say() {
echo
echo "\033[32m===> \033[1;37m${1}\033[0m"
}
wait_for_green() {
IDX="${1}"
START_TIME=$(date +%s)
printf " -> Waiting for the index status to turn green."
while [ "$(curl -s "localhost:9200/_cat/indices/${IDX}?h=health" | awk '{print $1}')" != "green" ]; do
RUNNING_TIME=$(( $(date +%s) - START_TIME ))
if [ $RUNNING_TIME -ge $TIMEOUT ]; then
printf "\033[31mTIMEOUT WAITING FOR %d\033[0m .. " "${IDX}"
break
fi
printf '.'
sleep $DELAY
done
echo "$RUNNING_TIME seconds to complete."
}
open_index() {
IDX="${1}"
printf " * Opening index .. "
curl -s -XPOST "localhost:9200/${IDX}/_open"
echo
}
close_index() {
IDX="${1}"
printf " * Closing index .. "
curl -s -XPOST "localhost:9200/${IDX}/_close"
echo "${IDX}" >> /tmp/es-verify-done.out
echo
}
check_index() {
IDX="${1}"
say "Checking ${IDX}"
open_index "${IDX}"
wait_for_green "${IDX}"
close_index "${IDX}"
}
curl -s 'localhost:9200/_cat/indices?h=status,index' | grep -v '^open' | sort -nr | while read -r _ INDEX; do
check_index "$INDEX"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment