Skip to content

Instantly share code, notes, and snippets.

@jcantrill
Last active July 17, 2018 18:02
Show Gist options
  • Save jcantrill/84e3482333461e94c47f756c473d18b9 to your computer and use it in GitHub Desktop.
Save jcantrill/84e3482333461e94c47f756c473d18b9 to your computer and use it in GitHub Desktop.
Allocate all non-assigned primary shards
#!/bin/bash
# This script allocates all primary shards that are unassigned to a a given node using
# the openshift binary. The binary must be in the path and the user executing the script
# must have access to logging project. The inputs are:
# pod An Elasticsearch pod name
# node An node Elasticsearch cluster which should be any one of the DC's. We could
# probably infer this from the pod name
pod=$1
node=$2
read -r -d '' SCRIPT << EOF
import fileinput
import re
import sys
payload=''
node="${node}"
pattern = re.compile('(.*).*(\d).(r|p).([A-Z_]*)')
for line in fileinput.input():
m = pattern.match(line)
index = m.group(1).strip()
shard = m.group(2)
type = m.group(3)
if type == 'p':
if payload:
payload = payload + ","
payload = payload + '{"allocate":{"index":"%s","shard":%s,"node":"%s","allow_primary":"true"}}' % (index, shard, node)
payload = payload + "".format(index, shard, node)
payload = "{\"commands\":[" + payload + "]}"
sys.stdout.write(payload)
EOF
PAYLOAD=$(./unassigned $pod | python -c "${SCRIPT}")
#echo "$PAYLOAD"
oc exec -c elasticsearch $pod -- es_util --query=_cluster/reroute?pretty -XPOST -d ${PAYLOAD}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment