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 ##############################################################################
12 import yardstick.ssh as ssh
13 from baseattacker import BaseAttacker
15 LOG = logging.getLogger(__name__)
18 def _execute_shell_command(command, stdin=None):
19 '''execute shell script with error handling'''
23 output = subprocess.check_output(command, stdin=stdin, shell=True)
26 output = traceback.format_exc()
27 LOG.error("exec command '%s' error:\n ", command)
28 LOG.error(traceback.format_exc())
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)
39 ip = host.get("ip", None)
40 user = host.get("user", "root")
41 ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
42 key_filename = host.get("key_filename", "~/.ssh/id_rsa")
44 self.connection = ssh.SSH(user, ip, key_filename=key_filename,
46 self.connection.wait(timeout=600)
47 LOG.debug("ssh host success!")
50 self.ipmi_ip = host.get("ipmi_ip", None)
51 self.ipmi_user = host.get("ipmi_user", "root")
52 self.ipmi_pwd = host.get("ipmi_pwd", None)
54 self.fault_cfg = BaseAttacker.attacker_cfgs.get('bare-metal-down')
55 self.check_script = self.get_script_fullpath(
56 self.fault_cfg['check_script'])
57 self.recovery_script = self.get_script_fullpath(
58 self.fault_cfg['recovery_script'])
61 self.setup_done = True
64 with open(self.check_script, "r") as stdin_file:
65 exit_status, stdout, stderr = self.connection.execute(
66 "/bin/sh -s {0} -W 10".format(self.host_ip),
69 LOG.debug("check ret: %s out:%s err:%s",
70 exit_status, stdout, stderr)
71 if not stdout or "running" not in stdout:
72 LOG.info("the host (ipmi_ip:%s) is not running!", self.ipmi_ip)
77 def inject_fault(self):
78 exit_status, stdout, stderr = self.connection.execute(
80 LOG.debug("inject fault ret: %s out:%s err:%s",
81 exit_status, stdout, stderr)
83 LOG.info("inject fault success")
86 jump_host_name = self._config.get("jump_host", None)
87 self.jump_connection = None
88 if jump_host_name is not None:
89 host = self._context.get(jump_host_name, None)
90 ip = host.get("ip", None)
91 user = host.get("user", "root")
92 ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
93 pwd = host.get("pwd", None)
95 LOG.debug("jump_host ip:%s user:%s", ip, user)
96 self.jump_connection = ssh.SSH(user, ip, password=pwd,
98 self.jump_connection.wait(timeout=600)
99 LOG.debug("ssh jump host success!")
101 if self.jump_connection is not None:
102 with open(self.recovery_script, "r") as stdin_file:
103 exit_status, stdout, stderr = self.jump_connection.execute(
104 "/bin/bash -s {0} {1} {2} {3}".format(
105 self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"),
108 exit_status, stdout = _execute_shell_command(
109 "/bin/bash -s {0} {1} {2} {3}".format(
110 self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"),
111 stdin=open(self.recovery_script, "r"))
114 def _test(): # pragma: no cover
116 "ipmi_ip": "10.20.0.5",
118 "ipmi_pwd": "123456",
121 "key_filename": "/root/.ssh/id_rsa"
123 context = {"node1": host}
125 'fault_type': 'bear-metal-down',
128 ins = BaremetalAttacker(attacker_cfg, context)
133 if __name__ == '__main__': # pragma: no cover