Add support for Python 3
[yardstick.git] / yardstick / benchmark / core / plugin.py
1 ##############################################################################
2 # Copyright (c) 2016 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 """ Handler for yardstick command 'plugin' """
11
12 from __future__ import print_function
13 from __future__ import absolute_import
14 import os
15 import sys
16 import yaml
17 import time
18 import logging
19 import pkg_resources
20 import yardstick.ssh as ssh
21
22 from yardstick.common.task_template import TaskTemplate
23
24 LOG = logging.getLogger(__name__)
25
26
27 class Plugin(object):
28     """Plugin commands.
29
30        Set of commands to manage plugins.
31     """
32
33     def install(self, args):
34         """Install a plugin."""
35
36         total_start_time = time.time()
37         parser = PluginParser(args.input_file[0])
38
39         plugins, deployment = parser.parse_plugin()
40         plugin_name = plugins.get("name")
41         print("Installing plugin: %s" % plugin_name)
42
43         LOG.info("Executing _install_setup()")
44         self._install_setup(plugin_name, deployment)
45
46         LOG.info("Executing _run()")
47         self._run(plugin_name)
48
49         total_end_time = time.time()
50         LOG.info("total finished in %d secs",
51                  total_end_time - total_start_time)
52
53         print("Done, exiting")
54
55     def remove(self, args):
56         """Remove a plugin."""
57
58         total_start_time = time.time()
59         parser = PluginParser(args.input_file[0])
60
61         plugins, deployment = parser.parse_plugin()
62         plugin_name = plugins.get("name")
63         print("Removing plugin: %s" % plugin_name)
64
65         LOG.info("Executing _remove_setup()")
66         self._remove_setup(plugin_name, deployment)
67
68         LOG.info("Executing _run()")
69         self._run(plugin_name)
70
71         total_end_time = time.time()
72         LOG.info("total finished in %d secs",
73                  total_end_time - total_start_time)
74
75         print("Done, exiting")
76
77     def _install_setup(self, plugin_name, deployment):
78         """Deployment environment setup"""
79         target_script = plugin_name + ".bash"
80         self.script = pkg_resources.resource_filename(
81             'yardstick.resources', 'scripts/install/' + target_script)
82
83         deployment_user = deployment.get("user")
84         deployment_ssh_port = deployment.get("ssh_port", ssh.DEFAULT_PORT)
85         deployment_ip = deployment.get("ip", None)
86         deployment_password = deployment.get("password", None)
87         deployment_key_filename = deployment.get("key_filename",
88                                                  "/root/.ssh/id_rsa")
89
90         if deployment_ip == "local":
91             installer_ip = os.environ.get("INSTALLER_IP", None)
92
93             if deployment_password is not None:
94                 self._login_via_password(deployment_user, installer_ip,
95                                          deployment_password,
96                                          deployment_ssh_port)
97             else:
98                 self._login_via_key(self, deployment_user, installer_ip,
99                                     deployment_key_filename,
100                                     deployment_ssh_port)
101         else:
102             if deployment_password is not None:
103                 self._login_via_password(deployment_user, deployment_ip,
104                                          deployment_password,
105                                          deployment_ssh_port)
106             else:
107                 self._login_via_key(self, deployment_user, deployment_ip,
108                                     deployment_key_filename,
109                                     deployment_ssh_port)
110         # copy script to host
111         remotepath = '~/%s.sh' % plugin_name
112
113         LOG.info("copying script to host: %s", remotepath)
114         self.client._put_file_shell(self.script, remotepath)
115
116     def _remove_setup(self, plugin_name, deployment):
117         """Deployment environment setup"""
118         target_script = plugin_name + ".bash"
119         self.script = pkg_resources.resource_filename(
120             'yardstick.resources', 'scripts/remove/' + target_script)
121
122         deployment_user = deployment.get("user")
123         deployment_ssh_port = deployment.get("ssh_port", ssh.DEFAULT_PORT)
124         deployment_ip = deployment.get("ip", None)
125         deployment_password = deployment.get("password", None)
126         deployment_key_filename = deployment.get("key_filename",
127                                                  "/root/.ssh/id_rsa")
128
129         if deployment_ip == "local":
130             installer_ip = os.environ.get("INSTALLER_IP", None)
131
132             if deployment_password is not None:
133                 self._login_via_password(deployment_user, installer_ip,
134                                          deployment_password,
135                                          deployment_ssh_port)
136             else:
137                 self._login_via_key(self, deployment_user, installer_ip,
138                                     deployment_key_filename,
139                                     deployment_ssh_port)
140         else:
141             if deployment_password is not None:
142                 self._login_via_password(deployment_user, deployment_ip,
143                                          deployment_password,
144                                          deployment_ssh_port)
145             else:
146                 self._login_via_key(self, deployment_user, deployment_ip,
147                                     deployment_key_filename,
148                                     deployment_ssh_port)
149
150         # copy script to host
151         remotepath = '~/%s.sh' % plugin_name
152
153         LOG.info("copying script to host: %s", remotepath)
154         self.client._put_file_shell(self.script, remotepath)
155
156     def _login_via_password(self, user, ip, password, ssh_port):
157         LOG.info("Log in via pw, user:%s, host:%s", user, ip)
158         self.client = ssh.SSH(user, ip, password=password, port=ssh_port)
159         self.client.wait(timeout=600)
160
161     def _login_via_key(self, user, ip, key_filename, ssh_port):
162         LOG.info("Log in via key, user:%s, host:%s", user, ip)
163         self.client = ssh.SSH(user, ip, key_filename=key_filename,
164                               port=ssh_port)
165         self.client.wait(timeout=600)
166
167     def _run(self, plugin_name):
168         """Run installation script """
169         cmd = "sudo bash %s" % plugin_name + ".sh"
170
171         LOG.info("Executing command: %s", cmd)
172         status, stdout, stderr = self.client.execute(cmd)
173
174
175 class PluginParser(object):
176     """Parser for plugin configration files in yaml format"""
177
178     def __init__(self, path):
179         self.path = path
180
181     def parse_plugin(self):
182         """parses the plugin file and return a plugins instance
183            and a deployment instance
184         """
185
186         print("Parsing plugin config:", self.path)
187
188         try:
189             kw = {}
190             with open(self.path) as f:
191                 try:
192                     input_plugin = f.read()
193                     rendered_plugin = TaskTemplate.render(input_plugin, **kw)
194                 except Exception as e:
195                     print("Failed to render template:\n%(plugin)s\n%(err)s\n"
196                           % {"plugin": input_plugin, "err": e})
197                     raise e
198                 print("Input plugin is:\n%s\n" % rendered_plugin)
199
200                 cfg = yaml.load(rendered_plugin)
201         except IOError as ioerror:
202             sys.exit(ioerror)
203
204         self._check_schema(cfg["schema"], "plugin")
205
206         return cfg["plugins"], cfg["deployment"]
207
208     def _check_schema(self, cfg_schema, schema_type):
209         """Check if configration file is using the correct schema type"""
210
211         if cfg_schema != "yardstick:" + schema_type + ":0.1":
212             sys.exit("error: file %s has unknown schema %s" % (self.path,
213                                                                cfg_schema))