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 from __future__ import absolute_import
12 from yardstick.benchmark.scenarios.availability.operation.baseoperation \
16 import yardstick.ssh as ssh
17 from yardstick.benchmark.scenarios.availability.util \
18 import buildshellparams, execute_shell_command, \
19 read_stdout_item, build_shell_command
21 LOG = logging.getLogger(__name__)
24 class GeneralOperaion(BaseOperation):
26 __operation__type__ = "general-operation"
29 LOG.debug("config:%s context:%s", self._config, self._context)
30 host = self._context.get(self._config.get('host', None), None)
32 self.connection = None
34 self.connection = ssh.SSH.from_node(
35 host, defaults={"user": "root"})
36 self.connection.wait(timeout=600)
37 LOG.debug("ssh host success!")
39 self.key = self._config['key']
40 self.operation_key = self._config['operation_key']
42 if "action_parameter" in self._config:
43 self.actionParameter_config = self._config['action_parameter']
45 if "rollback_parameter" in self._config:
46 rollbackParameter = self._config['rollback_parameter']
47 str = buildshellparams(
48 rollbackParameter, True if self.connection else False)
49 l = list(item for item in rollbackParameter.values())
50 self.rollback_param = str.format(*l)
52 self.operation_cfgs = BaseOperation.operation_cfgs.get(
54 self.action_script = self.get_script_fullpath(
55 self.operation_cfgs['action_script'])
56 self.rollback_script = self.get_script_fullpath(
57 self.operation_cfgs['rollback_script'])
60 if "action_parameter" in self._config:
63 self.actionParameter_config,
64 True if self.connection else False,
65 self.intermediate_variables)
67 with open(self.action_script, "r") as stdin_file:
68 exit_status, stdout, stderr = self.connection.execute(
69 "sudo {}".format(self.action_param),
72 exit_status, stdout = \
73 execute_shell_command(
74 "/bin/bash {0} {1}".format(
75 self.action_script, self.action_param))
78 with open(self.action_script, "r") as stdin_file:
79 exit_status, stdout, stderr = self.connection.execute(
83 exit_status, stdout = execute_shell_command(
84 "/bin/bash {0}".format(self.action_script))
87 LOG.debug("success,the operation's output is: %s", stdout)
88 if "return_parameter" in self._config:
89 returnParameter = self._config['return_parameter']
90 for key, item in returnParameter.items():
91 value = read_stdout_item(stdout, key)
92 LOG.debug("intermediate variables %s: %s", item, value)
93 self.intermediate_variables[item] = value
96 "the operation's error, stdout:%s, stderr:%s",
100 if "rollback_parameter" in self._config:
102 with open(self.rollback_script, "r") as stdin_file:
103 exit_status, stdout, stderr = self.connection.execute(
104 "sudo {}".format(self.rollback_param),
107 exit_status, stdout = \
108 execute_shell_command(
109 "/bin/bash {0} {1}".format(
110 self.rollback_script, self.rollback_param))
113 with open(self.rollback_script, "r") as stdin_file:
114 exit_status, stdout, stderr = self.connection.execute(
118 exit_status, stdout = execute_shell_command(
119 "/bin/bash {0}".format(self.rollback_script))