|
1
|
|
|
# pylint: disable=no-member |
|
2
|
|
|
|
|
3
|
|
|
from curator.api.utils import * # noqa |
|
4
|
|
|
from curator.api.filter import * # noqa |
|
5
|
|
|
from items_filter import ItemsFilter |
|
6
|
|
|
from easydict import EasyDict |
|
7
|
|
|
from utils import xstr |
|
8
|
|
|
import sys |
|
9
|
|
|
import logging |
|
10
|
|
|
|
|
11
|
|
|
logger = logging.getLogger(__name__) |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class ItemsSelector(object): |
|
15
|
|
|
|
|
16
|
|
|
def __init__(self, client, **opts): |
|
17
|
|
|
self.opts = EasyDict(opts) |
|
18
|
|
|
self.client = client |
|
19
|
|
|
self.ifilter = ItemsFilter().build(**opts) |
|
20
|
|
|
|
|
21
|
|
|
def _apply_filters(self, source_items, act_on): |
|
22
|
|
|
"""Applies filters to a list of indices or snapshots. |
|
23
|
|
|
:param source_items: List of indices or snapshots. |
|
24
|
|
|
:param act_on: Specifies whether we act on indices or snapshots. |
|
25
|
|
|
""" |
|
26
|
|
|
opts = self.opts |
|
27
|
|
|
all_items_selected = opts.get('all_{0}'.format(act_on), None) |
|
28
|
|
|
|
|
29
|
|
|
# Choose explicitly chosen indices or snapshots |
|
30
|
|
|
# |
|
31
|
|
|
if act_on == 'indices': |
|
32
|
|
|
explicit_items = opts.index or [] |
|
33
|
|
|
else: |
|
34
|
|
|
explicit_items = opts.snapshot or [] |
|
35
|
|
|
|
|
36
|
|
|
# I don't care about using only timestring if it's a `dry_run` of show |
|
37
|
|
|
if not any((xstr(opts.newer_than), xstr(opts.older_than), opts.dry_run)) and \ |
|
38
|
|
|
opts.timestring: |
|
39
|
|
|
logger.warn('Used only timestring parameter.') |
|
40
|
|
|
logger.warn('Actions can be performed on all %s matching %s', act_on, opts.timestring) |
|
41
|
|
|
|
|
42
|
|
|
logger.debug("Full list of %s: %s", act_on, source_items) |
|
43
|
|
|
|
|
44
|
|
|
if not source_items: |
|
45
|
|
|
print 'ERROR. No {0} found in Elasticsearch.'.format(act_on) |
|
46
|
|
|
sys.exit(1) |
|
47
|
|
|
else: |
|
48
|
|
|
working_list = source_items |
|
49
|
|
|
|
|
50
|
|
|
# No filters has been added and not all items selected, |
|
51
|
|
|
# this means index or snapshot parameter is used alone. |
|
52
|
|
|
if not all_items_selected and not self.ifilter.filter_list: |
|
53
|
|
|
working_list = [] |
|
54
|
|
|
else: |
|
55
|
|
|
# Otherwise safely apply filtering |
|
56
|
|
|
working_list = self.ifilter.apply(working_list, act_on=act_on) |
|
57
|
|
|
|
|
58
|
|
|
# Include explict items into resulting working list. |
|
59
|
|
|
if explicit_items: |
|
60
|
|
|
working_list.extend((i for i in explicit_items if i in source_items)) |
|
61
|
|
|
|
|
62
|
|
|
if not working_list: |
|
63
|
|
|
logger.error('No %s matched provided args: %s', act_on, opts) |
|
64
|
|
|
print "ERROR. No {} found in Elasticsearch.".format(act_on) |
|
65
|
|
|
sys.exit(99) |
|
66
|
|
|
|
|
67
|
|
|
# Make a sorted, unique list of indices/snapshots |
|
68
|
|
|
return sorted(list(set(working_list))) |
|
69
|
|
|
|
|
70
|
|
|
def snapshots(self, on_nofilters_showall=False): |
|
71
|
|
|
""" |
|
72
|
|
|
Get a list of snapshots to act on from the provided arguments. |
|
73
|
|
|
""" |
|
74
|
|
|
if not any((self.opts.all_snapshots, self.opts.snapshot, self.ifilter.filter_list)): |
|
75
|
|
|
if on_nofilters_showall: |
|
76
|
|
|
self.opts.all_snapshots = True |
|
77
|
|
|
else: |
|
78
|
|
|
print 'Error: At least one snapshot filter parameter must be provided!' |
|
79
|
|
|
sys.exit(1) |
|
80
|
|
|
|
|
81
|
|
|
if not self.opts.repository: |
|
82
|
|
|
print 'Missing required parameter: repository.' |
|
83
|
|
|
sys.exit(1) |
|
84
|
|
|
|
|
85
|
|
|
# Get a master-list of snapshots |
|
86
|
|
|
snapshots = get_snapshots(self.client, repository=self.opts.repository) |
|
87
|
|
|
return self._apply_filters(snapshots, act_on='snapshots') |
|
88
|
|
|
|
|
89
|
|
|
def indices(self, on_nofilters_showall=False): |
|
90
|
|
|
""" |
|
91
|
|
|
Get a list of indices to act on from the provided arguments. |
|
92
|
|
|
""" |
|
93
|
|
|
# Check if we have selection to operate |
|
94
|
|
|
if not any((self.opts.all_indices, self.opts.index, self.ifilter.filter_list)): |
|
95
|
|
|
if on_nofilters_showall: |
|
96
|
|
|
self.opts.all_indices = True |
|
97
|
|
|
else: |
|
98
|
|
|
print 'Error: At least one index filter parameter must be provided!' |
|
99
|
|
|
sys.exit(1) |
|
100
|
|
|
|
|
101
|
|
|
# Get a master-list of indices |
|
102
|
|
|
indices = get_indices(self.client) |
|
103
|
|
|
return self._apply_filters(indices, act_on='indices') |
|
104
|
|
|
|
|
105
|
|
|
def fetch(self, act_on, on_nofilters_showall=False): |
|
106
|
|
|
if act_on not in ['indices', 'snapshots']: |
|
107
|
|
|
raise ValueError('invalid argument: %s', act_on) |
|
108
|
|
|
|
|
109
|
|
|
if act_on == 'indices': |
|
110
|
|
|
return self.indices(on_nofilters_showall=on_nofilters_showall) # noqa |
|
111
|
|
|
else: |
|
112
|
|
|
return self.snapshots(on_nofilters_showall=on_nofilters_showall) # noqa |
|
113
|
|
|
|