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 def do_remove(self, args):
54 '''Remove a plugin.'''
56 total_start_time = time.time()
57 parser = PluginParser(args.input_file[0])
59 plugins, deployment = parser.parse_plugin()
60 plugin_name = plugins.get("name")
61 print("Remove plugin: %s" % plugin_name)
63 self._remove_setup(plugin_name, deployment)
65 self._run(plugin_name)
67 total_end_time = time.time()
68 LOG.info("total finished in %d secs",
69 total_end_time - total_start_time)
71 print("Done, exiting")
73 def _install_setup(self, plugin_name, deployment):
74 '''Deployment environment setup'''
75 target_script = plugin_name + ".bash"
76 self.script = pkg_resources.resource_filename(
77 'yardstick.resources', 'script/install/' + target_script)
79 deployment_user = deployment.get("user")
80 deployment_ip = deployment.get("ip")
82 deployment_password = deployment.get("password")
83 LOG.debug("user:%s, host:%s", deployment_user, deployment_ip)
84 self.client = ssh.SSH(deployment_user, deployment_ip,
85 password=deployment_password)
86 self.client.wait(timeout=600)
89 cmd = "cat > ~/%s.sh" % plugin_name
90 self.client.run(cmd, stdin=open(self.script, 'rb'))
92 def _remove_setup(self, plugin_name, deployment):
93 '''Deployment environment setup'''
94 target_script = plugin_name + ".bash"
95 self.script = pkg_resources.resource_filename(
96 'yardstick.resources', 'script/remove/' + target_script)
98 deployment_user = deployment.get("user")
99 deployment_ip = deployment.get("ip")
101 deployment_password = deployment.get("password")
102 LOG.debug("user:%s, host:%s", deployment_user, deployment_ip)
103 self.client = ssh.SSH(deployment_user, deployment_ip,
104 password=deployment_password)
105 self.client.wait(timeout=600)
107 # copy script to host
108 cmd = "cat > ~/%s.sh" % plugin_name
109 self.client.run(cmd, stdin=open(self.script, 'rb'))
111 def _run(self, plugin_name):
112 '''Run installation script '''
113 cmd = "sudo bash %s" % plugin_name + ".sh"
115 LOG.debug("Executing command: %s", cmd)
116 status, stdout, stderr = self.client.execute(cmd)
119 class PluginParser(object):
120 '''Parser for plugin configration files in yaml format'''
121 def __init__(self, path):
124 def parse_plugin(self):
125 '''parses the plugin file and return a plugins instance
126 and a deployment instance
129 print "Parsing plugin config:", self.path
133 with open(self.path) as f:
135 input_plugin = f.read()
136 rendered_plugin = TaskTemplate.render(input_plugin, **kw)
137 except Exception as e:
138 print(("Failed to render template:\n%(plugin)s\n%(err)s\n")
139 % {"plugin": input_plugin, "err": e})
141 print(("Input plugin is:\n%s\n") % rendered_plugin)
143 cfg = yaml.load(rendered_plugin)
144 except IOError as ioerror:
147 self._check_schema(cfg["schema"], "plugin")
149 return cfg["plugins"], cfg["deployment"]
151 def _check_schema(self, cfg_schema, schema_type):
152 '''Check if configration file is using the correct schema type'''
154 if cfg_schema != "yardstick:" + schema_type + ":0.1":
155 sys.exit("error: file %s has unknown schema %s" % (self.path,