Completed
Push — master ( 062836...3184f1 )
by Edward
06:10 queued 02:54
created

NodesPollNow   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
B _pollnow_nodes() 0 32 5
D run() 0 51 9
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import time
17
18
from lib.actions import OrionBaseAction
19
from lib.utils import send_user_error
20
21
22
class NodesPollNow(OrionBaseAction):
23
    def run(self, nodes, count, pause):
24
        """
25
        Invoke a PollNow verb against a Orion Node.
26
27
        Args:
28
        - node: The caption in Orion of the node to poll.
29
        - count: Number of polls to complete.
30
        - pause: Number of seconds to wait between each cycle.
31
32
        Returns
33
            True: As PollNow does not return any data.
34
35
        Raises:
36
            IndexError: When a nodes is not found.
37
        """
38
39
        self.results = {'down': [],
40
                        'up': [],
41
                        'extra_count': False}
42
43
        self.connect()
44
        self.orion_nodes = []
45
46
        for node in nodes:
47
            orion_node = self.get_node(node)
48
49
            if orion_node.npm:
50
                self.orion_nodes.append(orion_node.npm_id)
51
            else:
52
                error_msg = "Node not found"
53
                send_user_error(error_msg)
54
                raise ValueError(error_msg)
55
56
        for c in range(count):
57
            self._pollnow_nodes(count, pause)
58
59
            if len(self.orion_nodes) == 0:
60
                self.results['last_count'] = c
61
                break
62
        else:
63
            self.results['last_count'] = count
64
65
        if len(self.orion_nodes) > 0:
66
            self.results['extra_pass'] = True
67
            for c in range(count):
68
                self._pollnow_nodes(count, pause)
69
            else:
70
                self.results['left'] = self.orion_nodes
71
72
        # These Invoke's return None, so we just return True
73
        return self.results
74
75
    def _pollnow_nodes(self, count, pause):
76
        """
77
        Carry out a poll on any remaining orion nodes and remove those
78
        that are up or down.
79
80
        Args:
81
        - count: Number of polls to complete.
82
        - pause: Number of seconds to wait between each cycle.
83
84
        Returns:
85
        - None.
86
87
        Raises:
88
        - None.
89
        """
90
        for npm_id in self.orion_nodes:
91
            swql = "SELECT Status FROM Orion.Nodes WHERE NodeID=@NodeID"
92
            kargs = {'NodeID': npm_id}
93
            orion_data = self.query(swql, **kargs)
94
95
            if orion_data['results'][0]['Status'] == 1:
96
                self.results['up'].append(npm_id)
97
                self.orion_nodes.remove(npm_id)
98
            elif orion_data['results'][0]['Status'] == 2:
99
                self.orion_nodes.remove(npm_id)
100
                self.results['down'].append(npm_id)
101
102
            self.invoke("Orion.Nods",
103
                        "PollNow",
104
                        npm_id)
105
        else:
106
                time.sleep(pause)
107