Skip to content

Instantly share code, notes, and snippets.

@ThePsyjo
Last active February 17, 2022 15:04
Show Gist options
  • Save ThePsyjo/e046e1b452db14b15ee643b4a0c21224 to your computer and use it in GitHub Desktop.
Save ThePsyjo/e046e1b452db14b15ee643b4a0c21224 to your computer and use it in GitHub Desktop.
fix_policies.sh
# ~/.es has to contain {"username": "...", "password": "...", "endpoint": "https://..."}
es_config(){ jq -r ".$1" < ~/.es; }
es_api ()
{
url=$1;
shift;
curl -ku $(es_config username):$(es_config password) -s "$(es_config endpoint):9200/${url}" "$@"
}
plist="policy1 policy2"
#plist="test_policy"
for p in ${plist}
do
[[ -z "${p}" ]] && continue
# echo "=== $p"
policy="$(es_api _opendistro/_ism/policies/${p})"
# echo "${policy}"
patterns="$(jq -r '.policy | select(.ism_template != null) | .ism_template.index_patterns[]' <<< "${policy}" | paste -sd,)"
[[ -z "$patterns" ]] && { echo "no index patterns found in ${p}"; continue; }
# echo "checking patterns '$patterns'..."
for idx in $(es_api "_opendistro/_ism/explain/${patterns}" | jq -r 'if .error then empty else . end | to_entries[] | select(.value."index.opendistro.index_state_management.policy_id" == null) | .key')
do
echo -n "$idx is missing policy ${p}"
if [[ "$1" == "go" ]]
then
echo -n ', fixing... '
es_api "_opendistro/_ism/add/${idx}" -XPOST -H 'Content-type: application/json' -d "{\"policy_id\": \"${p}\"}"
echo ' done'
else
echo
fi
done
done
@Venka619
Copy link

Heyy @ThePsyjo, I was able to run on the Elasticsearch master node, I was thinking to automate script on my kubernetes cluster. Did you try deploying and automating the script. If yes can you help me out with the same

@ThePsyjo
Copy link
Author

hey @Venka619, you can add this script to a curl image and just schedule it as a cronjob

apiVersion: batch/v1
kind: CronJob
...

Just make sure that both curl and jq are available in the image.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment