Merge "Add test case description and modify task file for TC004"
[yardstick.git] / yardstick / cmd / commands / plugin.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
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 ##############################################################################
9
10 """ Handler for yardstick command 'plugin' """
11
12 import os
13 import sys
14 import yaml
15 import time
16 import logging
17 import pkg_resources
18 import yardstick.ssh as ssh
19
20 from yardstick.common.utils import cliargs
21 from yardstick.common.task_template import TaskTemplate
22
23 LOG = logging.getLogger(__name__)
24
25
26 class PluginCommands(object):
27     '''Plugin commands.
28
29        Set of commands to manage plugins.
30     '''
31
32     @cliargs("input_file", type=str, help="path to plugin configuration file",
33              nargs=1)
34     def do_install(self, args):
35         '''Install a plugin.'''
36
37         total_start_time = time.time()
38         parser = PluginParser(args.input_file[0])
39
40         plugins, deployment = parser.parse_plugin()
41         plugin_name = plugins.get("name")
42         print("Installing plugin: %s" % plugin_name)
43
44         self._install_setup(plugin_name, deployment)
45
46         self._run(plugin_name)
47
48         total_end_time = time.time()
49         LOG.info("total finished in %d secs",
50                  total_end_time - total_start_time)
51
52         print("Done, exiting")
53
54     @cliargs("input_file", type=str, help="path to plugin configuration file",
55              nargs=1)
56     def do_remove(self, args):
57         '''Remove a plugin.'''
58
59         total_start_time = time.time()
60         parser = PluginParser(args.input_file[0])
61
62         plugins, deployment = parser.parse_plugin()
63         plugin_name = plugins.get("name")
64         print("Remove plugin: %s" % plugin_name)
65
66         self._remove_setup(plugin_name, deployment)
67
68         self._run(plugin_name)
69
70         total_end_time = time.time()
71         LOG.info("total finished in %d secs",
72                  total_end_time - total_start_time)
73
74         print("Done, exiting")
75
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)
81
82         deployment_user = deployment.get("user")
83         deployment_ip = deployment.get("ip")
84         deployment_password = deployment.get("password")
85
86         if deployment_ip == "local":
87             installer_ip = os.environ.get("INSTALLER_IP", None)
88
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)
93         else:
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)
98
99         # copy script to host
100         cmd = "cat > ~/%s.sh" % plugin_name
101         self.client.run(cmd, stdin=open(self.script, 'rb'))
102
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)
108
109         deployment_user = deployment.get("user")
110         deployment_ip = deployment.get("ip")
111         deployment_password = deployment.get("password")
112
113         if deployment_ip == "local":
114             installer_ip = os.environ.get("INSTALLER_IP", None)
115
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)
120         else:
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)
125
126         # copy script to host
127         cmd = "cat > ~/%s.sh" % plugin_name
128         self.client.run(cmd, stdin=open(self.script, 'rb'))
129
130     def _run(self, plugin_name):
131         '''Run installation script '''
132         cmd = "sudo bash %s" % plugin_name + ".sh"
133
134         LOG.debug("Executing command: %s", cmd)
135         status, stdout, stderr = self.client.execute(cmd)
136
137
138 class PluginParser(object):
139     '''Parser for plugin configration files in yaml format'''
140     def __init__(self, path):
141         self.path = path
142
143     def parse_plugin(self):
144         '''parses the plugin file and return a plugins instance
145            and a deployment instance
146         '''
147
148         print "Parsing plugin config:", self.path
149
150         try:
151             kw = {}
152             with open(self.path) as f:
153                 try:
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})
159                     raise e
160                 print(("Input plugin is:\n%s\n") % rendered_plugin)
161
162                 cfg = yaml.load(rendered_plugin)
163         except IOError as ioerror:
164             sys.exit(ioerror)
165
166         self._check_schema(cfg["schema"], "plugin")
167
168         return cfg["plugins"], cfg["deployment"]
169
170     def _check_schema(self, cfg_schema, schema_type):
171         '''Check if configration file is using the correct schema type'''
172
173         if cfg_schema != "yardstick:" + schema_type + ":0.1":
174             sys.exit("error: file %s has unknown schema %s" % (self.path,
175                                                                cfg_schema))