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