Merge "[Yardstick-233] Add tc_42 yaml file"
[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 sys
13 import yaml
14 import time
15 import logging
16 import pkg_resources
17 import yardstick.ssh as ssh
18
19 from yardstick.common.utils import cliargs
20 from yardstick.common.task_template import TaskTemplate
21
22 LOG = logging.getLogger(__name__)
23
24
25 class PluginCommands(object):
26     '''Plugin commands.
27
28        Set of commands to manage plugins.
29     '''
30
31     @cliargs("input_file", type=str, help="path to plugin configuration file",
32              nargs=1)
33     def do_install(self, args):
34         '''Install a plugin.'''
35
36         total_start_time = time.time()
37         parser = PluginParser(args.input_file[0])
38
39         plugins, deployment = parser.parse_plugin()
40         plugin_name = plugins.get("name")
41         print("Installing plugin: %s" % plugin_name)
42
43         self._install_setup(plugin_name, deployment)
44
45         self._run(plugin_name)
46
47         total_end_time = time.time()
48         LOG.info("total finished in %d secs",
49                  total_end_time - total_start_time)
50
51         print("Done, exiting")
52
53     @cliargs("input_file", type=str, help="path to plugin configuration file",
54              nargs=1)
55     def do_remove(self, args):
56         '''Remove a plugin.'''
57
58         total_start_time = time.time()
59         parser = PluginParser(args.input_file[0])
60
61         plugins, deployment = parser.parse_plugin()
62         plugin_name = plugins.get("name")
63         print("Remove plugin: %s" % plugin_name)
64
65         self._remove_setup(plugin_name, deployment)
66
67         self._run(plugin_name)
68
69         total_end_time = time.time()
70         LOG.info("total finished in %d secs",
71                  total_end_time - total_start_time)
72
73         print("Done, exiting")
74
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)
80
81         deployment_user = deployment.get("user")
82         deployment_ip = deployment.get("ip")
83
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)
89
90         # copy script to host
91         cmd = "cat > ~/%s.sh" % plugin_name
92         self.client.run(cmd, stdin=open(self.script, 'rb'))
93
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)
99
100         deployment_user = deployment.get("user")
101         deployment_ip = deployment.get("ip")
102
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)
108
109         # copy script to host
110         cmd = "cat > ~/%s.sh" % plugin_name
111         self.client.run(cmd, stdin=open(self.script, 'rb'))
112
113     def _run(self, plugin_name):
114         '''Run installation script '''
115         cmd = "sudo bash %s" % plugin_name + ".sh"
116
117         LOG.debug("Executing command: %s", cmd)
118         status, stdout, stderr = self.client.execute(cmd)
119
120
121 class PluginParser(object):
122     '''Parser for plugin configration files in yaml format'''
123     def __init__(self, path):
124         self.path = path
125
126     def parse_plugin(self):
127         '''parses the plugin file and return a plugins instance
128            and a deployment instance
129         '''
130
131         print "Parsing plugin config:", self.path
132
133         try:
134             kw = {}
135             with open(self.path) as f:
136                 try:
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})
142                     raise e
143                 print(("Input plugin is:\n%s\n") % rendered_plugin)
144
145                 cfg = yaml.load(rendered_plugin)
146         except IOError as ioerror:
147             sys.exit(ioerror)
148
149         self._check_schema(cfg["schema"], "plugin")
150
151         return cfg["plugins"], cfg["deployment"]
152
153     def _check_schema(self, cfg_schema, schema_type):
154         '''Check if configration file is using the correct schema type'''
155
156         if cfg_schema != "yardstick:" + schema_type + ":0.1":
157             sys.exit("error: file %s has unknown schema %s" % (self.path,
158                                                                cfg_schema))