Merge "Update release note for Danube.2.0"
[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
20 LOG = logging.getLogger(__name__)
21
22
23 class GeneralOperaion(BaseOperation):
24
25     __operation__type__ = "general-operation"
26
27     def setup(self):
28         LOG.debug("config:%s context:%s", self._config, self._context)
29         host = self._context.get(self._config.get('host', None), None)
30
31         self.connection = None
32         if host:
33             self.connection = ssh.SSH.from_node(
34                 host, defaults={"user": "root"})
35             self.connection.wait(timeout=600)
36             LOG.debug("ssh host success!")
37
38         self.key = self._config['key']
39         self.operation_key = self._config['operation_key']
40
41         if "action_parameter" in self._config:
42             actionParameter = self._config['action_parameter']
43             str = buildshellparams(
44                 actionParameter, True if self.connection else False)
45             l = list(item for item in actionParameter.values())
46             self.action_param = str.format(*l)
47
48         if "rollback_parameter" in self._config:
49             rollbackParameter = self._config['rollback_parameter']
50             str = buildshellparams(
51                 rollbackParameter, True if self.connection else False)
52             l = list(item for item in rollbackParameter.values())
53             self.rollback_param = str.format(*l)
54
55         self.operation_cfgs = BaseOperation.operation_cfgs.get(
56             self.operation_key)
57         self.action_script = self.get_script_fullpath(
58             self.operation_cfgs['action_script'])
59         self.rollback_script = self.get_script_fullpath(
60             self.operation_cfgs['rollback_script'])
61
62     def run(self):
63         if "action_parameter" in self._config:
64             if self.connection:
65                 with open(self.action_script, "r") as stdin_file:
66                     exit_status, stdout, stderr = self.connection.execute(
67                         self.action_param,
68                         stdin=stdin_file)
69             else:
70                 exit_status, stdout = \
71                     execute_shell_command(
72                         "/bin/bash {0} {1}".format(
73                             self.action_script, self.action_param))
74         else:
75             if self.connection:
76                 with open(self.action_script, "r") as stdin_file:
77                     exit_status, stdout, stderr = self.connection.execute(
78                         "/bin/sh -s ",
79                         stdin=stdin_file)
80             else:
81                 exit_status, stdout = execute_shell_command(
82                     "/bin/bash {0}".format(self.action_script))
83
84         if exit_status == 0:
85             LOG.debug("success,the operation's output is: %s", stdout)
86         else:
87             LOG.error(
88                 "the operation's error, stdout:%s, stderr:%s",
89                 stdout, stderr)
90
91     def rollback(self):
92         if "rollback_parameter" in self._config:
93             if self.connection:
94                 with open(self.rollback_script, "r") as stdin_file:
95                     exit_status, stdout, stderr = self.connection.execute(
96                         self.rollback_param,
97                         stdin=stdin_file)
98             else:
99                 exit_status, stdout = \
100                     execute_shell_command(
101                         "/bin/bash {0} {1}".format(
102                             self.rollback_script, self.rollback_param))
103         else:
104             if self.connection:
105                 with open(self.rollback_script, "r") as stdin_file:
106                     exit_status, stdout, stderr = self.connection.execute(
107                         "/bin/sh -s ",
108                         stdin=stdin_file)
109             else:
110                 exit_status, stdout = execute_shell_command(
111                     "/bin/bash {0}".format(self.rollback_script))