Completed
Push — master ( b80751...9b4ac9 )
by Manas
02:08
created

CuratorAction.command()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
dl 0
loc 5
rs 9.4286
1
# pylint: disable=no-member
2
3
from curator_invoke import CuratorInvoke
4
from esbase_action import ESBaseAction
5
from curator.api.utils import index_closed
6
import logging
7
import sys
8
9
logger = logging.getLogger(__name__)
10
11
12
class CuratorAction(ESBaseAction):
13
14
    def __init__(self, config=None):
15
        super(CuratorAction, self).__init__(config=config)
16
        self.success = True
17
        self._act_on = None
18
        self._command = None
19
        self._api = None
20
        self._action = None
21
22
    @property
23
    def action(self):
24
        return self._action
25
26
    @property
27
    def act_on(self):
28
        if not self._act_on:
29
            _act_on = 'indices' if self.action.startswith('indices') else 'snapshots'
30
            self._act_on = _act_on
31
        return self._act_on
32
33
    @property
34
    def command(self):
35
        if not self._command:
36
            self._command = self.action.split('.')[1]
37
        return self._command
38
39
    @property
40
    def api(self):
41
        if not self._api:
42
            self._api = CuratorInvoke(**self.config)
43
        return self._api
44
45
    def show_dry_run(self):
46
        """
47
        Log dry run output with the command which would have been executed.
48
        """
49
        command = self.command
50
        items = self.api.fetch(act_on=self.act_on, on_nofilters_showall=True)
51
        print "DRY RUN MODE. No changes will be made."
52
        print "DRY RUN MODE. Executing command {} {}.".format(command, self.act_on)
53
        for item in items:
54
            if self.act_on == 'snapshots':
55
                print "DRY RUN: {0}: {1}".format(command, item)
56
            else:
57
                print "DRY RUN: {0}: {1}{2}".format(command, item, ' (CLOSED)'
58
                                                    if index_closed(self.client, item) else '')
59
60
    def do_show(self):
61
        """
62
        Show indices or snapshots command.
63
        """
64
        if not self.config.dry_run:
65
            for item in self.api.fetch(act_on=self.act_on,
66
                                       on_nofilters_showall=True):
67
                print item
68
        else:
69
            self.show_dry_run()
70
        sys.exit(0)
71
72
    @staticmethod
73
    def exit_msg(success):
74
        """
75
        Display a message corresponding to whether the job completed
76
        successfully or not then exit.
77
        """
78
        if success:
79
            logger.info("Job completed successfully.")
80
        else:
81
            logger.warn("Job did not complete successfully.")
82
        sys.exit(0) if success else sys.exit(1)
83
84
    def do_command(self):
85
        """
86
        Do the command.
87
        """
88
        # Show and exit
89
        self.command == "show" and self.do_show()
90
91
        if self.config.dry_run:
92
            self.show_dry_run()
93
        else:
94
            logger.info("Job starting: %s %s", self.command, self.act_on)
95
            logger.debug("Params: %s", self.config)
96
97
            success = self.api.invoke(command=self.command, act_on=self.act_on)
98
            self.exit_msg(success)
99