Merge "vsperf: Initial integration with VSPERF"
[yardstick.git] / yardstick / benchmark / scenarios / availability / operation / operation_general.py
1 ##############################################################################
2 # Copyright (c) 2016 Juan Qiu and others
3 # juan_ qiu@tongji.edu.cn
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 import logging
10 from baseoperation import BaseOperation
11 import yardstick.ssh as ssh
12 from yardstick.benchmark.scenarios.availability.util import buildshellparams
13
14 LOG = logging.getLogger(__name__)
15
16
17 class GeneralOperaion(BaseOperation):
18
19     __operation__type__ = "general-operation"
20
21     def setup(self):
22         LOG.debug("config:%s context:%s" % (self._config, self._context))
23         host = self._context.get(self._config['host'], None)
24         ip = host.get("ip", None)
25         user = host.get("user", "root")
26         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
27
28         self.connection = ssh.SSH(user, ip, key_filename=key_filename)
29         self.connection.wait(timeout=600)
30         LOG.debug("ssh host success!")
31
32         self.key = self._config['key']
33
34         if "action_parameter" in self._config:
35             actionParameter = self._config['action_parameter']
36             str = buildshellparams(actionParameter)
37             l = list(item for item in actionParameter.values())
38             self.action_param = str.format(*l)
39
40         if "rollback_parameter" in self._config:
41             rollbackParameter = self._config['rollback_parameter']
42             str = buildshellparams(rollbackParameter)
43             l = list(item for item in rollbackParameter.values())
44             self.rollback_param = str.format(*l)
45
46         self.operation_cfgs = BaseOperation.operation_cfgs.get(self.key)
47         self.action_script = self.get_script_fullpath(
48             self.operation_cfgs['action_script'])
49         self.rollback_script = self.get_script_fullpath(
50             self.operation_cfgs['rollback_script'])
51
52     def run(self):
53         if "action_parameter" in self._config:
54             exit_status, stdout, stderr = self.connection.execute(
55                 self.action_param,
56                 stdin=open(self.action_script, "r"))
57         else:
58             exit_status, stdout, stderr = self.connection.execute(
59                 "/bin/sh -s ",
60                 stdin=open(self.action_script, "r"))
61
62         if exit_status == 0:
63             LOG.debug("success,the operation's output is: {0}".format(stdout))
64         else:
65             LOG.error(
66                 "the operation's error, stdout:%s, stderr:%s" %
67                 (stdout, stderr))
68
69     def rollback(self):
70         if "rollback_parameter" in self._config:
71             exit_status, stdout, stderr = self.connection.execute(
72                 self.rollback_param,
73                 stdin=open(self.rollback_script, "r"))
74         else:
75             exit_status, stdout, stderr = self.connection.execute(
76                 "/bin/sh -s ",
77                 stdin=open(self.rollback_script, "r"))