Coverage for curator/helpers/utils.py: 100%

1"""Helper utilities

2

3 The kind that don't fit in testers, getters, date_ops, or converters

4"""

5 import logging

6 from es_client.helpers.utils import ensure_list

7 from curator.exceptions import FailedExecution

8

9 def 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.

13

14 :param indices: The list of indices

15

16 :type indices: list

17

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

34

35 def report_failure(exception):

36 """

37 Raise a :py:exc:`~.curator.exceptions.FailedExecution` exception and include the original error

38 message.

39

40 :param exception: The upstream exception.

41

42 :type exception: :py:exc:Exception

43

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 )

50

51 def show_dry_run(ilo, action, **kwargs):

52 """

53 Log dry run output with the action which would have been executed.

54

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

58

59

60 :type ilo: :py:class:`~.curator.indexlist.IndexList`

61 :type action: str

62 :type kwargs: dict

63

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)

81

82 def 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.

86

87 :type indices: list

88

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