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!")
43 self.host_ip = host['ip']
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)
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'])
56 self.setup_done = True
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),
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)
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)
78 LOG.info("inject fault success")
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)
86 LOG.debug("jump_host ip:%s user:%s", host['ip'], host['user'])
87 self.jump_connection = ssh.SSH.from_node(
89 # why do we allow pwd for password?
90 defaults={"user": "root", "password": host.get("pwd")}
92 self.jump_connection.wait(timeout=600)
93 LOG.debug("ssh jump host success!")
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"),
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"))
108 def _test(): # pragma: no cover
110 "ipmi_ip": "10.20.0.5",
112 "ipmi_pwd": "123456",
115 "key_filename": "/root/.ssh/id_rsa"
117 context = {"node1": host}
119 'fault_type': 'bear-metal-down',
122 ins = BaremetalAttacker(attacker_cfg, context)
127 if __name__ == '__main__': # pragma: no cover