Merge "Modify ComputeCapacity scenario host key name"
[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     def do_remove(self, args):
54         '''Remove a plugin.'''
55
56         total_start_time = time.time()
57         parser = PluginParser(args.input_file[0])
58
59         plugins, deployment = parser.parse_plugin()
60         plugin_name = plugins.get("name")
61         print("Remove plugin: %s" % plugin_name)
62
63         self._remove_setup(plugin_name, deployment)
64
65         self._run(plugin_name)
66
67         total_end_time = time.time()
68         LOG.info("total finished in %d secs",
69                  total_end_time - total_start_time)
70
71         print("Done, exiting")
72
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)
78
79         deployment_user = deployment.get("user")
80         deployment_ip = deployment.get("ip")
81
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)
87
88         # copy script to host
89         cmd = "cat > ~/%s.sh" % plugin_name
90         self.client.run(cmd, stdin=open(self.script, 'rb'))
91
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)
97
98         deployment_user = deployment.get("user")
99         deployment_ip = deployment.get("ip")
100
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)
106
107         # copy script to host
108         cmd = "cat > ~/%s.sh" % plugin_name
109         self.client.run(cmd, stdin=open(self.script, 'rb'))
110
111     def _run(self, plugin_name):
112         '''Run installation script '''
113         cmd = "sudo bash %s" % plugin_name + ".sh"
114
115         LOG.debug("Executing command: %s", cmd)
116         status, stdout, stderr = self.client.execute(cmd)
117
118
119 class PluginParser(object):
120     '''Parser for plugin configration files in yaml format'''
121     def __init__(self, path):
122         self.path = path
123
124     def parse_plugin(self):
125         '''parses the plugin file and return a plugins instance
126            and a deployment instance
127         '''
128
129         print "Parsing plugin config:", self.path
130
131         try:
132             kw = {}
133             with open(self.path) as f:
134                 try:
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})
140                     raise e
141                 print(("Input plugin is:\n%s\n") % rendered_plugin)
142
143                 cfg = yaml.load(rendered_plugin)
144         except IOError as ioerror:
145             sys.exit(ioerror)
146
147         self._check_schema(cfg["schema"], "plugin")
148
149         return cfg["plugins"], cfg["deployment"]
150
151     def _check_schema(self, cfg_schema, schema_type):
152         '''Check if configration file is using the correct schema type'''
153
154         if cfg_schema != "yardstick:" + schema_type + ":0.1":
155             sys.exit("error: file %s has unknown schema %s" % (self.path,
156                                                                cfg_schema))