add yardstick iruya 9.0.0 release notes
[yardstick.git] / yardstick / benchmark / scenarios / availability / attacker / attacker_baremetal.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd. and others
3 #
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
10 import logging
11 import subprocess
12
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios.availability.attacker.baseattacker import \
15     BaseAttacker
16
17 LOG = logging.getLogger(__name__)
18
19
20 def _execute_shell_command(command, stdin=None):
21     """execute shell script with error handling"""
22     exitcode = 0
23     output = []
24     try:
25         output = subprocess.check_output(command, stdin=stdin, shell=True)
26     except Exception:  # pylint: disable=broad-except
27         exitcode = -1
28         LOG.error("exec command '%s' error:\n ", command, exc_info=True)
29
30     return exitcode, output
31
32
33 class BaremetalAttacker(BaseAttacker):
34     __attacker_type__ = 'bare-metal-down'
35
36     def setup(self):
37         # baremetal down need to recover even sla pass
38         self.mandatory = True
39         LOG.debug("config:%s context:%s", self._config, self._context)
40         host = self._context.get(self._config['host'], None)
41
42         self.connection = ssh.SSH.from_node(host, defaults={"user": "root"})
43         self.connection.wait(timeout=600)
44         LOG.debug("ssh host success!")
45
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)
50
51             LOG.debug("jump_host ip:%s user:%s", jump_host['ip'], jump_host['user'])
52             self.jump_connection = ssh.SSH.from_node(
53                 jump_host,
54                 defaults={"user": "root", "password": jump_host.get("password")}
55             )
56             self.jump_connection.wait(timeout=600)
57             LOG.debug("ssh jump host success!")
58
59         self.host_ip = host['ip']
60
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)
64
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'])
71
72         if self.check():
73             self.setup_done = True
74
75     def check(self):
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),
79                 stdin=stdin_file)
80
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)
85             return False
86
87         return True
88
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)
97             else:
98                 _execute_shell_command(cmd, stdin=stdin_file)
99         LOG.info("Inject fault END")
100
101     def recover(self):
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)
108             else:
109                 _execute_shell_command(cmd, stdin=stdin_file)
110         LOG.info("Recover fault END")