8bf9156099add25aa371714f19f003a0a5ba8924
[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 from __future__ import absolute_import
11 import errno
12 import subprocess
13 import os
14 import collections
15 import logging
16
17 import yaml
18 import pkg_resources
19
20 from yardstick import ssh
21 from yardstick.benchmark.contexts.base import Context
22 from yardstick.common.constants import YARDSTICK_ROOT_PATH
23
24 LOG = logging.getLogger(__name__)
25
26
27 class NodeContext(Context):
28     """Class that handle nodes info"""
29
30     __context_type__ = "Node"
31
32     def __init__(self):
33         self.name = None
34         self.file_path = None
35         self.nodes = []
36         self.controllers = []
37         self.computes = []
38         self.baremetals = []
39         self.env = {}
40         super(NodeContext, self).__init__()
41
42     def read_config_file(self):
43         """Read from config file"""
44
45         with open(self.file_path) as stream:
46             LOG.info("Parsing pod file: %s", self.file_path)
47             cfg = yaml.load(stream)
48         return cfg
49
50     def init(self, attrs):
51         """initializes itself from the supplied arguments"""
52         self.name = attrs["name"]
53         self.file_path = attrs.get("file", "pod.yaml")
54
55         try:
56             cfg = self.read_config_file()
57         except IOError as ioerror:
58             if ioerror.errno == errno.ENOENT:
59                 self.file_path = \
60                     os.path.join(YARDSTICK_ROOT_PATH, self.file_path)
61                 cfg = self.read_config_file()
62             else:
63                 raise
64
65         self.nodes.extend(cfg["nodes"])
66         self.controllers.extend([node for node in cfg["nodes"]
67                                  if node["role"] == "Controller"])
68         self.computes.extend([node for node in cfg["nodes"]
69                               if node["role"] == "Compute"])
70         self.baremetals.extend([node for node in cfg["nodes"]
71                                 if node["role"] == "Baremetal"])
72         LOG.debug("Nodes: %r", self.nodes)
73         LOG.debug("Controllers: %r", self.controllers)
74         LOG.debug("Computes: %r", self.computes)
75         LOG.debug("BareMetals: %r", self.baremetals)
76
77         self.env = attrs.get('env', {})
78         LOG.debug("Env: %r", self.env)
79
80     def deploy(self):
81         config_type = self.env.get('type', '')
82         if config_type == 'ansible':
83             self._dispatch_ansible('setup')
84         elif config_type == 'script':
85             self._dispatch_script('setup')
86
87     def undeploy(self):
88         config_type = self.env.get('type', '')
89         if config_type == 'ansible':
90             self._dispatch_ansible('teardown')
91         elif config_type == 'script':
92             self._dispatch_script('teardown')
93         super(NodeContext, self).undeploy()
94
95     def _dispatch_script(self, key):
96         steps = self.env.get(key, [])
97         for step in steps:
98             for host, info in step.items():
99                 self._execute_script(host, info)
100
101     def _dispatch_ansible(self, key):
102         try:
103             step = self.env[key]
104         except KeyError:
105             pass
106         else:
107             self._do_ansible_job(step)
108
109     def _do_ansible_job(self, path):
110         cmd = 'ansible-playbook -i inventory.ini %s' % path
111         base = '/home/opnfv/repos/yardstick/ansible'
112         p = subprocess.Popen(cmd, shell=True, cwd=base)
113         p.communicate()
114
115     def _get_server(self, attr_name):
116         """lookup server info by name from context
117         attr_name: a name for a server listed in nodes config file
118         """
119         if isinstance(attr_name, collections.Mapping):
120             return None
121
122         if self.name != attr_name.split(".")[1]:
123             return None
124         node_name = attr_name.split(".")[0]
125         matching_nodes = (n for n in self.nodes if n["name"] == node_name)
126
127         try:
128             # A clone is created in order to avoid affecting the
129             # original one.
130             node = dict(next(matching_nodes))
131         except StopIteration:
132             return None
133
134         try:
135             duplicate = next(matching_nodes)
136         except StopIteration:
137             pass
138         else:
139             raise ValueError("Duplicate nodes!!! Nodes: %s %s",
140                              (matching_nodes, duplicate))
141
142         node["name"] = attr_name
143         return node
144
145     def _execute_script(self, node_name, info):
146         if node_name == 'local':
147             self._execute_local_script(info)
148         else:
149             self._execute_remote_script(node_name, info)
150
151     def _execute_remote_script(self, node_name, info):
152         prefix = self.env.get('prefix', '')
153         script, options = self._get_script(info)
154
155         script_file = pkg_resources.resource_filename(prefix, script)
156
157         self._get_client(node_name)
158         self.client._put_file_shell(script_file, '~/{}'.format(script))
159
160         cmd = 'sudo bash {} {}'.format(script, options)
161         status, stdout, stderr = self.client.execute(cmd)
162         if status:
163             raise RuntimeError(stderr)
164
165     def _execute_local_script(self, info):
166         script, options = self._get_script(info)
167         script = os.path.join(YARDSTICK_ROOT_PATH, script)
168         cmd = ['bash', script, options]
169
170         p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
171         LOG.debug('\n%s', p.communicate()[0])
172
173     def _get_script(self, info):
174         return info.get('script'), info.get('options', '')
175
176     def _get_client(self, node_name):
177         node = self._get_node_info(node_name.strip())
178
179         if node is None:
180             raise SystemExit('No such node')
181
182         user = node.get('user', 'ubuntu')
183         ssh_port = node.get("ssh_port", ssh.DEFAULT_PORT)
184         ip = node.get('ip')
185         pwd = node.get('password')
186         key_fname = node.get('key_filename', '/root/.ssh/id_rsa')
187
188         if pwd is not None:
189             LOG.debug("Log in via pw, user:%s, host:%s, password:%s",
190                       user, ip, pwd)
191             self.client = ssh.SSH(user, ip, password=pwd, port=ssh_port)
192         else:
193             LOG.debug("Log in via key, user:%s, host:%s, key_filename:%s",
194                       user, ip, key_fname)
195             self.client = ssh.SSH(user, ip, key_filename=key_fname,
196                                   port=ssh_port)
197
198         self.client.wait(timeout=600)
199
200     def _get_node_info(self, name):
201         return next((n for n in self.nodes if n['name'].strip() == name))