Completed
Pull Request — master (#515)
by
unknown
03:08
created

NodesPollNow   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 86
rs 10
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
B _pollnow_nodes() 0 32 5
D run() 0 52 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, platform, 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
        - platform: The orion platform to act on.
30
        - count: Number of polls to complete.
31
        - pause: Number of seconds to wait between each cycle.
32
33
        Returns
34
            True: As PollNow does not return any data.
35
36
        Raises:
37
            IndexError: When a nodes is not found.
38
        """
39
40
        self.results = {'down': [],
41
                        'up': [],
42
                        'extra_count': False}
43
44
        self.connect(platform)
45
        self.orion_nodes = []
46
47
        for node in nodes:
48
            orion_node = self.get_node(node)
49
50
            if orion_node.npm:
51
                self.orion_nodes.append(orion_node.npm_id)
52
            else:
53
                error_msg = "Node not found"
54
                send_user_error(error_msg)
55
                raise ValueError(error_msg)
56
57
        for c in range(count):
58
            self._pollnow_nodes(count, pause)
59
60
            if len(self.orion_nodes) == 0:
61
                self.results['last_count'] = c
62
                break
63
        else:
64
            self.results['last_count'] = count
65
66
        if len(self.orion_nodes) > 0:
67
            self.results['extra_pass'] = True
68
            for c in range(count):
69
                self._pollnow_nodes(count, pause)
70
            else:
71
                self.results['left'] = self.orion_nodes
72
73
        # These Invoke's return None, so we just return True
74
        return self.results
75
76
    def _pollnow_nodes(self, count, pause):
77
        """
78
        Carry out a poll on any remaining orion nodes and remove those
79
        that are up or down.
80
81
        Args:
82
        - count: Number of polls to complete.
83
        - pause: Number of seconds to wait between each cycle.
84
85
        Returns:
86
        - None.
87
88
        Raises:
89
        - None.
90
        """
91
        for npm_id in self.orion_nodes:
92
            self.invoke("Orion.Nods",
93
                        "PollNow",
94
                        npm_id)
95
96
            swql = "SELECT Status FROM Orion.Nodes WHERE NodeID=@NodeID"
97
            kargs = {'NodeID': npm_id}
98
            orion_data = self.query(swql, **kargs)
99
100
            if orion_data['results'][0]['Status'] == 1:
101
                self.results['up'].append(npm_id)
102
                self.orion_nodes.remove(npm_id)
103
            elif orion_data['results'][0]['Status'] == 2:
104
                self.orion_nodes.remove(npm_id)
105
                self.results['down'].append(npm_id)
106
        else:
107
                time.sleep(pause)
108