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 LOG.info("Executing _install_setup()")
45 self._install_setup(plugin_name, deployment)
47 LOG.info("Executing _run()")
48 self._run(plugin_name)
50 total_end_time = time.time()
51 LOG.info("total finished in %d secs",
52 total_end_time - total_start_time)
54 print("Done, exiting")
56 @cliargs("input_file", type=str, help="path to plugin configuration file",
58 def do_remove(self, args):
59 '''Remove a plugin.'''
61 total_start_time = time.time()
62 parser = PluginParser(args.input_file[0])
64 plugins, deployment = parser.parse_plugin()
65 plugin_name = plugins.get("name")
66 print("Removing plugin: %s" % plugin_name)
68 LOG.info("Executing _remove_setup()")
69 self._remove_setup(plugin_name, deployment)
71 LOG.info("Executing _run()")
72 self._run(plugin_name)
74 total_end_time = time.time()
75 LOG.info("total finished in %d secs",
76 total_end_time - total_start_time)
78 print("Done, exiting")
80 def _install_setup(self, plugin_name, deployment):
81 '''Deployment environment setup'''
82 target_script = plugin_name + ".bash"
83 self.script = pkg_resources.resource_filename(
84 'yardstick.resources', 'scripts/install/' + target_script)
86 deployment_user = deployment.get("user")
87 deployment_ssh_port = deployment.get("ssh_port", ssh.DEFAULT_PORT)
88 deployment_ip = deployment.get("ip")
89 deployment_password = deployment.get("password")
91 if deployment_ip == "local":
92 installer_ip = os.environ.get("INSTALLER_IP", None)
94 LOG.info("user:%s, host:%s", deployment_user, installer_ip)
95 self.client = ssh.SSH(deployment_user, installer_ip,
96 password=deployment_password,
97 port=deployment_ssh_port)
98 self.client.wait(timeout=600)
100 LOG.info("user:%s, host:%s", deployment_user, deployment_ip)
101 self.client = ssh.SSH(deployment_user, deployment_ip,
102 password=deployment_password,
103 port=deployment_ssh_port)
104 self.client.wait(timeout=600)
106 # copy script to host
107 cmd = "cat > ~/%s.sh" % plugin_name
109 LOG.info("copying script to host: %s", cmd)
110 self.client.run(cmd, stdin=open(self.script, 'rb'))
112 def _remove_setup(self, plugin_name, deployment):
113 '''Deployment environment setup'''
114 target_script = plugin_name + ".bash"
115 self.script = pkg_resources.resource_filename(
116 'yardstick.resources', 'scripts/remove/' + target_script)
118 deployment_user = deployment.get("user")
119 deployment_ssh_port = deployment.get("ssh_port", ssh.DEFAULT_PORT)
120 deployment_ip = deployment.get("ip")
121 deployment_password = deployment.get("password")
123 if deployment_ip == "local":
124 installer_ip = os.environ.get("INSTALLER_IP", None)
126 LOG.info("user:%s, host:%s", deployment_user, installer_ip)
127 self.client = ssh.SSH(deployment_user, installer_ip,
128 password=deployment_password,
129 port=deployment_ssh_port)
130 self.client.wait(timeout=600)
132 LOG.info("user:%s, host:%s", deployment_user, deployment_ip)
133 self.client = ssh.SSH(deployment_user, deployment_ip,
134 password=deployment_password,
135 port=deployment_ssh_port)
136 self.client.wait(timeout=600)
138 # copy script to host
139 cmd = "cat > ~/%s.sh" % plugin_name
141 LOG.info("copying script to host: %s", cmd)
142 self.client.run(cmd, stdin=open(self.script, 'rb'))
144 def _run(self, plugin_name):
145 '''Run installation script '''
146 cmd = "sudo bash %s" % plugin_name + ".sh"
148 LOG.info("Executing command: %s", cmd)
149 status, stdout, stderr = self.client.execute(cmd)
152 class PluginParser(object):
153 '''Parser for plugin configration files in yaml format'''
155 def __init__(self, path):
158 def parse_plugin(self):
159 '''parses the plugin file and return a plugins instance
160 and a deployment instance
163 print "Parsing plugin config:", self.path
167 with open(self.path) as f:
169 input_plugin = f.read()
170 rendered_plugin = TaskTemplate.render(input_plugin, **kw)
171 except Exception as e:
172 print(("Failed to render template:\n%(plugin)s\n%(err)s\n")
173 % {"plugin": input_plugin, "err": e})
175 print(("Input plugin is:\n%s\n") % rendered_plugin)
177 cfg = yaml.load(rendered_plugin)
178 except IOError as ioerror:
181 self._check_schema(cfg["schema"], "plugin")
183 return cfg["plugins"], cfg["deployment"]
185 def _check_schema(self, cfg_schema, schema_type):
186 '''Check if configration file is using the correct schema type'''
188 if cfg_schema != "yardstick:" + schema_type + ":0.1":
189 sys.exit("error: file %s has unknown schema %s" % (self.path,