Coverage for curator/actions/close.py: 100%

1"""Close index action class"""

2 import logging

3 from curator.helpers.testers import verify_index_list

4 from curator.helpers.utils import chunk_index_list, report_failure, show_dry_run, to_csv

5

6 class Close:

7 """Close Action Class"""

8 def __init__(self, ilo, delete_aliases=False, skip_flush=False):

9 """

10 :param ilo: An IndexList Object

11 :param delete_aliases: Delete any associated aliases before closing indices.

12 :param skip_flush: Do not flush indices before closing.

13

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

15 :type delete_aliases: bool

16 :type skip_flush: bool

17 """

18 verify_index_list(ilo)

19 #: The :py:class:`~.curator.indexlist.IndexList` object passed from param ``ilo``

20 self.index_list = ilo

21 #: The value passed as ``delete_aliases``

22 self.delete_aliases = delete_aliases

23 #: The value passed as ``skip_flush``

24 self.skip_flush = skip_flush

25 #: The :py:class:`~.elasticsearch.Elasticsearch` client object derived from

26 #: :py:attr:`index_list`

27 self.client = ilo.client

28 self.loggit = logging.getLogger('curator.actions.close')

29

30 def do_dry_run(self):

31 """Log what the output would be, but take no action."""

32 show_dry_run(self.index_list, 'close', **{'delete_aliases':self.delete_aliases})

33

34 def do_action(self):

35 """

36 :py:meth:`~.elasticsearch.client.IndicesClient.close` open indices in :py:attr:`index_list`

37 """

38 self.index_list.filter_closed()

39 self.index_list.empty_list_check()

40 self.loggit.info(

41 'Closing %s selected indices: %s',

42 len(self.index_list.indices), self.index_list.indices

43 )

44 try:

45 index_lists = chunk_index_list(self.index_list.indices)

46 for lst in index_lists:

47 lst_as_csv = to_csv(lst)

48 self.loggit.debug('CSV list of indices to close: %s', lst_as_csv)

49 if self.delete_aliases:

50 self.loggit.info('Deleting aliases from indices before closing.')

51 self.loggit.debug('Deleting aliases from: %s', lst)

52 try:

53 self.client.indices.delete_alias(index=lst_as_csv, name='_all')

54 self.loggit.debug('Deleted aliases from: %s', lst)

55 # pylint: disable=broad-except

56 except Exception as err:

57 self.loggit.warning(

58 'Some indices may not have had aliases. Exception: %s', err)

59 if not self.skip_flush:

60 self.client.indices.flush(index=lst_as_csv, ignore_unavailable=True, force=True)

61 self.client.indices.close(index=lst_as_csv, ignore_unavailable=True)

62 # pylint: disable=broad-except

63 except Exception as err:

64 report_failure(err)