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' """
17 import yardstick.ssh as ssh
19 from yardstick.common.utils import cliargs
20 from yardstick.common.task_template import TaskTemplate
22 LOG = logging.getLogger(__name__)
25 class PluginCommands(object):
28 Set of commands to manage plugins.
31 @cliargs("input_file", type=str, help="path to plugin configuration file",
33 def do_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 self._install_setup(plugin_name, deployment)
45 self._run(plugin_name)
47 total_end_time = time.time()
48 LOG.info("total finished in %d secs",
49 total_end_time - total_start_time)
51 print("Done, exiting")
53 @cliargs("input_file", type=str, help="path to plugin configuration file",
55 def do_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("Remove plugin: %s" % plugin_name)
65 self._remove_setup(plugin_name, deployment)
67 self._run(plugin_name)
69 total_end_time = time.time()
70 LOG.info("total finished in %d secs",
71 total_end_time - total_start_time)
73 print("Done, exiting")
75 def _install_setup(self, plugin_name, deployment):
76 '''Deployment environment setup'''
77 target_script = plugin_name + ".bash"
78 self.script = pkg_resources.resource_filename(
79 'yardstick.resources', 'scripts/install/' + target_script)
81 deployment_user = deployment.get("user")
82 deployment_ip = deployment.get("ip")
84 deployment_password = deployment.get("password")
85 LOG.debug("user:%s, host:%s", deployment_user, deployment_ip)
86 self.client = ssh.SSH(deployment_user, deployment_ip,
87 password=deployment_password)
88 self.client.wait(timeout=600)
91 cmd = "cat > ~/%s.sh" % plugin_name
92 self.client.run(cmd, stdin=open(self.script, 'rb'))
94 def _remove_setup(self, plugin_name, deployment):
95 '''Deployment environment setup'''
96 target_script = plugin_name + ".bash"
97 self.script = pkg_resources.resource_filename(
98 'yardstick.resources', 'scripts/remove/' + target_script)
100 deployment_user = deployment.get("user")
101 deployment_ip = deployment.get("ip")
103 deployment_password = deployment.get("password")
104 LOG.debug("user:%s, host:%s", deployment_user, deployment_ip)
105 self.client = ssh.SSH(deployment_user, deployment_ip,
106 password=deployment_password)
107 self.client.wait(timeout=600)
109 # copy script to host
110 cmd = "cat > ~/%s.sh" % plugin_name
111 self.client.run(cmd, stdin=open(self.script, 'rb'))
113 def _run(self, plugin_name):
114 '''Run installation script '''
115 cmd = "sudo bash %s" % plugin_name + ".sh"
117 LOG.debug("Executing command: %s", cmd)
118 status, stdout, stderr = self.client.execute(cmd)
121 class PluginParser(object):
122 '''Parser for plugin configration files in yaml format'''
123 def __init__(self, path):
126 def parse_plugin(self):
127 '''parses the plugin file and return a plugins instance
128 and a deployment instance
131 print "Parsing plugin config:", self.path
135 with open(self.path) as f:
137 input_plugin = f.read()
138 rendered_plugin = TaskTemplate.render(input_plugin, **kw)
139 except Exception as e:
140 print(("Failed to render template:\n%(plugin)s\n%(err)s\n")
141 % {"plugin": input_plugin, "err": e})
143 print(("Input plugin is:\n%s\n") % rendered_plugin)
145 cfg = yaml.load(rendered_plugin)
146 except IOError as ioerror:
149 self._check_schema(cfg["schema"], "plugin")
151 return cfg["plugins"], cfg["deployment"]
153 def _check_schema(self, cfg_schema, schema_type):
154 '''Check if configration file is using the correct schema type'''
156 if cfg_schema != "yardstick:" + schema_type + ":0.1":
157 sys.exit("error: file %s has unknown schema %s" % (self.path,