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)
26 except Exception: # pylint: disable=broad-except
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 # baremetal down need to recover even sla pass
39 LOG.debug("config:%s context:%s", self._config, self._context)
40 host = self._context.get(self._config['host'], None)
42 self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
43 self.connection.wait(timeout=600)
44 LOG.debug("ssh host success!")
46 jump_host_name = self._config.get("jump_host", None)
47 self.jump_connection = None
48 if jump_host_name is not None:
49 jump_host = self._context.get(jump_host_name, None)
51 LOG.debug("jump_host ip:%s user:%s", jump_host['ip'], jump_host['user'])
52 self.jump_connection = ssh.SSH.from_node(
54 defaults={"user": "root", "password": jump_host.get("password")}
56 self.jump_connection.wait(timeout=600)
57 LOG.debug("ssh jump host success!")
59 self.host_ip = host['ip']
61 self.ipmi_ip = host.get("ipmi_ip", None)
62 self.ipmi_user = host.get("ipmi_user", "root")
63 self.ipmi_pwd = host.get("ipmi_password", None)
65 self.fault_cfg = BaseAttacker.attacker_cfgs.get('bare-metal-down')
66 self.check_script = self.get_script_fullpath(
67 self.fault_cfg['check_script'])
68 self.inject_script = self.get_script_fullpath(self.fault_cfg['inject_script'])
69 self.recovery_script = self.get_script_fullpath(
70 self.fault_cfg['recovery_script'])
73 self.setup_done = True
76 with open(self.check_script, "r") as stdin_file:
77 exit_status, stdout, stderr = self.connection.execute(
78 "sudo /bin/sh -s {0} -W 10".format(self.host_ip),
81 LOG.debug("check ret: %s out:%s err:%s",
82 exit_status, stdout, stderr)
83 if not stdout or "running" not in stdout:
84 LOG.info("the host (ipmi_ip:%s) is not running!", self.ipmi_ip)
89 def inject_fault(self):
90 LOG.info("Inject fault START")
91 cmd = "sudo /bin/bash -s {0} {1} {2} {3}".format(
92 self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "off")
93 with open(self.inject_script, "r") as stdin_file:
94 if self.jump_connection is not None:
95 LOG.info("Power off node via IPMI")
96 self.jump_connection.execute(cmd, stdin=stdin_file)
98 _execute_shell_command(cmd, stdin=stdin_file)
99 LOG.info("Inject fault END")
102 LOG.info("Recover fault START")
103 cmd = "sudo /bin/bash -s {0} {1} {2} {3}".format(
104 self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on")
105 with open(self.recovery_script, "r") as stdin_file:
106 if self.jump_connection is not None:
107 self.jump_connection.execute(cmd, stdin=stdin_file)
109 _execute_shell_command(cmd, stdin=stdin_file)
110 LOG.info("Recover fault END")