1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
10 """ Handler for yardstick command 'plugin' """
12 from __future__ import print_function
13 from __future__ import absolute_import
20 import yardstick.ssh as ssh
22 from yardstick.common.task_template import TaskTemplate
24 LOG = logging.getLogger(__name__)
30 Set of commands to manage plugins.
33 def install(self, args):
34 """Install a plugin."""
36 total_start_time = time.time()
37 parser = PluginParser(args.input_file[0])
39 plugins, deployment = parser.parse_plugin()
40 plugin_name = plugins.get("name")
41 print("Installing plugin: %s" % plugin_name)
43 LOG.info("Executing _install_setup()")
44 self._install_setup(plugin_name, deployment)
46 LOG.info("Executing _run()")
47 self._run(plugin_name)
49 total_end_time = time.time()
50 LOG.info("total finished in %d secs",
51 total_end_time - total_start_time)
53 print("Done, exiting")
55 def remove(self, args):
56 """Remove a plugin."""
58 total_start_time = time.time()
59 parser = PluginParser(args.input_file[0])
61 plugins, deployment = parser.parse_plugin()
62 plugin_name = plugins.get("name")
63 print("Removing plugin: %s" % plugin_name)
65 LOG.info("Executing _remove_setup()")
66 self._remove_setup(plugin_name, deployment)
68 LOG.info("Executing _run()")
69 self._run(plugin_name)
71 total_end_time = time.time()
72 LOG.info("total finished in %d secs",
73 total_end_time - total_start_time)
75 print("Done, exiting")
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)
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",
90 if deployment_ip == "local":
91 installer_ip = os.environ.get("INSTALLER_IP", None)
93 if deployment_password is not None:
94 self._login_via_password(deployment_user, installer_ip,
98 self._login_via_key(self, deployment_user, installer_ip,
99 deployment_key_filename,
102 if deployment_password is not None:
103 self._login_via_password(deployment_user, deployment_ip,
107 self._login_via_key(self, deployment_user, deployment_ip,
108 deployment_key_filename,
110 # copy script to host
111 remotepath = '~/%s.sh' % plugin_name
113 LOG.info("copying script to host: %s", remotepath)
114 self.client._put_file_shell(self.script, remotepath)
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)
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",
129 if deployment_ip == "local":
130 installer_ip = os.environ.get("INSTALLER_IP", None)
132 if deployment_password is not None:
133 self._login_via_password(deployment_user, installer_ip,
137 self._login_via_key(self, deployment_user, installer_ip,
138 deployment_key_filename,
141 if deployment_password is not None:
142 self._login_via_password(deployment_user, deployment_ip,
146 self._login_via_key(self, deployment_user, deployment_ip,
147 deployment_key_filename,
150 # copy script to host
151 remotepath = '~/%s.sh' % plugin_name
153 LOG.info("copying script to host: %s", remotepath)
154 self.client._put_file_shell(self.script, remotepath)
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)
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,
165 self.client.wait(timeout=600)
167 def _run(self, plugin_name):
168 """Run installation script """
169 cmd = "sudo bash %s" % plugin_name + ".sh"
171 LOG.info("Executing command: %s", cmd)
172 status, stdout, stderr = self.client.execute(cmd)
175 class PluginParser(object):
176 """Parser for plugin configration files in yaml format"""
178 def __init__(self, path):
181 def parse_plugin(self):
182 """parses the plugin file and return a plugins instance
183 and a deployment instance
186 print("Parsing plugin config:", self.path)
190 with open(self.path) as f:
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})
198 print("Input plugin is:\n%s\n" % rendered_plugin)
200 cfg = yaml.load(rendered_plugin)
201 except IOError as ioerror:
204 self._check_schema(cfg["schema"], "plugin")
206 return cfg["plugins"], cfg["deployment"]
208 def _check_schema(self, cfg_schema, schema_type):
209 """Check if configration file is using the correct schema type"""
211 if cfg_schema != "yardstick:" + schema_type + ":0.1":
212 sys.exit("error: file %s has unknown schema %s" % (self.path,