# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
+from __future__ import absolute_import
import logging
-import traceback
import subprocess
+
import yardstick.ssh as ssh
-from baseattacker import BaseAttacker
+from yardstick.benchmark.scenarios.availability.attacker.baseattacker import \
+ BaseAttacker
LOG = logging.getLogger(__name__)
def _execute_shell_command(command, stdin=None):
- '''execute shell script with error handling'''
+ """execute shell script with error handling"""
exitcode = 0
output = []
try:
output = subprocess.check_output(command, stdin=stdin, shell=True)
- except Exception:
+ except Exception: # pylint: disable=broad-except
exitcode = -1
- output = traceback.format_exc()
- LOG.error("exec command '%s' error:\n " % command)
- LOG.error(traceback.format_exc())
+ LOG.error("exec command '%s' error:\n ", command, exc_info=True)
return exitcode, output
class BaremetalAttacker(BaseAttacker):
-
__attacker_type__ = 'bare-metal-down'
def setup(self):
- LOG.debug("config:%s context:%s" % (self._config, self._context))
+ # baremetal down need to recover even sla pass
+ self.mandatory = True
+ LOG.debug("config:%s context:%s", self._config, self._context)
host = self._context.get(self._config['host'], None)
- ip = host.get("ip", None)
- user = host.get("user", "root")
- key_filename = host.get("key_filename", "~/.ssh/id_rsa")
- self.connection = ssh.SSH(user, ip, key_filename=key_filename)
+ self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
self.connection.wait(timeout=600)
LOG.debug("ssh host success!")
- self.host_ip = ip
+
+ jump_host_name = self._config.get("jump_host", None)
+ self.jump_connection = None
+ if jump_host_name is not None:
+ jump_host = self._context.get(jump_host_name, None)
+
+ LOG.debug("jump_host ip:%s user:%s", jump_host['ip'], jump_host['user'])
+ self.jump_connection = ssh.SSH.from_node(
+ jump_host,
+ # why do we allow pwd for password?
+ defaults={"user": "root", "password": jump_host.get("pwd")}
+ )
+ self.jump_connection.wait(timeout=600)
+ LOG.debug("ssh jump host success!")
+
+ self.host_ip = host['ip']
self.ipmi_ip = host.get("ipmi_ip", None)
self.ipmi_user = host.get("ipmi_user", "root")
self.fault_cfg = BaseAttacker.attacker_cfgs.get('bare-metal-down')
self.check_script = self.get_script_fullpath(
self.fault_cfg['check_script'])
+ self.inject_script = self.get_script_fullpath(self.fault_cfg['inject_script'])
self.recovery_script = self.get_script_fullpath(
self.fault_cfg['recovery_script'])
self.setup_done = True
def check(self):
- exit_status, stdout, stderr = self.connection.execute(
- "/bin/sh -s {0} -W 10".format(self.host_ip),
- stdin=open(self.check_script, "r"))
+ with open(self.check_script, "r") as stdin_file:
+ exit_status, stdout, stderr = self.connection.execute(
+ "sudo /bin/sh -s {0} -W 10".format(self.host_ip),
+ stdin=stdin_file)
- LOG.debug("check ret: %s out:%s err:%s" %
- (exit_status, stdout, stderr))
+ LOG.debug("check ret: %s out:%s err:%s",
+ exit_status, stdout, stderr)
if not stdout or "running" not in stdout:
- LOG.info("the host (ipmi_ip:%s) is not running!" % self.ipmi_ip)
+ LOG.info("the host (ipmi_ip:%s) is not running!", self.ipmi_ip)
return False
return True
def inject_fault(self):
- exit_status, stdout, stderr = self.connection.execute(
- "shutdown -h now")
- LOG.debug("inject fault ret: %s out:%s err:%s" %
- (exit_status, stdout, stderr))
- if not exit_status:
- LOG.info("inject fault success")
+ LOG.info("Inject fault START")
+ cmd = "sudo /bin/bash -s {0} {1} {2} {3}".format(
+ self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "off")
+ with open(self.inject_script, "r") as stdin_file:
+ if self.jump_connection is not None:
+ LOG.info("Power off node via IPMI")
+ self.jump_connection.execute(cmd, stdin=stdin_file)
+ else:
+ _execute_shell_command(cmd, stdin=stdin_file)
+ LOG.info("Inject fault END")
def recover(self):
- jump_host_name = self._config.get("jump_host", None)
- self.jump_connection = None
- if jump_host_name is not None:
- host = self._context.get(jump_host_name, None)
- ip = host.get("ip", None)
- user = host.get("user", "root")
- pwd = host.get("pwd", None)
-
- LOG.debug("jump_host ip:%s user:%s" % (ip, user))
- self.jump_connection = ssh.SSH(user, ip, password=pwd)
- self.jump_connection.wait(timeout=600)
- LOG.debug("ssh jump host success!")
-
- if self.jump_connection is not None:
- exit_status, stdout, stderr = self.jump_connection.execute(
- "/bin/bash -s {0} {1} {2} {3}".format(
- self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"),
- stdin=open(self.recovery_script, "r"))
- else:
- exit_status, stdout = _execute_shell_command(
- "/bin/bash -s {0} {1} {2} {3}".format(
- self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on"),
- stdin=open(self.recovery_script, "r"))
+ LOG.info("Recover fault START")
+ cmd = "sudo /bin/bash -s {0} {1} {2} {3}".format(
+ self.ipmi_ip, self.ipmi_user, self.ipmi_pwd, "on")
+ with open(self.recovery_script, "r") as stdin_file:
+ if self.jump_connection is not None:
+ self.jump_connection.execute(cmd, stdin=stdin_file)
+ else:
+ _execute_shell_command(cmd, stdin=stdin_file)
+ LOG.info("Recover fault END")
def _test(): # pragma: no cover