Coverage for curator/helpers/utils.py: 100%
37 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-21 13:36 -0600
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-21 13:36 -0600
1"""Helper utilities
3The kind that don't fit in testers, getters, date_ops, or converters
4"""
5import logging
6from es_client.helpers.utils import ensure_list
7from curator.exceptions import FailedExecution
9def chunk_index_list(indices):
10 """
11 This utility chunks very large index lists into 3KB chunks.
12 It measures the size as a csv string, then converts back into a list for the return value.
14 :param indices: The list of indices
16 :type indices: list
18 :returns: A list of lists (each a piece of the original ``indices``)
19 :rtype: list
20 """
21 chunks = []
22 chunk = ""
23 for index in indices:
24 if len(chunk) < 3072:
25 if not chunk:
26 chunk = index
27 else:
28 chunk += "," + index
29 else:
30 chunks.append(chunk.split(','))
31 chunk = index
32 chunks.append(chunk.split(','))
33 return chunks
35def report_failure(exception):
36 """
37 Raise a :py:exc:`~.curator.exceptions.FailedExecution` exception and include the original error
38 message.
40 :param exception: The upstream exception.
42 :type exception: :py:exc:Exception
44 :rtype: None
45 """
46 raise FailedExecution(
47 f'Exception encountered. Rerun with loglevel DEBUG and/or check Elasticsearch logs for'
48 f'more information. Exception: {exception}'
49 )
51def show_dry_run(ilo, action, **kwargs):
52 """
53 Log dry run output with the action which would have been executed.
55 :param ilo: An IndexList Object
56 :param action: The ``action`` to be performed.
57 :param kwargs: Any other args to show in the log output
60 :type ilo: :py:class:`~.curator.indexlist.IndexList`
61 :type action: str
62 :type kwargs: dict
64 :rtype: None
65 """
66 logger = logging.getLogger(__name__)
67 logger.info('DRY-RUN MODE. No changes will be made.')
68 msg = f'(CLOSED) indices may be shown that may not be acted on by action "{action}".'
69 logger.info(msg)
70 indices = sorted(ilo.indices)
71 for idx in indices:
72 # Dry runs need index state, so we collect it here if it's not present.
73 try:
74 index_closed = ilo.index_info[idx]['state'] == 'close'
75 except KeyError:
76 ilo.get_index_state()
77 index_closed = ilo.index_info[idx]['state'] == 'close'
78 var = ' (CLOSED)' if index_closed else ''
79 msg = f'DRY-RUN: {action}: {idx}{var} with arguments: {kwargs}'
80 logger.info(msg)
82def to_csv(indices):
83 """
84 :param indices: A list of indices to act on, or a single value, which could be
85 in the format of a csv string already.
87 :type indices: list
89 :returns: A csv string from a list of indices, or a single value if only one value is present
90 :rtype: str
91 """
92 indices = ensure_list(indices) # in case of a single value passed
93 if indices:
94 return ','.join(sorted(indices))
95 return None