78bce8259960f06c0e32e512f95c9bf187445ddb
[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 os
12 import yaml
13 import logging
14
15 from yardstick.benchmark.contexts.base import Context
16 from yardstick.definitions import YARDSTICK_ROOT_PATH
17
18 LOG = logging.getLogger(__name__)
19
20
21 class NodeContext(Context):
22     '''Class that handle nodes info'''
23
24     __context_type__ = "Node"
25
26     def __init__(self):
27         self.name = None
28         self.file_path = None
29         self.nodes = []
30         self.controllers = []
31         self.computes = []
32         self.baremetals = []
33         super(self.__class__, self).__init__()
34
35     def init(self, attrs):
36         '''initializes itself from the supplied arguments'''
37         self.name = attrs["name"]
38         self.file_path = attrs.get("file", "pod.yaml")
39         if not os.path.exists(self.file_path):
40             self.file_path = os.path.join(YARDSTICK_ROOT_PATH, self.file_path)
41
42         LOG.info("Parsing pod file: %s", self.file_path)
43
44         try:
45             with open(self.file_path) as stream:
46                 cfg = yaml.load(stream)
47         except IOError as ioerror:
48             sys.exit(ioerror)
49
50         self.nodes.extend(cfg["nodes"])
51         self.controllers.extend([node for node in cfg["nodes"]
52                                 if node["role"] == "Controller"])
53         self.computes.extend([node for node in cfg["nodes"]
54                              if node["role"] == "Compute"])
55         self.baremetals.extend([node for node in cfg["nodes"]
56                                if node["role"] == "Baremetal"])
57         LOG.debug("Nodes: %r", self.nodes)
58         LOG.debug("Controllers: %r", self.controllers)
59         LOG.debug("Computes: %r", self.computes)
60         LOG.debug("BareMetals: %r", self.baremetals)
61
62     def deploy(self):
63         '''don't need to deploy'''
64         pass
65
66     def undeploy(self):
67         '''don't need to undeploy'''
68         pass
69
70     def _get_server(self, attr_name):
71         '''lookup server info by name from context
72         attr_name: a name for a server listed in nodes config file
73         '''
74         if type(attr_name) is dict:
75             return None
76
77         if self.name != attr_name.split(".")[1]:
78             return None
79         node_name = attr_name.split(".")[0]
80         nodes = [n for n in self.nodes
81                  if n["name"] == node_name]
82         if len(nodes) == 0:
83             return None
84         elif len(nodes) > 1:
85             LOG.error("Duplicate nodes!!!")
86             LOG.error("Nodes: %r", nodes)
87             sys.exit(-1)
88
89         # A clone is created in order to avoid affecting the
90         # original one.
91         node = dict(nodes[0])
92         node["name"] = attr_name
93         return node