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