es_migration.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #! bin/bash
  2. CA=${1:-/etc/openshift/logging/ca.crt}
  3. KEY=${2:-/etc/openshift/logging/system.admin.key}
  4. CERT=${3:-/etc/openshift/logging/system.admin.crt}
  5. openshift_logging_es_host=${4:-logging-es}
  6. openshift_logging_es_port=${5:-9200}
  7. namespace=${6:-logging}
  8. # for each index in _cat/indices
  9. # skip indices that begin with . - .kibana, .operations, etc.
  10. # skip indices that contain a uuid
  11. # get a list of unique project
  12. # daterx - the date regex that matches the .%Y.%m.%d at the end of the indices
  13. # we are interested in - the awk will strip that part off
  14. function get_list_of_indices() {
  15. curl -s --cacert $CA --key $KEY --cert $CERT https://$openshift_logging_es_host:$openshift_logging_es_port/_cat/indices | \
  16. awk -v daterx='[.]20[0-9]{2}[.][0-1]?[0-9][.][0-9]{1,2}$' \
  17. '$3 !~ "^[.]" && $3 !~ "^[^.]+[.][^.]+"daterx && $3 !~ "^project." && $3 ~ daterx {print gensub(daterx, "", "", $3)}' | \
  18. sort -u
  19. }
  20. # for each index in _cat/indices
  21. # skip indices that begin with . - .kibana, .operations, etc.
  22. # get a list of unique project.uuid
  23. # daterx - the date regex that matches the .%Y.%m.%d at the end of the indices
  24. # we are interested in - the awk will strip that part off
  25. function get_list_of_proj_uuid_indices() {
  26. curl -s --cacert $CA --key $KEY --cert $CERT https://$openshift_logging_es_host:$openshift_logging_es_port/_cat/indices | \
  27. awk -v daterx='[.]20[0-9]{2}[.][0-1]?[0-9][.][0-9]{1,2}$' \
  28. '$3 !~ "^[.]" && $3 ~ "^[^.]+[.][^.]+"daterx && $3 !~ "^project." && $3 ~ daterx {print gensub(daterx, "", "", $3)}' | \
  29. sort -u
  30. }
  31. if [[ -z "$(oc get pods -l component=es -o jsonpath='{.items[?(@.status.phase == "Running")].metadata.name}')" ]]; then
  32. echo "No Elasticsearch pods found running. Cannot update common data model."
  33. exit 1
  34. fi
  35. count=$(get_list_of_indices | wc -l)
  36. if [ $count -eq 0 ]; then
  37. echo No matching indices found - skipping update_for_uuid
  38. else
  39. echo Creating aliases for $count index patterns . . .
  40. {
  41. echo '{"actions":['
  42. get_list_of_indices | \
  43. while IFS=. read proj ; do
  44. # e.g. make test.uuid.* an alias of test.* so we can search for
  45. # /test.uuid.*/_search and get both the test.uuid.* and
  46. # the test.* indices
  47. uid=$(oc get project "$proj" -o jsonpath='{.metadata.uid}' 2>/dev/null)
  48. [ -n "$uid" ] && echo "{\"add\":{\"index\":\"$proj.*\",\"alias\":\"$proj.$uuid.*\"}}"
  49. done
  50. echo ']}'
  51. } | curl -s --cacert $CA --key $KEY --cert $CERT -XPOST -d @- "https://$openshift_logging_es_host:$openshift_logging_es_port/_aliases"
  52. fi
  53. count=$(get_list_of_proj_uuid_indices | wc -l)
  54. if [ $count -eq 0 ] ; then
  55. echo No matching indexes found - skipping update_for_common_data_model
  56. exit 0
  57. fi
  58. echo Creating aliases for $count index patterns . . .
  59. # for each index in _cat/indices
  60. # skip indices that begin with . - .kibana, .operations, etc.
  61. # get a list of unique project.uuid
  62. # daterx - the date regex that matches the .%Y.%m.%d at the end of the indices
  63. # we are interested in - the awk will strip that part off
  64. {
  65. echo '{"actions":['
  66. get_list_of_proj_uuid_indices | \
  67. while IFS=. read proj uuid ; do
  68. # e.g. make project.test.uuid.* and alias of test.uuid.* so we can search for
  69. # /project.test.uuid.*/_search and get both the test.uuid.* and
  70. # the project.test.uuid.* indices
  71. echo "{\"add\":{\"index\":\"$proj.$uuid.*\",\"alias\":\"${PROJ_PREFIX}$proj.$uuid.*\"}}"
  72. done
  73. echo ']}'
  74. } | curl -s --cacert $CA --key $KEY --cert $CERT -XPOST -d @- "https://$openshift_logging_es_host:$openshift_logging_es_port/_aliases"