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' """
18 import yardstick.ssh as ssh
20 from yardstick.common.utils import cliargs
21 from yardstick.common.task_template import TaskTemplate
23 LOG = logging.getLogger(__name__)
26 class PluginCommands(object):
29 Set of commands to manage plugins.
32 @cliargs("input_file", type=str, help="path to plugin configuration file",
34 def do_install(self, args):
35 '''Install a plugin.'''
37 total_start_time = time.time()
38 parser = PluginParser(args.input_file[0])
40 plugins, deployment = parser.parse_plugin()
41 plugin_name = plugins.get("name")
42 print("Installing plugin: %s" % plugin_name)
44 self._install_setup(plugin_name, deployment)
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 @cliargs("input_file", type=str, help="path to plugin configuration file",
56 def do_remove(self, args):
57 '''Remove a plugin.'''
59 total_start_time = time.time()
60 parser = PluginParser(args.input_file[0])
62 plugins, deployment = parser.parse_plugin()
63 plugin_name = plugins.get("name")
64 print("Remove plugin: %s" % plugin_name)
66 self._remove_setup(plugin_name, deployment)
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_ip = deployment.get("ip")
84 deployment_password = deployment.get("password")
86 if deployment_ip == "local":
87 installer_ip = os.environ.get("INSTALLER_IP", None)
89 LOG.debug("user:%s, host:%s", deployment_user, installer_ip)
90 self.client = ssh.SSH(deployment_user, installer_ip,
91 password=deployment_password)
92 self.client.wait(timeout=600)
94 LOG.debug("user:%s, host:%s", deployment_user, deployment_ip)
95 self.client = ssh.SSH(deployment_user, deployment_ip,
96 password=deployment_password)
97 self.client.wait(timeout=600)
100 cmd = "cat > ~/%s.sh" % plugin_name
101 self.client.run(cmd, stdin=open(self.script, 'rb'))
103 def _remove_setup(self, plugin_name, deployment):
104 '''Deployment environment setup'''
105 target_script = plugin_name + ".bash"
106 self.script = pkg_resources.resource_filename(
107 'yardstick.resources', 'scripts/remove/' + target_script)
109 deployment_user = deployment.get("user")
110 deployment_ip = deployment.get("ip")
111 deployment_password = deployment.get("password")
113 if deployment_ip == "local":
114 installer_ip = os.environ.get("INSTALLER_IP", None)
116 LOG.debug("user:%s, host:%s", deployment_user, installer_ip)
117 self.client = ssh.SSH(deployment_user, installer_ip,
118 password=deployment_password)
119 self.client.wait(timeout=600)
121 LOG.debug("user:%s, host:%s", deployment_user, deployment_ip)
122 self.client = ssh.SSH(deployment_user, deployment_ip,
123 password=deployment_password)
124 self.client.wait(timeout=600)
126 # copy script to host
127 cmd = "cat > ~/%s.sh" % plugin_name
128 self.client.run(cmd, stdin=open(self.script, 'rb'))
130 def _run(self, plugin_name):
131 '''Run installation script '''
132 cmd = "sudo bash %s" % plugin_name + ".sh"
134 LOG.debug("Executing command: %s", cmd)
135 status, stdout, stderr = self.client.execute(cmd)
138 class PluginParser(object):
139 '''Parser for plugin configration files in yaml format'''
140 def __init__(self, path):
143 def parse_plugin(self):
144 '''parses the plugin file and return a plugins instance
145 and a deployment instance
148 print "Parsing plugin config:", self.path
152 with open(self.path) as f:
154 input_plugin = f.read()
155 rendered_plugin = TaskTemplate.render(input_plugin, **kw)
156 except Exception as e:
157 print(("Failed to render template:\n%(plugin)s\n%(err)s\n")
158 % {"plugin": input_plugin, "err": e})
160 print(("Input plugin is:\n%s\n") % rendered_plugin)
162 cfg = yaml.load(rendered_plugin)
163 except IOError as ioerror:
166 self._check_schema(cfg["schema"], "plugin")
168 return cfg["plugins"], cfg["deployment"]
170 def _check_schema(self, cfg_schema, schema_type):
171 '''Check if configration file is using the correct schema type'''
173 if cfg_schema != "yardstick:" + schema_type + ":0.1":
174 sys.exit("error: file %s has unknown schema %s" % (self.path,