Merge "Disable spoof check on vfs in sriov setup"
[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 from __future__ import absolute_import
10 import logging
11
12 from yardstick.benchmark.scenarios.availability.operation.baseoperation \
13     import \
14     BaseOperation
15
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
20
21 LOG = logging.getLogger(__name__)
22
23
24 class GeneralOperaion(BaseOperation):
25
26     __operation__type__ = "general-operation"
27
28     def setup(self):
29         LOG.debug("config:%s context:%s", self._config, self._context)
30         host = self._context.get(self._config.get('host', None), None)
31
32         self.connection = None
33         if host:
34             self.connection = ssh.SSH.from_node(
35                 host, defaults={"user": "root"})
36             self.connection.wait(timeout=600)
37             LOG.debug("ssh host success!")
38
39         self.key = self._config['key']
40         self.operation_key = self._config['operation_key']
41
42         if "action_parameter" in self._config:
43             self.actionParameter_config = self._config['action_parameter']
44
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)
51
52         self.operation_cfgs = BaseOperation.operation_cfgs.get(
53             self.operation_key)
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'])
58
59     def run(self):
60         if "action_parameter" in self._config:
61             self.action_param = \
62                 build_shell_command(
63                     self.actionParameter_config,
64                     True if self.connection else False,
65                     self.intermediate_variables)
66             if self.connection:
67                 with open(self.action_script, "r") as stdin_file:
68                     exit_status, stdout, stderr = self.connection.execute(
69                         "sudo {}".format(self.action_param),
70                         stdin=stdin_file)
71             else:
72                 exit_status, stdout = \
73                     execute_shell_command(
74                         "/bin/bash {0} {1}".format(
75                             self.action_script, self.action_param))
76         else:
77             if self.connection:
78                 with open(self.action_script, "r") as stdin_file:
79                     exit_status, stdout, stderr = self.connection.execute(
80                         "sudo /bin/sh -s ",
81                         stdin=stdin_file)
82             else:
83                 exit_status, stdout = execute_shell_command(
84                     "/bin/bash {0}".format(self.action_script))
85
86         if exit_status == 0:
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
94         else:
95             LOG.error(
96                 "the operation's error, stdout:%s, stderr:%s",
97                 stdout, stderr)
98
99     def rollback(self):
100         if "rollback_parameter" in self._config:
101             if self.connection:
102                 with open(self.rollback_script, "r") as stdin_file:
103                     exit_status, stdout, stderr = self.connection.execute(
104                         "sudo {}".format(self.rollback_param),
105                         stdin=stdin_file)
106             else:
107                 exit_status, stdout = \
108                     execute_shell_command(
109                         "/bin/bash {0} {1}".format(
110                             self.rollback_script, self.rollback_param))
111         else:
112             if self.connection:
113                 with open(self.rollback_script, "r") as stdin_file:
114                     exit_status, stdout, stderr = self.connection.execute(
115                         "sudo /bin/sh -s ",
116                         stdin=stdin_file)
117             else:
118                 exit_status, stdout = execute_shell_command(
119                     "/bin/bash {0}".format(self.rollback_script))