Merge "Disable spoof check on vfs in sriov setup"
[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
44         jump_host_name = self._config.get("jump_host", None)
45         self.jump_connection = None
46         if jump_host_name is not None:
47             jump_host = self._context.get(jump_host_name, None)
48
49             LOG.debug("jump_host ip:%s user:%s", jump_host['ip'], jump_host['user'])
50             self.jump_connection = ssh.SSH.from_node(
51                 jump_host,
52                 # why do we allow pwd for password?
53                 defaults={"user": "root", "password": jump_host.get("pwd")}
54             )
55             self.jump_connection.wait(timeout=600)
56             LOG.debug("ssh jump host success!")
57
58         self.host_ip = host['ip']
59
60         self.ipmi_ip = host.get("ipmi_ip", None)
61         self.ipmi_user = host.get("ipmi_user", "root")
62         self.ipmi_pwd = host.get("ipmi_pwd", None)
63
64         self.fault_cfg = BaseAttacker.attacker_cfgs.get('bare-metal-down')
65         self.check_script = self.get_script_fullpath(
66             self.fault_cfg['check_script'])
67         self.inject_script = self.get_script_fullpath(self.fault_cfg['inject_script'])
68         self.recovery_script = self.get_script_fullpath(
69             self.fault_cfg['recovery_script'])
70
71         if self.check():
72             self.setup_done = True
73
74     def check(self):
75         with open(self.check_script, "r") as stdin_file:
76             exit_status, stdout, stderr = self.connection.execute(
77                 "sudo /bin/sh -s {0} -W 10".format(self.host_ip),
78                 stdin=stdin_file)
79
80         LOG.debug("check ret: %s out:%s err:%s",
81                   exit_status, stdout, stderr)
82         if not stdout or "running" not in stdout:
83             LOG.info("the host (ipmi_ip:%s) is not running!", self.ipmi_ip)
84             return False
85
86         return True
87
88     def inject_fault(self):
89         LOG.info("Inject fault START")
90         cmd = "sudo /bin/bash -s {0} {1} {2} {3}".format(
91             self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "off")
92         with open(self.inject_script, "r") as stdin_file:
93             if self.jump_connection is not None:
94                 LOG.info("Power off node via IPMI")
95                 self.jump_connection.execute(cmd, stdin=stdin_file)
96             else:
97                 _execute_shell_command(cmd, stdin=stdin_file)
98         LOG.info("Inject fault END")
99
100     def recover(self):
101         LOG.info("Recover fault START")
102         cmd = "sudo /bin/bash -s {0} {1} {2} {3}".format(
103             self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on")
104         with open(self.recovery_script, "r") as stdin_file:
105             if self.jump_connection is not None:
106                 self.jump_connection.execute(cmd, stdin=stdin_file)
107             else:
108                 _execute_shell_command(cmd, stdin=stdin_file)
109         LOG.info("Recover fault END")
110
111
112 def _test():  # pragma: no cover
113     host = {
114         "ipmi_ip": "10.20.0.5",
115         "ipmi_user": "root",
116         "ipmi_pwd": "123456",
117         "ip": "10.20.0.5",
118         "user": "root",
119         "key_filename": "/root/.ssh/id_rsa"
120     }
121     context = {"node1": host}
122     attacker_cfg = {
123         'fault_type': 'bear-metal-down',
124         'host': 'node1',
125     }
126     ins = BaremetalAttacker(attacker_cfg, context)
127     ins.setup()
128     ins.inject_fault()
129
130
131 if __name__ == '__main__':  # pragma: no cover
132     _test()