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
19 import yardstick.ssh as ssh
21 from yardstick.common.task_template import TaskTemplate
23 LOG = logging.getLogger(__name__)
29 Set of commands to manage plugins.
32 def install(self, args):
33 """Install a plugin."""
35 total_start_time = time.time()
36 parser = PluginParser(args.input_file[0])
38 plugins, deployment = parser.parse_plugin()
39 plugin_name = plugins.get("name")
40 print("Installing plugin: %s" % plugin_name)
42 LOG.info("Executing _install_setup()")
43 self._install_setup(plugin_name, deployment)
45 LOG.info("Executing _run()")
46 self._run(plugin_name)
48 total_end_time = time.time()
49 LOG.info("total finished in %d secs",
50 total_end_time - total_start_time)
52 print("Done, exiting")
54 def remove(self, args):
55 """Remove a plugin."""
57 total_start_time = time.time()
58 parser = PluginParser(args.input_file[0])
60 plugins, deployment = parser.parse_plugin()
61 plugin_name = plugins.get("name")
62 print("Removing plugin: %s" % plugin_name)
64 LOG.info("Executing _remove_setup()")
65 self._remove_setup(plugin_name, deployment)
67 LOG.info("Executing _run()")
68 self._run(plugin_name)
70 total_end_time = time.time()
71 LOG.info("total finished in %d secs",
72 total_end_time - total_start_time)
74 print("Done, exiting")
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)
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",
89 if deployment_ip == "local":
90 installer_ip = os.environ.get("INSTALLER_IP", None)
92 if deployment_password is not None:
93 self._login_via_password(deployment_user, installer_ip,
97 self._login_via_key(self, deployment_user, installer_ip,
98 deployment_key_filename,
101 if deployment_password is not None:
102 self._login_via_password(deployment_user, deployment_ip,
106 self._login_via_key(self, deployment_user, deployment_ip,
107 deployment_key_filename,
109 # copy script to host
110 remotepath = '~/%s.sh' % plugin_name
112 LOG.info("copying script to host: %s", remotepath)
113 self.client._put_file_shell(self.script, remotepath)
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)
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",
128 if deployment_ip == "local":
129 installer_ip = os.environ.get("INSTALLER_IP", None)
131 if deployment_password is not None:
132 self._login_via_password(deployment_user, installer_ip,
136 self._login_via_key(self, deployment_user, installer_ip,
137 deployment_key_filename,
140 if deployment_password is not None:
141 self._login_via_password(deployment_user, deployment_ip,
145 self._login_via_key(self, deployment_user, deployment_ip,
146 deployment_key_filename,
149 # copy script to host
150 remotepath = '~/%s.sh' % plugin_name
152 LOG.info("copying script to host: %s", remotepath)
153 self.client._put_file_shell(self.script, remotepath)
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)
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,
164 self.client.wait(timeout=600)
166 def _run(self, plugin_name):
167 """Run installation script """
168 cmd = "sudo bash %s" % plugin_name + ".sh"
170 LOG.info("Executing command: %s", cmd)
171 status, stdout, stderr = self.client.execute(cmd)
174 class PluginParser(object):
175 """Parser for plugin configration files in yaml format"""
177 def __init__(self, path):
180 def parse_plugin(self):
181 """parses the plugin file and return a plugins instance
182 and a deployment instance
185 print ("Parsing plugin config:", self.path)
189 with open(self.path) as f:
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})
197 print(("Input plugin is:\n%s\n") % rendered_plugin)
199 cfg = yaml.load(rendered_plugin)
200 except IOError as ioerror:
203 self._check_schema(cfg["schema"], "plugin")
205 return cfg["plugins"], cfg["deployment"]
207 def _check_schema(self, cfg_schema, schema_type):
208 """Check if configration file is using the correct schema type"""
210 if cfg_schema != "yardstick:" + schema_type + ":0.1":
211 sys.exit("error: file %s has unknown schema %s" % (self.path,