Support NodeContext type
[yardstick.git] / yardstick / benchmark / contexts / node.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import sys
11 import yaml
12 import logging
13
14 from yardstick.benchmark.contexts.base import Context
15
16 LOG = logging.getLogger(__name__)
17
18
19 class NodeContext(Context):
20     '''Class that handle nodes info'''
21
22     __context_type__ = "Node"
23
24     def __init__(self):
25         self.name = None
26         self.file_path = None
27         self.nodes = []
28         self.controllers = []
29         self.computes = []
30         self.baremetals = []
31         super(self.__class__, self).__init__()
32
33     def init(self, attrs):
34         '''initializes itself from the supplied arguments'''
35         self.name = attrs["name"]
36         self.file_path = attrs.get("file", "/etc/yardstick/nodes/pod.yaml")
37
38         LOG.info("Parsing pod file: %s", self.file_path)
39
40         try:
41             with open(self.file_path) as stream:
42                 cfg = yaml.load(stream)
43         except IOError as ioerror:
44             sys.exit(ioerror)
45
46         self.nodes.extend(cfg["nodes"])
47         self.controllers.extend([node for node in cfg["nodes"]
48                                 if node["role"] == "Controller"])
49         self.computes.extend([node for node in cfg["nodes"]
50                              if node["role"] == "Compute"])
51         self.baremetals.extend([node for node in cfg["nodes"]
52                                if node["role"] == "Baremetal"])
53         LOG.debug("Nodes: %r", self.nodes)
54         LOG.debug("Controllers: %r", self.controllers)
55         LOG.debug("Computes: %r", self.computes)
56         LOG.debug("BareMetals: %r", self.baremetals)
57
58     def deploy(self):
59         '''don't need to deploy'''
60         pass
61
62     def undeploy(self):
63         '''don't need to undeploy'''
64         pass
65
66     def _get_server(self, attr_name):
67         '''lookup server info by name from context
68         attr_name: a name for a server listed in nodes config file
69         '''
70         if type(attr_name) is dict:
71             return None
72
73         if self.name != attr_name.split(".")[1]:
74             return None
75         node_name = attr_name.split(".")[0]
76         nodes = [n for n in self.nodes
77                  if n["name"] == node_name]
78         if len(nodes) == 0:
79             return None
80         elif len(nodes) > 1:
81             LOG.error("Duplicate nodes!!!")
82             LOG.error("Nodes: %r" % nodes)
83             sys.exit(-1)
84
85         node = nodes[0]
86
87         server = {
88             "name": attr_name,
89             "ip": node["ip"],
90             "user": node["user"],
91             "key_filename": node["key_filename"]
92         }
93
94         return server