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
14 import yardstick.ssh as ssh
15 from yardstick.benchmark.scenarios.availability.attacker.baseattacker import \
18 LOG = logging.getLogger(__name__)
21 def _execute_shell_command(command, stdin=None):
22 """execute shell script with error handling"""
26 output = subprocess.check_output(command, stdin=stdin, shell=True)
29 output = traceback.format_exc()
30 LOG.error("exec command '%s' error:\n ", command)
31 LOG.error(traceback.format_exc())
33 return exitcode, output
36 class BaremetalAttacker(BaseAttacker):
37 __attacker_type__ = 'bare-metal-down'
40 LOG.debug("config:%s context:%s", self._config, self._context)
41 host = self._context.get(self._config['host'], None)
43 self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
44 self.connection.wait(timeout=600)
45 LOG.debug("ssh host success!")
46 self.host_ip = host['ip']
48 self.ipmi_ip = host.get("ipmi_ip", None)
49 self.ipmi_user = host.get("ipmi_user", "root")
50 self.ipmi_pwd = host.get("ipmi_pwd", None)
52 self.fault_cfg = BaseAttacker.attacker_cfgs.get('bare-metal-down')
53 self.check_script = self.get_script_fullpath(
54 self.fault_cfg['check_script'])
55 self.recovery_script = self.get_script_fullpath(
56 self.fault_cfg['recovery_script'])
59 self.setup_done = True
62 with open(self.check_script, "r") as stdin_file:
63 exit_status, stdout, stderr = self.connection.execute(
64 "sudo /bin/sh -s {0} -W 10".format(self.host_ip),
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)
75 def inject_fault(self):
76 exit_status, stdout, stderr = self.connection.execute(
77 "sudo shutdown -h now")
78 LOG.debug("inject fault ret: %s out:%s err:%s",
79 exit_status, stdout, stderr)
81 LOG.info("inject fault success")
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)
89 LOG.debug("jump_host ip:%s user:%s", host['ip'], host['user'])
90 self.jump_connection = ssh.SSH.from_node(
92 # why do we allow pwd for password?
93 defaults={"user": "root", "password": host.get("pwd")}
95 self.jump_connection.wait(timeout=600)
96 LOG.debug("ssh jump host success!")
98 if self.jump_connection is not None:
99 with open(self.recovery_script, "r") as stdin_file:
100 self.jump_connection.execute(
101 "sudo /bin/bash -s {0} {1} {2} {3}".format(
102 self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"),
105 _execute_shell_command(
106 "sudo /bin/bash -s {0} {1} {2} {3}".format(
107 self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"),
108 stdin=open(self.recovery_script, "r"))
111 def _test(): # pragma: no cover
113 "ipmi_ip": "10.20.0.5",
115 "ipmi_pwd": "123456",
118 "key_filename": "/root/.ssh/id_rsa"
120 context = {"node1": host}
122 'fault_type': 'bear-metal-down',
125 ins = BaremetalAttacker(attacker_cfg, context)
130 if __name__ == '__main__': # pragma: no cover