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
19 import yardstick.ssh as ssh
21 from yardstick.common.task_template import TaskTemplate
22 from yardstick.common.yaml_loader import yaml_load
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 LOG.info("Installing plugin: %s", plugin_name)
43 LOG.debug("Executing _install_setup()")
44 self._install_setup(plugin_name, deployment)
46 LOG.debug("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 LOG.info("Plugin %s Done, exiting", plugin_name)
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_ip = deployment.get("ip", None)
85 if deployment_ip == "local":
86 self.client = ssh.SSH.from_node(deployment, overrides={
87 # host can't be None, fail if no JUMP_HOST_IP
88 'ip': os.environ["JUMP_HOST_IP"],
91 self.client = ssh.SSH.from_node(deployment)
92 self.client.wait(timeout=600)
95 remotepath = '~/%s.sh' % plugin_name
97 LOG.info("copying script to host: %s", remotepath)
98 self.client._put_file_shell(self.script, remotepath)
100 def _remove_setup(self, plugin_name, deployment):
101 """Deployment environment setup"""
102 target_script = plugin_name + ".bash"
103 self.script = pkg_resources.resource_filename(
104 'yardstick.resources', 'scripts/remove/' + target_script)
106 deployment_ip = deployment.get("ip", None)
108 if deployment_ip == "local":
109 self.client = ssh.SSH.from_node(deployment, overrides={
110 # host can't be None, fail if no JUMP_HOST_IP
111 'ip': os.environ["JUMP_HOST_IP"],
114 self.client = ssh.SSH.from_node(deployment)
115 self.client.wait(timeout=600)
117 # copy script to host
118 remotepath = '~/%s.sh' % plugin_name
120 LOG.info("copying script to host: %s", remotepath)
121 self.client._put_file_shell(self.script, remotepath)
123 def _run(self, plugin_name):
124 """Run installation script """
125 cmd = "sudo bash %s" % plugin_name + ".sh"
127 LOG.info("Executing command: %s", cmd)
128 self.client.execute(cmd)
131 class PluginParser(object):
132 """Parser for plugin configration files in yaml format"""
134 def __init__(self, path):
137 def parse_plugin(self):
138 """parses the plugin file and return a plugins instance
139 and a deployment instance
142 print("Parsing plugin config:", self.path)
146 with open(self.path) as f:
148 input_plugin = f.read()
149 rendered_plugin = TaskTemplate.render(input_plugin, **kw)
150 except Exception as e:
151 print("Failed to render template:\n%(plugin)s\n%(err)s\n"
152 % {"plugin": input_plugin, "err": e})
154 print("Input plugin is:\n%s\n" % rendered_plugin)
156 cfg = yaml_load(rendered_plugin)
157 except IOError as ioerror:
160 self._check_schema(cfg["schema"], "plugin")
162 return cfg["plugins"], cfg["deployment"]
164 def _check_schema(self, cfg_schema, schema_type):
165 """Check if configration file is using the correct schema type"""
167 if cfg_schema != "yardstick:" + schema_type + ":0.1":
168 sys.exit("error: file %s has unknown schema %s" % (self.path,