|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +ROOT="$(readlink -f "$(dirname "${0}")/../../../")" |
| 4 | + |
| 5 | +# shellcheck source=scripts/migration/lib.sh |
| 6 | +source "${ROOT}/scripts/migration/lib.sh" |
| 7 | + |
| 8 | +run() { |
| 9 | + case "${1:-}" in |
| 10 | + execute) |
| 11 | + |
| 12 | + if [[ "${CK8S_CLUSTER}" =~ ^(sc|both)$ ]]; then |
| 13 | + log_info "operation on service cluster" |
| 14 | + |
| 15 | + |
| 16 | + user="admin" |
| 17 | + password=$(sops --config "${CK8S_CONFIG_PATH}/.sops.yaml" -d "${CK8S_CONFIG_PATH}"/secrets.yaml | yq4 '.opensearch.adminPassword') |
| 18 | + os_url=https://opensearch.$(yq4 '.global.opsDomain' "${CK8S_CONFIG_PATH}"/common-config.yaml) |
| 19 | + |
| 20 | + resp=$(curl -sS -kL -u "${user}:${password}" -X GET "${os_url}"/.kibana_1) |
| 21 | + if [[ $(echo "${resp}" | jq -r 'to_entries | .[0].key') == ".kibana_1" ]]; then |
| 22 | + |
| 23 | + log_info "- Index .kibana_1 already exists, skipping" |
| 24 | + |
| 25 | + elif [[ $(echo "${resp}" | jq -r '.error.type') == "index_not_found_exception" ]]; then |
| 26 | + |
| 27 | + log_info "- Cloning index .opensearch_dashboards to .kibana" |
| 28 | + |
| 29 | + log_info "- Getting name of .opensearch_dashboards index" |
| 30 | + os_dashboards_index=$(curl -sS -kL -u "${user}:${password}" -X GET "${os_url}"/_alias/.opensearch_dashboards | jq -r 'to_entries | .[0].key') |
| 31 | + |
| 32 | + if [[ "${os_dashboards_index}" != .opensearch_dashboards* ]]; then |
| 33 | + log_fatal "Failed to get index name of the .opensearch_dashboards alias" |
| 34 | + fi |
| 35 | + |
| 36 | + log_info "- Marking index '${os_dashboards_index}' as read-only" |
| 37 | + resp=$(curl -sS -kL -u "${user}:${password}" -X PUT "${os_url}"/"${os_dashboards_index}"/_settings -H 'Content-Type: application/json' -d' |
| 38 | + { |
| 39 | + "settings": { |
| 40 | + "index.blocks.write": true |
| 41 | + } |
| 42 | + } |
| 43 | + ') |
| 44 | + |
| 45 | + acknowledged=$(echo "${resp}" | grep "^{" | jq -r '.acknowledged') |
| 46 | + if [ "${acknowledged}" = "true" ]; then |
| 47 | + log_info "- Marked '${os_dashboards_index}' as read-only" |
| 48 | + else |
| 49 | + log_fatal "Failed to mark index '${os_dashboards_index}' as read-only" "${resp}" |
| 50 | + fi |
| 51 | + |
| 52 | + log_info "- Cloning index '${os_dashboards_index}' to index .kibana_1" |
| 53 | + resp=$(curl -sS -kL -u "${user}:${password}" -X PUT "${os_url}"/"${os_dashboards_index}"/_clone/.kibana_1) |
| 54 | + acknowledged=$(echo "${resp}" | grep "^{" | jq -r '.acknowledged') |
| 55 | + if [ "${acknowledged}" = "true" ]; then |
| 56 | + log_info "- Successfully cloned '${os_dashboards_index}' to .kibana_1" |
| 57 | + else |
| 58 | + log_fatal "Failed to clone index '${os_dashboards_index}' to .kibana_1" "${resp}" |
| 59 | + fi |
| 60 | + |
| 61 | + |
| 62 | + log_info "- Disabling read-only mode for '${os_dashboards_index}'" |
| 63 | + resp=$(curl -sS -kL -u "${user}:${password}" -X PUT "${os_url}"/"${os_dashboards_index}"/_settings -H 'Content-Type: application/json' -d' |
| 64 | + { |
| 65 | + "settings": { |
| 66 | + "index.blocks.write": false |
| 67 | + } |
| 68 | + } |
| 69 | + ') |
| 70 | + acknowledged=$(echo "${resp}" | grep "^{" | jq -r '.acknowledged') |
| 71 | + if [ "${acknowledged}" = "true" ]; then |
| 72 | + log_info "- Successfully disabled read-only mode for '${os_dashboards_index}'" |
| 73 | + else |
| 74 | + log_fatal "Failed to disable read-only mode for '${os_dashboards_index}'" "${resp}" |
| 75 | + fi |
| 76 | + |
| 77 | + log_info "- Disabling read-only mode for .kibana_1" |
| 78 | + resp=$(curl -sS -kL -u "${user}:${password}" -X PUT "${os_url}"/.kibana_1/_settings -H 'Content-Type: application/json' -d' |
| 79 | + { |
| 80 | + "settings": { |
| 81 | + "index.blocks.write": false |
| 82 | + } |
| 83 | + } |
| 84 | + ') |
| 85 | + acknowledged=$(echo "${resp}" | grep "^{" | jq -r '.acknowledged') |
| 86 | + if [ "${acknowledged}" = "true" ]; then |
| 87 | + log_info "- Successfully disabled read-only mode for .kibana_1" |
| 88 | + else |
| 89 | + log_fatal "Failed to disable read-only mode for .kibana_1" "${resp}" |
| 90 | + fi |
| 91 | + else |
| 92 | + log_fatal "Failed to check if index .kibana_1 already exists" "${resp}" |
| 93 | + fi |
| 94 | + |
| 95 | + resp=$(curl -sS -kL -u "${user}:${password}" -X GET "${os_url}"/_alias/.kibana) |
| 96 | + if [[ $(echo "${resp}" | jq -r 'to_entries | .[0].key') == .kibana* ]]; then |
| 97 | + log_info "- Alias .kibana already exists, skipping" |
| 98 | + elif [[ $(echo "${resp}" | jq -r '.status') == "404" ]]; then |
| 99 | + log_info "- Creating alias .kibana" |
| 100 | + resp=$(curl -sS -kL -u "${user}:${password}" -X PUT "${os_url}"/.kibana_1/_aliases/.kibana) |
| 101 | + acknowledged=$(echo "${resp}" | grep "^{" | jq -r '.acknowledged') |
| 102 | + if [ "${acknowledged}" = "true" ]; then |
| 103 | + log_info "- Successfully created alias .kibana" |
| 104 | + else |
| 105 | + log_fatal "Failed to create alias .kibana" "${resp}" |
| 106 | + fi |
| 107 | + else |
| 108 | + log_fatal "Failed to check if alias .kibana exists" "${resp}" |
| 109 | + fi |
| 110 | + |
| 111 | + os_dashboards_index=$(curl -sS -kL -u "${user}:${password}" -X GET "${os_url}"/_alias/.opensearch_dashboards | jq -r 'to_entries | .[0].key') |
| 112 | + if [[ "${os_dashboards_index}" != .opensearch_dashboards* ]]; then |
| 113 | + log_info "- Skipping: Alias .opensearch_dashboards doesn't exist" |
| 114 | + else |
| 115 | + log_info "- Deleting index '${os_dashboards_index}'" |
| 116 | + resp=$(curl -sS -kL -u "${user}:${password}" -X DELETE "${os_url}"/"${os_dashboards_index}") |
| 117 | + acknowledged=$(echo "${resp}" | grep "^{" | jq -r '.acknowledged') |
| 118 | + if [ "${acknowledged}" = "true" ]; then |
| 119 | + log_info "- Successfully deleted index '${os_dashboards_index}'" |
| 120 | + else |
| 121 | + log_fatal "Failed to delete index '${os_dashboards_index}'" "${resp}" |
| 122 | + fi |
| 123 | + fi |
| 124 | + |
| 125 | + log_info "- Removing opensearch-configurer" |
| 126 | + helmfile_destroy sc name=opensearch-configurer |
| 127 | + log_info "- Upgrading Opensearch" |
| 128 | + helmfile_do sc -lapp=opensearch sync |
| 129 | + fi |
| 130 | + ;; |
| 131 | + rollback) |
| 132 | + log_warn "rollback not implemented" |
| 133 | + |
| 134 | + # if [[ "${CK8S_CLUSTER}" =~ ^(sc|both)$ ]]; then |
| 135 | + # log_info "rollback operation on service cluster" |
| 136 | + # fi |
| 137 | + # if [[ "${CK8S_CLUSTER}" =~ ^(wc|both)$ ]]; then |
| 138 | + # log_info "rollback operation on workload cluster" |
| 139 | + # fi |
| 140 | + ;; |
| 141 | + *) |
| 142 | + log_fatal "usage: \"${0}\" <execute|rollback>" |
| 143 | + ;; |
| 144 | + esac |
| 145 | +} |
| 146 | + |
| 147 | +run "${@}" |
0 commit comments