2021-05-04 11:24:09 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import curator
|
|
|
|
import elasticsearch
|
|
|
|
import os
|
|
|
|
import ssl
|
|
|
|
import sys
|
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
TIMEOUT = 120
|
|
|
|
|
2021-05-04 11:24:09 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
if len(sys.argv) != 3:
|
2021-10-06 11:09:10 +00:00
|
|
|
print(
|
|
|
|
'USAGE: [INDEX_PREFIX=(default "")] [ARCHIVE=(default false)] ... {} NUM_OF_DAYS http://HOSTNAME[:PORT]'.format(
|
|
|
|
sys.argv[0]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
"NUM_OF_DAYS ... delete indices that are older than the given number of days."
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
"HOSTNAME ... specifies which Elasticsearch hosts URL to search and delete indices from."
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
"TIMEOUT ... number of seconds to wait for master node response, default: {}".format(
|
|
|
|
TIMEOUT
|
|
|
|
)
|
|
|
|
)
|
|
|
|
print("INDEX_PREFIX ... specifies index prefix.")
|
|
|
|
print("INDEX_DATE_SEPARATOR ... specifies index date separator.")
|
|
|
|
print(
|
|
|
|
"ARCHIVE ... specifies whether to remove archive indices (only works for rollover) (default false)."
|
|
|
|
)
|
|
|
|
print(
|
|
|
|
"ROLLOVER ... specifies whether to remove indices created by rollover (default false)."
|
|
|
|
)
|
|
|
|
print("ES_USERNAME ... The username required by Elasticsearch.")
|
|
|
|
print("ES_PASSWORD ... The password required by Elasticsearch.")
|
|
|
|
print("ES_TLS ... enable TLS (default false).")
|
|
|
|
print("ES_TLS_CA ... Path to TLS CA file.")
|
|
|
|
print("ES_TLS_CERT ... Path to TLS certificate file.")
|
|
|
|
print("ES_TLS_KEY ... Path to TLS key file.")
|
|
|
|
print(
|
|
|
|
"ES_TLS_SKIP_HOST_VERIFY ... (insecure) Skip server's certificate chain and host name verification."
|
|
|
|
)
|
2021-05-04 11:24:09 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
client = create_client(
|
|
|
|
os.getenv("ES_USERNAME"),
|
|
|
|
os.getenv("ES_PASSWORD"),
|
|
|
|
str2bool(os.getenv("ES_TLS", "false")),
|
|
|
|
os.getenv("ES_TLS_CA"),
|
|
|
|
os.getenv("ES_TLS_CERT"),
|
|
|
|
os.getenv("ES_TLS_KEY"),
|
|
|
|
str2bool(os.getenv("ES_TLS_SKIP_HOST_VERIFY", "false")),
|
|
|
|
)
|
2021-05-04 11:24:09 +00:00
|
|
|
ilo = curator.IndexList(client)
|
2021-10-06 11:09:10 +00:00
|
|
|
empty_list(ilo, "Elasticsearch has no indices")
|
2021-05-04 11:24:09 +00:00
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
prefix = os.getenv("INDEX_PREFIX", "")
|
|
|
|
if prefix != "":
|
|
|
|
prefix += "-"
|
|
|
|
separator = os.getenv("INDEX_DATE_SEPARATOR", "-")
|
2021-05-04 11:24:09 +00:00
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
if str2bool(os.getenv("ARCHIVE", "false")):
|
2021-05-04 11:24:09 +00:00
|
|
|
filter_archive_indices_rollover(ilo, prefix)
|
|
|
|
else:
|
2021-10-06 11:09:10 +00:00
|
|
|
if str2bool(os.getenv("ROLLOVER", "false")):
|
2021-05-04 11:24:09 +00:00
|
|
|
filter_main_indices_rollover(ilo, prefix)
|
|
|
|
else:
|
|
|
|
filter_main_indices(ilo, prefix, separator)
|
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
empty_list(ilo, "No indices to delete")
|
2021-05-04 11:24:09 +00:00
|
|
|
|
|
|
|
for index in ilo.working_list():
|
|
|
|
print("Removing", index)
|
|
|
|
timeout = int(os.getenv("TIMEOUT", TIMEOUT))
|
|
|
|
delete_indices = curator.DeleteIndices(ilo, master_timeout=timeout)
|
|
|
|
delete_indices.do_action()
|
|
|
|
|
|
|
|
|
|
|
|
def filter_main_indices(ilo, prefix, separator):
|
|
|
|
date_regex = "\d{4}" + separator + "\d{2}" + separator + "\d{2}"
|
|
|
|
time_string = "%Y" + separator + "%m" + separator + "%d"
|
|
|
|
|
2021-10-06 11:09:10 +00:00
|
|
|
ilo.filter_by_regex(
|
|
|
|
kind="regex", value=prefix + "jaeger-(span|service|dependencies)-" + date_regex
|
|
|
|
)
|
2021-05-04 11:24:09 +00:00
|
|
|
empty_list(ilo, "No indices to delete")
|
|
|
|
# This excludes archive index as we use source='name'
|
|
|
|
# source `creation_date` would include archive index
|
2021-10-06 11:09:10 +00:00
|
|
|
ilo.filter_by_age(
|
|
|
|
source="name",
|
|
|
|
direction="older",
|
|
|
|
timestring=time_string,
|
|
|
|
unit="days",
|
|
|
|
unit_count=int(sys.argv[1]),
|
|
|
|
)
|
2021-05-04 11:24:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def filter_main_indices_rollover(ilo, prefix):
|
2021-10-06 11:09:10 +00:00
|
|
|
ilo.filter_by_regex(kind="regex", value=prefix + "jaeger-(span|service)-\d{6}")
|
2021-05-04 11:24:09 +00:00
|
|
|
empty_list(ilo, "No indices to delete")
|
|
|
|
# do not remove active write indices
|
2021-10-06 11:09:10 +00:00
|
|
|
ilo.filter_by_alias(aliases=[prefix + "jaeger-span-write"], exclude=True)
|
2021-05-04 11:24:09 +00:00
|
|
|
empty_list(ilo, "No indices to delete")
|
2021-10-06 11:09:10 +00:00
|
|
|
ilo.filter_by_alias(aliases=[prefix + "jaeger-service-write"], exclude=True)
|
2021-05-04 11:24:09 +00:00
|
|
|
empty_list(ilo, "No indices to delete")
|
2021-10-06 11:09:10 +00:00
|
|
|
ilo.filter_by_age(
|
|
|
|
source="creation_date",
|
|
|
|
direction="older",
|
|
|
|
unit="days",
|
|
|
|
unit_count=int(sys.argv[1]),
|
|
|
|
)
|
2021-05-04 11:24:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def filter_archive_indices_rollover(ilo, prefix):
|
|
|
|
# Remove only rollover archive indices
|
|
|
|
# Do not remove active write archive index
|
2021-10-06 11:09:10 +00:00
|
|
|
ilo.filter_by_regex(kind="regex", value=prefix + "jaeger-span-archive-\d{6}")
|
2021-05-04 11:24:09 +00:00
|
|
|
empty_list(ilo, "No indices to delete")
|
2021-10-06 11:09:10 +00:00
|
|
|
ilo.filter_by_alias(aliases=[prefix + "jaeger-span-archive-write"], exclude=True)
|
2021-05-04 11:24:09 +00:00
|
|
|
empty_list(ilo, "No indices to delete")
|
2021-10-06 11:09:10 +00:00
|
|
|
ilo.filter_by_age(
|
|
|
|
source="creation_date",
|
|
|
|
direction="older",
|
|
|
|
unit="days",
|
|
|
|
unit_count=int(sys.argv[1]),
|
|
|
|
)
|
2021-05-04 11:24:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def empty_list(ilo, error_msg):
|
|
|
|
try:
|
|
|
|
ilo.empty_list_check()
|
|
|
|
except curator.NoIndices:
|
|
|
|
print(error_msg)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
|
|
def str2bool(v):
|
2021-10-06 11:09:10 +00:00
|
|
|
return v.lower() in ("true", "1")
|
2021-05-04 11:24:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_client(username, password, tls, ca, cert, key, skipHostVerify):
|
|
|
|
context = ssl.create_default_context()
|
|
|
|
if ca is not None:
|
|
|
|
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=ca)
|
|
|
|
elif skipHostVerify:
|
|
|
|
context.check_hostname = False
|
|
|
|
context.verify_mode = ssl.CERT_NONE
|
|
|
|
if username is not None and password is not None:
|
2021-10-06 11:09:10 +00:00
|
|
|
return elasticsearch.Elasticsearch(
|
|
|
|
sys.argv[2:], http_auth=(username, password), ssl_context=context
|
|
|
|
)
|
2021-05-04 11:24:09 +00:00
|
|
|
elif tls:
|
|
|
|
context.load_cert_chain(certfile=cert, keyfile=key)
|
|
|
|
return elasticsearch.Elasticsearch(sys.argv[2:], ssl_context=context)
|
|
|
|
else:
|
|
|
|
return elasticsearch.Elasticsearch(sys.argv[2:], ssl_context=context)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|