1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
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
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios.availability.attacker.baseattacker import \
17 LOG = logging.getLogger(__name__)
20 def _execute_shell_command(command, stdin=None):
21 """execute shell script with error handling"""
25 output = subprocess.check_output(command, stdin=stdin, shell=True)
28 LOG.error("exec command '%s' error:\n ", command, exc_info=True)
30 return exitcode, output
33 class BaremetalAttacker(BaseAttacker):
34 __attacker_type__ = 'bare-metal-down'
37 LOG.debug("config:%s context:%s", self._config, self._context)
38 host = self._context.get(self._config['host'], None)
40 self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
41 self.connection.wait(timeout=600)
42 LOG.debug("ssh host success!")
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)
49 LOG.debug("jump_host ip:%s user:%s", jump_host['ip'], jump_host['user'])
50 self.jump_connection = ssh.SSH.from_node(
52 # why do we allow pwd for password?
53 defaults={"user": "root", "password": jump_host.get("pwd")}
55 self.jump_connection.wait(timeout=600)
56 LOG.debug("ssh jump host success!")
58 self.host_ip = host['ip']
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)
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'])
72 self.setup_done = True
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),
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)
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)
97 _execute_shell_command(cmd, stdin=stdin_file)
98 LOG.info("Inject fault END")
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)
108 _execute_shell_command(cmd, stdin=stdin_file)
109 LOG.info("Recover fault END")
112 def _test(): # pragma: no cover
114 "ipmi_ip": "10.20.0.5",
116 "ipmi_pwd": "123456",
119 "key_filename": "/root/.ssh/id_rsa"
121 context = {"node1": host}
123 'fault_type': 'bear-metal-down',
126 ins = BaremetalAttacker(attacker_cfg, context)
131 if __name__ == '__main__': # pragma: no cover