Merge "switch logging to proper usage"
[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         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
27         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
28
29         self.connection = ssh.SSH(user, ip, key_filename=key_filename,
30                                   port=ssh_port)
31         self.connection.wait(timeout=600)
32         LOG.debug("ssh host success!")
33
34         self.key = self._config['key']
35         self.operation_key = self._config['operation_key']
36
37         if "action_parameter" in self._config:
38             actionParameter = self._config['action_parameter']
39             str = buildshellparams(actionParameter)
40             l = list(item for item in actionParameter.values())
41             self.action_param = str.format(*l)
42
43         if "rollback_parameter" in self._config:
44             rollbackParameter = self._config['rollback_parameter']
45             str = buildshellparams(rollbackParameter)
46             l = list(item for item in rollbackParameter.values())
47             self.rollback_param = str.format(*l)
48
49         self.operation_cfgs = BaseOperation.operation_cfgs.get(
50             self.operation_key)
51         self.action_script = self.get_script_fullpath(
52             self.operation_cfgs['action_script'])
53         self.rollback_script = self.get_script_fullpath(
54             self.operation_cfgs['rollback_script'])
55
56     def run(self):
57         if "action_parameter" in self._config:
58             exit_status, stdout, stderr = self.connection.execute(
59                 self.action_param,
60                 stdin=open(self.action_script, "r"))
61         else:
62             exit_status, stdout, stderr = self.connection.execute(
63                 "/bin/sh -s ",
64                 stdin=open(self.action_script, "r"))
65
66         if exit_status == 0:
67             LOG.debug("success,the operation's output is: {0}".format(stdout))
68         else:
69             LOG.error(
70                 "the operation's error, stdout:%s, stderr:%s",
71                 stdout, stderr)
72
73     def rollback(self):
74         if "rollback_parameter" in self._config:
75             exit_status, stdout, stderr = self.connection.execute(
76                 self.rollback_param,
77                 stdin=open(self.rollback_script, "r"))
78         else:
79             exit_status, stdout, stderr = self.connection.execute(
80                 "/bin/sh -s ",
81                 stdin=open(self.rollback_script, "r"))