2021-08-06 12:59:39 +05:30
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
#
|
|
|
|
|
# The OpenSearch Contributors require contributions made to
|
|
|
|
|
# this file be licensed under the Apache-2.0 license or a
|
|
|
|
|
# compatible open source license.
|
|
|
|
|
#
|
|
|
|
|
# Modifications Copyright OpenSearch Contributors. See
|
|
|
|
|
# GitHub history for details.
|
|
|
|
|
#
|
2020-12-04 08:10:37 -06:00
|
|
|
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
|
|
|
# license agreements. See the NOTICE file distributed with
|
|
|
|
|
# this work for additional information regarding copyright
|
|
|
|
|
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
|
|
|
# the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing,
|
|
|
|
|
# software distributed under the License is distributed on an
|
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
# KIND, either express or implied. See the License for the
|
|
|
|
|
# specific language governing permissions and limitations
|
|
|
|
|
# under the License.
|
|
|
|
|
|
2022-10-04 00:15:18 +05:30
|
|
|
|
2020-12-04 08:10:37 -06:00
|
|
|
import time
|
2023-11-09 10:51:20 -05:00
|
|
|
from typing import Any
|
2021-01-13 14:21:04 -06:00
|
|
|
|
2021-09-16 14:59:29 +05:30
|
|
|
from opensearchpy import OpenSearch
|
2020-12-04 08:10:37 -06:00
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_cluster(client: Any) -> None:
|
2020-12-04 08:10:37 -06:00
|
|
|
"""Wipes a cluster clean between test cases"""
|
|
|
|
|
close_after_wipe = False
|
|
|
|
|
try:
|
|
|
|
|
# If client is async we need to replace the client
|
|
|
|
|
# with a synchronous one.
|
2021-09-16 14:59:29 +05:30
|
|
|
from opensearchpy import AsyncOpenSearch
|
2020-12-04 08:10:37 -06:00
|
|
|
|
2021-08-13 15:51:50 +05:30
|
|
|
if isinstance(client, AsyncOpenSearch):
|
|
|
|
|
client = OpenSearch(client.transport.hosts, verify_certs=False)
|
2020-12-04 08:10:37 -06:00
|
|
|
close_after_wipe = True
|
|
|
|
|
except ImportError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
wipe_snapshots(client)
|
|
|
|
|
wipe_indices(client)
|
|
|
|
|
|
2021-08-05 20:17:03 +05:30
|
|
|
client.indices.delete_template(name="*")
|
|
|
|
|
client.indices.delete_index_template(name="*")
|
|
|
|
|
client.cluster.delete_component_template(name="*")
|
2020-12-04 08:10:37 -06:00
|
|
|
|
|
|
|
|
wipe_cluster_settings(client)
|
|
|
|
|
|
|
|
|
|
wait_for_cluster_state_updates_to_finish(client)
|
|
|
|
|
if close_after_wipe:
|
|
|
|
|
client.close()
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_cluster_settings(client: Any) -> None:
|
2020-12-04 08:10:37 -06:00
|
|
|
settings = client.cluster.get_settings()
|
2023-11-09 10:51:20 -05:00
|
|
|
new_settings: Any = {}
|
2020-12-04 08:10:37 -06:00
|
|
|
for name, value in settings.items():
|
|
|
|
|
if value:
|
|
|
|
|
new_settings.setdefault(name, {})
|
2021-02-05 13:18:53 -06:00
|
|
|
for key in value.keys():
|
2020-12-04 08:10:37 -06:00
|
|
|
new_settings[name][key + ".*"] = None
|
|
|
|
|
if new_settings:
|
|
|
|
|
client.cluster.put_settings(body=new_settings)
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_snapshots(client: Any) -> None:
|
2020-12-04 08:10:37 -06:00
|
|
|
"""Deletes all the snapshots and repositories from the cluster"""
|
2021-07-13 19:57:34 -05:00
|
|
|
in_progress_snapshots = []
|
|
|
|
|
|
|
|
|
|
repos = client.snapshot.get_repository(repository="_all")
|
|
|
|
|
for repo_name, repo in repos.items():
|
2020-12-04 08:10:37 -06:00
|
|
|
if repo["type"] == "fs":
|
2021-07-13 19:57:34 -05:00
|
|
|
snapshots = client.snapshot.get(
|
|
|
|
|
repository=repo_name, snapshot="_all", ignore_unavailable=True
|
2020-12-04 08:10:37 -06:00
|
|
|
)
|
2021-07-13 19:57:34 -05:00
|
|
|
for snapshot in snapshots["snapshots"]:
|
|
|
|
|
if snapshot["state"] == "IN_PROGRESS":
|
|
|
|
|
in_progress_snapshots.append(snapshot)
|
|
|
|
|
else:
|
|
|
|
|
client.snapshot.delete(
|
|
|
|
|
repository=repo_name,
|
|
|
|
|
snapshot=snapshot["snapshot"],
|
|
|
|
|
ignore=404,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
client.snapshot.delete_repository(repository=repo_name, ignore=404)
|
2020-12-04 08:10:37 -06:00
|
|
|
|
2021-07-13 19:57:34 -05:00
|
|
|
assert in_progress_snapshots == []
|
2020-12-04 08:10:37 -06:00
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_data_streams(client: Any) -> None:
|
2021-07-13 19:57:34 -05:00
|
|
|
try:
|
|
|
|
|
client.indices.delete_data_stream(name="*", expand_wildcards="all")
|
|
|
|
|
except Exception:
|
|
|
|
|
client.indices.delete_data_stream(name="*")
|
2020-12-04 08:10:37 -06:00
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_indices(client: Any) -> None:
|
2020-12-04 08:10:37 -06:00
|
|
|
client.indices.delete(
|
2021-04-22 12:08:05 -05:00
|
|
|
index="*,-.ds-ilm-history-*",
|
2020-12-04 08:10:37 -06:00
|
|
|
expand_wildcards="all",
|
|
|
|
|
ignore=404,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_searchable_snapshot_indices(client: Any) -> None:
|
2021-07-13 19:57:34 -05:00
|
|
|
cluster_metadata = client.cluster.state(
|
|
|
|
|
metric="metadata",
|
|
|
|
|
filter_path="metadata.indices.*.settings.index.store.snapshot",
|
|
|
|
|
)
|
|
|
|
|
if cluster_metadata:
|
|
|
|
|
for index in cluster_metadata["metadata"]["indices"].keys():
|
|
|
|
|
client.indices.delete(index=index)
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_slm_policies(client: Any) -> None:
|
2020-12-04 08:10:37 -06:00
|
|
|
for policy in client.slm.get_lifecycle():
|
|
|
|
|
client.slm.delete_lifecycle(policy_id=policy["name"])
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_auto_follow_patterns(client: Any) -> None:
|
2020-12-04 08:10:37 -06:00
|
|
|
for pattern in client.ccr.get_auto_follow_pattern()["patterns"]:
|
|
|
|
|
client.ccr.delete_auto_follow_pattern(name=pattern["name"])
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_node_shutdown_metadata(client: Any) -> None:
|
2021-07-13 19:57:34 -05:00
|
|
|
shutdown_status = client.shutdown.get_node()
|
|
|
|
|
# If response contains these two keys the feature flag isn't enabled
|
|
|
|
|
# on this cluster so skip this step now.
|
|
|
|
|
if "_nodes" in shutdown_status and "cluster_name" in shutdown_status:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for shutdown_node in shutdown_status.get("nodes", []):
|
|
|
|
|
node_id = shutdown_node["node_id"]
|
|
|
|
|
client.shutdown.delete_node(node_id=node_id)
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wipe_tasks(client: Any) -> None:
|
2020-12-04 08:10:37 -06:00
|
|
|
tasks = client.tasks.list()
|
2024-01-25 18:17:09 -05:00
|
|
|
for _, node in tasks.get("node", {}).items():
|
2020-12-04 08:10:37 -06:00
|
|
|
for task_id in node.get("tasks", ()):
|
|
|
|
|
client.tasks.cancel(task_id=task_id, wait_for_completion=True)
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wait_for_pending_tasks(client: Any, filter: Any, timeout: int = 30) -> None:
|
2020-12-04 08:10:37 -06:00
|
|
|
end_time = time.time() + timeout
|
|
|
|
|
while time.time() < end_time:
|
|
|
|
|
tasks = client.cat.tasks(detailed=True).split("\n")
|
|
|
|
|
if not any(filter in task for task in tasks):
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wait_for_pending_datafeeds_and_jobs(client: Any, timeout: int = 30) -> None:
|
2021-07-13 19:57:34 -05:00
|
|
|
end_time = time.time() + timeout
|
|
|
|
|
while time.time() < end_time:
|
|
|
|
|
if (
|
|
|
|
|
client.ml.get_datafeeds(datafeed_id="*", allow_no_datafeeds=True)["count"]
|
|
|
|
|
== 0
|
|
|
|
|
):
|
|
|
|
|
break
|
|
|
|
|
while time.time() < end_time:
|
|
|
|
|
if client.ml.get_jobs(job_id="*", allow_no_jobs=True)["count"] == 0:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
2023-11-09 10:51:20 -05:00
|
|
|
def wait_for_cluster_state_updates_to_finish(client: Any, timeout: int = 30) -> None:
|
2020-12-04 08:10:37 -06:00
|
|
|
end_time = time.time() + timeout
|
|
|
|
|
while time.time() < end_time:
|
|
|
|
|
if not client.cluster.pending_tasks().get("tasks", ()):
|
|
|
|
|
break
|