Merge "Code Clean for HA Testing Framework"
[yardstick.git] / yardstick / benchmark / scenarios / availability / attacker / attacker_baremetal.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
3 #
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 import traceback
11 import subprocess
12 import yardstick.ssh as ssh
13 from baseattacker import BaseAttacker
14
15 LOG = logging.getLogger(__name__)
16
17
18 def _execute_shell_command(command, stdin=None):
19     '''execute shell script with error handling'''
20     exitcode = 0
21     output = []
22     try:
23         output = subprocess.check_output(command, stdin=stdin, shell=True)
24     except Exception:
25         exitcode = -1
26         output = traceback.format_exc()
27         LOG.error("exec command '%s' error:\n " % command)
28         LOG.error(traceback.format_exc())
29
30     return exitcode, output
31
32
33 class BaremetalAttacker(BaseAttacker):
34
35     __attacker_type__ = 'bare-metal-down'
36
37     def setup(self):
38         LOG.debug("config:%s context:%s" % (self._config, self._context))
39         host = self._context.get(self._config['host'], None)
40         ip = host.get("ip", None)
41         user = host.get("user", "root")
42         key_filename = host.get("key_filename", "~/.ssh/id_rsa")
43
44         self.connection = ssh.SSH(user, ip, key_filename=key_filename)
45         self.connection.wait(timeout=600)
46         LOG.debug("ssh host success!")
47         self.host_ip = ip
48
49         self.ipmi_ip = host.get("ipmi_ip", None)
50         self.ipmi_user = host.get("ipmi_user", "root")
51         self.ipmi_pwd = host.get("ipmi_pwd", None)
52
53         self.fault_cfg = BaseAttacker.attacker_cfgs.get('bare-metal-down')
54         self.check_script = self.get_script_fullpath(
55             self.fault_cfg['check_script'])
56         self.recovery_script = self.get_script_fullpath(
57             self.fault_cfg['recovery_script'])
58
59         if self.check():
60             self.setup_done = True
61
62     def check(self):
63         exit_status, stdout, stderr = self.connection.execute(
64             "/bin/sh -s {0} -W 10".format(self.host_ip),
65             stdin=open(self.check_script, "r"))
66
67         LOG.debug("check ret: %s out:%s err:%s" %
68                   (exit_status, stdout, stderr))
69         if not stdout or "running" not in stdout:
70             LOG.info("the host (ipmi_ip:%s) is not running!" % self.ipmi_ip)
71             return False
72
73         return True
74
75     def inject_fault(self):
76         exit_status, stdout, stderr = self.connection.execute(
77             "shutdown -h now")
78         LOG.debug("inject fault ret: %s out:%s err:%s" %
79                   (exit_status, stdout, stderr))
80         if not exit_status:
81             LOG.info("inject fault success")
82
83     def recover(self):
84         jump_host_name = self._config.get("jump_host", None)
85         self.jump_connection = None
86         if jump_host_name is not None:
87             host = self._context.get(jump_host_name, None)
88             ip = host.get("ip", None)
89             user = host.get("user", "root")
90             pwd = host.get("pwd", None)
91
92             LOG.debug("jump_host ip:%s user:%s" % (ip, user))
93             self.jump_connection = ssh.SSH(user, ip, password=pwd)
94             self.jump_connection.wait(timeout=600)
95             LOG.debug("ssh jump host success!")
96
97         if self.jump_connection is not None:
98             exit_status, stdout, stderr = self.jump_connection.execute(
99                 "/bin/bash -s {0} {1} {2} {3}".format(
100                     self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"),
101                 stdin=open(self.recovery_script, "r"))
102         else:
103             exit_status, stdout = _execute_shell_command(
104                 "/bin/bash -s {0} {1} {2} {3}".format(
105                     self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"),
106                 stdin=open(self.recovery_script, "r"))
107
108
109 def _test():  # pragma: no cover
110     host = {
111         "ipmi_ip": "10.20.0.5",
112         "ipmi_user": "root",
113         "ipmi_pwd": "123456",
114         "ip": "10.20.0.5",
115         "user": "root",
116         "key_filename": "/root/.ssh/id_rsa"
117     }
118     context = {"node1": host}
119     attacker_cfg = {
120         'fault_type': 'bear-metal-down',
121         'host': 'node1',
122     }
123     ins = BaremetalAttacker(attacker_cfg, context)
124     ins.setup()
125     ins.inject_fault()
126
127
128 if __name__ == '__main__':  # pragma: no cover
129     _test()