2019-10-28 11:55:53 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
#set -x
|
|
|
|
|
|
|
|
function queryAPI() {
|
2020-05-28 21:06:36 +00:00
|
|
|
local method=$1
|
|
|
|
local url_path=$2
|
|
|
|
local payload=$3
|
|
|
|
cmd="curl -f -s -X${method} http://localhost:9200/${url_path}"
|
|
|
|
if [[ -n "${payload}" ]]; then
|
|
|
|
cmd+=" -H 'content-type:application/json' -d '${payload}'"
|
2019-10-28 11:55:53 +00:00
|
|
|
fi
|
|
|
|
eval "${cmd}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function create() {
|
|
|
|
queryAPI PUT "${1}" '{"settings":{"number_of_replicas":1,"number_of_shards": 1}}'
|
|
|
|
}
|
|
|
|
|
|
|
|
function reindex() {
|
|
|
|
queryAPI POST "_reindex" "{\"source\":{\"index\":\"${1}\"},\"dest\":{\"index\":\"${2}\"}}"
|
|
|
|
}
|
|
|
|
|
2020-05-27 07:23:12 +00:00
|
|
|
function clone() {
|
2020-05-27 09:53:19 +00:00
|
|
|
queryAPI POST "${1}/_clone/${2}" '{"settings":{"index.blocks.write":null}}'
|
2020-05-27 07:23:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:10:52 +00:00
|
|
|
function read_only() {
|
2020-05-27 09:53:19 +00:00
|
|
|
queryAPI PUT "${1}/_settings" '{"settings":{"index.blocks.write":"true"}}'
|
2020-05-27 09:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function wait_green() {
|
2020-05-27 09:53:19 +00:00
|
|
|
queryAPI GET "_cluster/health/${1}?wait_for_status=green&timeout=9000s"
|
2020-05-27 09:10:52 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 11:55:53 +00:00
|
|
|
function delete() {
|
|
|
|
queryAPI DELETE "${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function stats() {
|
|
|
|
queryAPI GET "${1}/_stats"
|
|
|
|
}
|
|
|
|
|
|
|
|
function docs_count() {
|
|
|
|
stats "${1}" | jq '._all.primaries.docs.count'
|
|
|
|
}
|
|
|
|
|
|
|
|
function size_in_bytes() {
|
|
|
|
stats "${1}" | jq '._all.primaries.store.size_in_bytes / (1000*1000) | floor'
|
|
|
|
}
|
|
|
|
|
|
|
|
index="$1"
|
|
|
|
if [[ -z "${index}" ]]; then
|
|
|
|
echo "First argument has to be an index name!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
old="${index}"
|
|
|
|
new="${index}-re"
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2020-05-27 07:23:12 +00:00
|
|
|
echo "*----------- ${old} ------------------------------"
|
|
|
|
echo "? $(printf '%-21s' ${old}) - Count: $(docs_count ${old})"
|
|
|
|
echo "? $(printf '%-21s' ${old}) - Size: $(size_in_bytes ${old}) MB"
|
2019-10-28 11:55:53 +00:00
|
|
|
echo "+ Creating: ${new}"
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2019-10-28 12:49:06 +00:00
|
|
|
create "${new}" > /dev/null
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2019-10-28 11:55:53 +00:00
|
|
|
echo "+ Reindexing: ${old} -> ${new}"
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2019-10-28 12:49:06 +00:00
|
|
|
reindex "${old}" "${new}" > /dev/null
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2020-05-27 07:23:12 +00:00
|
|
|
echo "? $(printf '%-21s' ${new}) - Count: $(docs_count ${new})"
|
|
|
|
echo "? $(printf '%-21s' ${new}) - Size: $(size_in_bytes ${new}) MB"
|
2019-10-28 11:55:53 +00:00
|
|
|
echo "! Deleting: ${old}"
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2019-10-28 12:49:06 +00:00
|
|
|
delete "${old}" > /dev/null
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2020-05-27 07:23:12 +00:00
|
|
|
echo "+ Cloning: ${new} -> ${old}"
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2020-05-27 09:10:52 +00:00
|
|
|
read_only "${new}" > /dev/null
|
2020-05-28 21:06:36 +00:00
|
|
|
clone "${new}" "${old}" > /dev/null
|
|
|
|
|
2020-05-27 09:53:19 +00:00
|
|
|
echo "+ Waiting: ${old}"
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2020-05-27 09:53:19 +00:00
|
|
|
wait_green "${old}" > /dev/null
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2020-05-27 07:23:12 +00:00
|
|
|
echo "? $(printf '%-21s' ${old}) - Count: $(docs_count ${old})"
|
|
|
|
echo "? $(printf '%-21s' ${old}) - Size: $(size_in_bytes ${old}) MB"
|
2019-10-28 11:55:53 +00:00
|
|
|
echo "! Deleting: ${new}"
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2019-10-28 12:49:06 +00:00
|
|
|
delete "${new}" > /dev/null
|
2020-05-28 21:06:36 +00:00
|
|
|
|
2020-05-27 07:23:12 +00:00
|
|
|
echo "*----------- ${old} ------------------------------"
|