Merge "complement sceanrio for joid, apex, noha"
[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         self.operation_key = self._config['operation_key']
34
35         if "action_parameter" in self._config:
36             actionParameter = self._config['action_parameter']
37             str = buildshellparams(actionParameter)
38             l = list(item for item in actionParameter.values())
39             self.action_param = str.format(*l)
40
41         if "rollback_parameter" in self._config:
42             rollbackParameter = self._config['rollback_parameter']
43             str = buildshellparams(rollbackParameter)
44             l = list(item for item in rollbackParameter.values())
45             self.rollback_param = str.format(*l)
46
47         self.operation_cfgs = BaseOperation.operation_cfgs.get(
48             self.operation_key)
49         self.action_script = self.get_script_fullpath(
50             self.operation_cfgs['action_script'])
51         self.rollback_script = self.get_script_fullpath(
52             self.operation_cfgs['rollback_script'])
53
54     def run(self):
55         if "action_parameter" in self._config:
56             exit_status, stdout, stderr = self.connection.execute(
57                 self.action_param,
58                 stdin=open(self.action_script, "r"))
59         else:
60             exit_status, stdout, stderr = self.connection.execute(
61                 "/bin/sh -s ",
62                 stdin=open(self.action_script, "r"))
63
64         if exit_status == 0:
65             LOG.debug("success,the operation's output is: {0}".format(stdout))
66         else:
67             LOG.error(
68                 "the operation's error, stdout:%s, stderr:%s" %
69                 (stdout, stderr))
70
71     def rollback(self):
72         if "rollback_parameter" in self._config:
73             exit_status, stdout, stderr = self.connection.execute(
74                 self.rollback_param,
75                 stdin=open(self.rollback_script, "r"))
76         else:
77             exit_status, stdout, stderr = self.connection.execute(
78                 "/bin/sh -s ",
79                 stdin=open(self.rollback_script, "r"))